• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/scalar-imagic.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <xnnpack/common.h>
13 #include <xnnpack/math.h>
14 #include <xnnpack/vcvt.h>
15 
16 #include <fp16.h>
17 
18 
xnn_f32_qu8_vcvt_ukernel__scalar_imagic_x4(size_t n,const float * x,uint8_t * y,const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_qu8_vcvt_ukernel__scalar_imagic_x4(
20     size_t n,
21     const float* x,
22     uint8_t* y,
23     const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const float vscale = params->scalar_imagic.scale;
31   const float vmagic_bias = params->scalar_imagic.magic_bias;
32   const int32_t vmagic_min = params->scalar_imagic.magic_min;
33   const int32_t vmagic_max = params->scalar_imagic.magic_max;
34   const int32_t vmagic_bias_less_zero_point = params->scalar_imagic.magic_bias_less_zero_point;
35 
36   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
37     float vx0 = x[0];
38     float vx1 = x[1];
39     float vx2 = x[2];
40     float vx3 = x[3];
41     x += 4;
42 
43     vx0 *= vscale;
44     vx1 *= vscale;
45     vx2 *= vscale;
46     vx3 *= vscale;
47 
48     vx0 += vmagic_bias;
49     vx1 += vmagic_bias;
50     vx2 += vmagic_bias;
51     vx3 += vmagic_bias;
52 
53     int32_t vy0 = (int32_t) fp32_to_bits(vx0);
54     int32_t vy1 = (int32_t) fp32_to_bits(vx1);
55     int32_t vy2 = (int32_t) fp32_to_bits(vx2);
56     int32_t vy3 = (int32_t) fp32_to_bits(vx3);
57 
58     vy0 = math_max_s32(vy0, vmagic_min);
59     vy1 = math_max_s32(vy1, vmagic_min);
60     vy2 = math_max_s32(vy2, vmagic_min);
61     vy3 = math_max_s32(vy3, vmagic_min);
62 
63     vy0 = math_min_s32(vy0, vmagic_max);
64     vy1 = math_min_s32(vy1, vmagic_max);
65     vy2 = math_min_s32(vy2, vmagic_max);
66     vy3 = math_min_s32(vy3, vmagic_max);
67 
68     vy0 -= vmagic_bias_less_zero_point;
69     vy1 -= vmagic_bias_less_zero_point;
70     vy2 -= vmagic_bias_less_zero_point;
71     vy3 -= vmagic_bias_less_zero_point;
72 
73     y[0] = (uint8_t) vy0;
74     y[1] = (uint8_t) vy1;
75     y[2] = (uint8_t) vy2;
76     y[3] = (uint8_t) vy3;
77     y += 4;
78   }
79   if XNN_UNLIKELY(n != 0) {
80     do {
81       float vx = *x++;
82       vx *= vscale;
83       vx += vmagic_bias;
84 
85       int32_t vy = (int32_t) fp32_to_bits(vx);
86       vy = math_max_s32(vy, vmagic_min);
87       vy = math_min_s32(vy, vmagic_max);
88       vy -= vmagic_bias_less_zero_point;
89 
90       *y++ = (uint8_t) vy;
91 
92       n -= sizeof(float);
93     } while (n != 0);
94   }
95 }
96