Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-sse2-u24.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-vcvt/sse.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 <emmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/unaligned.h"
16 #include "xnnpack/vcvt.h"
17
18
19 void xnn_f32_f16_vcvt_ukernel__sse2_u24(
20 size_t batch,
21 const float* input,
22 xnn_float16* output,
23 const void* params) XNN_OOB_READS
24 {
25 assert(batch != 0);
26 assert(batch % sizeof(float) == 0);
27 assert(input != NULL);
28 assert(output != NULL);
29
30 const __m128 vnonsign_mask = _mm_castsi128_ps(_mm_set1_epi32(UINT32_C(0x7FFFFFFF)));
31 const __m128i vexp_bias = _mm_set1_epi32(UINT32_C(0x07800000));
32 const __m128 vscale_to_inf = _mm_set1_ps(0x1.0p+112f);
33 const __m128i vexpw_max = _mm_set1_epi32(UINT32_C(0x7F800000));
34 const __m128 vscale_to_zero = _mm_set1_ps(0x1.0p-110f);
35 const __m128i vbias_min = _mm_set1_epi32(UINT32_C(0x40008000)); // 0x8000, 0x4000, 0x8000, 0x4000, ...
36 const __m128i vmanth_mask = _mm_set1_epi32(UINT32_C(0x00000FFF));
37 const __m128i vexph_mask = _mm_set1_epi32(UINT32_C(0x00007C00));
38 const __m128i vnanh = _mm_set1_epi16(UINT16_C(0x7E00));
39
40 XNN_FORCE_REALIZATION(vnonsign_mask);
41 XNN_FORCE_REALIZATION(vexp_bias);
42 XNN_FORCE_REALIZATION(vscale_to_inf);
43 XNN_FORCE_REALIZATION(vexpw_max);
44 XNN_FORCE_REALIZATION(vscale_to_zero);
45 XNN_FORCE_REALIZATION(vbias_min);
46 XNN_FORCE_REALIZATION(vmanth_mask);
47 XNN_FORCE_REALIZATION(vexph_mask);
48 XNN_FORCE_REALIZATION(vnanh);
49
50 uint16_t* o = (uint16_t*) output;
51 for (; batch >= 24 * sizeof(float); batch -= 24 * sizeof(float)) {
52 const __m128 vx0 = _mm_loadu_ps(input);
53 const __m128 vx1 = _mm_loadu_ps(input + 4);
54 const __m128 vx2 = _mm_loadu_ps(input + 8);
55 const __m128 vx3 = _mm_loadu_ps(input + 12);
56 const __m128 vx4 = _mm_loadu_ps(input + 16);
57 const __m128 vx5 = _mm_loadu_ps(input + 20);
58 input += 24;
59
60 const __m128 vabsx0 = _mm_and_ps(vx0, vnonsign_mask);
61 const __m128 vabsx1 = _mm_and_ps(vx1, vnonsign_mask);
62 const __m128 vabsx2 = _mm_and_ps(vx2, vnonsign_mask);
63 const __m128 vabsx3 = _mm_and_ps(vx3, vnonsign_mask);
64 const __m128 vabsx4 = _mm_and_ps(vx4, vnonsign_mask);
65 const __m128 vabsx5 = _mm_and_ps(vx5, vnonsign_mask);
66
67 const __m128 vsignx0 = _mm_xor_ps(vx0, vabsx0);
68 const __m128 vsignx1 = _mm_xor_ps(vx1, vabsx1);
69 const __m128 vsignx2 = _mm_xor_ps(vx2, vabsx2);
70 const __m128 vsignx3 = _mm_xor_ps(vx3, vabsx3);
71 const __m128 vsignx4 = _mm_xor_ps(vx4, vabsx4);
72 const __m128 vsignx5 = _mm_xor_ps(vx5, vabsx5);
73
74 __m128i vbias0 = _mm_add_epi32(_mm_castps_si128(vabsx0), vexp_bias);
75 __m128i vbias1 = _mm_add_epi32(_mm_castps_si128(vabsx1), vexp_bias);
76 __m128i vbias2 = _mm_add_epi32(_mm_castps_si128(vabsx2), vexp_bias);
77 __m128i vbias3 = _mm_add_epi32(_mm_castps_si128(vabsx3), vexp_bias);
78 __m128i vbias4 = _mm_add_epi32(_mm_castps_si128(vabsx4), vexp_bias);
79 __m128i vbias5 = _mm_add_epi32(_mm_castps_si128(vabsx5), vexp_bias);
80
81 __m128 vf0 = _mm_mul_ps(vabsx0, vscale_to_inf);
82 __m128 vf1 = _mm_mul_ps(vabsx1, vscale_to_inf);
83 __m128 vf2 = _mm_mul_ps(vabsx2, vscale_to_inf);
84 __m128 vf3 = _mm_mul_ps(vabsx3, vscale_to_inf);
85 __m128 vf4 = _mm_mul_ps(vabsx4, vscale_to_inf);
86 __m128 vf5 = _mm_mul_ps(vabsx5, vscale_to_inf);
87
88 const __m128i vnanmaskw0 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx0), vexpw_max);
89 const __m128i vnanmaskw1 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx1), vexpw_max);
90 const __m128i vnanmaskw2 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx2), vexpw_max);
91 const __m128i vnanmaskw3 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx3), vexpw_max);
92 const __m128i vnanmaskw4 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx4), vexpw_max);
93 const __m128i vnanmaskw5 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx5), vexpw_max);
94
95 vbias0 = _mm_and_si128(vbias0, vexpw_max);
96 vbias1 = _mm_and_si128(vbias1, vexpw_max);
97 vbias2 = _mm_and_si128(vbias2, vexpw_max);
98 vbias3 = _mm_and_si128(vbias3, vexpw_max);
99 vbias4 = _mm_and_si128(vbias4, vexpw_max);
100 vbias5 = _mm_and_si128(vbias5, vexpw_max);
101
102 vf0 = _mm_mul_ps(vf0, vscale_to_zero);
103 vf1 = _mm_mul_ps(vf1, vscale_to_zero);
104 vf2 = _mm_mul_ps(vf2, vscale_to_zero);
105 vf3 = _mm_mul_ps(vf3, vscale_to_zero);
106 vf4 = _mm_mul_ps(vf4, vscale_to_zero);
107 vf5 = _mm_mul_ps(vf5, vscale_to_zero);
108
109 const __m128i vnanmaskh0 = _mm_packs_epi32(vnanmaskw0, vnanmaskw1);
110 const __m128i vnanmaskh1 = _mm_packs_epi32(vnanmaskw2, vnanmaskw3);
111 const __m128i vnanmaskh2 = _mm_packs_epi32(vnanmaskw4, vnanmaskw5);
112
113 const __m128i vsignh0 = _mm_packs_epi32(_mm_castps_si128(vsignx0), _mm_castps_si128(vsignx1));
114 const __m128i vsignh1 = _mm_packs_epi32(_mm_castps_si128(vsignx2), _mm_castps_si128(vsignx3));
115 const __m128i vsignh2 = _mm_packs_epi32(_mm_castps_si128(vsignx4), _mm_castps_si128(vsignx5));
116
117 vbias0 = _mm_max_epi16(vbias0, vbias_min);
118 vbias1 = _mm_max_epi16(vbias1, vbias_min);
119 vbias2 = _mm_max_epi16(vbias2, vbias_min);
120 vbias3 = _mm_max_epi16(vbias3, vbias_min);
121 vbias4 = _mm_max_epi16(vbias4, vbias_min);
122 vbias5 = _mm_max_epi16(vbias5, vbias_min);
123
124 __m128i vh0 = _mm_and_si128(vnanh, vnanmaskh0);
125 __m128i vh1 = _mm_and_si128(vnanh, vnanmaskh1);
126 __m128i vh2 = _mm_and_si128(vnanh, vnanmaskh2);
127
128 vf0 = _mm_add_ps(vf0, _mm_castsi128_ps(vbias0));
129 vf1 = _mm_add_ps(vf1, _mm_castsi128_ps(vbias1));
130 vf2 = _mm_add_ps(vf2, _mm_castsi128_ps(vbias2));
131 vf3 = _mm_add_ps(vf3, _mm_castsi128_ps(vbias3));
132 vf4 = _mm_add_ps(vf4, _mm_castsi128_ps(vbias4));
133 vf5 = _mm_add_ps(vf5, _mm_castsi128_ps(vbias5));
134
135 vh0 = _mm_or_si128(vh0, vsignh0);
136 vh1 = _mm_or_si128(vh1, vsignh1);
137 vh2 = _mm_or_si128(vh2, vsignh2);
138
139 __m128i vexpw0 = _mm_srli_epi32(_mm_castps_si128(vf0), 13);
140 __m128i vexpw1 = _mm_srli_epi32(_mm_castps_si128(vf1), 13);
141 __m128i vexpw2 = _mm_srli_epi32(_mm_castps_si128(vf2), 13);
142 __m128i vexpw3 = _mm_srli_epi32(_mm_castps_si128(vf3), 13);
143 __m128i vexpw4 = _mm_srli_epi32(_mm_castps_si128(vf4), 13);
144 __m128i vexpw5 = _mm_srli_epi32(_mm_castps_si128(vf5), 13);
145
146 const __m128i vmantw0 = _mm_and_si128(_mm_castps_si128(vf0), vmanth_mask);
147 const __m128i vmantw1 = _mm_and_si128(_mm_castps_si128(vf1), vmanth_mask);
148 const __m128i vmantw2 = _mm_and_si128(_mm_castps_si128(vf2), vmanth_mask);
149 const __m128i vmantw3 = _mm_and_si128(_mm_castps_si128(vf3), vmanth_mask);
150 const __m128i vmantw4 = _mm_and_si128(_mm_castps_si128(vf4), vmanth_mask);
151 const __m128i vmantw5 = _mm_and_si128(_mm_castps_si128(vf5), vmanth_mask);
152
153 vexpw0 = _mm_and_si128(vexpw0, vexph_mask);
154 vexpw1 = _mm_and_si128(vexpw1, vexph_mask);
155 vexpw2 = _mm_and_si128(vexpw2, vexph_mask);
156 vexpw3 = _mm_and_si128(vexpw3, vexph_mask);
157 vexpw4 = _mm_and_si128(vexpw4, vexph_mask);
158 vexpw5 = _mm_and_si128(vexpw5, vexph_mask);
159
160 const __m128i vnonsignw0 = _mm_add_epi32(vmantw0, vexpw0);
161 const __m128i vnonsignw1 = _mm_add_epi32(vmantw1, vexpw1);
162 const __m128i vnonsignw2 = _mm_add_epi32(vmantw2, vexpw2);
163 const __m128i vnonsignw3 = _mm_add_epi32(vmantw3, vexpw3);
164 const __m128i vnonsignw4 = _mm_add_epi32(vmantw4, vexpw4);
165 const __m128i vnonsignw5 = _mm_add_epi32(vmantw5, vexpw5);
166
167 const __m128i vnonsignh0 = _mm_packs_epi32(vnonsignw0, vnonsignw1);
168 const __m128i vnonsignh1 = _mm_packs_epi32(vnonsignw2, vnonsignw3);
169 const __m128i vnonsignh2 = _mm_packs_epi32(vnonsignw4, vnonsignw5);
170
171 vh0 = _mm_or_si128(vh0, _mm_andnot_si128(vnanmaskh0, vnonsignh0));
172 vh1 = _mm_or_si128(vh1, _mm_andnot_si128(vnanmaskh1, vnonsignh1));
173 vh2 = _mm_or_si128(vh2, _mm_andnot_si128(vnanmaskh2, vnonsignh2));
174
175 _mm_storeu_si128((__m128i*) o, vh0);
176 _mm_storeu_si128((__m128i*) (o + 8), vh1);
177 _mm_storeu_si128((__m128i*) (o + 16), vh2);
178 o += 24;
179 }
180 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
181 const __m128 vx_lo = _mm_loadu_ps(input);
182 const __m128 vx_hi = _mm_loadu_ps(input + 4);
183 input += 8;
184
185 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
186 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
187
188 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
189 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
190 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
191 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
192 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
193 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
194 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
195 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
196
197 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
198 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
199 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
200 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
201 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
202 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
203
204 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
205 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
206 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
207
208 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
209 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
210 vh = _mm_or_si128(vh, vsignh);
211
212 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
213 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
214 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
215 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
216
217 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
218 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
219
220 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
221 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
222
223 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
224
225 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
226
227 _mm_storeu_si128((__m128i*) o, vh);
228 o += 8;
229 }
230 if XNN_UNPREDICTABLE(batch != 0) {
231 const __m128 vx_lo = _mm_loadu_ps(input);
232 const float* input_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
233 const __m128 vx_hi = _mm_loadu_ps(input_hi);
234
235 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
236 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
237
238 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
239 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
240 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
241 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
242 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
243 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
244 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
245 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
246
247 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
248 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
249 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
250 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
251 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
252 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
253
254 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
255 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
256 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
257
258 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
259 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
260 vh = _mm_or_si128(vh, vsignh);
261
262 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
263 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
264 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
265 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
266
267 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
268 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
269
270 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
271 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
272
273 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
274
275 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
276
277 if (batch & (4 * sizeof(float))) {
278 _mm_storel_epi64((__m128i*) o, vh);
279 vh = _mm_unpackhi_epi64(vh, vh);
280 o += 4;
281 }
282 if (batch & (2 * sizeof(float))) {
283 unaligned_store_u32(o, (uint32_t) _mm_cvtsi128_si32(vh));
284 vh = _mm_srli_epi64(vh, 32);
285 o += 2;
286 }
287 if (batch & (1 * sizeof(float))) {
288 *o = (uint16_t) _mm_cvtsi128_si32(vh);
289 }
290 }
291 }