forked from JishinMaster/simd_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avx512_mathfun.h
359 lines (284 loc) · 12.3 KB
/
avx512_mathfun.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Project : SIMD_Utils
* Version : 0.2.5
* Author : JishinMaster
* Licence : BSD-2
*/
/*
AVX implementation of sin, cos, sincos, exp and log
Based on "sse_mathfun.h", by Julien Pommier
and "avx_mathfun.h" by Giovanni Garberoglio
http://gruntthepeon.free.fr/ssemath/
*/
#include <immintrin.h>
/* natural logarithm computed for 8 simultaneous float
return NaN for x <= 0
*/
static inline v16sf log512_ps(v16sf x)
{
v16si imm0;
v16sf one = *(v16sf *) _ps512_1;
v16sf invalid_mask = (v16sf) _mm512_movm_epi32(_mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_LE_OS));
x = _mm512_max_ps(x, *(v16sf *) _ps512_min_norm_pos); /* cut off denormalized stuff */
// can be done with AVX2
imm0 = _mm512_srli_epi32(_mm512_castps_si512(x), 23);
/* keep only the fractional part */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_mant_mask);
x = _mm512_or_ps(x, *(v16sf *) _ps512_0p5);
// this is again another AVX2 instruction
imm0 = _mm512_sub_epi32(imm0, *(v16si *) _pi32_512_0x7f);
v16sf e = _mm512_cvtepi32_ps(imm0);
e = _mm512_add_ps(e, one);
v16sf mask = (v16sf) _mm512_movm_epi32(_mm512_cmp_ps_mask(x, *(v16sf *) _ps512_cephes_SQRTHF, _CMP_LT_OS));
v16sf tmp = _mm512_and_ps(x, mask);
x = _mm512_sub_ps(x, one);
e = _mm512_sub_ps(e, _mm512_and_ps(one, mask));
x = _mm512_add_ps(x, tmp);
v16sf z = _mm512_mul_ps(x, x);
v16sf y = _mm512_fmadd_ps(*(v16sf *) _ps512_cephes_log_p0, x, *(v16sf *) _ps512_cephes_log_p1);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p2);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p3);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p4);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p5);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p6);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p7);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_log_p8);
y = _mm512_mul_ps(y, x);
y = _mm512_mul_ps(y, z);
y = _mm512_fmadd_ps(e, *(v16sf *) _ps512_cephes_log_q1, y);
y = _mm512_fnmadd_ps(z, *(v16sf *) _ps512_0p5, y);
tmp = _mm512_fmadd_ps(e, *(v16sf *) _ps512_cephes_log_q2, y);
x = _mm512_add_ps(x, tmp);
x = _mm512_or_ps(x, invalid_mask); // negative arg will be NAN
return x;
}
static inline v16sf exp512_ps(v16sf x)
{
v16sf tmp = _mm512_setzero_ps(), fx;
v16si imm0;
v16sf one = *(v16sf *) _ps512_1;
x = _mm512_min_ps(x, *(v16sf *) _ps512_exp_hi);
x = _mm512_max_ps(x, *(v16sf *) _ps512_exp_lo);
/* express exp(x) as exp(g + n*log(2)) */
fx = _mm512_fmadd_ps(x, *(v16sf *) _ps512_cephes_LOG2EF, *(v16sf *) _ps512_0p5);
tmp = _mm512_floor_ps(fx);
/* if greater, substract 1 */
v16sf mask = (v16sf) _mm512_movm_epi32(_mm512_cmp_ps_mask(tmp, fx, _CMP_GT_OS));
mask = _mm512_and_ps(mask, one);
fx = _mm512_sub_ps(tmp, mask);
x = _mm512_fnmadd_ps(fx, *(v16sf *) _ps512_cephes_exp_C1, x);
x = _mm512_fnmadd_ps(fx, *(v16sf *) _ps512_cephes_exp_C2, x);
v16sf z = _mm512_mul_ps(x, x);
v16sf y = _mm512_fmadd_ps(*(v16sf *) _ps512_cephes_exp_p0, x, *(v16sf *) _ps512_cephes_exp_p1);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_exp_p2);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_exp_p3);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_exp_p4);
y = _mm512_fmadd_ps(y, x, *(v16sf *) _ps512_cephes_exp_p5);
y = _mm512_fmadd_ps(y, z, x);
y = _mm512_add_ps(y, one);
/* build 2^n */
imm0 = _mm512_cvttps_epi32(fx);
// another two AVX2 instructions
imm0 = _mm512_add_epi32(imm0, *(v16si *) _pi32_512_0x7f);
imm0 = _mm512_slli_epi32(imm0, 23);
v16sf pow2n = _mm512_castsi512_ps(imm0);
y = _mm512_mul_ps(y, pow2n);
return y;
}
static inline v16sf sin512_ps(v16sf x)
{ // any x
v16sf xmm3, sign_bit, y;
v16si imm0, imm2;
sign_bit = x;
/* take the absolute value */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_sign_mask);
/* extract the sign bit (upper one) */
sign_bit = _mm512_and_ps(sign_bit, *(v16sf *) _ps512_sign_mask);
/* scale by 4/Pi */
y = _mm512_mul_ps(x, *(v16sf *) _ps512_cephes_FOPI);
/* store the integer part of y in mm0 */
imm2 = _mm512_cvttps_epi32(y);
/* j=(j+1) & (~1) (see the cephes sources) */
// another two AVX2 instruction
imm2 = _mm512_add_epi32(imm2, *(v16si *) _pi32_512_1);
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_inv1);
y = _mm512_cvtepi32_ps(imm2);
/* get the swap sign flag */
imm0 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_4);
imm0 = _mm512_slli_epi32(imm0, 29);
/* get the polynom selection mask
there is one polynom for 0 <= x <= Pi/4
and another one for Pi/4<x<=Pi/2
Both branches will be computed.
*/
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_2);
imm2 = (__m512i) _mm512_maskz_set1_epi32(_mm512_cmpeq_epi32_mask(imm2, *(v16si *) _pi32_512_0), -1);
v16sf swap_sign_bit = _mm512_castsi512_ps(imm0);
#if 1
// Cast integer 0000 FFFF (negative int) to mmask type. Is there a better way?
__mmask16 poly_mask = _mm512_cmplt_epi32_mask(imm2, _mm512_setzero_si512());
#else
v16sf poly_mask = _mm512_castsi512_ps(imm2);
#endif
sign_bit = _mm512_xor_ps(sign_bit, swap_sign_bit);
/* The magic pass: "Extended precision modular arithmetic"
x = ((x - y * DP1) - y * DP2) - y * DP3; */
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP1, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP2, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP3, x);
/* Evaluate the first polynom (0 <= x <= Pi/4) */
v16sf z = _mm512_mul_ps(x, x);
y = _mm512_fmadd_ps(*(v16sf *) _ps512_coscof_p0, z, *(v16sf *) _ps512_coscof_p1);
y = _mm512_fmadd_ps(y, z, *(v16sf *) _ps512_coscof_p2);
y = _mm512_mul_ps(y, z);
y = _mm512_mul_ps(y, z);
y = _mm512_fnmadd_ps(z, *(v16sf *) _ps512_0p5, y);
y = _mm512_add_ps(y, *(v16sf *) _ps512_1);
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
v16sf y2 = _mm512_fmadd_ps(*(v16sf *) _ps512_sincof_p0, z, *(v16sf *) _ps512_sincof_p1);
y2 = _mm512_fmadd_ps(y2, z, *(v16sf *) _ps512_sincof_p2);
y2 = _mm512_mul_ps(y2, z);
y2 = _mm512_fmadd_ps(y2, x, x);
/* select the correct result from the two polynoms */
#if 1
y = _mm512_mask_blend_ps(poly_mask, y, y2);
#else
y2 = _mm512_and_ps(poly_mask, y2); //, xmm3);
y = _mm512_andnot_ps(poly_mask, y);
y = _mm512_add_ps(y, y2);
#endif
/* update the sign */
y = _mm512_xor_ps(y, sign_bit);
return y;
}
/* almost the same as sin_ps */
static inline v16sf cos512_ps(v16sf x)
{ // any x
v16sf y;
v16si imm0, imm2;
/* take the absolute value */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_sign_mask);
/* scale by 4/Pi */
y = _mm512_mul_ps(x, *(v16sf *) _ps512_cephes_FOPI);
/* store the integer part of y in mm0 */
imm2 = _mm512_cvttps_epi32(y);
/* j=(j+1) & (~1) (see the cephes sources) */
imm2 = _mm512_add_epi32(imm2, *(v16si *) _pi32_512_1);
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_inv1);
y = _mm512_cvtepi32_ps(imm2);
imm2 = _mm512_sub_epi32(imm2, *(v16si *) _pi32_512_2);
/* get the swap sign flag */
imm0 = _mm512_andnot_si512(imm2, *(v16si *) _pi32_512_4);
imm0 = _mm512_slli_epi32(imm0, 29);
/* get the polynom selection mask */
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_2);
imm2 = (__m512i) _mm512_maskz_set1_epi32(_mm512_cmpeq_epi32_mask(imm2, *(v16si *) _pi32_512_0), -1);
v16sf sign_bit = _mm512_castsi512_ps(imm0);
#if 1
// Cast integer 0000 FFFF (negative int) to mmask type. Is there a better way?
__mmask16 poly_mask = _mm512_cmplt_epi32_mask(imm2, _mm512_setzero_si512());
#else
v16sf poly_mask = _mm512_castsi512_ps(imm2);
#endif
/* The magic pass: "Extended precision modular arithmetic"
x = ((x - y * DP1) - y * DP2) - y * DP3; */
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP1, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP2, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP3, x);
/* Evaluate the first polynom (0 <= x <= Pi/4) */
v16sf z = _mm512_mul_ps(x, x);
y = _mm512_fmadd_ps(*(v16sf *) _ps512_coscof_p0, z, *(v16sf *) _ps512_coscof_p1);
y = _mm512_fmadd_ps(y, z, *(v16sf *) _ps512_coscof_p2);
y = _mm512_mul_ps(y, z);
y = _mm512_mul_ps(y, z);
y = _mm512_fnmadd_ps(z, *(v16sf *) _ps512_0p5, y);
y = _mm512_add_ps(y, *(v16sf *) _ps512_1);
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
v16sf y2 = _mm512_fmadd_ps(*(v16sf *) _ps512_sincof_p0, z, *(v16sf *) _ps512_sincof_p1);
y2 = _mm512_fmadd_ps(y2, z, *(v16sf *) _ps512_sincof_p2);
y2 = _mm512_mul_ps(y2, z);
y2 = _mm512_fmadd_ps(y2, x, x);
/* select the correct result from the two polynoms */
#if 1
y = _mm512_mask_blend_ps(poly_mask, y, y2);
#else
y2 = _mm512_and_ps(poly_mask, y2); //, xmm3);
y = _mm512_andnot_ps(poly_mask, y);
y = _mm512_add_ps(y, y2);
#endif
/* update the sign */
y = _mm512_xor_ps(y, sign_bit);
return y;
}
static inline void sincos512_ps(v16sf x, v16sf *s, v16sf *c)
{
v16sf xmm1, xmm2, sign_bit_sin, y;
v16si imm0, imm2, imm4;
sign_bit_sin = x;
/* take the absolute value */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_sign_mask);
/* extract the sign bit (upper one) */
sign_bit_sin = _mm512_and_ps(sign_bit_sin, *(v16sf *) _ps512_sign_mask);
/* scale by 4/Pi */
y = _mm512_mul_ps(x, *(v16sf *) _ps512_cephes_FOPI);
/* store the integer part of y in imm2 */
imm2 = _mm512_cvttps_epi32(y);
/* j=(j+1) & (~1) (see the cephes sources) */
imm2 = _mm512_add_epi32(imm2, *(v16si *) _pi32_512_1);
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_inv1);
y = _mm512_cvtepi32_ps(imm2);
imm4 = imm2;
/* get the swap sign flag for the sine */
imm0 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_4);
imm0 = _mm512_slli_epi32(imm0, 29);
// v16sf swap_sign_bit_sin = _mm512_castsi512_ps(imm0);
/* get the polynom selection mask for the sine*/
imm2 = _mm512_and_si512(imm2, *(v16si *) _pi32_512_2);
imm2 = (__m512i) _mm512_maskz_set1_epi32(_mm512_cmpeq_epi32_mask(imm2, *(v16si *) _pi32_512_0), -1);
// v16sf poly_mask = _mm512_castsi512_ps(imm2);
v16sf swap_sign_bit_sin = _mm512_castsi512_ps(imm0);
#if 1
// Cast integer 0000 FFFF (negative int) to mmask type. Is there a better way?
__mmask16 poly_mask = _mm512_cmplt_epi32_mask(imm2, _mm512_setzero_si512());
#else
v16sf poly_mask = _mm512_castsi512_ps(imm2);
#endif
/* The magic pass: "Extended precision modular arithmetic"
x = ((x - y * DP1) - y * DP2) - y * DP3; */
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP1, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP2, x);
x = _mm512_fmadd_ps(y, *(v16sf *) _ps512_minus_cephes_DP3, x);
imm4 = _mm512_sub_epi32(imm4, *(v16si *) _pi32_512_2);
imm4 = _mm512_andnot_si512(imm4, *(v16si *) _pi32_512_4);
imm4 = _mm512_slli_epi32(imm4, 29);
v16sf sign_bit_cos = _mm512_castsi512_ps(imm4);
sign_bit_sin = _mm512_xor_ps(sign_bit_sin, swap_sign_bit_sin);
/* Evaluate the first polynom (0 <= x <= Pi/4) */
v16sf z = _mm512_mul_ps(x, x);
y = _mm512_fmadd_ps(*(v16sf *) _ps512_coscof_p0, z, *(v16sf *) _ps512_coscof_p1);
y = _mm512_fmadd_ps(y, z, *(v16sf *) _ps512_coscof_p2);
y = _mm512_mul_ps(y, z);
y = _mm512_mul_ps(y, z);
y = _mm512_fnmadd_ps(z, *(v16sf *) _ps512_0p5, y);
y = _mm512_add_ps(y, *(v16sf *) _ps512_1);
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
v16sf y2 = _mm512_fmadd_ps(*(v16sf *) _ps512_sincof_p0, z, *(v16sf *) _ps512_sincof_p1);
y2 = _mm512_fmadd_ps(y2, z, *(v16sf *) _ps512_sincof_p2);
y2 = _mm512_mul_ps(y2, z);
y2 = _mm512_fmadd_ps(y2, x, x);
/* select the correct result from the two polynoms */
#if 1
xmm1 = _mm512_mask_blend_ps(poly_mask, y, y2);
xmm2 = _mm512_mask_blend_ps(poly_mask, y2, y);
#else
v16sf ysin2 = _mm512_and_ps(poly_mask, y2);
v16sf ysin1 = _mm512_andnot_ps(poly_mask, y);
y2 = _mm512_sub_ps(y2, ysin2);
y = _mm512_sub_ps(y, ysin1);
xmm1 = _mm512_add_ps(ysin1, ysin2);
xmm2 = _mm512_add_ps(y, y2);
#endif
/* update the sign */
*s = _mm512_xor_ps(xmm1, sign_bit_sin);
*c = _mm512_xor_ps(xmm2, sign_bit_cos);
}