1 // Auto-generated file. Do not edit!
2 // Template: src/f32-raddextexp/avx512f-p5-scalef.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/common.h>
16 #include <xnnpack/intrinsics-polyfill.h>
17 #include <xnnpack/raddextexp.h>
18
19
xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x192(size_t elements,const float * x,float * sum)20 void xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x192(
21 size_t elements,
22 const float* x,
23 float* sum)
24 {
25 assert(elements % sizeof(float) == 0);
26
27 const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
28 const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
29 const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
30
31 const __m512 vc0 = _mm512_set1_ps(1.0f);
32 const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
33 const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
34 const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
35 const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
36 const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
37
38 const __m512 vminus_inf = _mm512_set1_ps(-INFINITY);
39
40 __m512 vaccv0 = _mm512_setzero_ps();
41 __m512 vacce0 = vminus_inf;
42 for (; elements >= 192 * sizeof(float); elements -= 192 * sizeof(float)) {
43 // Load 192 (12x16) inputs at a time.
44 const __m512 vx0 = _mm512_loadu_ps(x);
45 const __m512 vx1 = _mm512_loadu_ps(x + 16);
46 const __m512 vx2 = _mm512_loadu_ps(x + 32);
47 const __m512 vx3 = _mm512_loadu_ps(x + 48);
48 const __m512 vx4 = _mm512_loadu_ps(x + 64);
49 const __m512 vx5 = _mm512_loadu_ps(x + 80);
50 const __m512 vx6 = _mm512_loadu_ps(x + 96);
51 const __m512 vx7 = _mm512_loadu_ps(x + 112);
52 const __m512 vx8 = _mm512_loadu_ps(x + 128);
53 const __m512 vx9 = _mm512_loadu_ps(x + 144);
54 const __m512 vx10 = _mm512_loadu_ps(x + 160);
55 const __m512 vx11 = _mm512_loadu_ps(x + 176);
56 x += 192;
57
58 // Compute reduced argument elements := round(x / log(2)).
59 const __m512 vn0 = _mm512_roundscale_ps(_mm512_mul_ps(vx0, vlog2e), 0);
60 const __m512 vn1 = _mm512_roundscale_ps(_mm512_mul_ps(vx1, vlog2e), 0);
61 const __m512 vn2 = _mm512_roundscale_ps(_mm512_mul_ps(vx2, vlog2e), 0);
62 const __m512 vn3 = _mm512_roundscale_ps(_mm512_mul_ps(vx3, vlog2e), 0);
63 const __m512 vn4 = _mm512_roundscale_ps(_mm512_mul_ps(vx4, vlog2e), 0);
64 const __m512 vn5 = _mm512_roundscale_ps(_mm512_mul_ps(vx5, vlog2e), 0);
65 const __m512 vn6 = _mm512_roundscale_ps(_mm512_mul_ps(vx6, vlog2e), 0);
66 const __m512 vn7 = _mm512_roundscale_ps(_mm512_mul_ps(vx7, vlog2e), 0);
67 const __m512 vn8 = _mm512_roundscale_ps(_mm512_mul_ps(vx8, vlog2e), 0);
68 const __m512 vn9 = _mm512_roundscale_ps(_mm512_mul_ps(vx9, vlog2e), 0);
69 const __m512 vn10 = _mm512_roundscale_ps(_mm512_mul_ps(vx10, vlog2e), 0);
70 const __m512 vn11 = _mm512_roundscale_ps(_mm512_mul_ps(vx11, vlog2e), 0);
71
72 // Compute reduced argument t := x - elements * log(2).
73 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
74 __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_hi, vx0);
75 __m512 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_hi, vx1);
76 __m512 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_hi, vx2);
77 __m512 vt3 = _mm512_fmadd_ps(vn3, vminus_ln2_hi, vx3);
78 __m512 vt4 = _mm512_fmadd_ps(vn4, vminus_ln2_hi, vx4);
79 __m512 vt5 = _mm512_fmadd_ps(vn5, vminus_ln2_hi, vx5);
80 __m512 vt6 = _mm512_fmadd_ps(vn6, vminus_ln2_hi, vx6);
81 __m512 vt7 = _mm512_fmadd_ps(vn7, vminus_ln2_hi, vx7);
82 __m512 vt8 = _mm512_fmadd_ps(vn8, vminus_ln2_hi, vx8);
83 __m512 vt9 = _mm512_fmadd_ps(vn9, vminus_ln2_hi, vx9);
84 __m512 vt10 = _mm512_fmadd_ps(vn10, vminus_ln2_hi, vx10);
85 __m512 vt11 = _mm512_fmadd_ps(vn11, vminus_ln2_hi, vx11);
86
87 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_lo, vt0);
88 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_lo, vt1);
89 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_lo, vt2);
90 vt3 = _mm512_fmadd_ps(vn3, vminus_ln2_lo, vt3);
91 vt4 = _mm512_fmadd_ps(vn4, vminus_ln2_lo, vt4);
92 vt5 = _mm512_fmadd_ps(vn5, vminus_ln2_lo, vt5);
93 vt6 = _mm512_fmadd_ps(vn6, vminus_ln2_lo, vt6);
94 vt7 = _mm512_fmadd_ps(vn7, vminus_ln2_lo, vt7);
95 vt8 = _mm512_fmadd_ps(vn8, vminus_ln2_lo, vt8);
96 vt9 = _mm512_fmadd_ps(vn9, vminus_ln2_lo, vt9);
97 vt10 = _mm512_fmadd_ps(vn10, vminus_ln2_lo, vt10);
98 vt11 = _mm512_fmadd_ps(vn11, vminus_ln2_lo, vt11);
99
100 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
101 __m512 vp0 = _mm512_fmadd_ps(vc5, vt0, vc4);
102 __m512 vp1 = _mm512_fmadd_ps(vc5, vt1, vc4);
103 __m512 vp2 = _mm512_fmadd_ps(vc5, vt2, vc4);
104 __m512 vp3 = _mm512_fmadd_ps(vc5, vt3, vc4);
105 __m512 vp4 = _mm512_fmadd_ps(vc5, vt4, vc4);
106 __m512 vp5 = _mm512_fmadd_ps(vc5, vt5, vc4);
107 __m512 vp6 = _mm512_fmadd_ps(vc5, vt6, vc4);
108 __m512 vp7 = _mm512_fmadd_ps(vc5, vt7, vc4);
109 __m512 vp8 = _mm512_fmadd_ps(vc5, vt8, vc4);
110 __m512 vp9 = _mm512_fmadd_ps(vc5, vt9, vc4);
111 __m512 vp10 = _mm512_fmadd_ps(vc5, vt10, vc4);
112 __m512 vp11 = _mm512_fmadd_ps(vc5, vt11, vc4);
113
114 vp0 = _mm512_fmadd_ps(vp0, vt0, vc3);
115 vp1 = _mm512_fmadd_ps(vp1, vt1, vc3);
116 vp2 = _mm512_fmadd_ps(vp2, vt2, vc3);
117 vp3 = _mm512_fmadd_ps(vp3, vt3, vc3);
118 vp4 = _mm512_fmadd_ps(vp4, vt4, vc3);
119 vp5 = _mm512_fmadd_ps(vp5, vt5, vc3);
120 vp6 = _mm512_fmadd_ps(vp6, vt6, vc3);
121 vp7 = _mm512_fmadd_ps(vp7, vt7, vc3);
122 vp8 = _mm512_fmadd_ps(vp8, vt8, vc3);
123 vp9 = _mm512_fmadd_ps(vp9, vt9, vc3);
124 vp10 = _mm512_fmadd_ps(vp10, vt10, vc3);
125 vp11 = _mm512_fmadd_ps(vp11, vt11, vc3);
126
127 vp0 = _mm512_fmadd_ps(vp0, vt0, vc2);
128 vp1 = _mm512_fmadd_ps(vp1, vt1, vc2);
129 vp2 = _mm512_fmadd_ps(vp2, vt2, vc2);
130 vp3 = _mm512_fmadd_ps(vp3, vt3, vc2);
131 vp4 = _mm512_fmadd_ps(vp4, vt4, vc2);
132 vp5 = _mm512_fmadd_ps(vp5, vt5, vc2);
133 vp6 = _mm512_fmadd_ps(vp6, vt6, vc2);
134 vp7 = _mm512_fmadd_ps(vp7, vt7, vc2);
135 vp8 = _mm512_fmadd_ps(vp8, vt8, vc2);
136 vp9 = _mm512_fmadd_ps(vp9, vt9, vc2);
137 vp10 = _mm512_fmadd_ps(vp10, vt10, vc2);
138 vp11 = _mm512_fmadd_ps(vp11, vt11, vc2);
139
140 vp0 = _mm512_fmadd_ps(vp0, vt0, vc1);
141 vp1 = _mm512_fmadd_ps(vp1, vt1, vc1);
142 vp2 = _mm512_fmadd_ps(vp2, vt2, vc1);
143 vp3 = _mm512_fmadd_ps(vp3, vt3, vc1);
144 vp4 = _mm512_fmadd_ps(vp4, vt4, vc1);
145 vp5 = _mm512_fmadd_ps(vp5, vt5, vc1);
146 vp6 = _mm512_fmadd_ps(vp6, vt6, vc1);
147 vp7 = _mm512_fmadd_ps(vp7, vt7, vc1);
148 vp8 = _mm512_fmadd_ps(vp8, vt8, vc1);
149 vp9 = _mm512_fmadd_ps(vp9, vt9, vc1);
150 vp10 = _mm512_fmadd_ps(vp10, vt10, vc1);
151 vp11 = _mm512_fmadd_ps(vp11, vt11, vc1);
152
153 vp0 = _mm512_fmadd_ps(vp0, vt0, vc0);
154 vp1 = _mm512_fmadd_ps(vp1, vt1, vc0);
155 vp2 = _mm512_fmadd_ps(vp2, vt2, vc0);
156 vp3 = _mm512_fmadd_ps(vp3, vt3, vc0);
157 vp4 = _mm512_fmadd_ps(vp4, vt4, vc0);
158 vp5 = _mm512_fmadd_ps(vp5, vt5, vc0);
159 vp6 = _mm512_fmadd_ps(vp6, vt6, vc0);
160 vp7 = _mm512_fmadd_ps(vp7, vt7, vc0);
161 vp8 = _mm512_fmadd_ps(vp8, vt8, vc0);
162 vp9 = _mm512_fmadd_ps(vp9, vt9, vc0);
163 vp10 = _mm512_fmadd_ps(vp10, vt10, vc0);
164 vp11 = _mm512_fmadd_ps(vp11, vt11, vc0);
165
166 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation where
167 // - vnX is "exponent"
168 // - vpX is "mantissa"
169 //
170 // exp2(ae) * av + exp2(be) * bv =
171 // = exp2(max(ae, be)) * exp2(ae - max(ae, be)) * av + exp2(max(ae, be)) * exp2(be - max(ae, be)) * bv
172 // = exp2(max_e) * (exp2(ae - max_e) * av + exp2(be - max_e) * bv)
173 // = exp2(max_e) * (exp2(delta_ae) * av + exp2(delta_be) * bv)
174 //
175 // For computational efficiency we add three "extended" floating-point numbers at a time.
176 __m512 vmax_e0 = _mm512_max_ps(vacce0, vn0);
177 vmax_e0 = _mm512_max_ps(vmax_e0, vn1);
178 vmax_e0 = _mm512_max_ps(vmax_e0, vn2);
179 vmax_e0 = _mm512_max_ps(vmax_e0, vn3);
180 vmax_e0 = _mm512_max_ps(vmax_e0, vn4);
181 vmax_e0 = _mm512_max_ps(vmax_e0, vn5);
182 vmax_e0 = _mm512_max_ps(vmax_e0, vn6);
183 vmax_e0 = _mm512_max_ps(vmax_e0, vn7);
184 vmax_e0 = _mm512_max_ps(vmax_e0, vn8);
185 vmax_e0 = _mm512_max_ps(vmax_e0, vn9);
186 vmax_e0 = _mm512_max_ps(vmax_e0, vn10);
187 vmax_e0 = _mm512_max_ps(vmax_e0, vn11);
188
189 const __m512 vdelta_acce0 = _mm512_sub_ps(vacce0, vmax_e0);
190 const __m512 vdelta_e0 = _mm512_sub_ps(vn0, vmax_e0);
191 const __m512 vdelta_e1 = _mm512_sub_ps(vn1, vmax_e0);
192 const __m512 vdelta_e2 = _mm512_sub_ps(vn2, vmax_e0);
193 const __m512 vdelta_e3 = _mm512_sub_ps(vn3, vmax_e0);
194 const __m512 vdelta_e4 = _mm512_sub_ps(vn4, vmax_e0);
195 const __m512 vdelta_e5 = _mm512_sub_ps(vn5, vmax_e0);
196 const __m512 vdelta_e6 = _mm512_sub_ps(vn6, vmax_e0);
197 const __m512 vdelta_e7 = _mm512_sub_ps(vn7, vmax_e0);
198 const __m512 vdelta_e8 = _mm512_sub_ps(vn8, vmax_e0);
199 const __m512 vdelta_e9 = _mm512_sub_ps(vn9, vmax_e0);
200 const __m512 vdelta_e10 = _mm512_sub_ps(vn10, vmax_e0);
201 const __m512 vdelta_e11 = _mm512_sub_ps(vn11, vmax_e0);
202
203 // Update accumulated "mantissa" and "exponent" values
204 vaccv0 = _mm512_scalef_ps(vaccv0, vdelta_acce0);
205 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp0, vdelta_e0));
206 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp1, vdelta_e1));
207 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp2, vdelta_e2));
208 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp3, vdelta_e3));
209 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp4, vdelta_e4));
210 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp5, vdelta_e5));
211 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp6, vdelta_e6));
212 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp7, vdelta_e7));
213 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp8, vdelta_e8));
214 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp9, vdelta_e9));
215 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp10, vdelta_e10));
216 vaccv0 = _mm512_add_ps(vaccv0, _mm512_scalef_ps(vp11, vdelta_e11));
217
218 vacce0 = vmax_e0;
219 }
220
221 // Reduce partial sums of "extended" floating-point numbers into a single "extended" SIMD vector of sums.
222 __m512 vaccv = vaccv0;
223 __m512 vacce = vacce0;
224
225 for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
226 // Load 16 inputs at a time.
227 const __m512 vx = _mm512_loadu_ps(x);
228 x += 16;
229
230 // Compute reduced argument elements := round(x / log(2)).
231 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
232
233 // Compute reduced argument t := x - elements * log(2).
234 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
235 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
236 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
237
238 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
239 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
240 vp = _mm512_fmadd_ps(vp, vt, vc3);
241 vp = _mm512_fmadd_ps(vp, vt, vc2);
242 vp = _mm512_fmadd_ps(vp, vt, vc1);
243 vp = _mm512_fmadd_ps(vp, vt, vc0);
244
245 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
246 const __m512 vmax_e = _mm512_max_ps(vacce, vn);
247 const __m512 vdelta_acce = _mm512_sub_ps(vacce, vmax_e);
248 const __m512 vdelta_e = _mm512_sub_ps(vn, vmax_e);
249 vaccv = _mm512_scalef_ps(vaccv, vdelta_acce);
250 vaccv = _mm512_add_ps(vaccv, _mm512_scalef_ps(vp, vdelta_e));
251
252 vacce = vmax_e;
253 }
254 if XNN_UNLIKELY(elements != 0) {
255 // Prepare mask for valid 32-bit elements (depends on elements).
256 elements >>= 2 /* log2(sizeof(float)) */;
257 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1)));
258
259 // Load up to 15 inputs at a time.
260 const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
261
262 // Compute reduced argument elements := round(x / log(2)).
263 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
264
265 // Compute reduced argument t := x - elements * log(2).
266 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
267 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
268 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
269
270 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
271 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
272 vp = _mm512_fmadd_ps(vp, vt, vc3);
273 vp = _mm512_fmadd_ps(vp, vt, vc2);
274 vp = _mm512_fmadd_ps(vp, vt, vc1);
275 vp = _mm512_fmadd_ps(vp, vt, vc0);
276
277 // Accumulate "extended" floating-point numbers in ("mantissa", "exponent") representation.
278 const __m512 vmax_e = _mm512_mask_max_ps(vacce, vmask, vacce, vn);
279 const __m512 vdelta_acce = _mm512_sub_ps(vacce, vmax_e);
280 const __m512 vdelta_e = _mm512_sub_ps(vn, vmax_e);
281 vaccv = _mm512_mask_scalef_ps(vaccv, vmask, vaccv, vdelta_acce);
282 vaccv = _mm512_mask_add_ps(vaccv, vmask, vaccv, _mm512_maskz_scalef_ps(vmask, vp, vdelta_e));
283 vacce = vmax_e;
284 }
285
286 // Reduce partial sums of "extended" floating-point numbers into a single "extended" floating-point sum.
287 const float vmax_acce = _mm512_reduce_max_ps(vacce);
288 const __m512 vdelta_acce = _mm512_sub_ps(vacce, _mm512_set1_ps(vmax_acce));
289
290 sum[0] = _mm512_reduce_add_ps(_mm512_scalef_ps(vaccv, vdelta_acce));
291 sum[1] = vmax_acce;
292
293 _mm256_zeroupper();
294 }
295