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_x80_acc5(size_t elements,const float * x,float * sum)20 void xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc5(
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 vaccv2 = _mm256_setzero_ps();
47 __m256 vaccv3 = _mm256_setzero_ps();
48 __m256 vaccv4 = _mm256_setzero_ps();
49 __m256 vacce0 = vminus_inf;
50 __m256 vacce1 = vminus_inf;
51 __m256 vacce2 = vminus_inf;
52 __m256 vacce3 = vminus_inf;
53 __m256 vacce4 = vminus_inf;
54 for (; elements >= 80 * sizeof(float); elements -= 80 * sizeof(float)) {
55 // Load 80 (10x8) inputs at a time.
56 const __m256 vx0 = _mm256_loadu_ps(x);
57 const __m256 vx1 = _mm256_loadu_ps(x + 8);
58 const __m256 vx2 = _mm256_loadu_ps(x + 16);
59 const __m256 vx3 = _mm256_loadu_ps(x + 24);
60 const __m256 vx4 = _mm256_loadu_ps(x + 32);
61 const __m256 vx5 = _mm256_loadu_ps(x + 40);
62 const __m256 vx6 = _mm256_loadu_ps(x + 48);
63 const __m256 vx7 = _mm256_loadu_ps(x + 56);
64 const __m256 vx8 = _mm256_loadu_ps(x + 64);
65 const __m256 vx9 = _mm256_loadu_ps(x + 72);
66 x += 80;
67
68 // Compute reduced argument elements := round(x / log(2)).
69 const __m256 vn0 = _mm256_round_ps(_mm256_mul_ps(vx0, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
70 const __m256 vn1 = _mm256_round_ps(_mm256_mul_ps(vx1, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
71 const __m256 vn2 = _mm256_round_ps(_mm256_mul_ps(vx2, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
72 const __m256 vn3 = _mm256_round_ps(_mm256_mul_ps(vx3, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
73 const __m256 vn4 = _mm256_round_ps(_mm256_mul_ps(vx4, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
74 const __m256 vn5 = _mm256_round_ps(_mm256_mul_ps(vx5, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
75 const __m256 vn6 = _mm256_round_ps(_mm256_mul_ps(vx6, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
76 const __m256 vn7 = _mm256_round_ps(_mm256_mul_ps(vx7, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
77 const __m256 vn8 = _mm256_round_ps(_mm256_mul_ps(vx8, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
78 const __m256 vn9 = _mm256_round_ps(_mm256_mul_ps(vx9, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
79
80 // Compute reduced argument t := x - elements * log(2).
81 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
82 __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0);
83 __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_hi, vx1);
84 __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_hi, vx2);
85 __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_hi, vx3);
86 __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_hi, vx4);
87 __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_hi, vx5);
88 __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_hi, vx6);
89 __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_hi, vx7);
90 __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2_hi, vx8);
91 __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2_hi, vx9);
92
93 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
94 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
95 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_lo, vt2);
96 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_lo, vt3);
97 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_lo, vt4);
98 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_lo, vt5);
99 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_lo, vt6);
100 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_lo, vt7);
101 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2_lo, vt8);
102 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2_lo, vt9);
103
104 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
105 __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
106 __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
107 __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
108 __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
109 __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
110 __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
111 __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
112 __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
113 __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
114 __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
115
116 vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
117 vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
118 vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
119 vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
120 vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
121 vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
122 vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
123 vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
124 vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
125 vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
126
127 vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
128 vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
129 vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
130 vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
131 vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
132 vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
133 vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
134 vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
135 vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
136 vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
137
138 vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
139 vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
140 vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
141 vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
142 vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
143 vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
144 vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
145 vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
146 vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
147 vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
148
149 vp0 = _mm256_fmadd_ps(vp0, vt0, vc0);
150 vp1 = _mm256_fmadd_ps(vp1, vt1, vc0);
151 vp2 = _mm256_fmadd_ps(vp2, vt2, vc0);
152 vp3 = _mm256_fmadd_ps(vp3, vt3, vc0);
153 vp4 = _mm256_fmadd_ps(vp4, vt4, vc0);
154 vp5 = _mm256_fmadd_ps(vp5, vt5, vc0);
155 vp6 = _mm256_fmadd_ps(vp6, vt6, vc0);
156 vp7 = _mm256_fmadd_ps(vp7, vt7, vc0);
157 vp8 = _mm256_fmadd_ps(vp8, vt8, vc0);
158 vp9 = _mm256_fmadd_ps(vp9, vt9, vc0);
159
160 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation where
161 // - vnX is "exponent"
162 // - vpX is "mantissa"
163 //
164 // exp2(ae) * av + exp2(be) * bv =
165 // = exp2(max(ae, be)) * exp2(ae - max(ae, be)) * av + exp2(max(ae, be)) * exp2(be - max(ae, be)) * bv
166 // = exp2(max_e) * (exp2(ae - max_e) * av + exp2(be - max_e) * bv)
167 // = exp2(max_e) * (exp2(delta_ae) * av + exp2(delta_be) * bv)
168 //
169 // For computational efficiency we may add several "extended" floating-point numbers at a time.
170 __m256 vmax_e0 = _mm256_max_ps(vacce0, vn0);
171 __m256 vmax_e1 = _mm256_max_ps(vacce1, vn1);
172 __m256 vmax_e2 = _mm256_max_ps(vacce2, vn2);
173 __m256 vmax_e3 = _mm256_max_ps(vacce3, vn3);
174 __m256 vmax_e4 = _mm256_max_ps(vacce4, vn4);
175 vmax_e0 = _mm256_max_ps(vmax_e0, vn5);
176 vmax_e1 = _mm256_max_ps(vmax_e1, vn6);
177 vmax_e2 = _mm256_max_ps(vmax_e2, vn7);
178 vmax_e3 = _mm256_max_ps(vmax_e3, vn8);
179 vmax_e4 = _mm256_max_ps(vmax_e4, vn9);
180
181 // For computational efficiency, replace exp2(delta_e) with 0.0f when delta_e <= -127.0.
182 // This replacement is done in two steps:
183 // 1. Clamp minimum delta_e at -127.0.
184 // 2. Map delta_e to scale factor 0.0 when delta_e == -127.0
185 const __m256 vdelta_acce0 = _mm256_max_ps(_mm256_sub_ps(vacce0, vmax_e0), vmin_exponent);
186 const __m256 vdelta_acce1 = _mm256_max_ps(_mm256_sub_ps(vacce1, vmax_e1), vmin_exponent);
187 const __m256 vdelta_acce2 = _mm256_max_ps(_mm256_sub_ps(vacce2, vmax_e2), vmin_exponent);
188 const __m256 vdelta_acce3 = _mm256_max_ps(_mm256_sub_ps(vacce3, vmax_e3), vmin_exponent);
189 const __m256 vdelta_acce4 = _mm256_max_ps(_mm256_sub_ps(vacce4, vmax_e4), vmin_exponent);
190 const __m256 vdelta_e0 = _mm256_max_ps(_mm256_sub_ps(vn0, vmax_e0), vmin_exponent);
191 const __m256 vdelta_e1 = _mm256_max_ps(_mm256_sub_ps(vn1, vmax_e1), vmin_exponent);
192 const __m256 vdelta_e2 = _mm256_max_ps(_mm256_sub_ps(vn2, vmax_e2), vmin_exponent);
193 const __m256 vdelta_e3 = _mm256_max_ps(_mm256_sub_ps(vn3, vmax_e3), vmin_exponent);
194 const __m256 vdelta_e4 = _mm256_max_ps(_mm256_sub_ps(vn4, vmax_e4), vmin_exponent);
195 const __m256 vdelta_e5 = _mm256_max_ps(_mm256_sub_ps(vn5, vmax_e0), vmin_exponent);
196 const __m256 vdelta_e6 = _mm256_max_ps(_mm256_sub_ps(vn6, vmax_e1), vmin_exponent);
197 const __m256 vdelta_e7 = _mm256_max_ps(_mm256_sub_ps(vn7, vmax_e2), vmin_exponent);
198 const __m256 vdelta_e8 = _mm256_max_ps(_mm256_sub_ps(vn8, vmax_e3), vmin_exponent);
199 const __m256 vdelta_e9 = _mm256_max_ps(_mm256_sub_ps(vn9, vmax_e4), vmin_exponent);
200
201 // Convert delta-exponents into scale factors:
202 // - s = exp2(delta_e) when delta_e > -127.0
203 // - s = 0.0 when delta_e <= -127.0
204 //
205 // Note: delta-exponents can not exceed 0.0, thus scale factors can not exceed 1.0.
206 const __m256 vaccs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce0, vmagic_bias)), 23));
207 const __m256 vaccs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce1, vmagic_bias)), 23));
208 const __m256 vaccs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce2, vmagic_bias)), 23));
209 const __m256 vaccs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce3, vmagic_bias)), 23));
210 const __m256 vaccs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce4, vmagic_bias)), 23));
211 const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e0, vmagic_bias)), 23));
212 const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e1, vmagic_bias)), 23));
213 const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e2, vmagic_bias)), 23));
214 const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e3, vmagic_bias)), 23));
215 const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e4, vmagic_bias)), 23));
216 const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e5, vmagic_bias)), 23));
217 const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e6, vmagic_bias)), 23));
218 const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e7, vmagic_bias)), 23));
219 const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e8, vmagic_bias)), 23));
220 const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e9, vmagic_bias)), 23));
221
222 // Update accumulated "mantissa" and "exponent" values
223 vaccv0 = _mm256_mul_ps(vaccv0, vaccs0);
224 vaccv1 = _mm256_mul_ps(vaccv1, vaccs1);
225 vaccv2 = _mm256_mul_ps(vaccv2, vaccs2);
226 vaccv3 = _mm256_mul_ps(vaccv3, vaccs3);
227 vaccv4 = _mm256_mul_ps(vaccv4, vaccs4);
228 vaccv0 = _mm256_fmadd_ps(vp0, vs0, vaccv0);
229 vaccv1 = _mm256_fmadd_ps(vp1, vs1, vaccv1);
230 vaccv2 = _mm256_fmadd_ps(vp2, vs2, vaccv2);
231 vaccv3 = _mm256_fmadd_ps(vp3, vs3, vaccv3);
232 vaccv4 = _mm256_fmadd_ps(vp4, vs4, vaccv4);
233 vaccv0 = _mm256_fmadd_ps(vp5, vs5, vaccv0);
234 vaccv1 = _mm256_fmadd_ps(vp6, vs6, vaccv1);
235 vaccv2 = _mm256_fmadd_ps(vp7, vs7, vaccv2);
236 vaccv3 = _mm256_fmadd_ps(vp8, vs8, vaccv3);
237 vaccv4 = _mm256_fmadd_ps(vp9, vs9, vaccv4);
238
239 vacce0 = vmax_e0;
240 vacce1 = vmax_e1;
241 vacce2 = vmax_e2;
242 vacce3 = vmax_e3;
243 vacce4 = vmax_e4;
244 }
245
246 // Reduce partial sums of "extended" floating-point numbers into a single "extended" SIMD vector of sums.
247 const __m256 vmax_acce01 = _mm256_max_ps(vacce0, vacce1);
248 const __m256 vmax_acce23 = _mm256_max_ps(vacce2, vacce3);
249 const __m256 vmax_acce4 = vacce4;
250 const __m256 vmax_acce0123 = _mm256_max_ps(vmax_acce01, vmax_acce23);
251 const __m256 vmax_acce01234 = _mm256_max_ps(vmax_acce0123, vmax_acce4);
252
253 const __m256 vdelta_acce0 = _mm256_max_ps(_mm256_sub_ps(vacce0, vmax_acce01234), vmin_exponent);
254 const __m256 vdelta_acce1 = _mm256_max_ps(_mm256_sub_ps(vacce1, vmax_acce01234), vmin_exponent);
255 const __m256 vdelta_acce2 = _mm256_max_ps(_mm256_sub_ps(vacce2, vmax_acce01234), vmin_exponent);
256 const __m256 vdelta_acce3 = _mm256_max_ps(_mm256_sub_ps(vacce3, vmax_acce01234), vmin_exponent);
257 const __m256 vdelta_acce4 = _mm256_max_ps(_mm256_sub_ps(vacce4, vmax_acce01234), vmin_exponent);
258
259 const __m256 vaccs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce0, vmagic_bias)), 23));
260 const __m256 vaccs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce1, vmagic_bias)), 23));
261 const __m256 vaccs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce2, vmagic_bias)), 23));
262 const __m256 vaccs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce3, vmagic_bias)), 23));
263 const __m256 vaccs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce4, vmagic_bias)), 23));
264
265 __m256 vaccv = _mm256_mul_ps(vaccv0, vaccs0);
266 vaccv = _mm256_fmadd_ps(vaccv1, vaccs1, vaccv);
267 vaccv = _mm256_fmadd_ps(vaccv2, vaccs2, vaccv);
268 vaccv = _mm256_fmadd_ps(vaccv3, vaccs3, vaccv);
269 vaccv = _mm256_fmadd_ps(vaccv4, vaccs4, vaccv);
270 __m256 vacce = vmax_acce01234;
271
272 for (; elements >= 8 * sizeof(float); elements -= 8 * sizeof(float)) {
273 // Load 8 inputs at a time.
274 const __m256 vx = _mm256_loadu_ps(x);
275 x += 8;
276
277 // Compute reduced argument elements := round(x / log(2)).
278 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
279
280 // Compute reduced argument t := x - elements * log(2).
281 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
282 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
283 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
284
285 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
286 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
287 vp = _mm256_fmadd_ps(vp, vt, vc3);
288 vp = _mm256_fmadd_ps(vp, vt, vc2);
289 vp = _mm256_fmadd_ps(vp, vt, vc1);
290 vp = _mm256_fmadd_ps(vp, vt, vc0);
291
292 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
293 const __m256 vmax_e = _mm256_max_ps(vacce, vn);
294
295 // For computational efficiency, clamp minimum exp2(delta_e) at -127.0. It will be mapped to 0.0 scale factor later.
296 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_e), vmin_exponent);
297 const __m256 vdelta_e = _mm256_max_ps(_mm256_sub_ps(vn, vmax_e), vmin_exponent);
298
299 // Convert exponents into scale factors.
300 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
301 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e, vmagic_bias)), 23));
302
303 // Update accumulated "mantissa" and "exponent" values.
304 vaccv = _mm256_mul_ps(vaccv, vaccs);
305 vaccv = _mm256_fmadd_ps(vp, vs, vaccv);
306
307 vacce = vmax_e;
308 }
309 if XNN_UNLIKELY(elements != 0) {
310 assert(elements >= 1 * sizeof(float));
311 assert(elements <= 7 * sizeof(float));
312 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - elements));
313
314 // Load up to 7 inputs at a time.
315 const __m256 vx = _mm256_maskload_ps(x, vmask);
316
317 // Compute reduced argument elements := round(x / log(2)).
318 __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
319
320 // Compute reduced argument t := x - elements * log(2).
321 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
322 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
323 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
324
325 // Correct reduced argument elements for masked out elements.
326 vn = _mm256_blendv_ps(vacce, vn, _mm256_castsi256_ps(vmask));
327
328 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
329 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
330 vp = _mm256_fmadd_ps(vp, vt, vc3);
331 vp = _mm256_fmadd_ps(vp, vt, vc2);
332 vp = _mm256_fmadd_ps(vp, vt, vc1);
333 vp = _mm256_fmadd_ps(vp, vt, vc0);
334 vp = _mm256_and_ps(vp, _mm256_castsi256_ps(vmask));
335
336 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
337 const __m256 vmax_e = _mm256_max_ps(vacce, vn);
338
339 // For computational efficiency, clamp minimum exp2(delta_e) at -127.0. It will be mapped to 0.0 scale factor later.
340 const __m256 vdelta_e = _mm256_max_ps(_mm256_sub_ps(vn, vmax_e), vmin_exponent);
341 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_e), vmin_exponent);
342
343 // Convert exponents into scale factors.
344 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_e, vmagic_bias)), 23));
345 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
346
347 // Update accumulated "mantissa" and "exponent" values.
348 vaccv = _mm256_mul_ps(vaccv, vaccs);
349 vaccv = _mm256_fmadd_ps(vp, vs, vaccv);
350
351 vacce = vmax_e;
352 }
353
354 // Reduce partial sums of "extended" floating-point numbers into a single "extended" floating-point sum.
355 __m256 vmax_acce = _mm256_max_ps(vacce, _mm256_permute2f128_ps(vacce, vacce, 1));
356 vmax_acce = _mm256_max_ps(vmax_acce, _mm256_shuffle_ps(vmax_acce, vmax_acce, _MM_SHUFFLE(1, 0, 3, 2)));
357 vmax_acce = _mm256_max_ps(vmax_acce, _mm256_shuffle_ps(vmax_acce, vmax_acce, _MM_SHUFFLE(2, 3, 0, 1)));
358 const __m256 vdelta_acce = _mm256_max_ps(_mm256_sub_ps(vacce, vmax_acce), vmin_exponent);
359 const __m256 vaccs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(vdelta_acce, vmagic_bias)), 23));
360
361 vaccv = _mm256_mul_ps(vaccv, vaccs);
362 __m128 vaccv_sum = _mm_add_ps(_mm256_castps256_ps128(vaccv), _mm256_extractf128_ps(vaccv, 1));
363 vaccv_sum = _mm_add_ps(vaccv_sum, _mm_movehl_ps(vaccv_sum, vaccv_sum));
364 vaccv_sum = _mm_add_ss(vaccv_sum, _mm_movehdup_ps(vaccv_sum));
365
366 _mm_store_ss(&sum[0], vaccv_sum);
367 _mm_store_ss(&sum[1], _mm256_castps256_ps128(vmax_acce));
368
369 _mm256_zeroupper();
370 }
371