1 // Auto-generated file. Do not edit!
2 // Template: src/f32-qs8-vcvt/scalar-fmagic.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
xnn_f32_qu8_vcvt_ukernel__scalar_fmagic_x3(size_t n,const float * x,uint8_t * y,const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_qu8_vcvt_ukernel__scalar_fmagic_x3(
18 size_t n,
19 const float* x,
20 uint8_t* y,
21 const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
22 {
23 assert(n != 0);
24 assert(n % sizeof(float) == 0);
25 assert(x != NULL);
26 assert(y != NULL);
27
28 const float vscale = params->scalar_fmagic.scale;
29 const float voutput_min_less_zero_point = params->scalar_fmagic.output_min_less_zero_point;
30 const float voutput_max_less_zero_point = params->scalar_fmagic.output_max_less_zero_point;
31 const float vmagic_bias = params->scalar_fmagic.magic_bias;
32 const int32_t vmagic_bias_less_zero_point = params->scalar_fmagic.magic_bias_less_zero_point;
33
34 for (; n >= 3 * sizeof(float); n -= 3 * sizeof(float)) {
35 float vx0 = x[0];
36 float vx1 = x[1];
37 float vx2 = x[2];
38 x += 3;
39
40 vx0 *= vscale;
41 vx1 *= vscale;
42 vx2 *= vscale;
43
44 vx0 = math_max_f32(vx0, voutput_min_less_zero_point);
45 vx1 = math_max_f32(vx1, voutput_min_less_zero_point);
46 vx2 = math_max_f32(vx2, voutput_min_less_zero_point);
47
48 vx0 = math_min_f32(vx0, voutput_max_less_zero_point);
49 vx1 = math_min_f32(vx1, voutput_max_less_zero_point);
50 vx2 = math_min_f32(vx2, voutput_max_less_zero_point);
51
52 vx0 += vmagic_bias;
53 vx1 += vmagic_bias;
54 vx2 += vmagic_bias;
55
56 int32_t vy0 = (int32_t) float_as_uint32(vx0);
57 int32_t vy1 = (int32_t) float_as_uint32(vx1);
58 int32_t vy2 = (int32_t) float_as_uint32(vx2);
59
60 vy0 -= vmagic_bias_less_zero_point;
61 vy1 -= vmagic_bias_less_zero_point;
62 vy2 -= vmagic_bias_less_zero_point;
63
64 y[0] = (uint8_t) vy0;
65 y[1] = (uint8_t) vy1;
66 y[2] = (uint8_t) vy2;
67 y += 3;
68 }
69 if XNN_UNLIKELY(n != 0) {
70 do {
71 float vx = *x++;
72 vx *= vscale;
73 vx = math_max_f32(vx, voutput_min_less_zero_point);
74 vx = math_min_f32(vx, voutput_max_less_zero_point);
75 vx += vmagic_bias;
76
77 int32_t vy = (int32_t) float_as_uint32(vx);
78 vy -= vmagic_bias_less_zero_point;
79
80 *y++ = (uint8_t) vy;
81
82 n -= sizeof(float);
83 } while (n != 0);
84 }
85 }
86