• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-f16-vcvt/scalar-fabsf.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 #include <math.h>
12 
13 #include <xnnpack/common.h>
14 #include <xnnpack/math.h>
15 #include <xnnpack/vcvt.h>
16 
17 #include <fp16.h>
18 
19 
xnn_f32_f16_vcvt_ukernel__scalar_fabsf_x2(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_f32_f16_vcvt_ukernel__scalar_fabsf_x2(
21     size_t n,
22     const float* input,
23     void* output,
24     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
25 {
26   assert(n != 0);
27   assert(n % sizeof(float) == 0);
28   assert(input != NULL);
29   assert(output != NULL);
30 
31   const float vscale_to_inf = params->scalar_fabsf.scale_to_inf;
32   const uint32_t vexp_bias = params->scalar_fabsf.exp_bias;
33   const float vscale_to_zero = params->scalar_fabsf.scale_to_zero;
34   const uint32_t vexpw_max = params->scalar_fabsf.expw_max;
35   const uint32_t vbias_min = params->scalar_fabsf.bias_min;
36   const uint16_t vexph_mask = params->scalar_fabsf.exph_mask;
37   const uint16_t vmanth_mask = params->scalar_fabsf.manth_mask;
38   const uint16_t vnanh = params->scalar_fabsf.nanh;
39 
40   uint16_t* o = (uint16_t*) output;
41   for (; n >= 2 * sizeof(float); n -= 2 * sizeof(float)) {
42     const float vx0 = input[0];
43     const float vx1 = input[1];
44     input += 2;
45 
46     const float vabsx0 = fabsf(vx0);
47     const float vabsx1 = fabsf(vx1);
48     uint32_t vsignw0 = fp32_to_bits(vx0);
49     uint32_t vsignw1 = fp32_to_bits(vx1);
50 
51     const uint32_t vnonsignw0 = fp32_to_bits(vabsx0);
52     const uint32_t vnonsignw1 = fp32_to_bits(vabsx1);
53     float vf0 = vabsx0 * vscale_to_inf;
54     float vf1 = vabsx1 * vscale_to_inf;
55 
56     uint32_t vbias0 = vnonsignw0 + vexp_bias;
57     uint32_t vbias1 = vnonsignw1 + vexp_bias;
58     vsignw0 ^= vnonsignw0;
59     vsignw1 ^= vnonsignw1;
60 
61     vf0 *= vscale_to_zero;
62     vf1 *= vscale_to_zero;
63     vbias0 &= vexpw_max;
64     vbias1 &= vexpw_max;
65 
66     vbias0 = math_max_u32(vbias0, vbias_min);
67     vbias1 = math_max_u32(vbias1, vbias_min);
68 
69     vf0 += fp32_from_bits(vbias0);
70     vf1 += fp32_from_bits(vbias1);
71 
72     const uint32_t vbits0 = fp32_to_bits(vf0);
73     const uint32_t vbits1 = fp32_to_bits(vf1);
74 
75     const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
76     const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
77     const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
78     const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
79     const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
80     const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
81 
82     uint16_t vh0 = vexph0 + vmanth0;
83     uint16_t vh1 = vexph1 + vmanth1;
84     if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
85       vh0 = vnanh;
86     }
87     if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
88       vh1 = vnanh;
89     }
90     vh0 |= vsignh0;
91     vh1 |= vsignh1;
92 
93     o[0] = vh0;
94     o[1] = vh1;
95     o += 2;
96   }
97   if XNN_UNLIKELY(n != 0) {
98     const float vx = *input;
99 
100     const float vabsx = fabsf(vx);
101     uint32_t vsignw = fp32_to_bits(vx);
102 
103     const uint32_t vnonsignw = fp32_to_bits(vabsx);
104     float vf = vabsx * vscale_to_inf;
105 
106     uint32_t vbias = vnonsignw + vexp_bias;
107     vsignw ^= vnonsignw;
108 
109     vf *= vscale_to_zero;
110     vbias &= vexpw_max;
111 
112     vbias = math_max_u32(vbias, vbias_min);
113 
114     vf += fp32_from_bits(vbias);
115 
116     const uint32_t vbits = fp32_to_bits(vf);
117 
118     const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
119     const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
120     const uint16_t vsignh = (uint16_t) (vsignw >> 16);
121 
122     uint16_t vh = vexph + vmanth;
123     if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
124       vh = vnanh;
125     }
126     vh |= vsignh;
127 
128     *o = vh;
129   }
130 }
131