• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fp16.h>
17 
18 
xnn_f32_qu8_vcvt_ukernel__wasm_fmagic_x3(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__wasm_fmagic_x3(
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_fmagic.scale;
31   const float voutput_min_less_zero_point = params->scalar_fmagic.output_min_less_zero_point;
32   const float voutput_max_less_zero_point = params->scalar_fmagic.output_max_less_zero_point;
33   const float vmagic_bias = params->scalar_fmagic.magic_bias;
34   const int32_t vmagic_bias_less_zero_point = params->scalar_fmagic.magic_bias_less_zero_point;
35 
36   for (; n >= 3 * sizeof(float); n -= 3 * sizeof(float)) {
37     float vx0 = x[0];
38     float vx1 = x[1];
39     float vx2 = x[2];
40     x += 3;
41 
42     vx0 *= vscale;
43     vx1 *= vscale;
44     vx2 *= vscale;
45 
46     vx0 = __builtin_wasm_max_f32(vx0, voutput_min_less_zero_point);
47     vx1 = __builtin_wasm_max_f32(vx1, voutput_min_less_zero_point);
48     vx2 = __builtin_wasm_max_f32(vx2, voutput_min_less_zero_point);
49 
50     vx0 = __builtin_wasm_min_f32(vx0, voutput_max_less_zero_point);
51     vx1 = __builtin_wasm_min_f32(vx1, voutput_max_less_zero_point);
52     vx2 = __builtin_wasm_min_f32(vx2, voutput_max_less_zero_point);
53 
54     vx0 += vmagic_bias;
55     vx1 += vmagic_bias;
56     vx2 += vmagic_bias;
57 
58     int32_t vy0 = (int32_t) fp32_to_bits(vx0);
59     int32_t vy1 = (int32_t) fp32_to_bits(vx1);
60     int32_t vy2 = (int32_t) fp32_to_bits(vx2);
61 
62     vy0 -= vmagic_bias_less_zero_point;
63     vy1 -= vmagic_bias_less_zero_point;
64     vy2 -= vmagic_bias_less_zero_point;
65 
66     y[0] = (uint8_t) vy0;
67     y[1] = (uint8_t) vy1;
68     y[2] = (uint8_t) vy2;
69     y += 3;
70   }
71   if XNN_UNLIKELY(n != 0) {
72     do {
73       float vx = *x++;
74       vx *= vscale;
75       vx = __builtin_wasm_max_f32(vx, voutput_min_less_zero_point);
76       vx = __builtin_wasm_min_f32(vx, voutput_max_less_zero_point);
77       vx += vmagic_bias;
78 
79       int32_t vy = (int32_t) fp32_to_bits(vx);
80       vy -= vmagic_bias_less_zero_point;
81 
82       *y++ = (uint8_t) vy;
83 
84       n -= sizeof(float);
85     } while (n != 0);
86   }
87 }
88