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
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)])17 void xnn_f32_qu8_vcvt_ukernel__scalar_imagic_x4(
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_imagic.scale;
29 const float vmagic_bias = params->scalar_imagic.magic_bias;
30 const int32_t vmagic_min = params->scalar_imagic.magic_min;
31 const int32_t vmagic_max = params->scalar_imagic.magic_max;
32 const int32_t vmagic_bias_less_zero_point = params->scalar_imagic.magic_bias_less_zero_point;
33
34 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
35 float vx0 = x[0];
36 float vx1 = x[1];
37 float vx2 = x[2];
38 float vx3 = x[3];
39 x += 4;
40
41 vx0 *= vscale;
42 vx1 *= vscale;
43 vx2 *= vscale;
44 vx3 *= vscale;
45
46 vx0 += vmagic_bias;
47 vx1 += vmagic_bias;
48 vx2 += vmagic_bias;
49 vx3 += vmagic_bias;
50
51 int32_t vy0 = (int32_t) float_as_uint32(vx0);
52 int32_t vy1 = (int32_t) float_as_uint32(vx1);
53 int32_t vy2 = (int32_t) float_as_uint32(vx2);
54 int32_t vy3 = (int32_t) float_as_uint32(vx3);
55
56 vy0 = math_max_s32(vy0, vmagic_min);
57 vy1 = math_max_s32(vy1, vmagic_min);
58 vy2 = math_max_s32(vy2, vmagic_min);
59 vy3 = math_max_s32(vy3, vmagic_min);
60
61 vy0 = math_min_s32(vy0, vmagic_max);
62 vy1 = math_min_s32(vy1, vmagic_max);
63 vy2 = math_min_s32(vy2, vmagic_max);
64 vy3 = math_min_s32(vy3, vmagic_max);
65
66 vy0 -= vmagic_bias_less_zero_point;
67 vy1 -= vmagic_bias_less_zero_point;
68 vy2 -= vmagic_bias_less_zero_point;
69 vy3 -= vmagic_bias_less_zero_point;
70
71 y[0] = (uint8_t) vy0;
72 y[1] = (uint8_t) vy1;
73 y[2] = (uint8_t) vy2;
74 y[3] = (uint8_t) vy3;
75 y += 4;
76 }
77 if XNN_UNLIKELY(n != 0) {
78 do {
79 float vx = *x++;
80 vx *= vscale;
81 vx += vmagic_bias;
82
83 int32_t vy = (int32_t) float_as_uint32(vx);
84 vy = math_max_s32(vy, vmagic_min);
85 vy = math_min_s32(vy, vmagic_max);
86 vy -= vmagic_bias_less_zero_point;
87
88 *y++ = (uint8_t) vy;
89
90 n -= sizeof(float);
91 } while (n != 0);
92 }
93 }
94