1 // Auto-generated file. Do not edit!
2 // Template: src/f32-raddextexp/avx2-p5.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2019 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 <immintrin.h>
14
15 #include <xnnpack/raddextexp.h>
16
17
18 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
19
xnn_f32_raddextexp_ukernel__avx2_p5_x64_acc2(size_t elements,const float * x,float * sum)20 void xnn_f32_raddextexp_ukernel__avx2_p5_x64_acc2(
21 size_t elements,
22 const float* x,
23 float* sum)
24 {
25 assert(elements % sizeof(float) == 0);
26
27 const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
28 const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
29 const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
30
31 // The smallest elements such that 2**elements is considered non-negligible.
32 // For smaller elements, 2**elements is replaced with zero.
33 const __m256 vmin_exponent = _mm256_set1_ps(-127.0f);
34 const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
35 const __m256 vminus_inf = _mm256_set1_ps(-INFINITY);
36
37 const __m256 vc0 = _mm256_set1_ps(1.0f);
38 const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
39 const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
40 const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
41 const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
42 const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
43
44 __m256 vaccv0 = _mm256_setzero_ps();
45 __m256 vaccv1 = _mm256_setzero_ps();
46 __m256 vacce0 = vminus_inf;
47 __m256 vacce1 = vminus_inf;
48 for (; elements >= 64 * sizeof(float); elements -= 64 * sizeof(float)) {
49 // Load 64 (8x8) inputs at a time.
50 const __m256 vx0 = _mm256_loadu_ps(x);
51 const __m256 vx1 = _mm256_loadu_ps(x + 8);
52 const __m256 vx2 = _mm256_loadu_ps(x + 16);
53 const __m256 vx3 = _mm256_loadu_ps(x + 24);
54 const __m256 vx4 = _mm256_loadu_ps(x + 32);
55 const __m256 vx5 = _mm256_loadu_ps(x + 40);
56 const __m256 vx6 = _mm256_loadu_ps(x + 48);
57 const __m256 vx7 = _mm256_loadu_ps(x + 56);
58 x += 64;
59
60 // Compute reduced argument elements := round(x / log(2)).
61 const __m256 vn0 = _mm256_round_ps(_mm256_mul_ps(vx0, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
62 const __m256 vn1 = _mm256_round_ps(_mm256_mul_ps(vx1, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
63 const __m256 vn2 = _mm256_round_ps(_mm256_mul_ps(vx2, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
64 const __m256 vn3 = _mm256_round_ps(_mm256_mul_ps(vx3, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
65 const __m256 vn4 = _mm256_round_ps(_mm256_mul_ps(vx4, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
66 const __m256 vn5 = _mm256_round_ps(_mm256_mul_ps(vx5, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
67 const __m256 vn6 = _mm256_round_ps(_mm256_mul_ps(vx6, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
68 const __m256 vn7 = _mm256_round_ps(_mm256_mul_ps(vx7, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
69
70 // Compute reduced argument t := x - elements * log(2).
71 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
72 __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0);
73 __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_hi, vx1);
74 __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_hi, vx2);
75 __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_hi, vx3);
76 __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_hi, vx4);
77 __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_hi, vx5);
78 __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_hi, vx6);
79 __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_hi, vx7);
80
81 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
82 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
83 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_lo, vt2);
84 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_lo, vt3);
85 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_lo, vt4);
86 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_lo, vt5);
87 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_lo, vt6);
88 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_lo, vt7);
89
90 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
91 __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
92 __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
93 __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
94 __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
95 __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
96 __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
97 __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
98 __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
99
100 vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
101 vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
102 vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
103 vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
104 vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
105 vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
106 vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
107 vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
108
109 vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
110 vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
111 vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
112 vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
113 vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
114 vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
115 vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
116 vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
117
118 vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
119 vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
120 vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
121 vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
122 vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
123 vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
124 vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
125 vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
126
127 vp0 = _mm256_fmadd_ps(vp0, vt0, vc0);
128 vp1 = _mm256_fmadd_ps(vp1, vt1, vc0);
129 vp2 = _mm256_fmadd_ps(vp2, vt2, vc0);
130 vp3 = _mm256_fmadd_ps(vp3, vt3, vc0);
131 vp4 = _mm256_fmadd_ps(vp4, vt4, vc0);
132 vp5 = _mm256_fmadd_ps(vp5, vt5, vc0);
133 vp6 = _mm256_fmadd_ps(vp6, vt6, vc0);
134 vp7 = _mm256_fmadd_ps(vp7, vt7, vc0);
135
136 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation where
137 // - vnX is "exponent"
138 // - vpX is "mantissa"
139 //
140 // exp2(ae) * av + exp2(be) * bv =
141 // = exp2(max(ae, be)) * exp2(ae - max(ae, be)) * av + exp2(max(ae, be)) * exp2(be - max(ae, be)) * bv
142 // = exp2(max_e) * (exp2(ae - max_e) * av + exp2(be - max_e) * bv)
143 // = exp2(max_e) * (exp2(delta_ae) * av + exp2(delta_be) * bv)
144 //
145 // For computational efficiency we may add several "extended" floating-point numbers at a time.
146 __m256 vmax_e0 = _mm256_max_ps(vacce0, vn0);
147 __m256 vmax_e1 = _mm256_max_ps(vacce1, vn1);
148 vmax_e0 = _mm256_max_ps(vmax_e0, vn2);
149 vmax_e1 = _mm256_max_ps(vmax_e1, vn3);
150 vmax_e0 = _mm256_max_ps(vmax_e0, vn4);
151 vmax_e1 = _mm256_max_ps(vmax_e1, vn5);
152 vmax_e0 = _mm256_max_ps(vmax_e0, vn6);
153 vmax_e1 = _mm256_max_ps(vmax_e1, vn7);
154
155 // For computational efficiency, replace exp2(delta_e) with 0.0f when delta_e <= -127.0.
156 // This replacement is done in two steps:
157 // 1. Clamp minimum delta_e at -127.0.
158 // 2. Map delta_e to scale factor 0.0 when delta_e == -127.0
159 const __m256 vdelta_acce0 = _mm256_max_ps(_mm256_sub_ps(vacce0, vmax_e0), vmin_exponent);
160 const __m256 vdelta_acce1 = _mm256_max_ps(_mm256_sub_ps(vacce1, vmax_e1), vmin_exponent);
161 const __m256 vdelta_e0 = _mm256_max_ps(_mm256_sub_ps(vn0, vmax_e0), vmin_exponent);
162 const __m256 vdelta_e1 = _mm256_max_ps(_mm256_sub_ps(vn1, vmax_e1), vmin_exponent);
163 const __m256 vdelta_e2 = _mm256_max_ps(_mm256_sub_ps(vn2, vmax_e0), vmin_exponent);
164 const __m256 vdelta_e3 = _mm256_max_ps(_mm256_sub_ps(vn3, vmax_e1), vmin_exponent);
165 const __m256 vdelta_e4 = _mm256_max_ps(_mm256_sub_ps(vn4, vmax_e0), vmin_exponent);
166 const __m256 vdelta_e5 = _mm256_max_ps(_mm256_sub_ps(vn5, vmax_e1), vmin_exponent);
167 const __m256 vdelta_e6 = _mm256_max_ps(_mm256_sub_ps(vn6, vmax_e0), vmin_exponent);
168 const __m256 vdelta_e7 = _mm256_max_ps(_mm256_sub_ps(vn7, vmax_e1), vmin_exponent);
169
170 // Convert delta-exponents into scale factors:
171 // - s = exp2(delta_e) when delta_e > -127.0
172 // - s = 0.0 when delta_e <= -127.0
173 //
174 // Note: delta-exponents can not exceed 0.0, thus scale factors can not exceed 1.0.
175 const __m256 vaccs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce0, vmagic_bias)), 23));
176 const __m256 vaccs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce1, vmagic_bias)), 23));
177 const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e0, vmagic_bias)), 23));
178 const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e1, vmagic_bias)), 23));
179 const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e2, vmagic_bias)), 23));
180 const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e3, vmagic_bias)), 23));
181 const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e4, vmagic_bias)), 23));
182 const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e5, vmagic_bias)), 23));
183 const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e6, vmagic_bias)), 23));
184 const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e7, vmagic_bias)), 23));
185
186 // Update accumulated "mantissa" and "exponent" values
187 vaccv0 = _mm256_mul_ps(vaccv0, vaccs0);
188 vaccv1 = _mm256_mul_ps(vaccv1, vaccs1);
189 vaccv0 = _mm256_fmadd_ps(vp0, vs0, vaccv0);
190 vaccv1 = _mm256_fmadd_ps(vp1, vs1, vaccv1);
191 vaccv0 = _mm256_fmadd_ps(vp2, vs2, vaccv0);
192 vaccv1 = _mm256_fmadd_ps(vp3, vs3, vaccv1);
193 vaccv0 = _mm256_fmadd_ps(vp4, vs4, vaccv0);
194 vaccv1 = _mm256_fmadd_ps(vp5, vs5, vaccv1);
195 vaccv0 = _mm256_fmadd_ps(vp6, vs6, vaccv0);
196 vaccv1 = _mm256_fmadd_ps(vp7, vs7, vaccv1);
197
198 vacce0 = vmax_e0;
199 vacce1 = vmax_e1;
200 }
201
202 // Reduce partial sums of "extended" floating-point numbers into a single "extended" SIMD vector of sums.
203 const __m256 vmax_acce01 = _mm256_max_ps(vacce0, vacce1);
204
205 const __m256 vdelta_acce0 = _mm256_max_ps(_mm256_sub_ps(vacce0, vmax_acce01), vmin_exponent);
206 const __m256 vdelta_acce1 = _mm256_max_ps(_mm256_sub_ps(vacce1, vmax_acce01), vmin_exponent);
207
208 const __m256 vaccs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce0, vmagic_bias)), 23));
209 const __m256 vaccs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce1, vmagic_bias)), 23));
210
211 __m256 vaccv = _mm256_mul_ps(vaccv0, vaccs0);
212 vaccv = _mm256_fmadd_ps(vaccv1, vaccs1, vaccv);
213 __m256 vacce = vmax_acce01;
214
215 for (; elements >= 8 * sizeof(float); elements -= 8 * sizeof(float)) {
216 // Load 8 inputs at a time.
217 const __m256 vx = _mm256_loadu_ps(x);
218 x += 8;
219
220 // Compute reduced argument elements := round(x / log(2)).
221 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
222
223 // Compute reduced argument t := x - elements * log(2).
224 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
225 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
226 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
227
228 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
229 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
230 vp = _mm256_fmadd_ps(vp, vt, vc3);
231 vp = _mm256_fmadd_ps(vp, vt, vc2);
232 vp = _mm256_fmadd_ps(vp, vt, vc1);
233 vp = _mm256_fmadd_ps(vp, vt, vc0);
234
235 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
236 const __m256 vmax_e = _mm256_max_ps(vacce, vn);
237
238 // For computational efficiency, clamp minimum exp2(delta_e) at -127.0. It will be mapped to 0.0 scale factor later.
239 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_e), vmin_exponent);
240 const __m256 vdelta_e = _mm256_max_ps(_mm256_sub_ps(vn, vmax_e), vmin_exponent);
241
242 // Convert exponents into scale factors.
243 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
244 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e, vmagic_bias)), 23));
245
246 // Update accumulated "mantissa" and "exponent" values.
247 vaccv = _mm256_mul_ps(vaccv, vaccs);
248 vaccv = _mm256_fmadd_ps(vp, vs, vaccv);
249
250 vacce = vmax_e;
251 }
252 if XNN_UNLIKELY(elements != 0) {
253 assert(elements >= 1 * sizeof(float));
254 assert(elements <= 7 * sizeof(float));
255 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - elements));
256
257 // Load up to 7 inputs at a time.
258 const __m256 vx = _mm256_maskload_ps(x, vmask);
259
260 // Compute reduced argument elements := round(x / log(2)).
261 __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
262
263 // Compute reduced argument t := x - elements * log(2).
264 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
265 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
266 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
267
268 // Correct reduced argument elements for masked out elements.
269 vn = _mm256_blendv_ps(vacce, vn, _mm256_castsi256_ps(vmask));
270
271 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
272 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
273 vp = _mm256_fmadd_ps(vp, vt, vc3);
274 vp = _mm256_fmadd_ps(vp, vt, vc2);
275 vp = _mm256_fmadd_ps(vp, vt, vc1);
276 vp = _mm256_fmadd_ps(vp, vt, vc0);
277 vp = _mm256_and_ps(vp, _mm256_castsi256_ps(vmask));
278
279 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
280 const __m256 vmax_e = _mm256_max_ps(vacce, vn);
281
282 // For computational efficiency, clamp minimum exp2(delta_e) at -127.0. It will be mapped to 0.0 scale factor later.
283 const __m256 vdelta_e = _mm256_max_ps(_mm256_sub_ps(vn, vmax_e), vmin_exponent);
284 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_e), vmin_exponent);
285
286 // Convert exponents into scale factors.
287 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e, vmagic_bias)), 23));
288 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
289
290 // Update accumulated "mantissa" and "exponent" values.
291 vaccv = _mm256_mul_ps(vaccv, vaccs);
292 vaccv = _mm256_fmadd_ps(vp, vs, vaccv);
293
294 vacce = vmax_e;
295 }
296
297 // Reduce partial sums of "extended" floating-point numbers into a single "extended" floating-point sum.
298 __m256 vmax_acce = _mm256_max_ps(vacce, _mm256_permute2f128_ps(vacce, vacce, 1));
299 vmax_acce = _mm256_max_ps(vmax_acce, _mm256_shuffle_ps(vmax_acce, vmax_acce, _MM_SHUFFLE(1, 0, 3, 2)));
300 vmax_acce = _mm256_max_ps(vmax_acce, _mm256_shuffle_ps(vmax_acce, vmax_acce, _MM_SHUFFLE(2, 3, 0, 1)));
301 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_acce), vmin_exponent);
302 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
303
304 vaccv = _mm256_mul_ps(vaccv, vaccs);
305 __m128 vaccv_sum = _mm_add_ps(_mm256_castps256_ps128(vaccv), _mm256_extractf128_ps(vaccv, 1));
306 vaccv_sum = _mm_add_ps(vaccv_sum, _mm_movehl_ps(vaccv_sum, vaccv_sum));
307 vaccv_sum = _mm_add_ss(vaccv_sum, _mm_movehdup_ps(vaccv_sum));
308
309 _mm_store_ss(&sum[0], vaccv_sum);
310 _mm_store_ss(&sum[1], _mm256_castps256_ps128(vmax_acce));
311
312 _mm256_zeroupper();
313 }
314