| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef AVX512_MATHFUN_H |
| 16 | #define AVX512_MATHFUN_H |
| 17 | |
| 18 | #include <immintrin.h> |
| 19 | #include <emmintrin.h> |
| 20 | |
| 21 | /* yes I know, the top of this file is quite ugly */ |
| 22 | |
| 23 | #ifdef _MSC_VER /* visual c++ */ |
| 24 | #define ALIGN64_BEG __declspec(align(64)) |
| 25 | #define ALIGN64_END |
| 26 | #else /* gcc or icc */ |
| 27 | #define ALIGN64_BEG |
| 28 | #define ALIGN64_END __attribute__((aligned(64))) |
| 29 | #endif |
| 30 | |
| 31 | /* declare some AVX constants -- why can't I figure a better way to do that? */ |
| 32 | #define _PS512_CONST(Name, Val) \ |
| 33 | static const ALIGN64_BEG float _ps512_##Name[16] ALIGN64_END = {Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val} |
| 34 | #define _PI32_CONST512(Name, Val) \ |
| 35 | static const ALIGN64_BEG int _pi32_512_##Name[16] ALIGN64_END = {Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val} |
| 36 | #define _PS512_CONST_TYPE(Name, Type, Val) \ |
| 37 | static const ALIGN64_BEG Type _ps512_##Name[16] ALIGN64_END = {Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val, Val} |
| 38 | |
| 39 | _PS512_CONST(1, 1.0f); |
| 40 | _PS512_CONST(0p5, 0.5f); |
| 41 | /* the smallest non denormalized float number */ |
| 42 | _PS512_CONST_TYPE(min_norm_pos, int, 0x00800000); |
| 43 | _PS512_CONST_TYPE(mant_mask, int, 0x7f800000); |
| 44 | _PS512_CONST_TYPE(inv_mant_mask, int, ~0x7f800000); |
| 45 | |
| 46 | _PS512_CONST_TYPE(sign_mask, int, (int)0x80000000); |
| 47 | |
| 48 | _PI32_CONST512(0, 0); |
| 49 | _PI32_CONST512(1, 1); |
| 50 | _PI32_CONST512(inv1, ~1); |
| 51 | _PI32_CONST512(2, 2); |
| 52 | _PI32_CONST512(4, 4); |
| 53 | _PI32_CONST512(0x7f, 0x7f); |
| 54 | |
| 55 | _PS512_CONST(cephes_SQRTHF, 0.707106781186547524f); |
| 56 | _PS512_CONST(cephes_log_p0, 7.0376836292E-2f); |
| 57 | _PS512_CONST(cephes_log_p1, -1.1514610310E-1f); |
| 58 | _PS512_CONST(cephes_log_p2, 1.1676998740E-1f); |
| 59 | _PS512_CONST(cephes_log_p3, -1.2420140846E-1f); |
| 60 | _PS512_CONST(cephes_log_p4, +1.4249322787E-1f); |
| 61 | _PS512_CONST(cephes_log_p5, -1.6668057665E-1f); |
| 62 | _PS512_CONST(cephes_log_p6, +2.0000714765E-1f); |
| 63 | _PS512_CONST(cephes_log_p7, -2.4999993993E-1f); |
| 64 | _PS512_CONST(cephes_log_p8, +3.3333331174E-1f); |
| 65 | _PS512_CONST(cephes_log_q1, -2.12194440e-4f); |
| 66 | _PS512_CONST(cephes_log_q2, 0.693359375f); |
| 67 | |
| 68 | /* natural logarithm computed for 8 simultaneous float |
| 69 | return NaN for x <= 0 |
| 70 | */ |
| 71 | static NCNN_FORCEINLINE __m512 log512_ps(__m512 x) |
| 72 | { |
| 73 | __m512i imm0; |
| 74 | __m512 one = *(__m512*)_ps512_1; |
| 75 | |
| 76 | __mmask16 invalid_mask = _mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_LE_OQ); |
| 77 | |
| 78 | x = _mm512_max_ps(x, *(__m512*)_ps512_min_norm_pos); /* cut off denormalized stuff */ |
| 79 | |
| 80 | imm0 = _mm512_srli_epi32(_mm512_castps_si512(x), 23); |
| 81 | |
| 82 | /* keep only the fractional part */ |
| 83 | x = _mm512_and_ps(x, *(__m512*)_ps512_inv_mant_mask); |
| 84 | x = _mm512_or_ps(x, *(__m512*)_ps512_0p5); |
| 85 | |
| 86 | imm0 = _mm512_sub_epi32(imm0, *(__m512i*)_pi32_512_0x7f); |
| 87 | __m512 e = _mm512_cvtepi32_ps(imm0); |
| 88 | |
| 89 | e = _mm512_add_ps(e, one); |
| 90 | |
| 91 | /* part2: |
| 92 | if( x < SQRTHF ) { |
| 93 | e -= 1; |
| 94 | x = x + x - 1.0; |
| 95 | } else { x = x - 1.0; } |
| 96 | */ |
| 97 | __mmask16 mask = _mm512_cmp_ps_mask(x, *(__m512*)_ps512_cephes_SQRTHF, _CMP_LT_OQ); |
| 98 | __m512 tmp = _mm512_sub_ps(x, one); |
| 99 | e = _mm512_mask_sub_ps(e, mask, e, one); |
| 100 | x = _mm512_mask_add_ps(tmp, mask, tmp, x); |
| 101 | |
| 102 | __m512 z = _mm512_mul_ps(x, x); |
| 103 | |
| 104 | __m512 y = *(__m512*)_ps512_cephes_log_p0; |
| 105 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p1); |
| 106 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p2); |
| 107 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p3); |
| 108 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p4); |
| 109 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p5); |
| 110 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p6); |
| 111 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p7); |
| 112 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_log_p8); |
| 113 | y = _mm512_mul_ps(y, x); |
| 114 | |
| 115 | y = _mm512_mul_ps(y, z); |
| 116 | |
| 117 | y = _mm512_fmadd_ps(e, *(__m512*)_ps512_cephes_log_q1, y); |
| 118 | |
| 119 | //y = -z * 0.5 + y |
| 120 | y = _mm512_fnmadd_ps(z, *(__m512*)_ps512_0p5, y); |
| 121 | |
| 122 | x = _mm512_add_ps(x, y); |
| 123 | x = _mm512_fmadd_ps(e, *(__m512*)_ps512_cephes_log_q2, x); |
| 124 | y = _mm512_or_ps(x, _mm512_castsi512_ps(_mm512_movm_epi32(invalid_mask))); // negative arg will be NAN |
| 125 | return y; |
| 126 | } |
| 127 | |
| 128 | _PS512_CONST(exp_hi, 88.3762626647949f); |
| 129 | _PS512_CONST(exp_lo, -88.3762626647949f); |
| 130 | |
| 131 | _PS512_CONST(cephes_LOG2EF, 1.44269504088896341f); |
| 132 | _PS512_CONST(cephes_exp_C1, 0.693359375f); |
| 133 | _PS512_CONST(cephes_exp_C2, -2.12194440e-4f); |
| 134 | |
| 135 | _PS512_CONST(cephes_exp_p0, 1.9875691500E-4f); |
| 136 | _PS512_CONST(cephes_exp_p1, 1.3981999507E-3f); |
| 137 | _PS512_CONST(cephes_exp_p2, 8.3334519073E-3f); |
| 138 | _PS512_CONST(cephes_exp_p3, 4.1665795894E-2f); |
| 139 | _PS512_CONST(cephes_exp_p4, 1.6666665459E-1f); |
| 140 | _PS512_CONST(cephes_exp_p5, 5.0000001201E-1f); |
| 141 | |
| 142 | static NCNN_FORCEINLINE __m512 exp512_ps(__m512 x) |
| 143 | { |
| 144 | __m512 tmp = _mm512_setzero_ps(), fx; |
| 145 | __m512i imm0; |
| 146 | __m512 one = *(__m512*)_ps512_1; |
| 147 | |
| 148 | x = _mm512_min_ps(x, *(__m512*)_ps512_exp_hi); |
| 149 | x = _mm512_max_ps(x, *(__m512*)_ps512_exp_lo); |
| 150 | |
| 151 | /* express exp(x) as exp(g + n*log(2)) */ |
| 152 | fx = _mm512_fmadd_ps(x, *(__m512*)_ps512_cephes_LOG2EF, *(__m512*)_ps512_0p5); |
| 153 | |
| 154 | tmp = _mm512_roundscale_ps(fx, _MM_FROUND_TO_NEG_INF); |
| 155 | |
| 156 | /* if greater, subtract 1 */ |
| 157 | __mmask16 mask = _mm512_cmp_ps_mask(tmp, fx, _CMP_GT_OQ); |
| 158 | fx = _mm512_mask_sub_ps(tmp, mask, tmp, one); |
| 159 | |
| 160 | // x = x - fx * exp_C1 |
| 161 | x = _mm512_fnmadd_ps(fx, *(__m512*)_ps512_cephes_exp_C1, x); |
| 162 | // x = x - fx * exp_C2 |
| 163 | x = _mm512_fnmadd_ps(fx, *(__m512*)_ps512_cephes_exp_C2, x); |
| 164 | |
| 165 | tmp = _mm512_mul_ps(x, x); |
| 166 | |
| 167 | __m512 y = *(__m512*)_ps512_cephes_exp_p0; |
| 168 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_exp_p1); |
| 169 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_exp_p2); |
| 170 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_exp_p3); |
| 171 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_exp_p4); |
| 172 | y = _mm512_fmadd_ps(y, x, *(__m512*)_ps512_cephes_exp_p5); |
| 173 | y = _mm512_fmadd_ps(y, tmp, x); |
| 174 | y = _mm512_add_ps(y, one); |
| 175 | |
| 176 | /* build 2^n */ |
| 177 | imm0 = _mm512_cvttps_epi32(fx); |
| 178 | imm0 = _mm512_add_epi32(imm0, *(__m512i*)_pi32_512_0x7f); |
| 179 | imm0 = _mm512_slli_epi32(imm0, 23); |
| 180 | __m512 pow2n = _mm512_castsi512_ps(imm0); |
| 181 | y = _mm512_mul_ps(y, pow2n); |
| 182 | return y; |
| 183 | } |
| 184 | |
| 185 | _PS512_CONST(tanh_hi, 9.0f); |
| 186 | _PS512_CONST(tanh_lo, -9.0f); |
| 187 | |
| 188 | _PS512_CONST(cephes_tanh_p0, -2.76076847742355E-16f); |
| 189 | _PS512_CONST(cephes_tanh_p1, 2.00018790482477E-13f); |
| 190 | _PS512_CONST(cephes_tanh_p2, -8.60467152213735E-11f); |
| 191 | _PS512_CONST(cephes_tanh_p3, 5.12229709037114E-08f); |
| 192 | _PS512_CONST(cephes_tanh_p4, 1.48572235717979E-05f); |
| 193 | _PS512_CONST(cephes_tanh_p5, 6.37261928875436E-04f); |
| 194 | _PS512_CONST(cephes_tanh_p6, 4.89352455891786E-03f); |
| 195 | |
| 196 | _PS512_CONST(cephes_tanh_p7, 1.19825839466702e-06f); |
| 197 | _PS512_CONST(cephes_tanh_p8, 1.18534705686654e-04f); |
| 198 | _PS512_CONST(cephes_tanh_p9, 2.26843463243900e-03f); |
| 199 | |
| 200 | // an approximation of tanh |
| 201 | static inline __m512 tanh512_ps(const __m512 x) |
| 202 | { |
| 203 | __m512 value = x; |
| 204 | value = _mm512_max_ps(*(__m512*)_ps512_tanh_lo, value); |
| 205 | value = _mm512_min_ps(*(__m512*)_ps512_tanh_hi, value); |
| 206 | |
| 207 | __m512 value_squared = _mm512_mul_ps(value, value); |
| 208 | |
| 209 | __m512 p; |
| 210 | p = _mm512_fmadd_ps(value_squared, *(__m512*)_ps512_cephes_tanh_p0, *(__m512*)_ps512_cephes_tanh_p1); |
| 211 | p = _mm512_fmadd_ps(p, value_squared, *(__m512*)_ps512_cephes_tanh_p2); |
| 212 | p = _mm512_fmadd_ps(p, value_squared, *(__m512*)_ps512_cephes_tanh_p3); |
| 213 | p = _mm512_fmadd_ps(p, value_squared, *(__m512*)_ps512_cephes_tanh_p4); |
| 214 | p = _mm512_fmadd_ps(p, value_squared, *(__m512*)_ps512_cephes_tanh_p5); |
| 215 | p = _mm512_fmadd_ps(p, value_squared, *(__m512*)_ps512_cephes_tanh_p6); |
| 216 | p = _mm512_mul_ps(p, value); |
| 217 | |
| 218 | __m512 q; |
| 219 | q = _mm512_fmadd_ps(value_squared, *(__m512*)_ps512_cephes_tanh_p7, *(__m512*)_ps512_cephes_tanh_p8); |
| 220 | q = _mm512_fmadd_ps(q, value_squared, *(__m512*)_ps512_cephes_tanh_p9); |
| 221 | q = _mm512_fmadd_ps(q, value_squared, *(__m512*)_ps512_cephes_tanh_p6); |
| 222 | |
| 223 | __m512 dst = _mm512_div_ps(p, q); |
| 224 | return dst; |
| 225 | } |
| 226 | |
| 227 | _PS512_CONST(minus_cephes_DP1, -0.78515625f); |
| 228 | _PS512_CONST(minus_cephes_DP2, -2.4187564849853515625e-4f); |
| 229 | _PS512_CONST(minus_cephes_DP3, -3.77489497744594108e-8f); |
| 230 | _PS512_CONST(sincof_p0, -1.9515295891E-4f); |
| 231 | _PS512_CONST(sincof_p1, 8.3321608736E-3f); |
| 232 | _PS512_CONST(sincof_p2, -1.6666654611E-1f); |
| 233 | _PS512_CONST(coscof_p0, 2.443315711809948E-005f); |
| 234 | _PS512_CONST(coscof_p1, -1.388731625493765E-003f); |
| 235 | _PS512_CONST(coscof_p2, 4.166664568298827E-002f); |
| 236 | _PS512_CONST(cephes_FOPI, 1.27323954473516f); // 4 / M_PI |
| 237 | |
| 238 | /* evaluation of 8 sines at onces using AVX intrisics |
| 239 | |
| 240 | The code is the exact rewriting of the cephes sinf function. |
| 241 | Precision is excellent as long as x < 8192 (I did not bother to |
| 242 | take into account the special handling they have for greater values |
| 243 | -- it does not return garbage for arguments over 8192, though, but |
| 244 | the extra precision is missing). |
| 245 | |
| 246 | Note that it is such that sinf((float)M_PI) = 8.74e-8, which is the |
| 247 | surprising but correct result. |
| 248 | |
| 249 | */ |
| 250 | static NCNN_FORCEINLINE __m512 sin512_ps(__m512 x) |
| 251 | { // any x |
| 252 | __m512 xmm1, xmm2, xmm3, sign_bit, y; |
| 253 | __m512i imm0, imm2; |
| 254 | |
| 255 | sign_bit = x; |
| 256 | /* take the absolute value */ |
| 257 | x = _mm512_castsi512_ps(_mm512_and_epi32(_mm512_castps_si512(x), _mm512_set1_epi32(0x7fffffff))); |
| 258 | /* extract the sign bit (upper one) */ |
| 259 | sign_bit = _mm512_and_ps(sign_bit, *(__m512*)_ps512_sign_mask); |
| 260 | |
| 261 | /* scale by 4/Pi */ |
| 262 | y = _mm512_mul_ps(x, *(__m512*)_ps512_cephes_FOPI); |
| 263 | |
| 264 | /* store the integer part of y in mm0 */ |
| 265 | imm2 = _mm512_cvttps_epi32(y); |
| 266 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 267 | imm2 = _mm512_add_epi32(imm2, *(__m512i*)_pi32_512_1); |
| 268 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_inv1); |
| 269 | y = _mm512_cvtepi32_ps(imm2); |
| 270 | |
| 271 | /* get the swap sign flag */ |
| 272 | imm0 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_4); |
| 273 | imm0 = _mm512_slli_epi32(imm0, 29); |
| 274 | /* get the polynom selection mask |
| 275 | there is one polynom for 0 <= x <= Pi/4 |
| 276 | and another one for Pi/4<x<=Pi/2 |
| 277 | |
| 278 | Both branches will be computed. |
| 279 | */ |
| 280 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_2); |
| 281 | imm2 = _mm512_movm_epi32(_mm512_cmpeq_epi32_mask(imm2, *(__m512i*)_pi32_512_0)); |
| 282 | |
| 283 | __m512 swap_sign_bit = _mm512_castsi512_ps(imm0); |
| 284 | __m512 poly_mask = _mm512_castsi512_ps(imm2); |
| 285 | sign_bit = _mm512_xor_ps(sign_bit, swap_sign_bit); |
| 286 | |
| 287 | /* The magic pass: "Extended precision modular arithmetic" |
| 288 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 289 | xmm1 = *(__m512*)_ps512_minus_cephes_DP1; |
| 290 | xmm2 = *(__m512*)_ps512_minus_cephes_DP2; |
| 291 | xmm3 = *(__m512*)_ps512_minus_cephes_DP3; |
| 292 | x = _mm512_fmadd_ps(y, xmm1, x); |
| 293 | x = _mm512_fmadd_ps(y, xmm2, x); |
| 294 | x = _mm512_fmadd_ps(y, xmm3, x); |
| 295 | |
| 296 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 297 | y = *(__m512*)_ps512_coscof_p0; |
| 298 | __m512 z = _mm512_mul_ps(x, x); |
| 299 | |
| 300 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p1); |
| 301 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p2); |
| 302 | y = _mm512_mul_ps(y, z); |
| 303 | y = _mm512_mul_ps(y, z); |
| 304 | // y = y - z * 0.5 |
| 305 | y = _mm512_fnmadd_ps(z, *(__m512*)_ps512_0p5, y); |
| 306 | y = _mm512_add_ps(y, *(__m512*)_ps512_1); |
| 307 | |
| 308 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 309 | |
| 310 | __m512 y2 = *(__m512*)_ps512_sincof_p0; |
| 311 | y2 = _mm512_fmadd_ps(y2, z, *(__m512*)_ps512_sincof_p1); |
| 312 | y2 = _mm512_fmadd_ps(y2, z, *(__m512*)_ps512_sincof_p2); |
| 313 | y2 = _mm512_mul_ps(y2, z); |
| 314 | y2 = _mm512_fmadd_ps(y2, x, x); |
| 315 | |
| 316 | /* select the correct result from the two polynoms */ |
| 317 | xmm3 = poly_mask; |
| 318 | y2 = _mm512_and_ps(xmm3, y2); //, xmm3); |
| 319 | y = _mm512_andnot_ps(xmm3, y); |
| 320 | y = _mm512_add_ps(y, y2); |
| 321 | /* update the sign */ |
| 322 | y = _mm512_xor_ps(y, sign_bit); |
| 323 | |
| 324 | return y; |
| 325 | } |
| 326 | |
| 327 | /* almost the same as sin_ps */ |
| 328 | static NCNN_FORCEINLINE __m512 cos512_ps(__m512 x) |
| 329 | { // any x |
| 330 | __m512 xmm1, xmm2, xmm3, y; |
| 331 | __m512i imm0, imm2; |
| 332 | |
| 333 | /* take the absolute value */ |
| 334 | x = _mm512_castsi512_ps(_mm512_and_epi32(_mm512_castps_si512(x), _mm512_set1_epi32(0x7fffffff))); |
| 335 | |
| 336 | /* scale by 4/Pi */ |
| 337 | y = _mm512_mul_ps(x, *(__m512*)_ps512_cephes_FOPI); |
| 338 | |
| 339 | /* store the integer part of y in mm0 */ |
| 340 | imm2 = _mm512_cvttps_epi32(y); |
| 341 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 342 | imm2 = _mm512_add_epi32(imm2, *(__m512i*)_pi32_512_1); |
| 343 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_inv1); |
| 344 | y = _mm512_cvtepi32_ps(imm2); |
| 345 | imm2 = _mm512_sub_epi32(imm2, *(__m512i*)_pi32_512_2); |
| 346 | |
| 347 | /* get the swap sign flag */ |
| 348 | imm0 = _mm512_andnot_si512(imm2, *(__m512i*)_pi32_512_4); |
| 349 | imm0 = _mm512_slli_epi32(imm0, 29); |
| 350 | /* get the polynom selection mask */ |
| 351 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_2); |
| 352 | imm2 = _mm512_movm_epi32(_mm512_cmpeq_epi32_mask(imm2, *(__m512i*)_pi32_512_0)); |
| 353 | |
| 354 | __m512 sign_bit = _mm512_castsi512_ps(imm0); |
| 355 | __m512 poly_mask = _mm512_castsi512_ps(imm2); |
| 356 | |
| 357 | /* The magic pass: "Extended precision modular arithmetic" |
| 358 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 359 | xmm1 = *(__m512*)_ps512_minus_cephes_DP1; |
| 360 | xmm2 = *(__m512*)_ps512_minus_cephes_DP2; |
| 361 | xmm3 = *(__m512*)_ps512_minus_cephes_DP3; |
| 362 | x = _mm512_fmadd_ps(y, xmm1, x); |
| 363 | x = _mm512_fmadd_ps(y, xmm2, x); |
| 364 | x = _mm512_fmadd_ps(y, xmm3, x); |
| 365 | |
| 366 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 367 | y = *(__m512*)_ps512_coscof_p0; |
| 368 | __m512 z = _mm512_mul_ps(x, x); |
| 369 | |
| 370 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p1); |
| 371 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p2); |
| 372 | y = _mm512_mul_ps(y, z); |
| 373 | y = _mm512_mul_ps(y, z); |
| 374 | // y = y - z * 0.5 |
| 375 | y = _mm512_fnmadd_ps(z, *(__m512*)_ps512_0p5, y); |
| 376 | y = _mm512_add_ps(y, *(__m512*)_ps512_1); |
| 377 | |
| 378 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 379 | |
| 380 | __m512 y2 = *(__m512*)_ps512_sincof_p0; |
| 381 | y2 = _mm512_mul_ps(y2, z); |
| 382 | y2 = _mm512_add_ps(y2, *(__m512*)_ps512_sincof_p1); |
| 383 | y2 = _mm512_mul_ps(y2, z); |
| 384 | y2 = _mm512_add_ps(y2, *(__m512*)_ps512_sincof_p2); |
| 385 | y2 = _mm512_mul_ps(y2, z); |
| 386 | y2 = _mm512_mul_ps(y2, x); |
| 387 | y2 = _mm512_add_ps(y2, x); |
| 388 | |
| 389 | /* select the correct result from the two polynoms */ |
| 390 | xmm3 = poly_mask; |
| 391 | y2 = _mm512_and_ps(xmm3, y2); //, xmm3); |
| 392 | y = _mm512_andnot_ps(xmm3, y); |
| 393 | y = _mm512_add_ps(y, y2); |
| 394 | /* update the sign */ |
| 395 | y = _mm512_xor_ps(y, sign_bit); |
| 396 | |
| 397 | return y; |
| 398 | } |
| 399 | |
| 400 | /* since sin512_ps and cos512_ps are almost identical, sincos512_ps could replace both of them.. |
| 401 | it is almost as fast, and gives you a free cosine with your sine */ |
| 402 | static NCNN_FORCEINLINE void sincos512_ps(__m512 x, __m512* s, __m512* c) |
| 403 | { |
| 404 | __m512 xmm1, xmm2, xmm3, sign_bit_sin, y; |
| 405 | __m512i imm0, imm2, imm4; |
| 406 | |
| 407 | sign_bit_sin = x; |
| 408 | /* take the absolute value */ |
| 409 | x = _mm512_castsi512_ps(_mm512_and_epi32(_mm512_castps_si512(x), _mm512_set1_epi32(0x7fffffff))); |
| 410 | /* extract the sign bit (upper one) */ |
| 411 | sign_bit_sin = _mm512_and_ps(sign_bit_sin, *(__m512*)_ps512_sign_mask); |
| 412 | |
| 413 | /* scale by 4/Pi */ |
| 414 | y = _mm512_mul_ps(x, *(__m512*)_ps512_cephes_FOPI); |
| 415 | |
| 416 | /* store the integer part of y in imm2 */ |
| 417 | imm2 = _mm512_cvttps_epi32(y); |
| 418 | |
| 419 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 420 | imm2 = _mm512_add_epi32(imm2, *(__m512i*)_pi32_512_1); |
| 421 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_inv1); |
| 422 | |
| 423 | y = _mm512_cvtepi32_ps(imm2); |
| 424 | imm4 = imm2; |
| 425 | |
| 426 | /* get the swap sign flag for the sine */ |
| 427 | imm0 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_4); |
| 428 | imm0 = _mm512_slli_epi32(imm0, 29); |
| 429 | //__m512 swap_sign_bit_sin = _mm512_castsi512_ps(imm0); |
| 430 | |
| 431 | /* get the polynom selection mask for the sine*/ |
| 432 | imm2 = _mm512_and_si512(imm2, *(__m512i*)_pi32_512_2); |
| 433 | imm2 = _mm512_movm_epi32(_mm512_cmpeq_epi32_mask(imm2, *(__m512i*)_pi32_512_0)); |
| 434 | //__m512 poly_mask = _mm512_castsi512_ps(imm2); |
| 435 | |
| 436 | __m512 swap_sign_bit_sin = _mm512_castsi512_ps(imm0); |
| 437 | __m512 poly_mask = _mm512_castsi512_ps(imm2); |
| 438 | |
| 439 | /* The magic pass: "Extended precision modular arithmetic" |
| 440 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 441 | xmm1 = *(__m512*)_ps512_minus_cephes_DP1; |
| 442 | xmm2 = *(__m512*)_ps512_minus_cephes_DP2; |
| 443 | xmm3 = *(__m512*)_ps512_minus_cephes_DP3; |
| 444 | x = _mm512_fmadd_ps(y, xmm1, x); |
| 445 | x = _mm512_fmadd_ps(y, xmm2, x); |
| 446 | x = _mm512_fmadd_ps(y, xmm3, x); |
| 447 | |
| 448 | imm4 = _mm512_sub_epi32(imm4, *(__m512i*)_pi32_512_2); |
| 449 | imm4 = _mm512_andnot_si512(imm4, *(__m512i*)_pi32_512_4); |
| 450 | imm4 = _mm512_slli_epi32(imm4, 29); |
| 451 | |
| 452 | __m512 sign_bit_cos = _mm512_castsi512_ps(imm4); |
| 453 | |
| 454 | sign_bit_sin = _mm512_xor_ps(sign_bit_sin, swap_sign_bit_sin); |
| 455 | |
| 456 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 457 | __m512 z = _mm512_mul_ps(x, x); |
| 458 | y = *(__m512*)_ps512_coscof_p0; |
| 459 | |
| 460 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p1); |
| 461 | y = _mm512_fmadd_ps(y, z, *(__m512*)_ps512_coscof_p2); |
| 462 | y = _mm512_mul_ps(y, z); |
| 463 | y = _mm512_mul_ps(y, z); |
| 464 | // y = y - z * 0.5 |
| 465 | y = _mm512_fnmadd_ps(z, *(__m512*)_ps512_0p5, y); |
| 466 | y = _mm512_add_ps(y, *(__m512*)_ps512_1); |
| 467 | |
| 468 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 469 | |
| 470 | __m512 y2 = *(__m512*)_ps512_sincof_p0; |
| 471 | y2 = _mm512_fmadd_ps(y2, z, *(__m512*)_ps512_sincof_p1); |
| 472 | y2 = _mm512_fmadd_ps(y2, z, *(__m512*)_ps512_sincof_p2); |
| 473 | y2 = _mm512_mul_ps(y2, z); |
| 474 | y2 = _mm512_fmadd_ps(y2, x, x); |
| 475 | |
| 476 | /* select the correct result from the two polynoms */ |
| 477 | xmm3 = poly_mask; |
| 478 | __m512 ysin2 = _mm512_and_ps(xmm3, y2); |
| 479 | __m512 ysin1 = _mm512_andnot_ps(xmm3, y); |
| 480 | y2 = _mm512_sub_ps(y2, ysin2); |
| 481 | y = _mm512_sub_ps(y, ysin1); |
| 482 | |
| 483 | xmm1 = _mm512_add_ps(ysin1, ysin2); |
| 484 | xmm2 = _mm512_add_ps(y, y2); |
| 485 | |
| 486 | /* update the sign */ |
| 487 | *s = _mm512_xor_ps(xmm1, sign_bit_sin); |
| 488 | *c = _mm512_xor_ps(xmm2, sign_bit_cos); |
| 489 | } |
| 490 | |
| 491 | static NCNN_FORCEINLINE __m512 tan512_ps(__m512 x) |
| 492 | { |
| 493 | __m512 ysin, ycos; |
| 494 | __m512 eps = _mm512_set1_ps(1E-8f); |
| 495 | sincos512_ps(x, &ysin, &ycos); |
| 496 | __mmask16 mask = _mm512_cmp_ps_mask(ycos, _mm512_setzero_ps(), _CMP_EQ_OS); |
| 497 | ycos = _mm512_mask_add_ps(ycos, mask, ycos, eps); |
| 498 | __m512 ytan = _mm512_div_ps(ysin, ycos); |
| 499 | return ytan; |
| 500 | } |
| 501 | |
| 502 | static NCNN_FORCEINLINE __m512 pow512_ps(__m512 a, __m512 b) |
| 503 | { |
| 504 | // pow(x, m) = exp(m * log(x)) |
| 505 | return exp512_ps(_mm512_mul_ps(b, log512_ps(a))); |
| 506 | } |
| 507 | |
| 508 | static NCNN_FORCEINLINE __m512 asin512_ps(__m512 x) |
| 509 | { |
| 510 | const __m512 magic_negative_zero = _mm512_set1_ps(-0.0f); |
| 511 | const __m512 magic_half_one = _mm512_set1_ps(0.5f); |
| 512 | const __m512 magic_one = _mm512_set1_ps(1.0f); |
| 513 | const __m512 magic_a4 = _mm512_set1_ps(0.023994016f); |
| 514 | const __m512 magic_a5 = _mm512_set1_ps(0.042417344f); |
| 515 | const __m512 magic_a2 = _mm512_set1_ps(0.07494697f); |
| 516 | const __m512 magic_a3 = _mm512_set1_ps(0.045520633f); |
| 517 | const __m512 magic_a0 = _mm512_set1_ps(1.0f); |
| 518 | const __m512 magic_a1 = _mm512_set1_ps(0.166667819f); |
| 519 | const __m512 magic_half_pi = _mm512_set1_ps(1.5707964f); |
| 520 | const __m512 magic_three = _mm512_set1_ps(3.0f); |
| 521 | |
| 522 | // negative_mask = magic_negative_zero && x; |
| 523 | __m512 negative_mask = _mm512_and_ps(magic_negative_zero, x); |
| 524 | |
| 525 | // absolute = abs(x); |
| 526 | __m512 absolute = _mm512_andnot_ps(magic_negative_zero, x); |
| 527 | |
| 528 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 529 | |
| 530 | // is_small_input = (absolute <= 0.5f); |
| 531 | __mmask16 is_small_input = _mm512_cmp_ps_mask( |
| 532 | absolute, |
| 533 | magic_half_one, |
| 534 | _CMP_LE_OQ); |
| 535 | |
| 536 | // is_big_input = (is_small_input ? 0.0f : 1.0f); |
| 537 | __m512 is_big_input = _mm512_maskz_mov_ps(~is_small_input, magic_one); |
| 538 | |
| 539 | // big_input_approx = sqrt(0.5f * (1 - absolute)); |
| 540 | __m512 big_input_approx = _mm512_sqrt_ps(_mm512_mul_ps( |
| 541 | magic_half_one, |
| 542 | _mm512_sub_ps(magic_one, absolute))); |
| 543 | |
| 544 | // input_approx = (is_small_input ? absolute : big_input_approx); |
| 545 | __m512 input_approx = _mm512_mask_mov_ps( |
| 546 | big_input_approx, |
| 547 | is_small_input, |
| 548 | absolute); |
| 549 | |
| 550 | // square_of_input_approx = input_approx * input_approx; |
| 551 | __m512 square_of_input_approx = _mm512_mul_ps(input_approx, input_approx); |
| 552 | |
| 553 | // fourth_power_of_input_approx = |
| 554 | // square_of_input_approx * square_of_input_approx; |
| 555 | __m512 fourth_power_of_input_approx = _mm512_mul_ps( |
| 556 | square_of_input_approx, square_of_input_approx); |
| 557 | |
| 558 | // TODO: Need more explanations. |
| 559 | // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2); |
| 560 | // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3); |
| 561 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a0); |
| 562 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a1); |
| 563 | // output_approx = ((square_of_input_approx * x4) + x3); |
| 564 | __m512 output_approx = _mm512_fmadd_ps( |
| 565 | square_of_input_approx, |
| 566 | _mm512_fmadd_ps( |
| 567 | fourth_power_of_input_approx, |
| 568 | _mm512_fmadd_ps( |
| 569 | fourth_power_of_input_approx, |
| 570 | magic_a5, |
| 571 | magic_a3), |
| 572 | magic_a1), |
| 573 | _mm512_fmadd_ps( |
| 574 | fourth_power_of_input_approx, |
| 575 | _mm512_fmadd_ps( |
| 576 | fourth_power_of_input_approx, |
| 577 | magic_a4, |
| 578 | magic_a2), |
| 579 | magic_a0)); |
| 580 | |
| 581 | // TODO: Need more explanations. |
| 582 | // x1 = ((0.5 * PI) * is_big_input); |
| 583 | // x2 = (output_approx * input_approx); |
| 584 | // x3 = (-(3.0f * is_big_input) + 1.0f); |
| 585 | // final_approx = ((x2 * x3) + x1); |
| 586 | __m512 final_approx = _mm512_fmadd_ps( |
| 587 | _mm512_mul_ps(output_approx, input_approx), |
| 588 | _mm512_fnmadd_ps(magic_three, is_big_input, magic_one), |
| 589 | _mm512_mul_ps(magic_half_pi, is_big_input)); |
| 590 | |
| 591 | // return (final_approx || negative_mask); |
| 592 | return _mm512_or_ps(final_approx, negative_mask); |
| 593 | } |
| 594 | |
| 595 | static NCNN_FORCEINLINE __m512 acos512_ps(__m512 x) |
| 596 | { |
| 597 | const __m512 magic_negative_zero = _mm512_set1_ps(-0.0f); |
| 598 | const __m512 magic_zero = _mm512_set1_ps(0.0f); |
| 599 | const __m512 magic_half_one = _mm512_set1_ps(0.5f); |
| 600 | const __m512 magic_one = _mm512_set1_ps(1.0f); |
| 601 | const __m512 magic_a4 = _mm512_set1_ps(0.023994016f); |
| 602 | const __m512 magic_a5 = _mm512_set1_ps(0.042417344f); |
| 603 | const __m512 magic_a2 = _mm512_set1_ps(0.07494697f); |
| 604 | const __m512 magic_a3 = _mm512_set1_ps(0.045520633f); |
| 605 | const __m512 magic_a0 = _mm512_set1_ps(1.0f); |
| 606 | const __m512 magic_a1 = _mm512_set1_ps(0.166667819f); |
| 607 | const __m512 magic_half_pi = _mm512_set1_ps(1.5707964f); |
| 608 | const __m512 magic_pi = _mm512_set1_ps(3.1415927f); |
| 609 | |
| 610 | // negative_mask = magic_negative_zero && x; |
| 611 | __m512 negative_mask = _mm512_and_ps(magic_negative_zero, x); |
| 612 | |
| 613 | // absolute = abs(x); |
| 614 | __m512 absolute = _mm512_andnot_ps(magic_negative_zero, x); |
| 615 | |
| 616 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 617 | |
| 618 | // is_small_input = (absolute <= 0.5f); |
| 619 | __mmask16 is_small_input = _mm512_cmp_ps_mask( |
| 620 | absolute, |
| 621 | magic_half_one, |
| 622 | _CMP_LE_OQ); |
| 623 | |
| 624 | // big_input_approx = sqrt(0.5f * (1 - absolute)); |
| 625 | __m512 big_input_approx = _mm512_sqrt_ps(_mm512_mul_ps( |
| 626 | magic_half_one, |
| 627 | _mm512_sub_ps(magic_one, absolute))); |
| 628 | |
| 629 | // input_approx = (is_small_input ? absolute : big_input_approx); |
| 630 | __m512 input_approx = _mm512_mask_mov_ps( |
| 631 | big_input_approx, |
| 632 | is_small_input, |
| 633 | absolute); |
| 634 | |
| 635 | // square_of_input_approx = input_approx * input_approx; |
| 636 | __m512 square_of_input_approx = _mm512_mul_ps(input_approx, input_approx); |
| 637 | |
| 638 | // fourth_power_of_input_approx = |
| 639 | // square_of_input_approx * square_of_input_approx; |
| 640 | __m512 fourth_power_of_input_approx = _mm512_mul_ps( |
| 641 | square_of_input_approx, square_of_input_approx); |
| 642 | |
| 643 | // TODO: Need more explanations. |
| 644 | // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2); |
| 645 | // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3); |
| 646 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a0); |
| 647 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a1); |
| 648 | // output_approx = ((square_of_input_approx * x4) + x3); |
| 649 | __m512 output_approx = _mm512_fmadd_ps( |
| 650 | square_of_input_approx, |
| 651 | _mm512_fmadd_ps( |
| 652 | fourth_power_of_input_approx, |
| 653 | _mm512_fmadd_ps( |
| 654 | fourth_power_of_input_approx, |
| 655 | magic_a5, |
| 656 | magic_a3), |
| 657 | magic_a1), |
| 658 | _mm512_fmadd_ps( |
| 659 | fourth_power_of_input_approx, |
| 660 | _mm512_fmadd_ps( |
| 661 | fourth_power_of_input_approx, |
| 662 | magic_a4, |
| 663 | magic_a2), |
| 664 | magic_a0)); |
| 665 | |
| 666 | // TODO: Need more explanations. |
| 667 | // x1 = (output_approx * input_approx); |
| 668 | __m512 x1 = _mm512_mul_ps(output_approx, input_approx); |
| 669 | |
| 670 | // TODO: Need more explanations. |
| 671 | // small_final_approx = ((0.5 * PI) - (x1 | negative_mask)); |
| 672 | __m512 small_final_approx = _mm512_sub_ps( |
| 673 | magic_half_pi, |
| 674 | _mm512_or_ps(x1, negative_mask)); |
| 675 | |
| 676 | // TODO: Need more explanations. |
| 677 | // big_final_approx = (((x < 0.0f) & PI) + ((x1 * 2) | negative_mask)); |
| 678 | __m512 big_final_approx = _mm512_add_ps( |
| 679 | _mm512_maskz_mov_ps( |
| 680 | _mm512_cmp_ps_mask(x, magic_zero, _CMP_LT_OQ), |
| 681 | magic_pi), |
| 682 | _mm512_or_ps(_mm512_add_ps(x1, x1), negative_mask)); |
| 683 | |
| 684 | // return (is_small_input ? small_final_approx : big_final_approx); |
| 685 | return _mm512_mask_mov_ps( |
| 686 | big_final_approx, |
| 687 | is_small_input, |
| 688 | small_final_approx); |
| 689 | } |
| 690 | |
| 691 | static NCNN_FORCEINLINE __m512 atan512_ps(__m512 x) |
| 692 | { |
| 693 | const __m512 magic_negative_zero = _mm512_set1_ps(-0.0f); |
| 694 | const __m512 magic_one = _mm512_set1_ps(1.0f); |
| 695 | const __m512 magic_negative_one = _mm512_set1_ps(-1.0f); |
| 696 | const __m512 magic_half_pi = _mm512_set1_ps(1.5707964f); |
| 697 | const __m512 magic_a0 = _mm512_set1_ps(1.0f); |
| 698 | const __m512 magic_a1 = _mm512_set1_ps(-0.33333072f); |
| 699 | const __m512 magic_a2 = _mm512_set1_ps(0.1999262f); |
| 700 | const __m512 magic_a3 = _mm512_set1_ps(-0.14203644f); |
| 701 | const __m512 magic_a4 = _mm512_set1_ps(0.10640934f); |
| 702 | const __m512 magic_a5 = _mm512_set1_ps(-0.07504295f); |
| 703 | const __m512 magic_a6 = _mm512_set1_ps(0.04269152f); |
| 704 | const __m512 magic_a7 = _mm512_set1_ps(-0.01606863f); |
| 705 | const __m512 magic_a8 = _mm512_set1_ps(0.0028498897f); |
| 706 | |
| 707 | // negative_mask = magic_negative_zero && x; |
| 708 | __m512 negative_mask = _mm512_and_ps(magic_negative_zero, x); |
| 709 | |
| 710 | // absolute = abs(x); |
| 711 | __m512 absolute = _mm512_andnot_ps(magic_negative_zero, x); |
| 712 | |
| 713 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 714 | |
| 715 | // is_small_input = (1.0f < absolute); |
| 716 | __mmask16 is_small_input = _mm512_cmp_ps_mask( |
| 717 | magic_one, |
| 718 | absolute, |
| 719 | _CMP_LT_OQ); |
| 720 | |
| 721 | // x1 = (is_small_input ? -1.0f : absolute); |
| 722 | // x2 = (is_small_input ? absolute : 1.0f) |
| 723 | // input_approx = x1 / x2; |
| 724 | __m512 input_approx = _mm512_div_ps( |
| 725 | _mm512_mask_mov_ps(absolute, is_small_input, magic_negative_one), |
| 726 | _mm512_mask_mov_ps(magic_one, is_small_input, absolute)); |
| 727 | |
| 728 | // square_of_input_approx = input_approx * input_approx; |
| 729 | __m512 square_of_input_approx = _mm512_mul_ps(input_approx, input_approx); |
| 730 | |
| 731 | // fourth_power_of_input_approx = |
| 732 | // square_of_input_approx * square_of_input_approx; |
| 733 | __m512 fourth_power_of_input_approx = _mm512_mul_ps( |
| 734 | square_of_input_approx, square_of_input_approx); |
| 735 | |
| 736 | // TODO: Need more explanations. |
| 737 | // x1 = ((fourth_power_of_input_approx * magic_a7) + magic_a5); |
| 738 | // x2 = ((fourth_power_of_input_approx * magic_a8) + magic_a6); |
| 739 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a3); |
| 740 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a4); |
| 741 | // x5 = ((fourth_power_of_input_approx * x3) + magic_a1); |
| 742 | // x6 = ((fourth_power_of_input_approx * x4) + magic_a2); |
| 743 | // x7 = ((fourth_power_of_input_approx * x6) + magic_a0); |
| 744 | // output_approx = ((square_of_input_approx * x5) + x7); |
| 745 | __m512 output_approx = _mm512_fmadd_ps( |
| 746 | square_of_input_approx, |
| 747 | _mm512_fmadd_ps( |
| 748 | fourth_power_of_input_approx, |
| 749 | _mm512_fmadd_ps( |
| 750 | fourth_power_of_input_approx, |
| 751 | _mm512_fmadd_ps( |
| 752 | fourth_power_of_input_approx, |
| 753 | magic_a7, |
| 754 | magic_a5), |
| 755 | magic_a3), |
| 756 | magic_a1), |
| 757 | _mm512_fmadd_ps( |
| 758 | fourth_power_of_input_approx, |
| 759 | _mm512_fmadd_ps( |
| 760 | fourth_power_of_input_approx, |
| 761 | _mm512_fmadd_ps( |
| 762 | fourth_power_of_input_approx, |
| 763 | _mm512_fmadd_ps( |
| 764 | fourth_power_of_input_approx, |
| 765 | magic_a8, |
| 766 | magic_a6), |
| 767 | magic_a4), |
| 768 | magic_a2), |
| 769 | magic_a0)); |
| 770 | |
| 771 | // TODO: Need more explanations. |
| 772 | // x1 = (output_approx * input_approx); |
| 773 | // if (is_small_input) x1 += (0.5 * PI); |
| 774 | // return (negative_mask ? -x1 : x1); |
| 775 | return _mm512_or_ps( |
| 776 | _mm512_add_ps( |
| 777 | _mm512_mul_ps(output_approx, input_approx), |
| 778 | _mm512_maskz_mov_ps(is_small_input, magic_half_pi)), |
| 779 | negative_mask); |
| 780 | } |
| 781 | |
| 782 | // MSVC 2017 x86 CI will be broken if use NCNN_FORCEINLINE for atan2512_ps. |
| 783 | // This function still be inlined compiled by MSVC 2017 even without that. |
| 784 | #if _MSC_VER < 1920 |
| 785 | static __m512 atan2512_ps(__m512 y, __m512 x) |
| 786 | #else |
| 787 | static NCNN_FORCEINLINE __m512 atan2512_ps(__m512 y, __m512 x) |
| 788 | #endif |
| 789 | { |
| 790 | // Reference: https://mazzo.li/posts/vectorized-atan2.html |
| 791 | |
| 792 | const __m512 magic_zero = _mm512_set1_ps(0.0f); |
| 793 | const __m512 magic_negative_zero = _mm512_set1_ps(-0.0f); |
| 794 | const __m512 magic_pi = _mm512_set1_ps(3.1415927f); |
| 795 | const __m512 magic_half_pi = _mm512_set1_ps(1.5707964f); |
| 796 | |
| 797 | // not_equal_zero_x = (x != 0.0f); |
| 798 | __mmask16 not_equal_zero_x = _mm512_cmp_ps_mask( |
| 799 | x, |
| 800 | magic_zero, |
| 801 | _CMP_NEQ_OQ); |
| 802 | |
| 803 | // not_equal_zero_y = (y != 0.0f); |
| 804 | __mmask16 not_equal_zero_y = _mm512_cmp_ps_mask( |
| 805 | y, |
| 806 | magic_zero, |
| 807 | _CMP_NEQ_OQ); |
| 808 | |
| 809 | // normal_mode = ((x != 0.0f) & (y != 0.0f)); |
| 810 | __mmask16 normal_mode = (not_equal_zero_x & not_equal_zero_y); |
| 811 | |
| 812 | // negative_mask_x = magic_negative_zero && x; |
| 813 | __m512 negative_mask_x = _mm512_and_ps(magic_negative_zero, x); |
| 814 | |
| 815 | // negative_mask_y = magic_negative_zero && y; |
| 816 | __m512 negative_mask_y = _mm512_and_ps(magic_negative_zero, y); |
| 817 | |
| 818 | // pi_additions = ((x < 0.0f) ? ((y < 0.0f) ? -PI : PI) : 0.0f); |
| 819 | __m512 pi_additions = _mm512_mask_mov_ps( |
| 820 | magic_zero, |
| 821 | _mm512_cmp_ps_mask(x, magic_zero, _CMP_LT_OQ), |
| 822 | _mm512_mask_mov_ps( |
| 823 | magic_pi, |
| 824 | _mm512_cmp_ps_mask(y, magic_zero, _CMP_LT_OQ), |
| 825 | _mm512_or_ps(magic_negative_zero, magic_pi))); |
| 826 | |
| 827 | // normal_result = (atan(y / x) + pi_additions); |
| 828 | __m512 normal_result = _mm512_add_ps( |
| 829 | atan512_ps(_mm512_div_ps(y, x)), |
| 830 | pi_additions); |
| 831 | |
| 832 | // negative_mask_full_x = ((negative_mask_x | PI) < 0.0f); |
| 833 | __mmask16 negative_mask_full_x = _mm512_cmp_ps_mask( |
| 834 | _mm512_or_ps(negative_mask_x, magic_pi), |
| 835 | magic_zero, |
| 836 | _CMP_LT_OQ); |
| 837 | |
| 838 | // x1 = (negative_mask_y ? -(0.5 * PI) : (0.5 * PI)); |
| 839 | // x2 = (negative_mask_full_x ? PI : 0.0f); |
| 840 | // special_result = ((y != 0.0f) ? x1 : x2); |
| 841 | __m512 special_result = _mm512_mask_mov_ps( |
| 842 | _mm512_mask_mov_ps(magic_zero, negative_mask_full_x, magic_pi), |
| 843 | not_equal_zero_y, |
| 844 | _mm512_or_ps(negative_mask_y, magic_half_pi)); |
| 845 | |
| 846 | // return (normal_mode ? normal_result : special_result); |
| 847 | return _mm512_mask_mov_ps(special_result, normal_mode, normal_result); |
| 848 | } |
| 849 | |
| 850 | static NCNN_FORCEINLINE __m512 abs512_ps(__m512 x) |
| 851 | { |
| 852 | const __m512 abs_mask = _mm512_castsi512_ps(_mm512_set1_epi32(0x7fffffff)); |
| 853 | return _mm512_and_ps(abs_mask, x); |
| 854 | } |
| 855 | |
| 856 | #endif // AVX512_MATHFUN_H |