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