Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-sse2-u16.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_u16(
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 >= 16 * sizeof(float); batch -= 16 * 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 input += 16;
57
58 const __m128 vabsx0 = _mm_and_ps(vx0, vnonsign_mask);
59 const __m128 vabsx1 = _mm_and_ps(vx1, vnonsign_mask);
60 const __m128 vabsx2 = _mm_and_ps(vx2, vnonsign_mask);
61 const __m128 vabsx3 = _mm_and_ps(vx3, vnonsign_mask);
62
63 const __m128 vsignx0 = _mm_xor_ps(vx0, vabsx0);
64 const __m128 vsignx1 = _mm_xor_ps(vx1, vabsx1);
65 const __m128 vsignx2 = _mm_xor_ps(vx2, vabsx2);
66 const __m128 vsignx3 = _mm_xor_ps(vx3, vabsx3);
67
68 __m128i vbias0 = _mm_add_epi32(_mm_castps_si128(vabsx0), vexp_bias);
69 __m128i vbias1 = _mm_add_epi32(_mm_castps_si128(vabsx1), vexp_bias);
70 __m128i vbias2 = _mm_add_epi32(_mm_castps_si128(vabsx2), vexp_bias);
71 __m128i vbias3 = _mm_add_epi32(_mm_castps_si128(vabsx3), vexp_bias);
72
73 __m128 vf0 = _mm_mul_ps(vabsx0, vscale_to_inf);
74 __m128 vf1 = _mm_mul_ps(vabsx1, vscale_to_inf);
75 __m128 vf2 = _mm_mul_ps(vabsx2, vscale_to_inf);
76 __m128 vf3 = _mm_mul_ps(vabsx3, vscale_to_inf);
77
78 const __m128i vnanmaskw0 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx0), vexpw_max);
79 const __m128i vnanmaskw1 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx1), vexpw_max);
80 const __m128i vnanmaskw2 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx2), vexpw_max);
81 const __m128i vnanmaskw3 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx3), vexpw_max);
82
83 vbias0 = _mm_and_si128(vbias0, vexpw_max);
84 vbias1 = _mm_and_si128(vbias1, vexpw_max);
85 vbias2 = _mm_and_si128(vbias2, vexpw_max);
86 vbias3 = _mm_and_si128(vbias3, vexpw_max);
87
88 vf0 = _mm_mul_ps(vf0, vscale_to_zero);
89 vf1 = _mm_mul_ps(vf1, vscale_to_zero);
90 vf2 = _mm_mul_ps(vf2, vscale_to_zero);
91 vf3 = _mm_mul_ps(vf3, vscale_to_zero);
92
93 const __m128i vnanmaskh0 = _mm_packs_epi32(vnanmaskw0, vnanmaskw1);
94 const __m128i vnanmaskh1 = _mm_packs_epi32(vnanmaskw2, vnanmaskw3);
95
96 const __m128i vsignh0 = _mm_packs_epi32(_mm_castps_si128(vsignx0), _mm_castps_si128(vsignx1));
97 const __m128i vsignh1 = _mm_packs_epi32(_mm_castps_si128(vsignx2), _mm_castps_si128(vsignx3));
98
99 vbias0 = _mm_max_epi16(vbias0, vbias_min);
100 vbias1 = _mm_max_epi16(vbias1, vbias_min);
101 vbias2 = _mm_max_epi16(vbias2, vbias_min);
102 vbias3 = _mm_max_epi16(vbias3, vbias_min);
103
104 __m128i vh0 = _mm_and_si128(vnanh, vnanmaskh0);
105 __m128i vh1 = _mm_and_si128(vnanh, vnanmaskh1);
106
107 vf0 = _mm_add_ps(vf0, _mm_castsi128_ps(vbias0));
108 vf1 = _mm_add_ps(vf1, _mm_castsi128_ps(vbias1));
109 vf2 = _mm_add_ps(vf2, _mm_castsi128_ps(vbias2));
110 vf3 = _mm_add_ps(vf3, _mm_castsi128_ps(vbias3));
111
112 vh0 = _mm_or_si128(vh0, vsignh0);
113 vh1 = _mm_or_si128(vh1, vsignh1);
114
115 __m128i vexpw0 = _mm_srli_epi32(_mm_castps_si128(vf0), 13);
116 __m128i vexpw1 = _mm_srli_epi32(_mm_castps_si128(vf1), 13);
117 __m128i vexpw2 = _mm_srli_epi32(_mm_castps_si128(vf2), 13);
118 __m128i vexpw3 = _mm_srli_epi32(_mm_castps_si128(vf3), 13);
119
120 const __m128i vmantw0 = _mm_and_si128(_mm_castps_si128(vf0), vmanth_mask);
121 const __m128i vmantw1 = _mm_and_si128(_mm_castps_si128(vf1), vmanth_mask);
122 const __m128i vmantw2 = _mm_and_si128(_mm_castps_si128(vf2), vmanth_mask);
123 const __m128i vmantw3 = _mm_and_si128(_mm_castps_si128(vf3), vmanth_mask);
124
125 vexpw0 = _mm_and_si128(vexpw0, vexph_mask);
126 vexpw1 = _mm_and_si128(vexpw1, vexph_mask);
127 vexpw2 = _mm_and_si128(vexpw2, vexph_mask);
128 vexpw3 = _mm_and_si128(vexpw3, vexph_mask);
129
130 const __m128i vnonsignw0 = _mm_add_epi32(vmantw0, vexpw0);
131 const __m128i vnonsignw1 = _mm_add_epi32(vmantw1, vexpw1);
132 const __m128i vnonsignw2 = _mm_add_epi32(vmantw2, vexpw2);
133 const __m128i vnonsignw3 = _mm_add_epi32(vmantw3, vexpw3);
134
135 const __m128i vnonsignh0 = _mm_packs_epi32(vnonsignw0, vnonsignw1);
136 const __m128i vnonsignh1 = _mm_packs_epi32(vnonsignw2, vnonsignw3);
137
138 vh0 = _mm_or_si128(vh0, _mm_andnot_si128(vnanmaskh0, vnonsignh0));
139 vh1 = _mm_or_si128(vh1, _mm_andnot_si128(vnanmaskh1, vnonsignh1));
140
141 _mm_storeu_si128((__m128i*) o, vh0);
142 _mm_storeu_si128((__m128i*) (o + 8), vh1);
143 o += 16;
144 }
145 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
146 const __m128 vx_lo = _mm_loadu_ps(input);
147 const __m128 vx_hi = _mm_loadu_ps(input + 4);
148 input += 8;
149
150 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
151 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
152
153 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
154 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
155 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
156 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
157 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
158 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
159 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
160 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
161
162 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
163 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
164 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
165 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
166 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
167 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
168
169 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
170 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
171 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
172
173 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
174 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
175 vh = _mm_or_si128(vh, vsignh);
176
177 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
178 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
179 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
180 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
181
182 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
183 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
184
185 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
186 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
187
188 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
189
190 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
191
192 _mm_storeu_si128((__m128i*) o, vh);
193 o += 8;
194 }
195 if XNN_UNPREDICTABLE(batch != 0) {
196 const __m128 vx_lo = _mm_loadu_ps(input);
197 const float* input_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
198 const __m128 vx_hi = _mm_loadu_ps(input_hi);
199
200 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
201 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
202
203 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
204 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
205 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
206 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
207 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
208 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
209 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
210 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
211
212 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
213 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
214 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
215 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
216 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
217 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
218
219 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
220 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
221 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
222
223 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
224 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
225 vh = _mm_or_si128(vh, vsignh);
226
227 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
228 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
229 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
230 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
231
232 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
233 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
234
235 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
236 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
237
238 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
239
240 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
241
242 if (batch & (4 * sizeof(float))) {
243 _mm_storel_epi64((__m128i*) o, vh);
244 vh = _mm_unpackhi_epi64(vh, vh);
245 o += 4;
246 }
247 if (batch & (2 * sizeof(float))) {
248 unaligned_store_u32(o, (uint32_t) _mm_cvtsi128_si32(vh));
249 vh = _mm_srli_epi64(vh, 32);
250 o += 2;
251 }
252 if (batch & (1 * sizeof(float))) {
253 *o = (uint16_t) _mm_cvtsi128_si32(vh);
254 }
255 }
256 }