Elffuss BraidHub

kikocis/ncnn public

master · 42.2 KiBRawPermalink
1/*
2 AVX implementation of sin, cos, sincos, exp and log
3
4 Based on "sse_mathfun.h", by Julien Pommier
5 http://gruntthepeon.free.fr/ssemath/
6
7 Copyright (C) 2012 Giovanni Garberoglio
8 Interdisciplinary Laboratory for Computational Science (LISC)
9 Fondazione Bruno Kessler and University of Trento
10 via Sommarive, 18
11 I-38123 Trento (Italy)
12
13 This software is provided 'as-is', without any express or implied
14 warranty. In no event will the authors be held liable for any damages
15 arising from the use of this software.
16
17 Permission is granted to anyone to use this software for any purpose,
18 including commercial applications, and to alter it and redistribute it
19 freely, subject to the following restrictions:
20
21 1. The origin of this software must not be misrepresented; you must not
22 claim that you wrote the original software. If you use this software
23 in a product, an acknowledgment in the product documentation would be
24 appreciated but is not required.
25 2. Altered source versions must be plainly marked as such, and must not be
26 misrepresented as being the original software.
27 3. This notice may not be removed or altered from any source distribution.
28
29 (this is the zlib license)
30*/
31
32#ifndef AVX_MATHFUN_H
33#define AVX_MATHFUN_H
34
35#include <immintrin.h>
36#include <emmintrin.h>
37#include <x86_usability.h>
38
39/* yes I know, the top of this file is quite ugly */
40
41#ifdef _MSC_VER /* visual c++ */
42#define ALIGN32_BEG __declspec(align(32))
43#define ALIGN32_END
44#else /* gcc or icc */
45#define ALIGN32_BEG
46#define ALIGN32_END __attribute__((aligned(32)))
47#endif
48
49#define _PI32AVX_CONST(Name, Val) \
50 static const ALIGN32_BEG int _pi32avx_##Name[4] ALIGN32_END = {Val, Val, Val, Val}
51
52_PI32AVX_CONST(1, 1);
53_PI32AVX_CONST(inv1, ~1);
54_PI32AVX_CONST(2, 2);
55_PI32AVX_CONST(4, 4);
56
57/* declare some AVX constants -- why can't I figure a better way to do that? */
58#define _PS256_CONST(Name, Val) \
59 static const ALIGN32_BEG float _ps256_##Name[8] ALIGN32_END = {Val, Val, Val, Val, Val, Val, Val, Val}
60#define _PI32_CONST256(Name, Val) \
61 static const ALIGN32_BEG int _pi32_256_##Name[8] ALIGN32_END = {Val, Val, Val, Val, Val, Val, Val, Val}
62#define _PS256_CONST_TYPE(Name, Type, Val) \
63 static const ALIGN32_BEG Type _ps256_##Name[8] ALIGN32_END = {Val, Val, Val, Val, Val, Val, Val, Val}
64
65_PS256_CONST(1, 1.0f);
66_PS256_CONST(0p5, 0.5f);
67/* the smallest non denormalized float number */
68_PS256_CONST_TYPE(min_norm_pos, int, 0x00800000);
69_PS256_CONST_TYPE(mant_mask, int, 0x7f800000);
70_PS256_CONST_TYPE(inv_mant_mask, int, ~0x7f800000);
71
72_PS256_CONST_TYPE(sign_mask, int, (int)0x80000000);
73_PS256_CONST_TYPE(inv_sign_mask, int, ~0x80000000);
74
75_PI32_CONST256(0, 0);
76_PI32_CONST256(1, 1);
77_PI32_CONST256(inv1, ~1);
78_PI32_CONST256(2, 2);
79_PI32_CONST256(4, 4);
80_PI32_CONST256(0x7f, 0x7f);
81
82_PS256_CONST(cephes_SQRTHF, 0.707106781186547524f);
83_PS256_CONST(cephes_log_p0, 7.0376836292E-2f);
84_PS256_CONST(cephes_log_p1, -1.1514610310E-1f);
85_PS256_CONST(cephes_log_p2, 1.1676998740E-1f);
86_PS256_CONST(cephes_log_p3, -1.2420140846E-1f);
87_PS256_CONST(cephes_log_p4, +1.4249322787E-1f);
88_PS256_CONST(cephes_log_p5, -1.6668057665E-1f);
89_PS256_CONST(cephes_log_p6, +2.0000714765E-1f);
90_PS256_CONST(cephes_log_p7, -2.4999993993E-1f);
91_PS256_CONST(cephes_log_p8, +3.3333331174E-1f);
92_PS256_CONST(cephes_log_q1, -2.12194440e-4f);
93_PS256_CONST(cephes_log_q2, 0.693359375f);
94
95#ifndef __AVX2__
96typedef union imm_xmm_union
97{
98 __m256i imm;
99 __m128i xmm[2];
100} imm_xmm_union;
101
102#define COPY_IMM_TO_XMM(imm_, xmm0_, xmm1_) \
103 { \
104 ALIGN32_BEG imm_xmm_union u ALIGN32_END; \
105 u.imm = imm_; \
106 xmm0_ = u.xmm[0]; \
107 xmm1_ = u.xmm[1]; \
108 }
109
110#define COPY_XMM_TO_IMM(xmm0_, xmm1_, imm_) \
111 { \
112 ALIGN32_BEG imm_xmm_union u ALIGN32_END; \
113 u.xmm[0] = xmm0_; \
114 u.xmm[1] = xmm1_; \
115 imm_ = u.imm; \
116 }
117
118#define AVX2_BITOP_USING_SSE2(fn) \
119 static NCNN_FORCEINLINE __m256i _mm256_comp_##fn(__m256i x, int a) \
120 { \
121 /* use SSE2 instruction to perform the bitop AVX2 */ \
122 __m128i x1, x2; \
123 __m256i ret; \
124 COPY_IMM_TO_XMM(x, x1, x2); \
125 x1 = _mm_##fn(x1, a); \
126 x2 = _mm_##fn(x2, a); \
127 COPY_XMM_TO_IMM(x1, x2, ret); \
128 return (ret); \
129 }
130#define AVX2_INTOP_USING_SSE2(fn) \
131 static NCNN_FORCEINLINE __m256i _mm256_comp_##fn(__m256i x, __m256i y) \
132 { \
133 /* use SSE2 instructions to perform the AVX2 integer operation */ \
134 __m128i x1, x2; \
135 __m128i y1, y2; \
136 __m256i ret; \
137 COPY_IMM_TO_XMM(x, x1, x2); \
138 COPY_IMM_TO_XMM(y, y1, y2); \
139 x1 = _mm_##fn(x1, y1); \
140 x2 = _mm_##fn(x2, y2); \
141 COPY_XMM_TO_IMM(x1, x2, ret); \
142 return (ret); \
143 }
144#else
145#define AVX2_BITOP_USING_SSE2(fn) \
146 static NCNN_FORCEINLINE __m256i _mm256_comp_##fn(__m256i x, int a) \
147 { \
148 return _mm256_##fn(x, a); \
149 }
150#define AVX2_INTOP_USING_SSE2(fn) \
151 static NCNN_FORCEINLINE __m256i _mm256_comp_##fn(__m256i x, __m256i y) \
152 { \
153 return _mm256_##fn(x, y); \
154 }
155#endif
156
157AVX2_BITOP_USING_SSE2(slli_epi32)
158AVX2_BITOP_USING_SSE2(srli_epi32)
159AVX2_INTOP_USING_SSE2(cmpeq_epi32)
160AVX2_INTOP_USING_SSE2(sub_epi32)
161AVX2_INTOP_USING_SSE2(add_epi32)
162
163// Replace 256 bit operations with 128 bit ones when AVX2 is disabled
164#ifndef __AVX2__
165AVX2_INTOP_USING_SSE2(and_si128)
166AVX2_INTOP_USING_SSE2(andnot_si128)
167#endif
168
169/* natural logarithm computed for 8 simultaneous float
170 return NaN for x <= 0
171*/
172static NCNN_FORCEINLINE __m256 log256_ps(__m256 x)
173{
174 __m256i imm0;
175 __m256 one = *(__m256*)_ps256_1;
176
177 //__m256 invalid_mask = _mm256_cmple_ps(x, _mm256_setzero_ps());
178 __m256 invalid_mask = _mm256_cmp_ps(x, _mm256_setzero_ps(), _CMP_LE_OS);
179
180 x = _mm256_max_ps(x, *(__m256*)_ps256_min_norm_pos); /* cut off denormalized stuff */
181
182 // can be done with AVX2
183 imm0 = _mm256_comp_srli_epi32(_mm256_castps_si256(x), 23);
184
185 /* keep only the fractional part */
186 x = _mm256_and_ps(x, *(__m256*)_ps256_inv_mant_mask);
187 x = _mm256_or_ps(x, *(__m256*)_ps256_0p5);
188
189 // this is again another AVX2 instruction
190 imm0 = _mm256_comp_sub_epi32(imm0, *(__m256i*)_pi32_256_0x7f);
191 __m256 e = _mm256_cvtepi32_ps(imm0);
192
193 e = _mm256_add_ps(e, one);
194
195 /* part2:
196 if( x < SQRTHF ) {
197 e -= 1;
198 x = x + x - 1.0;
199 } else { x = x - 1.0; }
200 */
201 //__m256 mask = _mm256_cmplt_ps(x, *(__m256*)_ps256_cephes_SQRTHF);
202 __m256 mask = _mm256_cmp_ps(x, *(__m256*)_ps256_cephes_SQRTHF, _CMP_LT_OS);
203 __m256 tmp = _mm256_and_ps(x, mask);
204 x = _mm256_sub_ps(x, one);
205 e = _mm256_sub_ps(e, _mm256_and_ps(one, mask));
206 x = _mm256_add_ps(x, tmp);
207
208 __m256 z = _mm256_mul_ps(x, x);
209
210 __m256 y = *(__m256*)_ps256_cephes_log_p0;
211 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p1);
212 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p2);
213 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p3);
214 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p4);
215 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p5);
216 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p6);
217 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p7);
218 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_log_p8);
219 y = _mm256_mul_ps(y, x);
220
221 y = _mm256_mul_ps(y, z);
222
223 y = _mm256_comp_fmadd_ps(e, *(__m256*)_ps256_cephes_log_q1, y);
224
225 //y = -z * 0.5 + y
226 y = _mm256_comp_fnmadd_ps(z, *(__m256*)_ps256_0p5, y);
227
228 x = _mm256_add_ps(x, y);
229 x = _mm256_comp_fmadd_ps(e, *(__m256*)_ps256_cephes_log_q2, x);
230 y = _mm256_or_ps(x, invalid_mask); // negative arg will be NAN
231 return y;
232}
233
234_PS256_CONST(exp_hi, 88.3762626647949f);
235_PS256_CONST(exp_lo, -88.3762626647949f);
236
237_PS256_CONST(cephes_LOG2EF, 1.44269504088896341f);
238_PS256_CONST(cephes_exp_C1, 0.693359375f);
239_PS256_CONST(cephes_exp_C2, -2.12194440e-4f);
240
241_PS256_CONST(cephes_exp_p0, 1.9875691500E-4f);
242_PS256_CONST(cephes_exp_p1, 1.3981999507E-3f);
243_PS256_CONST(cephes_exp_p2, 8.3334519073E-3f);
244_PS256_CONST(cephes_exp_p3, 4.1665795894E-2f);
245_PS256_CONST(cephes_exp_p4, 1.6666665459E-1f);
246_PS256_CONST(cephes_exp_p5, 5.0000001201E-1f);
247
248static NCNN_FORCEINLINE __m256 exp256_ps(__m256 x)
249{
250 __m256 tmp = _mm256_setzero_ps(), fx;
251 __m256i imm0;
252 __m256 one = *(__m256*)_ps256_1;
253
254 x = _mm256_min_ps(x, *(__m256*)_ps256_exp_hi);
255 x = _mm256_max_ps(x, *(__m256*)_ps256_exp_lo);
256
257 /* express exp(x) as exp(g + n*log(2)) */
258 fx = _mm256_comp_fmadd_ps(x, *(__m256*)_ps256_cephes_LOG2EF, *(__m256*)_ps256_0p5);
259
260 /* how to perform a floorf with SSE: just below */
261 //imm0 = _mm256_cvttps_epi32(fx);
262 //tmp = _mm256_cvtepi32_ps(imm0);
263
264 tmp = _mm256_floor_ps(fx);
265
266 /* if greater, subtract 1 */
267 //__m256 mask = _mm256_cmpgt_ps(tmp, fx);
268 __m256 mask = _mm256_cmp_ps(tmp, fx, _CMP_GT_OS);
269 mask = _mm256_and_ps(mask, one);
270 fx = _mm256_sub_ps(tmp, mask);
271
272 // x = x - fx * exp_C1
273 x = _mm256_comp_fnmadd_ps(fx, *(__m256*)_ps256_cephes_exp_C1, x);
274 // x = x - fx * exp_C2
275 x = _mm256_comp_fnmadd_ps(fx, *(__m256*)_ps256_cephes_exp_C2, x);
276
277 tmp = _mm256_mul_ps(x, x);
278
279 __m256 y = *(__m256*)_ps256_cephes_exp_p0;
280 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_exp_p1);
281 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_exp_p2);
282 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_exp_p3);
283 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_exp_p4);
284 y = _mm256_comp_fmadd_ps(y, x, *(__m256*)_ps256_cephes_exp_p5);
285 y = _mm256_comp_fmadd_ps(y, tmp, x);
286 y = _mm256_add_ps(y, one);
287
288 /* build 2^n */
289 imm0 = _mm256_cvttps_epi32(fx);
290 // another two AVX2 instructions
291 imm0 = _mm256_comp_add_epi32(imm0, *(__m256i*)_pi32_256_0x7f);
292 imm0 = _mm256_comp_slli_epi32(imm0, 23);
293 __m256 pow2n = _mm256_castsi256_ps(imm0);
294 y = _mm256_mul_ps(y, pow2n);
295 return y;
296}
297
298_PS256_CONST(tanh_hi, 9.0f);
299_PS256_CONST(tanh_lo, -9.0f);
300
301_PS256_CONST(cephes_tanh_p0, -2.76076847742355E-16f);
302_PS256_CONST(cephes_tanh_p1, 2.00018790482477E-13f);
303_PS256_CONST(cephes_tanh_p2, -8.60467152213735E-11f);
304_PS256_CONST(cephes_tanh_p3, 5.12229709037114E-08f);
305_PS256_CONST(cephes_tanh_p4, 1.48572235717979E-05f);
306_PS256_CONST(cephes_tanh_p5, 6.37261928875436E-04f);
307_PS256_CONST(cephes_tanh_p6, 4.89352455891786E-03f);
308
309_PS256_CONST(cephes_tanh_p7, 1.19825839466702e-06f);
310_PS256_CONST(cephes_tanh_p8, 1.18534705686654e-04f);
311_PS256_CONST(cephes_tanh_p9, 2.26843463243900e-03f);
312
313// an approximation of tanh
314static NCNN_FORCEINLINE __m256 tanh256_ps(__m256 x)
315{
316 __m256 value = x;
317 value = _mm256_max_ps(*(__m256*)_ps256_tanh_lo, value);
318 value = _mm256_min_ps(*(__m256*)_ps256_tanh_hi, value);
319
320 __m256 value_squared = _mm256_mul_ps(value, value);
321
322 __m256 p;
323 p = _mm256_comp_fmadd_ps(value_squared, *(__m256*)_ps256_cephes_tanh_p0, *(__m256*)_ps256_cephes_tanh_p1);
324 p = _mm256_comp_fmadd_ps(p, value_squared, *(__m256*)_ps256_cephes_tanh_p2);
325 p = _mm256_comp_fmadd_ps(p, value_squared, *(__m256*)_ps256_cephes_tanh_p3);
326 p = _mm256_comp_fmadd_ps(p, value_squared, *(__m256*)_ps256_cephes_tanh_p4);
327 p = _mm256_comp_fmadd_ps(p, value_squared, *(__m256*)_ps256_cephes_tanh_p5);
328 p = _mm256_comp_fmadd_ps(p, value_squared, *(__m256*)_ps256_cephes_tanh_p6);
329 p = _mm256_mul_ps(p, value);
330
331 __m256 q;
332 q = _mm256_comp_fmadd_ps(value_squared, *(__m256*)_ps256_cephes_tanh_p7, *(__m256*)_ps256_cephes_tanh_p8);
333 q = _mm256_comp_fmadd_ps(q, value_squared, *(__m256*)_ps256_cephes_tanh_p9);
334 q = _mm256_comp_fmadd_ps(q, value_squared, *(__m256*)_ps256_cephes_tanh_p6);
335
336 __m256 dst = _mm256_div_ps(p, q);
337 return dst;
338}
339
340_PS256_CONST(minus_cephes_DP1, -0.78515625f);
341_PS256_CONST(minus_cephes_DP2, -2.4187564849853515625e-4f);
342_PS256_CONST(minus_cephes_DP3, -3.77489497744594108e-8f);
343_PS256_CONST(sincof_p0, -1.9515295891E-4f);
344_PS256_CONST(sincof_p1, 8.3321608736E-3f);
345_PS256_CONST(sincof_p2, -1.6666654611E-1f);
346_PS256_CONST(coscof_p0, 2.443315711809948E-005f);
347_PS256_CONST(coscof_p1, -1.388731625493765E-003f);
348_PS256_CONST(coscof_p2, 4.166664568298827E-002f);
349_PS256_CONST(cephes_FOPI, 1.27323954473516f); // 4 / M_PI
350
351/* evaluation of 8 sines at onces using AVX intrisics
352
353 The code is the exact rewriting of the cephes sinf function.
354 Precision is excellent as long as x < 8192 (I did not bother to
355 take into account the special handling they have for greater values
356 -- it does not return garbage for arguments over 8192, though, but
357 the extra precision is missing).
358
359 Note that it is such that sinf((float)M_PI) = 8.74e-8, which is the
360 surprising but correct result.
361
362*/
363static NCNN_FORCEINLINE __m256 sin256_ps(__m256 x)
364{ // any x
365 __m256 xmm1, xmm2 = _mm256_setzero_ps(), xmm3, sign_bit, y;
366 __m256i imm0, imm2;
367
368#ifndef __AVX2__
369 __m128i imm0_1, imm0_2;
370 __m128i imm2_1, imm2_2;
371#endif
372
373 sign_bit = x;
374 /* take the absolute value */
375 x = _mm256_and_ps(x, *(__m256*)_ps256_inv_sign_mask);
376 /* extract the sign bit (upper one) */
377 sign_bit = _mm256_and_ps(sign_bit, *(__m256*)_ps256_sign_mask);
378
379 /* scale by 4/Pi */
380 y = _mm256_mul_ps(x, *(__m256*)_ps256_cephes_FOPI);
381
382 /*
383 Here we start a series of integer operations, which are in the
384 realm of AVX2.
385 If we don't have AVX, let's perform them using SSE2 directives
386 */
387
388#ifdef __AVX2__
389 /* store the integer part of y in mm0 */
390 imm2 = _mm256_cvttps_epi32(y);
391 /* j=(j+1) & (~1) (see the cephes sources) */
392 // another two AVX2 instruction
393 imm2 = _mm256_comp_add_epi32(imm2, *(__m256i*)_pi32_256_1);
394 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_inv1);
395 y = _mm256_cvtepi32_ps(imm2);
396
397 /* get the swap sign flag */
398 imm0 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_4);
399 imm0 = _mm256_comp_slli_epi32(imm0, 29);
400 /* get the polynom selection mask
401 there is one polynom for 0 <= x <= Pi/4
402 and another one for Pi/4<x<=Pi/2
403
404 Both branches will be computed.
405 */
406 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_2);
407 imm2 = _mm256_cmpeq_epi32(imm2, *(__m256i*)_pi32_256_0);
408#else
409 /* we use SSE2 routines to perform the integer ops */
410 COPY_IMM_TO_XMM(_mm256_cvttps_epi32(y), imm2_1, imm2_2);
411
412 imm2_1 = _mm_add_epi32(imm2_1, *(__m128i*)_pi32avx_1);
413 imm2_2 = _mm_add_epi32(imm2_2, *(__m128i*)_pi32avx_1);
414
415 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_inv1);
416 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_inv1);
417
418 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
419 y = _mm256_cvtepi32_ps(imm2);
420
421 imm0_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_4);
422 imm0_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_4);
423
424 imm0_1 = _mm_slli_epi32(imm0_1, 29);
425 imm0_2 = _mm_slli_epi32(imm0_2, 29);
426
427 COPY_XMM_TO_IMM(imm0_1, imm0_2, imm0);
428
429 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_2);
430 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_2);
431
432 imm2_1 = _mm_cmpeq_epi32(imm2_1, _mm_setzero_si128());
433 imm2_2 = _mm_cmpeq_epi32(imm2_2, _mm_setzero_si128());
434
435 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
436#endif
437
438 __m256 swap_sign_bit = _mm256_castsi256_ps(imm0);
439 __m256 poly_mask = _mm256_castsi256_ps(imm2);
440 sign_bit = _mm256_xor_ps(sign_bit, swap_sign_bit);
441
442 /* The magic pass: "Extended precision modular arithmetic"
443 x = ((x - y * DP1) - y * DP2) - y * DP3; */
444 xmm1 = *(__m256*)_ps256_minus_cephes_DP1;
445 xmm2 = *(__m256*)_ps256_minus_cephes_DP2;
446 xmm3 = *(__m256*)_ps256_minus_cephes_DP3;
447 x = _mm256_comp_fmadd_ps(y, xmm1, x);
448 x = _mm256_comp_fmadd_ps(y, xmm2, x);
449 x = _mm256_comp_fmadd_ps(y, xmm3, x);
450
451 /* Evaluate the first polynom (0 <= x <= Pi/4) */
452 y = *(__m256*)_ps256_coscof_p0;
453 __m256 z = _mm256_mul_ps(x, x);
454
455 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p1);
456 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p2);
457 y = _mm256_mul_ps(y, z);
458 y = _mm256_mul_ps(y, z);
459 // y = y - z * 0.5
460 y = _mm256_comp_fnmadd_ps(z, *(__m256*)_ps256_0p5, y);
461 y = _mm256_add_ps(y, *(__m256*)_ps256_1);
462
463 /* Evaluate the second polynom (Pi/4 <= x <= 0) */
464
465 __m256 y2 = *(__m256*)_ps256_sincof_p0;
466 y2 = _mm256_comp_fmadd_ps(y2, z, *(__m256*)_ps256_sincof_p1);
467 y2 = _mm256_comp_fmadd_ps(y2, z, *(__m256*)_ps256_sincof_p2);
468 y2 = _mm256_mul_ps(y2, z);
469 y2 = _mm256_comp_fmadd_ps(y2, x, x);
470
471 /* select the correct result from the two polynoms */
472 xmm3 = poly_mask;
473 y2 = _mm256_and_ps(xmm3, y2); //, xmm3);
474 y = _mm256_andnot_ps(xmm3, y);
475 y = _mm256_add_ps(y, y2);
476 /* update the sign */
477 y = _mm256_xor_ps(y, sign_bit);
478
479 return y;
480}
481
482/* almost the same as sin_ps */
483static NCNN_FORCEINLINE __m256 cos256_ps(__m256 x)
484{ // any x
485 __m256 xmm1, xmm2 = _mm256_setzero_ps(), xmm3, y;
486 __m256i imm0, imm2;
487
488#ifndef __AVX2__
489 __m128i imm0_1, imm0_2;
490 __m128i imm2_1, imm2_2;
491#endif
492
493 /* take the absolute value */
494 x = _mm256_and_ps(x, *(__m256*)_ps256_inv_sign_mask);
495
496 /* scale by 4/Pi */
497 y = _mm256_mul_ps(x, *(__m256*)_ps256_cephes_FOPI);
498
499#ifdef __AVX2__
500 /* store the integer part of y in mm0 */
501 imm2 = _mm256_cvttps_epi32(y);
502 /* j=(j+1) & (~1) (see the cephes sources) */
503 imm2 = _mm256_comp_add_epi32(imm2, *(__m256i*)_pi32_256_1);
504 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_inv1);
505 y = _mm256_cvtepi32_ps(imm2);
506 imm2 = _mm256_comp_sub_epi32(imm2, *(__m256i*)_pi32_256_2);
507
508 /* get the swap sign flag */
509 imm0 = _mm256_andnot_si256(imm2, *(__m256i*)_pi32_256_4);
510 imm0 = _mm256_comp_slli_epi32(imm0, 29);
511 /* get the polynom selection mask */
512 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_2);
513 imm2 = _mm256_cmpeq_epi32(imm2, *(__m256i*)_pi32_256_0);
514#else
515
516 /* we use SSE2 routines to perform the integer ops */
517 COPY_IMM_TO_XMM(_mm256_cvttps_epi32(y), imm2_1, imm2_2);
518
519 imm2_1 = _mm_add_epi32(imm2_1, *(__m128i*)_pi32avx_1);
520 imm2_2 = _mm_add_epi32(imm2_2, *(__m128i*)_pi32avx_1);
521
522 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_inv1);
523 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_inv1);
524
525 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
526 y = _mm256_cvtepi32_ps(imm2);
527
528 imm2_1 = _mm_sub_epi32(imm2_1, *(__m128i*)_pi32avx_2);
529 imm2_2 = _mm_sub_epi32(imm2_2, *(__m128i*)_pi32avx_2);
530
531 imm0_1 = _mm_andnot_si128(imm2_1, *(__m128i*)_pi32avx_4);
532 imm0_2 = _mm_andnot_si128(imm2_2, *(__m128i*)_pi32avx_4);
533
534 imm0_1 = _mm_slli_epi32(imm0_1, 29);
535 imm0_2 = _mm_slli_epi32(imm0_2, 29);
536
537 COPY_XMM_TO_IMM(imm0_1, imm0_2, imm0);
538
539 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_2);
540 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_2);
541
542 imm2_1 = _mm_cmpeq_epi32(imm2_1, _mm_setzero_si128());
543 imm2_2 = _mm_cmpeq_epi32(imm2_2, _mm_setzero_si128());
544
545 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
546#endif
547
548 __m256 sign_bit = _mm256_castsi256_ps(imm0);
549 __m256 poly_mask = _mm256_castsi256_ps(imm2);
550
551 /* The magic pass: "Extended precision modular arithmetic"
552 x = ((x - y * DP1) - y * DP2) - y * DP3; */
553 xmm1 = *(__m256*)_ps256_minus_cephes_DP1;
554 xmm2 = *(__m256*)_ps256_minus_cephes_DP2;
555 xmm3 = *(__m256*)_ps256_minus_cephes_DP3;
556 x = _mm256_comp_fmadd_ps(y, xmm1, x);
557 x = _mm256_comp_fmadd_ps(y, xmm2, x);
558 x = _mm256_comp_fmadd_ps(y, xmm3, x);
559
560 /* Evaluate the first polynom (0 <= x <= Pi/4) */
561 y = *(__m256*)_ps256_coscof_p0;
562 __m256 z = _mm256_mul_ps(x, x);
563
564 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p1);
565 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p2);
566 y = _mm256_mul_ps(y, z);
567 y = _mm256_mul_ps(y, z);
568 // y = y - z * 0.5
569 y = _mm256_comp_fnmadd_ps(z, *(__m256*)_ps256_0p5, y);
570 y = _mm256_add_ps(y, *(__m256*)_ps256_1);
571
572 /* Evaluate the second polynom (Pi/4 <= x <= 0) */
573
574 __m256 y2 = *(__m256*)_ps256_sincof_p0;
575 y2 = _mm256_mul_ps(y2, z);
576 y2 = _mm256_add_ps(y2, *(__m256*)_ps256_sincof_p1);
577 y2 = _mm256_mul_ps(y2, z);
578 y2 = _mm256_add_ps(y2, *(__m256*)_ps256_sincof_p2);
579 y2 = _mm256_mul_ps(y2, z);
580 y2 = _mm256_mul_ps(y2, x);
581 y2 = _mm256_add_ps(y2, x);
582
583 /* select the correct result from the two polynoms */
584 xmm3 = poly_mask;
585 y2 = _mm256_and_ps(xmm3, y2); //, xmm3);
586 y = _mm256_andnot_ps(xmm3, y);
587 y = _mm256_add_ps(y, y2);
588 /* update the sign */
589 y = _mm256_xor_ps(y, sign_bit);
590
591 return y;
592}
593
594/* since sin256_ps and cos256_ps are almost identical, sincos256_ps could replace both of them..
595 it is almost as fast, and gives you a free cosine with your sine */
596static NCNN_FORCEINLINE void sincos256_ps(__m256 x, __m256* s, __m256* c)
597{
598 __m256 xmm1, xmm2, xmm3 = _mm256_setzero_ps(), sign_bit_sin, y;
599 __m256i imm0, imm2, imm4;
600
601#ifndef __AVX2__
602 __m128i imm0_1, imm0_2;
603 __m128i imm2_1, imm2_2;
604 __m128i imm4_1, imm4_2;
605#endif
606
607 sign_bit_sin = x;
608 /* take the absolute value */
609 x = _mm256_and_ps(x, *(__m256*)_ps256_inv_sign_mask);
610 /* extract the sign bit (upper one) */
611 sign_bit_sin = _mm256_and_ps(sign_bit_sin, *(__m256*)_ps256_sign_mask);
612
613 /* scale by 4/Pi */
614 y = _mm256_mul_ps(x, *(__m256*)_ps256_cephes_FOPI);
615
616#ifdef __AVX2__
617 /* store the integer part of y in imm2 */
618 imm2 = _mm256_cvttps_epi32(y);
619
620 /* j=(j+1) & (~1) (see the cephes sources) */
621 imm2 = _mm256_comp_add_epi32(imm2, *(__m256i*)_pi32_256_1);
622 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_inv1);
623
624 y = _mm256_cvtepi32_ps(imm2);
625 imm4 = imm2;
626
627 /* get the swap sign flag for the sine */
628 imm0 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_4);
629 imm0 = _mm256_comp_slli_epi32(imm0, 29);
630 //__m256 swap_sign_bit_sin = _mm256_castsi256_ps(imm0);
631
632 /* get the polynom selection mask for the sine*/
633 imm2 = _mm256_and_si256(imm2, *(__m256i*)_pi32_256_2);
634 imm2 = _mm256_cmpeq_epi32(imm2, *(__m256i*)_pi32_256_0);
635 //__m256 poly_mask = _mm256_castsi256_ps(imm2);
636#else
637 /* we use SSE2 routines to perform the integer ops */
638 COPY_IMM_TO_XMM(_mm256_cvttps_epi32(y), imm2_1, imm2_2);
639
640 imm2_1 = _mm_add_epi32(imm2_1, *(__m128i*)_pi32avx_1);
641 imm2_2 = _mm_add_epi32(imm2_2, *(__m128i*)_pi32avx_1);
642
643 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_inv1);
644 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_inv1);
645
646 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
647 y = _mm256_cvtepi32_ps(imm2);
648
649 imm4_1 = imm2_1;
650 imm4_2 = imm2_2;
651
652 imm0_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_4);
653 imm0_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_4);
654
655 imm0_1 = _mm_slli_epi32(imm0_1, 29);
656 imm0_2 = _mm_slli_epi32(imm0_2, 29);
657
658 COPY_XMM_TO_IMM(imm0_1, imm0_2, imm0);
659
660 imm2_1 = _mm_and_si128(imm2_1, *(__m128i*)_pi32avx_2);
661 imm2_2 = _mm_and_si128(imm2_2, *(__m128i*)_pi32avx_2);
662
663 imm2_1 = _mm_cmpeq_epi32(imm2_1, _mm_setzero_si128());
664 imm2_2 = _mm_cmpeq_epi32(imm2_2, _mm_setzero_si128());
665
666 COPY_XMM_TO_IMM(imm2_1, imm2_2, imm2);
667#endif
668 __m256 swap_sign_bit_sin = _mm256_castsi256_ps(imm0);
669 __m256 poly_mask = _mm256_castsi256_ps(imm2);
670
671 /* The magic pass: "Extended precision modular arithmetic"
672 x = ((x - y * DP1) - y * DP2) - y * DP3; */
673 xmm1 = *(__m256*)_ps256_minus_cephes_DP1;
674 xmm2 = *(__m256*)_ps256_minus_cephes_DP2;
675 xmm3 = *(__m256*)_ps256_minus_cephes_DP3;
676 x = _mm256_comp_fmadd_ps(y, xmm1, x);
677 x = _mm256_comp_fmadd_ps(y, xmm2, x);
678 x = _mm256_comp_fmadd_ps(y, xmm3, x);
679
680#ifdef __AVX2__
681 imm4 = _mm256_comp_sub_epi32(imm4, *(__m256i*)_pi32_256_2);
682 imm4 = _mm256_andnot_si256(imm4, *(__m256i*)_pi32_256_4);
683 imm4 = _mm256_comp_slli_epi32(imm4, 29);
684#else
685 imm4_1 = _mm_sub_epi32(imm4_1, *(__m128i*)_pi32avx_2);
686 imm4_2 = _mm_sub_epi32(imm4_2, *(__m128i*)_pi32avx_2);
687
688 imm4_1 = _mm_andnot_si128(imm4_1, *(__m128i*)_pi32avx_4);
689 imm4_2 = _mm_andnot_si128(imm4_2, *(__m128i*)_pi32avx_4);
690
691 imm4_1 = _mm_slli_epi32(imm4_1, 29);
692 imm4_2 = _mm_slli_epi32(imm4_2, 29);
693
694 COPY_XMM_TO_IMM(imm4_1, imm4_2, imm4);
695#endif
696
697 __m256 sign_bit_cos = _mm256_castsi256_ps(imm4);
698
699 sign_bit_sin = _mm256_xor_ps(sign_bit_sin, swap_sign_bit_sin);
700
701 /* Evaluate the first polynom (0 <= x <= Pi/4) */
702 __m256 z = _mm256_mul_ps(x, x);
703 y = *(__m256*)_ps256_coscof_p0;
704
705 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p1);
706 y = _mm256_comp_fmadd_ps(y, z, *(__m256*)_ps256_coscof_p2);
707 y = _mm256_mul_ps(y, z);
708 y = _mm256_mul_ps(y, z);
709 // y = y - z * 0.5
710 y = _mm256_comp_fnmadd_ps(z, *(__m256*)_ps256_0p5, y);
711 y = _mm256_add_ps(y, *(__m256*)_ps256_1);
712
713 /* Evaluate the second polynom (Pi/4 <= x <= 0) */
714
715 __m256 y2 = *(__m256*)_ps256_sincof_p0;
716 y2 = _mm256_comp_fmadd_ps(y2, z, *(__m256*)_ps256_sincof_p1);
717 y2 = _mm256_comp_fmadd_ps(y2, z, *(__m256*)_ps256_sincof_p2);
718 y2 = _mm256_mul_ps(y2, z);
719 y2 = _mm256_comp_fmadd_ps(y2, x, x);
720
721 /* select the correct result from the two polynoms */
722 xmm3 = poly_mask;
723 __m256 ysin2 = _mm256_and_ps(xmm3, y2);
724 __m256 ysin1 = _mm256_andnot_ps(xmm3, y);
725 y2 = _mm256_sub_ps(y2, ysin2);
726 y = _mm256_sub_ps(y, ysin1);
727
728 xmm1 = _mm256_add_ps(ysin1, ysin2);
729 xmm2 = _mm256_add_ps(y, y2);
730
731 /* update the sign */
732 *s = _mm256_xor_ps(xmm1, sign_bit_sin);
733 *c = _mm256_xor_ps(xmm2, sign_bit_cos);
734}
735
736static NCNN_FORCEINLINE __m256 tan256_ps(__m256 x)
737{
738 __m256 ysin, ycos;
739 __m256 eps = _mm256_set1_ps(1E-8f);
740 sincos256_ps(x, &ysin, &ycos);
741 __m256 mask = _mm256_cmp_ps(ycos, _mm256_setzero_ps(), _CMP_EQ_OS);
742 __m256 _tmp = _mm256_and_ps(eps, mask);
743 ycos = _mm256_add_ps(ycos, _tmp);
744 __m256 ytan = _mm256_div_ps(ysin, ycos);
745 return ytan;
746}
747
748static NCNN_FORCEINLINE __m256 pow256_ps(__m256 a, __m256 b)
749{
750 // pow(x, m) = exp(m * log(x))
751 return exp256_ps(_mm256_mul_ps(b, log256_ps(a)));
752}
753
754static NCNN_FORCEINLINE __m256 asin256_ps(__m256 x)
755{
756 const __m256 magic_negative_zero = _mm256_set1_ps(-0.0f);
757 const __m256 magic_half_one = _mm256_set1_ps(0.5f);
758 const __m256 magic_one = _mm256_set1_ps(1.0f);
759 const __m256 magic_a4 = _mm256_set1_ps(0.023994016f);
760 const __m256 magic_a5 = _mm256_set1_ps(0.042417344f);
761 const __m256 magic_a2 = _mm256_set1_ps(0.07494697f);
762 const __m256 magic_a3 = _mm256_set1_ps(0.045520633f);
763 const __m256 magic_a0 = _mm256_set1_ps(1.0f);
764 const __m256 magic_a1 = _mm256_set1_ps(0.166667819f);
765 const __m256 magic_half_pi = _mm256_set1_ps(1.5707964f);
766 const __m256 magic_three = _mm256_set1_ps(3.0f);
767
768 // negative_mask = magic_negative_zero && x;
769 __m256 negative_mask = _mm256_and_ps(magic_negative_zero, x);
770
771 // absolute = abs(x);
772 __m256 absolute = _mm256_andnot_ps(magic_negative_zero, x);
773
774 // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation
775
776 // is_small_input = (absolute <= 0.5f);
777 __m256 is_small_input = _mm256_cmp_ps(absolute, magic_half_one, _CMP_LE_OQ);
778
779 // is_big_input = (is_small_input ? 0.0f : 1.0f);
780 __m256 is_big_input = _mm256_andnot_ps(is_small_input, magic_one);
781
782 // big_input_approx = sqrt(0.5f * (1 - absolute));
783 __m256 big_input_approx = _mm256_sqrt_ps(_mm256_mul_ps(
784 magic_half_one,
785 _mm256_sub_ps(magic_one, absolute)));
786
787 // input_approx = (is_small_input ? absolute : big_input_approx);
788 __m256 input_approx = _mm256_or_ps(
789 _mm256_and_ps(is_small_input, absolute),
790 _mm256_andnot_ps(is_small_input, big_input_approx));
791
792 // square_of_input_approx = input_approx * input_approx;
793 __m256 square_of_input_approx = _mm256_mul_ps(input_approx, input_approx);
794
795 // fourth_power_of_input_approx =
796 // square_of_input_approx * square_of_input_approx;
797 __m256 fourth_power_of_input_approx = _mm256_mul_ps(
798 square_of_input_approx, square_of_input_approx);
799
800 // TODO: Need more explanations.
801 // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2);
802 // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3);
803 // x3 = ((fourth_power_of_input_approx * x1) + magic_a0);
804 // x4 = ((fourth_power_of_input_approx * x2) + magic_a1);
805 // output_approx = ((square_of_input_approx * x4) + x3);
806 __m256 output_approx = _mm256_comp_fmadd_ps(
807 square_of_input_approx,
808 _mm256_comp_fmadd_ps(
809 fourth_power_of_input_approx,
810 _mm256_comp_fmadd_ps(
811 fourth_power_of_input_approx,
812 magic_a5,
813 magic_a3),
814 magic_a1),
815 _mm256_comp_fmadd_ps(
816 fourth_power_of_input_approx,
817 _mm256_comp_fmadd_ps(
818 fourth_power_of_input_approx,
819 magic_a4,
820 magic_a2),
821 magic_a0));
822
823 // TODO: Need more explanations.
824 // x1 = ((0.5 * PI) * is_big_input);
825 // x2 = (output_approx * input_approx);
826 // x3 = (-(3.0f * is_big_input) + 1.0f);
827 // final_approx = ((x2 * x3) + x1);
828 __m256 final_approx = _mm256_comp_fmadd_ps(
829 _mm256_mul_ps(output_approx, input_approx),
830 _mm256_comp_fnmadd_ps(magic_three, is_big_input, magic_one),
831 _mm256_mul_ps(magic_half_pi, is_big_input));
832
833 // return (final_approx || negative_mask);
834 return _mm256_or_ps(final_approx, negative_mask);
835}
836
837static NCNN_FORCEINLINE __m256 acos256_ps(__m256 x)
838{
839 const __m256 magic_negative_zero = _mm256_set1_ps(-0.0f);
840 const __m256 magic_zero = _mm256_set1_ps(0.0f);
841 const __m256 magic_half_one = _mm256_set1_ps(0.5f);
842 const __m256 magic_one = _mm256_set1_ps(1.0f);
843 const __m256 magic_a4 = _mm256_set1_ps(0.023994016f);
844 const __m256 magic_a5 = _mm256_set1_ps(0.042417344f);
845 const __m256 magic_a2 = _mm256_set1_ps(0.07494697f);
846 const __m256 magic_a3 = _mm256_set1_ps(0.045520633f);
847 const __m256 magic_a0 = _mm256_set1_ps(1.0f);
848 const __m256 magic_a1 = _mm256_set1_ps(0.166667819f);
849 const __m256 magic_half_pi = _mm256_set1_ps(1.5707964f);
850 const __m256 magic_pi = _mm256_set1_ps(3.1415927f);
851
852 // negative_mask = magic_negative_zero && x;
853 __m256 negative_mask = _mm256_and_ps(magic_negative_zero, x);
854
855 // absolute = abs(x);
856 __m256 absolute = _mm256_andnot_ps(magic_negative_zero, x);
857
858 // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation
859
860 // is_small_input = (absolute <= 0.5f);
861 __m256 is_small_input = _mm256_cmp_ps(absolute, magic_half_one, _CMP_LE_OQ);
862
863 // big_input_approx = sqrt(0.5f * (1 - absolute));
864 __m256 big_input_approx = _mm256_sqrt_ps(_mm256_mul_ps(
865 magic_half_one,
866 _mm256_sub_ps(magic_one, absolute)));
867
868 // input_approx = (is_small_input ? absolute : big_input_approx);
869 __m256 input_approx = _mm256_or_ps(
870 _mm256_and_ps(is_small_input, absolute),
871 _mm256_andnot_ps(is_small_input, big_input_approx));
872
873 // square_of_input_approx = input_approx * input_approx;
874 __m256 square_of_input_approx = _mm256_mul_ps(input_approx, input_approx);
875
876 // fourth_power_of_input_approx =
877 // square_of_input_approx * square_of_input_approx;
878 __m256 fourth_power_of_input_approx = _mm256_mul_ps(
879 square_of_input_approx, square_of_input_approx);
880
881 // TODO: Need more explanations.
882 // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2);
883 // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3);
884 // x3 = ((fourth_power_of_input_approx * x1) + magic_a0);
885 // x4 = ((fourth_power_of_input_approx * x2) + magic_a1);
886 // output_approx = ((square_of_input_approx * x4) + x3);
887 __m256 output_approx = _mm256_comp_fmadd_ps(
888 square_of_input_approx,
889 _mm256_comp_fmadd_ps(
890 fourth_power_of_input_approx,
891 _mm256_comp_fmadd_ps(
892 fourth_power_of_input_approx,
893 magic_a5,
894 magic_a3),
895 magic_a1),
896 _mm256_comp_fmadd_ps(
897 fourth_power_of_input_approx,
898 _mm256_comp_fmadd_ps(
899 fourth_power_of_input_approx,
900 magic_a4,
901 magic_a2),
902 magic_a0));
903
904 // TODO: Need more explanations.
905 // x1 = (output_approx * input_approx);
906 __m256 x1 = _mm256_mul_ps(output_approx, input_approx);
907
908 // TODO: Need more explanations.
909 // small_final_approx = ((0.5 * PI) - (x1 | negative_mask));
910 __m256 small_final_approx = _mm256_sub_ps(
911 magic_half_pi,
912 _mm256_or_ps(x1, negative_mask));
913
914 // TODO: Need more explanations.
915 // big_final_approx = (((x < 0.0f) & PI) + ((x1 * 2) | negative_mask));
916 __m256 big_final_approx = _mm256_add_ps(
917 _mm256_and_ps(_mm256_cmp_ps(x, magic_zero, _CMP_LT_OQ), magic_pi),
918 _mm256_or_ps(_mm256_add_ps(x1, x1), negative_mask));
919
920 // return (is_small_input ? small_final_approx : big_final_approx);
921 return _mm256_or_ps(
922 _mm256_and_ps(is_small_input, small_final_approx),
923 _mm256_andnot_ps(is_small_input, big_final_approx));
924}
925
926static NCNN_FORCEINLINE __m256 atan256_ps(__m256 x)
927{
928 const __m256 magic_negative_zero = _mm256_set1_ps(-0.0f);
929 const __m256 magic_one = _mm256_set1_ps(1.0f);
930 const __m256 magic_negative_one = _mm256_set1_ps(-1.0f);
931 const __m256 magic_half_pi = _mm256_set1_ps(1.5707964f);
932 const __m256 magic_a0 = _mm256_set1_ps(1.0f);
933 const __m256 magic_a1 = _mm256_set1_ps(-0.33333072f);
934 const __m256 magic_a2 = _mm256_set1_ps(0.1999262f);
935 const __m256 magic_a3 = _mm256_set1_ps(-0.14203644f);
936 const __m256 magic_a4 = _mm256_set1_ps(0.10640934f);
937 const __m256 magic_a5 = _mm256_set1_ps(-0.07504295f);
938 const __m256 magic_a6 = _mm256_set1_ps(0.04269152f);
939 const __m256 magic_a7 = _mm256_set1_ps(-0.01606863f);
940 const __m256 magic_a8 = _mm256_set1_ps(0.0028498897f);
941
942 // negative_mask = magic_negative_zero && x;
943 __m256 negative_mask = _mm256_and_ps(magic_negative_zero, x);
944
945 // absolute = abs(x);
946 __m256 absolute = _mm256_andnot_ps(magic_negative_zero, x);
947
948 // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation
949
950 // is_small_input = (1.0f < absolute);
951 __m256 is_small_input = _mm256_cmp_ps(magic_one, absolute, _CMP_LT_OQ);
952
953 // x1 = (is_small_input ? -1.0f : absolute);
954 // x2 = (is_small_input ? absolute : 1.0f)
955 // input_approx = x1 / x2;
956 __m256 input_approx = _mm256_div_ps(
957 _mm256_or_ps(
958 _mm256_and_ps(is_small_input, magic_negative_one),
959 _mm256_andnot_ps(is_small_input, absolute)),
960 _mm256_or_ps(
961 _mm256_and_ps(is_small_input, absolute),
962 _mm256_andnot_ps(is_small_input, magic_one)));
963
964 // square_of_input_approx = input_approx * input_approx;
965 __m256 square_of_input_approx = _mm256_mul_ps(input_approx, input_approx);
966
967 // fourth_power_of_input_approx =
968 // square_of_input_approx * square_of_input_approx;
969 __m256 fourth_power_of_input_approx = _mm256_mul_ps(
970 square_of_input_approx, square_of_input_approx);
971
972 // TODO: Need more explanations.
973 // x1 = ((fourth_power_of_input_approx * magic_a7) + magic_a5);
974 // x2 = ((fourth_power_of_input_approx * magic_a8) + magic_a6);
975 // x3 = ((fourth_power_of_input_approx * x1) + magic_a3);
976 // x4 = ((fourth_power_of_input_approx * x2) + magic_a4);
977 // x5 = ((fourth_power_of_input_approx * x3) + magic_a1);
978 // x6 = ((fourth_power_of_input_approx * x4) + magic_a2);
979 // x7 = ((fourth_power_of_input_approx * x6) + magic_a0);
980 // output_approx = ((square_of_input_approx * x5) + x7);
981 __m256 output_approx = _mm256_comp_fmadd_ps(
982 square_of_input_approx,
983 _mm256_comp_fmadd_ps(
984 fourth_power_of_input_approx,
985 _mm256_comp_fmadd_ps(
986 fourth_power_of_input_approx,
987 _mm256_comp_fmadd_ps(
988 fourth_power_of_input_approx,
989 magic_a7,
990 magic_a5),
991 magic_a3),
992 magic_a1),
993 _mm256_comp_fmadd_ps(
994 fourth_power_of_input_approx,
995 _mm256_comp_fmadd_ps(
996 fourth_power_of_input_approx,
997 _mm256_comp_fmadd_ps(
998 fourth_power_of_input_approx,
999 _mm256_comp_fmadd_ps(
1000 fourth_power_of_input_approx,
1001 magic_a8,
1002 magic_a6),
1003 magic_a4),
1004 magic_a2),
1005 magic_a0));
1006
1007 // TODO: Need more explanations.
1008 // x1 = (output_approx * input_approx);
1009 // if (is_small_input) x1 += (0.5 * PI);
1010 // return (negative_mask ? -x1 : x1);
1011 return _mm256_or_ps(
1012 _mm256_add_ps(
1013 _mm256_mul_ps(output_approx, input_approx),
1014 _mm256_and_ps(is_small_input, magic_half_pi)),
1015 negative_mask);
1016}
1017
1018static NCNN_FORCEINLINE __m256 atan2256_ps(__m256 y, __m256 x)
1019{
1020 // Reference: https://mazzo.li/posts/vectorized-atan2.html
1021
1022 const __m256 magic_zero = _mm256_set1_ps(0.0f);
1023 const __m256 magic_negative_zero = _mm256_set1_ps(-0.0f);
1024 const __m256 magic_pi = _mm256_set1_ps(3.1415927f);
1025 const __m256 magic_half_pi = _mm256_set1_ps(1.5707964f);
1026
1027 // not_equal_zero_x = (x != 0.0f);
1028 __m256 not_equal_zero_x = _mm256_cmp_ps(x, magic_zero, _CMP_NEQ_OQ);
1029
1030 // not_equal_zero_y = (y != 0.0f);
1031 __m256 not_equal_zero_y = _mm256_cmp_ps(y, magic_zero, _CMP_NEQ_OQ);
1032
1033 // normal_mode = ((x != 0.0f) & (y != 0.0f));
1034 __m256 normal_mode = _mm256_and_ps(not_equal_zero_x, not_equal_zero_y);
1035
1036 // negative_mask_x = magic_negative_zero && x;
1037 __m256 negative_mask_x = _mm256_and_ps(magic_negative_zero, x);
1038
1039 // negative_mask_y = magic_negative_zero && y;
1040 __m256 negative_mask_y = _mm256_and_ps(magic_negative_zero, y);
1041
1042 // pi_additions = ((x < 0.0f) ? ((y < 0.0f) ? -PI : PI) : 0.0f);
1043 __m256 pi_additions = _mm256_and_ps(
1044 _mm256_cmp_ps(x, magic_zero, _CMP_LT_OQ),
1045 _mm256_or_ps(
1046 _mm256_and_ps(
1047 _mm256_cmp_ps(y, magic_zero, _CMP_LT_OQ),
1048 magic_negative_zero),
1049 magic_pi));
1050
1051 // normal_result = (atan(y / x) + pi_additions);
1052 __m256 normal_result = _mm256_add_ps(
1053 atan256_ps(_mm256_div_ps(y, x)),
1054 pi_additions);
1055
1056 // negative_mask_full_x = ((negative_mask_x | PI) < 0.0f);
1057 __m256 negative_mask_full_x = _mm256_cmp_ps(
1058 _mm256_or_ps(negative_mask_x, magic_pi),
1059 magic_zero,
1060 _CMP_LT_OQ);
1061
1062 // x1 = (negative_mask_y ? -(0.5 * PI) : (0.5 * PI));
1063 // x2 = (negative_mask_full_x ? PI : 0.0f);
1064 // special_result = ((y != 0.0f) ? x1 : x2);
1065 __m256 special_result = _mm256_or_ps(
1066 _mm256_and_ps(
1067 not_equal_zero_y,
1068 _mm256_or_ps(negative_mask_y, magic_half_pi)),
1069 _mm256_andnot_ps(
1070 not_equal_zero_y,
1071 _mm256_or_ps(
1072 _mm256_and_ps(negative_mask_full_x, magic_pi),
1073 _mm256_andnot_ps(negative_mask_full_x, magic_zero))));
1074
1075 // return (normal_mode ? normal_result : special_result);
1076 return _mm256_or_ps(
1077 _mm256_and_ps(normal_mode, normal_result),
1078 _mm256_andnot_ps(normal_mode, special_result));
1079}
1080
1081static NCNN_FORCEINLINE __m256 abs256_ps(__m256 x)
1082{
1083 const __m256 abs_mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
1084 return _mm256_and_ps(abs_mask, x);
1085}
1086
1087#endif // AVX_MATHFUN_H