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_x4(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_x4(
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 >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
42 const float vx0 = input[0];
43 const float vx1 = input[1];
44 const float vx2 = input[2];
45 const float vx3 = input[3];
46 input += 4;
47
48 const float vabsx0 = fabsf(vx0);
49 const float vabsx1 = fabsf(vx1);
50 const float vabsx2 = fabsf(vx2);
51 const float vabsx3 = fabsf(vx3);
52 uint32_t vsignw0 = fp32_to_bits(vx0);
53 uint32_t vsignw1 = fp32_to_bits(vx1);
54 uint32_t vsignw2 = fp32_to_bits(vx2);
55 uint32_t vsignw3 = fp32_to_bits(vx3);
56
57 const uint32_t vnonsignw0 = fp32_to_bits(vabsx0);
58 const uint32_t vnonsignw1 = fp32_to_bits(vabsx1);
59 const uint32_t vnonsignw2 = fp32_to_bits(vabsx2);
60 const uint32_t vnonsignw3 = fp32_to_bits(vabsx3);
61 float vf0 = vabsx0 * vscale_to_inf;
62 float vf1 = vabsx1 * vscale_to_inf;
63 float vf2 = vabsx2 * vscale_to_inf;
64 float vf3 = vabsx3 * vscale_to_inf;
65
66 uint32_t vbias0 = vnonsignw0 + vexp_bias;
67 uint32_t vbias1 = vnonsignw1 + vexp_bias;
68 uint32_t vbias2 = vnonsignw2 + vexp_bias;
69 uint32_t vbias3 = vnonsignw3 + vexp_bias;
70 vsignw0 ^= vnonsignw0;
71 vsignw1 ^= vnonsignw1;
72 vsignw2 ^= vnonsignw2;
73 vsignw3 ^= vnonsignw3;
74
75 vf0 *= vscale_to_zero;
76 vf1 *= vscale_to_zero;
77 vf2 *= vscale_to_zero;
78 vf3 *= vscale_to_zero;
79 vbias0 &= vexpw_max;
80 vbias1 &= vexpw_max;
81 vbias2 &= vexpw_max;
82 vbias3 &= vexpw_max;
83
84 vbias0 = math_max_u32(vbias0, vbias_min);
85 vbias1 = math_max_u32(vbias1, vbias_min);
86 vbias2 = math_max_u32(vbias2, vbias_min);
87 vbias3 = math_max_u32(vbias3, vbias_min);
88
89 vf0 += fp32_from_bits(vbias0);
90 vf1 += fp32_from_bits(vbias1);
91 vf2 += fp32_from_bits(vbias2);
92 vf3 += fp32_from_bits(vbias3);
93
94 const uint32_t vbits0 = fp32_to_bits(vf0);
95 const uint32_t vbits1 = fp32_to_bits(vf1);
96 const uint32_t vbits2 = fp32_to_bits(vf2);
97 const uint32_t vbits3 = fp32_to_bits(vf3);
98
99 const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
100 const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
101 const uint16_t vexph2 = (uint16_t) (vbits2 >> 13) & vexph_mask;
102 const uint16_t vexph3 = (uint16_t) (vbits3 >> 13) & vexph_mask;
103 const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
104 const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
105 const uint16_t vmanth2 = (uint16_t) vbits2 & vmanth_mask;
106 const uint16_t vmanth3 = (uint16_t) vbits3 & vmanth_mask;
107 const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
108 const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
109 const uint16_t vsignh2 = (uint16_t) (vsignw2 >> 16);
110 const uint16_t vsignh3 = (uint16_t) (vsignw3 >> 16);
111
112 uint16_t vh0 = vexph0 + vmanth0;
113 uint16_t vh1 = vexph1 + vmanth1;
114 uint16_t vh2 = vexph2 + vmanth2;
115 uint16_t vh3 = vexph3 + vmanth3;
116 if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
117 vh0 = vnanh;
118 }
119 if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
120 vh1 = vnanh;
121 }
122 if XNN_UNPREDICTABLE(vnonsignw2 > vexpw_max) {
123 vh2 = vnanh;
124 }
125 if XNN_UNPREDICTABLE(vnonsignw3 > vexpw_max) {
126 vh3 = vnanh;
127 }
128 vh0 |= vsignh0;
129 vh1 |= vsignh1;
130 vh2 |= vsignh2;
131 vh3 |= vsignh3;
132
133 o[0] = vh0;
134 o[1] = vh1;
135 o[2] = vh2;
136 o[3] = vh3;
137 o += 4;
138 }
139 if XNN_UNLIKELY(n != 0) {
140 do {
141 const float vx = *input++;
142
143 const float vabsx = fabsf(vx);
144 uint32_t vsignw = fp32_to_bits(vx);
145
146 const uint32_t vnonsignw = fp32_to_bits(vabsx);
147 float vf = vabsx * vscale_to_inf;
148
149 uint32_t vbias = vnonsignw + vexp_bias;
150 vsignw ^= vnonsignw;
151
152 vf *= vscale_to_zero;
153 vbias &= vexpw_max;
154
155 vbias = math_max_u32(vbias, vbias_min);
156
157 vf += fp32_from_bits(vbias);
158
159 const uint32_t vbits = fp32_to_bits(vf);
160
161 const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
162 const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
163 const uint16_t vsignh = (uint16_t) (vsignw >> 16);
164
165 uint16_t vh = vexph + vmanth;
166 if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
167 vh = vnanh;
168 }
169 vh |= vsignh;
170
171 *o++ = vh;
172
173 n -= sizeof(float);
174 } while (n != 0);
175 }
176 }
177