| 1 | /* SIMD (SSE1+MMX or SSE2) implementation of sin, cos, exp and log |
| 2 | |
| 3 | Inspired by Intel Approximate Math library, and based on the |
| 4 | corresponding algorithms of the cephes math library |
| 5 | |
| 6 | The default is to use the SSE1 version. If you define USE_SSE2 the |
| 7 | the SSE2 intrinsics will be used in place of the MMX intrinsics. Do |
| 8 | not expect any significant performance improvement with SSE2. |
| 9 | */ |
| 10 | |
| 11 | /* Copyright (C) 2007 Julien Pommier |
| 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 SSE_MATHFUN_H |
| 33 | #define SSE_MATHFUN_H |
| 34 | |
| 35 | #define USE_SSE2 1 |
| 36 | |
| 37 | #include <xmmintrin.h> |
| 38 | #include <x86_usability.h> |
| 39 | |
| 40 | /* yes I know, the top of this file is quite ugly */ |
| 41 | |
| 42 | #ifdef _MSC_VER /* visual c++ */ |
| 43 | #define ALIGN16_BEG __declspec(align(16)) |
| 44 | #define ALIGN16_END |
| 45 | #else /* gcc or icc */ |
| 46 | #define ALIGN16_BEG |
| 47 | #define ALIGN16_END __attribute__((aligned(16))) |
| 48 | #endif |
| 49 | |
| 50 | /* __m128 is ugly to write */ |
| 51 | typedef __m128 v4sf; // vector of 4 float (sse1) |
| 52 | |
| 53 | #ifdef USE_SSE2 |
| 54 | #include <emmintrin.h> |
| 55 | typedef __m128i v4si; // vector of 4 int (sse2) |
| 56 | #else |
| 57 | typedef __m64 v2si; // vector of 2 int (mmx) |
| 58 | #endif |
| 59 | |
| 60 | /* declare some SSE constants -- why can't I figure a better way to do that? */ |
| 61 | #define _PS_CONST(Name, Val) \ |
| 62 | static const ALIGN16_BEG float _ps_##Name[4] ALIGN16_END = {Val, Val, Val, Val} |
| 63 | #define _PI32_CONST(Name, Val) \ |
| 64 | static const ALIGN16_BEG int _pi32_##Name[4] ALIGN16_END = {Val, Val, Val, Val} |
| 65 | #define _PS_CONST_TYPE(Name, Type, Val) \ |
| 66 | static const ALIGN16_BEG Type _ps_##Name[4] ALIGN16_END = {Val, Val, Val, Val} |
| 67 | |
| 68 | _PS_CONST(1, 1.0f); |
| 69 | _PS_CONST(0p5, 0.5f); |
| 70 | /* the smallest non denormalized float number */ |
| 71 | _PS_CONST_TYPE(min_norm_pos, int, 0x00800000); |
| 72 | _PS_CONST_TYPE(mant_mask, int, 0x7f800000); |
| 73 | _PS_CONST_TYPE(inv_mant_mask, int, ~0x7f800000); |
| 74 | |
| 75 | _PS_CONST_TYPE(sign_mask, int, (int)0x80000000); |
| 76 | _PS_CONST_TYPE(inv_sign_mask, int, ~0x80000000); |
| 77 | |
| 78 | _PI32_CONST(1, 1); |
| 79 | _PI32_CONST(inv1, ~1); |
| 80 | _PI32_CONST(2, 2); |
| 81 | _PI32_CONST(4, 4); |
| 82 | _PI32_CONST(0x7f, 0x7f); |
| 83 | |
| 84 | _PS_CONST(cephes_SQRTHF, 0.707106781186547524f); |
| 85 | _PS_CONST(cephes_log_p0, 7.0376836292E-2f); |
| 86 | _PS_CONST(cephes_log_p1, -1.1514610310E-1f); |
| 87 | _PS_CONST(cephes_log_p2, 1.1676998740E-1f); |
| 88 | _PS_CONST(cephes_log_p3, -1.2420140846E-1f); |
| 89 | _PS_CONST(cephes_log_p4, +1.4249322787E-1f); |
| 90 | _PS_CONST(cephes_log_p5, -1.6668057665E-1f); |
| 91 | _PS_CONST(cephes_log_p6, +2.0000714765E-1f); |
| 92 | _PS_CONST(cephes_log_p7, -2.4999993993E-1f); |
| 93 | _PS_CONST(cephes_log_p8, +3.3333331174E-1f); |
| 94 | _PS_CONST(cephes_log_q1, -2.12194440e-4f); |
| 95 | _PS_CONST(cephes_log_q2, 0.693359375f); |
| 96 | |
| 97 | #ifndef USE_SSE2 |
| 98 | typedef union xmm_mm_union |
| 99 | { |
| 100 | __m128 xmm; |
| 101 | __m64 mm[2]; |
| 102 | } xmm_mm_union; |
| 103 | |
| 104 | #define COPY_XMM_TO_MM(xmm_, mm0_, mm1_) \ |
| 105 | { \ |
| 106 | xmm_mm_union u; \ |
| 107 | u.xmm = xmm_; \ |
| 108 | mm0_ = u.mm[0]; \ |
| 109 | mm1_ = u.mm[1]; \ |
| 110 | } |
| 111 | |
| 112 | #define COPY_MM_TO_XMM(mm0_, mm1_, xmm_) \ |
| 113 | { \ |
| 114 | xmm_mm_union u; \ |
| 115 | u.mm[0] = mm0_; \ |
| 116 | u.mm[1] = mm1_; \ |
| 117 | xmm_ = u.xmm; \ |
| 118 | } |
| 119 | |
| 120 | #endif // USE_SSE2 |
| 121 | |
| 122 | /* natural logarithm computed for 4 simultaneous float |
| 123 | return NaN for x <= 0 |
| 124 | */ |
| 125 | static NCNN_FORCEINLINE v4sf log_ps(v4sf x) |
| 126 | { |
| 127 | #ifdef USE_SSE2 |
| 128 | v4si emm0; |
| 129 | #else |
| 130 | v2si mm0, mm1; |
| 131 | #endif |
| 132 | v4sf one = *(v4sf*)_ps_1; |
| 133 | |
| 134 | v4sf invalid_mask = _mm_cmple_ps(x, _mm_setzero_ps()); |
| 135 | |
| 136 | x = _mm_max_ps(x, *(v4sf*)_ps_min_norm_pos); /* cut off denormalized stuff */ |
| 137 | |
| 138 | #ifndef USE_SSE2 |
| 139 | /* part 1: x = frexpf(x, &e); */ |
| 140 | COPY_XMM_TO_MM(x, mm0, mm1); |
| 141 | mm0 = _mm_srli_pi32(mm0, 23); |
| 142 | mm1 = _mm_srli_pi32(mm1, 23); |
| 143 | #else |
| 144 | emm0 = _mm_srli_epi32(_mm_castps_si128(x), 23); |
| 145 | #endif |
| 146 | /* keep only the fractional part */ |
| 147 | x = _mm_and_ps(x, *(v4sf*)_ps_inv_mant_mask); |
| 148 | x = _mm_or_ps(x, *(v4sf*)_ps_0p5); |
| 149 | |
| 150 | #ifndef USE_SSE2 |
| 151 | /* now e=mm0:mm1 contain the really base-2 exponent */ |
| 152 | mm0 = _mm_sub_pi32(mm0, *(v2si*)_pi32_0x7f); |
| 153 | mm1 = _mm_sub_pi32(mm1, *(v2si*)_pi32_0x7f); |
| 154 | v4sf e = _mm_cvtpi32x2_ps(mm0, mm1); |
| 155 | _mm_empty(); /* bye bye mmx */ |
| 156 | #else |
| 157 | emm0 = _mm_sub_epi32(emm0, *(v4si*)_pi32_0x7f); |
| 158 | v4sf e = _mm_cvtepi32_ps(emm0); |
| 159 | #endif |
| 160 | |
| 161 | e = _mm_add_ps(e, one); |
| 162 | |
| 163 | /* part2: |
| 164 | if( x < SQRTHF ) { |
| 165 | e -= 1; |
| 166 | x = x + x - 1.0; |
| 167 | } else { x = x - 1.0; } |
| 168 | */ |
| 169 | v4sf mask = _mm_cmplt_ps(x, *(v4sf*)_ps_cephes_SQRTHF); |
| 170 | v4sf tmp = _mm_and_ps(x, mask); |
| 171 | x = _mm_sub_ps(x, one); |
| 172 | e = _mm_sub_ps(e, _mm_and_ps(one, mask)); |
| 173 | x = _mm_add_ps(x, tmp); |
| 174 | |
| 175 | v4sf z = _mm_mul_ps(x, x); |
| 176 | |
| 177 | v4sf y = *(v4sf*)_ps_cephes_log_p0; |
| 178 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p1); |
| 179 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p2); |
| 180 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p3); |
| 181 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p4); |
| 182 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p5); |
| 183 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p6); |
| 184 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p7); |
| 185 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_log_p8); |
| 186 | y = _mm_mul_ps(y, x); |
| 187 | |
| 188 | y = _mm_mul_ps(y, z); |
| 189 | |
| 190 | y = _mm_comp_fmadd_ps(e, *(v4sf*)_ps_cephes_log_q1, y); |
| 191 | |
| 192 | //y = -z * 0.5 + y |
| 193 | y = _mm_comp_fnmadd_ps(z, *(v4sf*)_ps_0p5, y); |
| 194 | |
| 195 | x = _mm_add_ps(x, y); |
| 196 | x = _mm_comp_fmadd_ps(e, *(v4sf*)_ps_cephes_log_q2, x); |
| 197 | x = _mm_or_ps(x, invalid_mask); // negative arg will be NAN |
| 198 | return x; |
| 199 | } |
| 200 | |
| 201 | _PS_CONST(exp_hi, 88.3762626647949f); |
| 202 | _PS_CONST(exp_lo, -88.3762626647949f); |
| 203 | |
| 204 | _PS_CONST(cephes_LOG2EF, 1.44269504088896341f); |
| 205 | _PS_CONST(cephes_exp_C1, 0.693359375f); |
| 206 | _PS_CONST(cephes_exp_C2, -2.12194440e-4f); |
| 207 | |
| 208 | _PS_CONST(cephes_exp_p0, 1.9875691500E-4f); |
| 209 | _PS_CONST(cephes_exp_p1, 1.3981999507E-3f); |
| 210 | _PS_CONST(cephes_exp_p2, 8.3334519073E-3f); |
| 211 | _PS_CONST(cephes_exp_p3, 4.1665795894E-2f); |
| 212 | _PS_CONST(cephes_exp_p4, 1.6666665459E-1f); |
| 213 | _PS_CONST(cephes_exp_p5, 5.0000001201E-1f); |
| 214 | |
| 215 | static NCNN_FORCEINLINE v4sf exp_ps(v4sf x) |
| 216 | { |
| 217 | v4sf tmp = _mm_setzero_ps(), fx; |
| 218 | #ifdef USE_SSE2 |
| 219 | v4si emm0; |
| 220 | #else |
| 221 | v2si mm0, mm1; |
| 222 | #endif |
| 223 | v4sf one = *(v4sf*)_ps_1; |
| 224 | |
| 225 | x = _mm_min_ps(x, *(v4sf*)_ps_exp_hi); |
| 226 | x = _mm_max_ps(x, *(v4sf*)_ps_exp_lo); |
| 227 | |
| 228 | /* express exp(x) as exp(g + n*log(2)) */ |
| 229 | fx = _mm_mul_ps(x, *(v4sf*)_ps_cephes_LOG2EF); |
| 230 | fx = _mm_add_ps(fx, *(v4sf*)_ps_0p5); |
| 231 | |
| 232 | /* how to perform a floorf with SSE: just below */ |
| 233 | #ifndef USE_SSE2 |
| 234 | /* step 1 : cast to int */ |
| 235 | tmp = _mm_movehl_ps(tmp, fx); |
| 236 | mm0 = _mm_cvttps_pi32(fx); |
| 237 | mm1 = _mm_cvttps_pi32(tmp); |
| 238 | /* step 2 : cast back to float */ |
| 239 | tmp = _mm_cvtpi32x2_ps(mm0, mm1); |
| 240 | #else |
| 241 | emm0 = _mm_cvttps_epi32(fx); |
| 242 | tmp = _mm_cvtepi32_ps(emm0); |
| 243 | #endif |
| 244 | /* if greater, substract 1 */ |
| 245 | v4sf mask = _mm_cmpgt_ps(tmp, fx); |
| 246 | mask = _mm_and_ps(mask, one); |
| 247 | fx = _mm_sub_ps(tmp, mask); |
| 248 | |
| 249 | // x = x - fx * exp_C1 |
| 250 | x = _mm_comp_fnmadd_ps(fx, *(v4sf*)_ps_cephes_exp_C1, x); |
| 251 | // x = x - fx * exp_C2 |
| 252 | x = _mm_comp_fnmadd_ps(fx, *(v4sf*)_ps_cephes_exp_C2, x); |
| 253 | |
| 254 | tmp = _mm_mul_ps(x, x); |
| 255 | |
| 256 | v4sf y = *(v4sf*)_ps_cephes_exp_p0; |
| 257 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_exp_p1); |
| 258 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_exp_p2); |
| 259 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_exp_p3); |
| 260 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_exp_p4); |
| 261 | y = _mm_comp_fmadd_ps(y, x, *(v4sf*)_ps_cephes_exp_p5); |
| 262 | y = _mm_comp_fmadd_ps(y, tmp, x); |
| 263 | |
| 264 | y = _mm_add_ps(y, one); |
| 265 | |
| 266 | /* build 2^n */ |
| 267 | #ifndef USE_SSE2 |
| 268 | z = _mm_movehl_ps(z, fx); |
| 269 | mm0 = _mm_cvttps_pi32(fx); |
| 270 | mm1 = _mm_cvttps_pi32(z); |
| 271 | mm0 = _mm_add_pi32(mm0, *(v2si*)_pi32_0x7f); |
| 272 | mm1 = _mm_add_pi32(mm1, *(v2si*)_pi32_0x7f); |
| 273 | mm0 = _mm_slli_pi32(mm0, 23); |
| 274 | mm1 = _mm_slli_pi32(mm1, 23); |
| 275 | |
| 276 | v4sf pow2n; |
| 277 | COPY_MM_TO_XMM(mm0, mm1, pow2n); |
| 278 | _mm_empty(); |
| 279 | #else |
| 280 | emm0 = _mm_cvttps_epi32(fx); |
| 281 | emm0 = _mm_add_epi32(emm0, *(v4si*)_pi32_0x7f); |
| 282 | emm0 = _mm_slli_epi32(emm0, 23); |
| 283 | v4sf pow2n = _mm_castsi128_ps(emm0); |
| 284 | #endif |
| 285 | y = _mm_mul_ps(y, pow2n); |
| 286 | return y; |
| 287 | } |
| 288 | |
| 289 | _PS_CONST(tanh_hi, 9.0f); |
| 290 | _PS_CONST(tanh_lo, -9.0f); |
| 291 | |
| 292 | _PS_CONST(cephes_tanh_p0, -2.76076847742355E-16f); |
| 293 | _PS_CONST(cephes_tanh_p1, 2.00018790482477E-13f); |
| 294 | _PS_CONST(cephes_tanh_p2, -8.60467152213735E-11f); |
| 295 | _PS_CONST(cephes_tanh_p3, 5.12229709037114E-08f); |
| 296 | _PS_CONST(cephes_tanh_p4, 1.48572235717979E-05f); |
| 297 | _PS_CONST(cephes_tanh_p5, 6.37261928875436E-04f); |
| 298 | _PS_CONST(cephes_tanh_p6, 4.89352455891786E-03f); |
| 299 | _PS_CONST(cephes_tanh_p7, 1.19825839466702e-06f); |
| 300 | _PS_CONST(cephes_tanh_p8, 1.18534705686654e-04f); |
| 301 | _PS_CONST(cephes_tanh_p9, 2.26843463243900e-03f); |
| 302 | |
| 303 | // an approximation of tanh |
| 304 | static inline v4sf tanh_ps(const v4sf x) |
| 305 | { |
| 306 | v4sf value = x; |
| 307 | value = _mm_max_ps(*(v4sf*)_ps_tanh_lo, value); |
| 308 | value = _mm_min_ps(*(v4sf*)_ps_tanh_hi, value); |
| 309 | |
| 310 | v4sf value_squared = _mm_mul_ps(value, value); |
| 311 | |
| 312 | v4sf p; |
| 313 | p = _mm_comp_fmadd_ps(value_squared, *(v4sf*)_ps_cephes_tanh_p0, *(v4sf*)_ps_cephes_tanh_p1); |
| 314 | p = _mm_comp_fmadd_ps(p, value_squared, *(v4sf*)_ps_cephes_tanh_p2); |
| 315 | p = _mm_comp_fmadd_ps(p, value_squared, *(v4sf*)_ps_cephes_tanh_p3); |
| 316 | p = _mm_comp_fmadd_ps(p, value_squared, *(v4sf*)_ps_cephes_tanh_p4); |
| 317 | p = _mm_comp_fmadd_ps(p, value_squared, *(v4sf*)_ps_cephes_tanh_p5); |
| 318 | p = _mm_comp_fmadd_ps(p, value_squared, *(v4sf*)_ps_cephes_tanh_p6); |
| 319 | p = _mm_mul_ps(p, value); |
| 320 | |
| 321 | v4sf q; |
| 322 | q = _mm_comp_fmadd_ps(value_squared, *(v4sf*)_ps_cephes_tanh_p7, *(v4sf*)_ps_cephes_tanh_p8); |
| 323 | q = _mm_comp_fmadd_ps(q, value_squared, *(v4sf*)_ps_cephes_tanh_p9); |
| 324 | q = _mm_comp_fmadd_ps(q, value_squared, *(v4sf*)_ps_cephes_tanh_p6); |
| 325 | |
| 326 | v4sf dst = _mm_div_ps(p, q); |
| 327 | return dst; |
| 328 | } |
| 329 | |
| 330 | _PS_CONST(minus_cephes_DP1, -0.78515625f); |
| 331 | _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4f); |
| 332 | _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8f); |
| 333 | _PS_CONST(sincof_p0, -1.9515295891E-4f); |
| 334 | _PS_CONST(sincof_p1, 8.3321608736E-3f); |
| 335 | _PS_CONST(sincof_p2, -1.6666654611E-1f); |
| 336 | _PS_CONST(coscof_p0, 2.443315711809948E-005f); |
| 337 | _PS_CONST(coscof_p1, -1.388731625493765E-003f); |
| 338 | _PS_CONST(coscof_p2, 4.166664568298827E-002f); |
| 339 | _PS_CONST(cephes_FOPI, 1.27323954473516f); // 4 / M_PI |
| 340 | |
| 341 | /* evaluation of 4 sines at onces, using only SSE1+MMX intrinsics so |
| 342 | it runs also on old athlons XPs and the pentium III of your grand |
| 343 | mother. |
| 344 | |
| 345 | The code is the exact rewriting of the cephes sinf function. |
| 346 | Precision is excellent as long as x < 8192 (I did not bother to |
| 347 | take into account the special handling they have for greater values |
| 348 | -- it does not return garbage for arguments over 8192, though, but |
| 349 | the extra precision is missing). |
| 350 | |
| 351 | Note that it is such that sinf((float)M_PI) = 8.74e-8, which is the |
| 352 | surprising but correct result. |
| 353 | |
| 354 | Performance is also surprisingly good, 1.33 times faster than the |
| 355 | macos vsinf SSE2 function, and 1.5 times faster than the |
| 356 | __vrs4_sinf of amd's ACML (which is only available in 64 bits). Not |
| 357 | too bad for an SSE1 function (with no special tuning) ! |
| 358 | However the latter libraries probably have a much better handling of NaN, |
| 359 | Inf, denormalized and other special arguments.. |
| 360 | |
| 361 | On my core 1 duo, the execution of this function takes approximately 95 cycles. |
| 362 | |
| 363 | From what I have observed on the experiments with Intel AMath lib, switching to an |
| 364 | SSE2 version would improve the perf by only 10%. |
| 365 | |
| 366 | Since it is based on SSE intrinsics, it has to be compiled at -O2 to |
| 367 | deliver full speed. |
| 368 | */ |
| 369 | static NCNN_FORCEINLINE v4sf sin_ps(v4sf x) |
| 370 | { // any x |
| 371 | v4sf xmm1, xmm2 = _mm_setzero_ps(), xmm3, sign_bit, y; |
| 372 | |
| 373 | #ifdef USE_SSE2 |
| 374 | v4si emm0, emm2; |
| 375 | #else |
| 376 | v2si mm0, mm1, mm2, mm3; |
| 377 | #endif |
| 378 | sign_bit = x; |
| 379 | /* take the absolute value */ |
| 380 | x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask); |
| 381 | /* extract the sign bit (upper one) */ |
| 382 | sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask); |
| 383 | |
| 384 | /* scale by 4/Pi */ |
| 385 | y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI); |
| 386 | |
| 387 | #ifdef USE_SSE2 |
| 388 | /* store the integer part of y in mm0 */ |
| 389 | emm2 = _mm_cvttps_epi32(y); |
| 390 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 391 | emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1); |
| 392 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1); |
| 393 | y = _mm_cvtepi32_ps(emm2); |
| 394 | |
| 395 | /* get the swap sign flag */ |
| 396 | emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4); |
| 397 | emm0 = _mm_slli_epi32(emm0, 29); |
| 398 | /* get the polynom selection mask |
| 399 | there is one polynom for 0 <= x <= Pi/4 |
| 400 | and another one for Pi/4<x<=Pi/2 |
| 401 | |
| 402 | Both branches will be computed. |
| 403 | */ |
| 404 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2); |
| 405 | emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128()); |
| 406 | |
| 407 | v4sf swap_sign_bit = _mm_castsi128_ps(emm0); |
| 408 | v4sf poly_mask = _mm_castsi128_ps(emm2); |
| 409 | sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit); |
| 410 | |
| 411 | #else |
| 412 | /* store the integer part of y in mm0:mm1 */ |
| 413 | xmm2 = _mm_movehl_ps(xmm2, y); |
| 414 | mm2 = _mm_cvttps_pi32(y); |
| 415 | mm3 = _mm_cvttps_pi32(xmm2); |
| 416 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 417 | mm2 = _mm_add_pi32(mm2, *(v2si*)_pi32_1); |
| 418 | mm3 = _mm_add_pi32(mm3, *(v2si*)_pi32_1); |
| 419 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_inv1); |
| 420 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_inv1); |
| 421 | y = _mm_cvtpi32x2_ps(mm2, mm3); |
| 422 | /* get the swap sign flag */ |
| 423 | mm0 = _mm_and_si64(mm2, *(v2si*)_pi32_4); |
| 424 | mm1 = _mm_and_si64(mm3, *(v2si*)_pi32_4); |
| 425 | mm0 = _mm_slli_pi32(mm0, 29); |
| 426 | mm1 = _mm_slli_pi32(mm1, 29); |
| 427 | /* get the polynom selection mask */ |
| 428 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_2); |
| 429 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_2); |
| 430 | mm2 = _mm_cmpeq_pi32(mm2, _mm_setzero_si64()); |
| 431 | mm3 = _mm_cmpeq_pi32(mm3, _mm_setzero_si64()); |
| 432 | v4sf swap_sign_bit, poly_mask; |
| 433 | COPY_MM_TO_XMM(mm0, mm1, swap_sign_bit); |
| 434 | COPY_MM_TO_XMM(mm2, mm3, poly_mask); |
| 435 | sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit); |
| 436 | _mm_empty(); /* good-bye mmx */ |
| 437 | #endif |
| 438 | |
| 439 | /* The magic pass: "Extended precision modular arithmetic" |
| 440 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 441 | xmm1 = *(v4sf*)_ps_minus_cephes_DP1; |
| 442 | xmm2 = *(v4sf*)_ps_minus_cephes_DP2; |
| 443 | xmm3 = *(v4sf*)_ps_minus_cephes_DP3; |
| 444 | x = _mm_comp_fmadd_ps(y, xmm1, x); |
| 445 | x = _mm_comp_fmadd_ps(y, xmm2, x); |
| 446 | x = _mm_comp_fmadd_ps(y, xmm3, x); |
| 447 | |
| 448 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 449 | y = *(v4sf*)_ps_coscof_p0; |
| 450 | v4sf z = _mm_mul_ps(x, x); |
| 451 | |
| 452 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p1); |
| 453 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p2); |
| 454 | y = _mm_mul_ps(y, z); |
| 455 | y = _mm_mul_ps(y, z); |
| 456 | y = _mm_comp_fnmadd_ps(z, *(v4sf*)_ps_0p5, y); |
| 457 | y = _mm_add_ps(y, *(v4sf*)_ps_1); |
| 458 | |
| 459 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 460 | |
| 461 | v4sf y2 = *(v4sf*)_ps_sincof_p0; |
| 462 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p1); |
| 463 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p2); |
| 464 | y2 = _mm_mul_ps(y2, z); |
| 465 | y2 = _mm_comp_fmadd_ps(y2, x, x); |
| 466 | |
| 467 | /* select the correct result from the two polynoms */ |
| 468 | xmm3 = poly_mask; |
| 469 | y2 = _mm_and_ps(xmm3, y2); //, xmm3); |
| 470 | y = _mm_andnot_ps(xmm3, y); |
| 471 | y = _mm_add_ps(y, y2); |
| 472 | /* update the sign */ |
| 473 | y = _mm_xor_ps(y, sign_bit); |
| 474 | return y; |
| 475 | } |
| 476 | |
| 477 | /* almost the same as sin_ps */ |
| 478 | static NCNN_FORCEINLINE v4sf cos_ps(v4sf x) |
| 479 | { // any x |
| 480 | v4sf xmm1, xmm2 = _mm_setzero_ps(), xmm3, y; |
| 481 | #ifdef USE_SSE2 |
| 482 | v4si emm0, emm2; |
| 483 | #else |
| 484 | v2si mm0, mm1, mm2, mm3; |
| 485 | #endif |
| 486 | /* take the absolute value */ |
| 487 | x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask); |
| 488 | |
| 489 | /* scale by 4/Pi */ |
| 490 | y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI); |
| 491 | |
| 492 | #ifdef USE_SSE2 |
| 493 | /* store the integer part of y in mm0 */ |
| 494 | emm2 = _mm_cvttps_epi32(y); |
| 495 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 496 | emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1); |
| 497 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1); |
| 498 | y = _mm_cvtepi32_ps(emm2); |
| 499 | |
| 500 | emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2); |
| 501 | |
| 502 | /* get the swap sign flag */ |
| 503 | emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4); |
| 504 | emm0 = _mm_slli_epi32(emm0, 29); |
| 505 | /* get the polynom selection mask */ |
| 506 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2); |
| 507 | emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128()); |
| 508 | |
| 509 | v4sf sign_bit = _mm_castsi128_ps(emm0); |
| 510 | v4sf poly_mask = _mm_castsi128_ps(emm2); |
| 511 | #else |
| 512 | /* store the integer part of y in mm0:mm1 */ |
| 513 | xmm2 = _mm_movehl_ps(xmm2, y); |
| 514 | mm2 = _mm_cvttps_pi32(y); |
| 515 | mm3 = _mm_cvttps_pi32(xmm2); |
| 516 | |
| 517 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 518 | mm2 = _mm_add_pi32(mm2, *(v2si*)_pi32_1); |
| 519 | mm3 = _mm_add_pi32(mm3, *(v2si*)_pi32_1); |
| 520 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_inv1); |
| 521 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_inv1); |
| 522 | |
| 523 | y = _mm_cvtpi32x2_ps(mm2, mm3); |
| 524 | |
| 525 | mm2 = _mm_sub_pi32(mm2, *(v2si*)_pi32_2); |
| 526 | mm3 = _mm_sub_pi32(mm3, *(v2si*)_pi32_2); |
| 527 | |
| 528 | /* get the swap sign flag in mm0:mm1 and the |
| 529 | polynom selection mask in mm2:mm3 */ |
| 530 | |
| 531 | mm0 = _mm_andnot_si64(mm2, *(v2si*)_pi32_4); |
| 532 | mm1 = _mm_andnot_si64(mm3, *(v2si*)_pi32_4); |
| 533 | mm0 = _mm_slli_pi32(mm0, 29); |
| 534 | mm1 = _mm_slli_pi32(mm1, 29); |
| 535 | |
| 536 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_2); |
| 537 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_2); |
| 538 | |
| 539 | mm2 = _mm_cmpeq_pi32(mm2, _mm_setzero_si64()); |
| 540 | mm3 = _mm_cmpeq_pi32(mm3, _mm_setzero_si64()); |
| 541 | |
| 542 | v4sf sign_bit, poly_mask; |
| 543 | COPY_MM_TO_XMM(mm0, mm1, sign_bit); |
| 544 | COPY_MM_TO_XMM(mm2, mm3, poly_mask); |
| 545 | _mm_empty(); /* good-bye mmx */ |
| 546 | #endif |
| 547 | /* The magic pass: "Extended precision modular arithmetic" |
| 548 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 549 | xmm1 = *(v4sf*)_ps_minus_cephes_DP1; |
| 550 | xmm2 = *(v4sf*)_ps_minus_cephes_DP2; |
| 551 | xmm3 = *(v4sf*)_ps_minus_cephes_DP3; |
| 552 | x = _mm_comp_fmadd_ps(y, xmm1, x); |
| 553 | x = _mm_comp_fmadd_ps(y, xmm2, x); |
| 554 | x = _mm_comp_fmadd_ps(y, xmm3, x); |
| 555 | |
| 556 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 557 | y = *(v4sf*)_ps_coscof_p0; |
| 558 | v4sf z = _mm_mul_ps(x, x); |
| 559 | |
| 560 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p1); |
| 561 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p2); |
| 562 | y = _mm_mul_ps(y, z); |
| 563 | y = _mm_mul_ps(y, z); |
| 564 | y = _mm_comp_fnmadd_ps(z, *(v4sf*)_ps_0p5, y); |
| 565 | y = _mm_add_ps(y, *(v4sf*)_ps_1); |
| 566 | |
| 567 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 568 | |
| 569 | v4sf y2 = *(v4sf*)_ps_sincof_p0; |
| 570 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p1); |
| 571 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p2); |
| 572 | y2 = _mm_mul_ps(y2, z); |
| 573 | y2 = _mm_comp_fmadd_ps(y2, x, x); |
| 574 | |
| 575 | /* select the correct result from the two polynoms */ |
| 576 | xmm3 = poly_mask; |
| 577 | y2 = _mm_and_ps(xmm3, y2); //, xmm3); |
| 578 | y = _mm_andnot_ps(xmm3, y); |
| 579 | y = _mm_add_ps(y, y2); |
| 580 | /* update the sign */ |
| 581 | y = _mm_xor_ps(y, sign_bit); |
| 582 | |
| 583 | return y; |
| 584 | } |
| 585 | |
| 586 | /* since sin_ps and cos_ps are almost identical, sincos_ps could replace both of them.. |
| 587 | it is almost as fast, and gives you a free cosine with your sine */ |
| 588 | static NCNN_FORCEINLINE void sincos_ps(v4sf x, v4sf* s, v4sf* c) |
| 589 | { |
| 590 | v4sf xmm1, xmm2, xmm3 = _mm_setzero_ps(), sign_bit_sin, y; |
| 591 | #ifdef USE_SSE2 |
| 592 | v4si emm0, emm2, emm4; |
| 593 | #else |
| 594 | v2si mm0, mm1, mm2, mm3, mm4, mm5; |
| 595 | #endif |
| 596 | sign_bit_sin = x; |
| 597 | /* take the absolute value */ |
| 598 | x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask); |
| 599 | /* extract the sign bit (upper one) */ |
| 600 | sign_bit_sin = _mm_and_ps(sign_bit_sin, *(v4sf*)_ps_sign_mask); |
| 601 | |
| 602 | /* scale by 4/Pi */ |
| 603 | y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI); |
| 604 | |
| 605 | #ifdef USE_SSE2 |
| 606 | /* store the integer part of y in emm2 */ |
| 607 | emm2 = _mm_cvttps_epi32(y); |
| 608 | |
| 609 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 610 | emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1); |
| 611 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1); |
| 612 | y = _mm_cvtepi32_ps(emm2); |
| 613 | |
| 614 | emm4 = emm2; |
| 615 | |
| 616 | /* get the swap sign flag for the sine */ |
| 617 | emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4); |
| 618 | emm0 = _mm_slli_epi32(emm0, 29); |
| 619 | v4sf swap_sign_bit_sin = _mm_castsi128_ps(emm0); |
| 620 | |
| 621 | /* get the polynom selection mask for the sine*/ |
| 622 | emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2); |
| 623 | emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128()); |
| 624 | v4sf poly_mask = _mm_castsi128_ps(emm2); |
| 625 | #else |
| 626 | /* store the integer part of y in mm2:mm3 */ |
| 627 | xmm3 = _mm_movehl_ps(xmm3, y); |
| 628 | mm2 = _mm_cvttps_pi32(y); |
| 629 | mm3 = _mm_cvttps_pi32(xmm3); |
| 630 | |
| 631 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 632 | mm2 = _mm_add_pi32(mm2, *(v2si*)_pi32_1); |
| 633 | mm3 = _mm_add_pi32(mm3, *(v2si*)_pi32_1); |
| 634 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_inv1); |
| 635 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_inv1); |
| 636 | |
| 637 | y = _mm_cvtpi32x2_ps(mm2, mm3); |
| 638 | |
| 639 | mm4 = mm2; |
| 640 | mm5 = mm3; |
| 641 | |
| 642 | /* get the swap sign flag for the sine */ |
| 643 | mm0 = _mm_and_si64(mm2, *(v2si*)_pi32_4); |
| 644 | mm1 = _mm_and_si64(mm3, *(v2si*)_pi32_4); |
| 645 | mm0 = _mm_slli_pi32(mm0, 29); |
| 646 | mm1 = _mm_slli_pi32(mm1, 29); |
| 647 | v4sf swap_sign_bit_sin; |
| 648 | COPY_MM_TO_XMM(mm0, mm1, swap_sign_bit_sin); |
| 649 | |
| 650 | /* get the polynom selection mask for the sine */ |
| 651 | |
| 652 | mm2 = _mm_and_si64(mm2, *(v2si*)_pi32_2); |
| 653 | mm3 = _mm_and_si64(mm3, *(v2si*)_pi32_2); |
| 654 | mm2 = _mm_cmpeq_pi32(mm2, _mm_setzero_si64()); |
| 655 | mm3 = _mm_cmpeq_pi32(mm3, _mm_setzero_si64()); |
| 656 | v4sf poly_mask; |
| 657 | COPY_MM_TO_XMM(mm2, mm3, poly_mask); |
| 658 | #endif |
| 659 | |
| 660 | /* The magic pass: "Extended precision modular arithmetic" |
| 661 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 662 | xmm1 = *(v4sf*)_ps_minus_cephes_DP1; |
| 663 | xmm2 = *(v4sf*)_ps_minus_cephes_DP2; |
| 664 | xmm3 = *(v4sf*)_ps_minus_cephes_DP3; |
| 665 | x = _mm_comp_fmadd_ps(y, xmm1, x); |
| 666 | x = _mm_comp_fmadd_ps(y, xmm2, x); |
| 667 | x = _mm_comp_fmadd_ps(y, xmm3, x); |
| 668 | |
| 669 | #ifdef USE_SSE2 |
| 670 | emm4 = _mm_sub_epi32(emm4, *(v4si*)_pi32_2); |
| 671 | emm4 = _mm_andnot_si128(emm4, *(v4si*)_pi32_4); |
| 672 | emm4 = _mm_slli_epi32(emm4, 29); |
| 673 | v4sf sign_bit_cos = _mm_castsi128_ps(emm4); |
| 674 | #else |
| 675 | /* get the sign flag for the cosine */ |
| 676 | mm4 = _mm_sub_pi32(mm4, *(v2si*)_pi32_2); |
| 677 | mm5 = _mm_sub_pi32(mm5, *(v2si*)_pi32_2); |
| 678 | mm4 = _mm_andnot_si64(mm4, *(v2si*)_pi32_4); |
| 679 | mm5 = _mm_andnot_si64(mm5, *(v2si*)_pi32_4); |
| 680 | mm4 = _mm_slli_pi32(mm4, 29); |
| 681 | mm5 = _mm_slli_pi32(mm5, 29); |
| 682 | v4sf sign_bit_cos; |
| 683 | COPY_MM_TO_XMM(mm4, mm5, sign_bit_cos); |
| 684 | _mm_empty(); /* good-bye mmx */ |
| 685 | #endif |
| 686 | |
| 687 | sign_bit_sin = _mm_xor_ps(sign_bit_sin, swap_sign_bit_sin); |
| 688 | |
| 689 | /* Evaluate the first polynom (0 <= x <= Pi/4) */ |
| 690 | v4sf z = _mm_mul_ps(x, x); |
| 691 | y = *(v4sf*)_ps_coscof_p0; |
| 692 | |
| 693 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p1); |
| 694 | y = _mm_comp_fmadd_ps(y, z, *(v4sf*)_ps_coscof_p2); |
| 695 | y = _mm_mul_ps(y, z); |
| 696 | y = _mm_mul_ps(y, z); |
| 697 | y = _mm_comp_fnmadd_ps(z, *(v4sf*)_ps_0p5, y); |
| 698 | y = _mm_add_ps(y, *(v4sf*)_ps_1); |
| 699 | |
| 700 | /* Evaluate the second polynom (Pi/4 <= x <= 0) */ |
| 701 | |
| 702 | v4sf y2 = *(v4sf*)_ps_sincof_p0; |
| 703 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p1); |
| 704 | y2 = _mm_comp_fmadd_ps(y2, z, *(v4sf*)_ps_sincof_p2); |
| 705 | y2 = _mm_mul_ps(y2, z); |
| 706 | y2 = _mm_comp_fmadd_ps(y2, x, x); |
| 707 | |
| 708 | /* select the correct result from the two polynoms */ |
| 709 | xmm3 = poly_mask; |
| 710 | v4sf ysin2 = _mm_and_ps(xmm3, y2); |
| 711 | v4sf ysin1 = _mm_andnot_ps(xmm3, y); |
| 712 | y2 = _mm_sub_ps(y2, ysin2); |
| 713 | y = _mm_sub_ps(y, ysin1); |
| 714 | |
| 715 | xmm1 = _mm_add_ps(ysin1, ysin2); |
| 716 | xmm2 = _mm_add_ps(y, y2); |
| 717 | |
| 718 | /* update the sign */ |
| 719 | *s = _mm_xor_ps(xmm1, sign_bit_sin); |
| 720 | *c = _mm_xor_ps(xmm2, sign_bit_cos); |
| 721 | } |
| 722 | |
| 723 | static NCNN_FORCEINLINE __m128 tan_ps(__m128 x) |
| 724 | { |
| 725 | __m128 ysin, ycos; |
| 726 | __m128 eps = _mm_set1_ps(1E-8f); |
| 727 | sincos_ps(x, &ysin, &ycos); |
| 728 | __m128 mask = _mm_cmpeq_ps(ycos, _mm_setzero_ps()); |
| 729 | __m128 _tmp = _mm_and_ps(eps, mask); |
| 730 | ycos = _mm_add_ps(ycos, _tmp); |
| 731 | __m128 ytan = _mm_div_ps(ysin, ycos); |
| 732 | return ytan; |
| 733 | } |
| 734 | |
| 735 | static NCNN_FORCEINLINE __m128 pow_ps(__m128 a, __m128 b) |
| 736 | { |
| 737 | // pow(x, m) = exp(m * log(x)) |
| 738 | return exp_ps(_mm_mul_ps(b, log_ps(a))); |
| 739 | } |
| 740 | |
| 741 | static NCNN_FORCEINLINE __m128 ceil_ps(__m128 x) |
| 742 | { |
| 743 | #if __SSE4_1__ |
| 744 | return _mm_ceil_ps(x); |
| 745 | #endif // __SSE4_1__ |
| 746 | |
| 747 | // Use negative zero as the sign bit mask. |
| 748 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 749 | |
| 750 | // The smallest float number that have no fractional part. (2^23) |
| 751 | const __m128 magic_smallest_no_fraction = _mm_set_ps1(8388608.0f); |
| 752 | |
| 753 | // absolute = abs(x); |
| 754 | __m128 absolute = _mm_andnot_ps(magic_negative_zero, x); |
| 755 | |
| 756 | // negative_mask = magic_negative_zero && x; |
| 757 | __m128 negative_mask = _mm_and_ps(magic_negative_zero, x); |
| 758 | |
| 759 | // no_fraction = (magic_smallest_no_fraction < absolute); |
| 760 | __m128 no_fraction = _mm_cmplt_ps(magic_smallest_no_fraction, absolute); |
| 761 | |
| 762 | // truncated = static_cast<float>(static_cast<uint32_t>(absolute)); |
| 763 | __m128 truncated = _mm_cvtepi32_ps(_mm_cvttps_epi32(absolute)); |
| 764 | |
| 765 | // truncated_with_sign = (truncated || negative_mask); |
| 766 | __m128 truncated_with_sign = _mm_or_ps(truncated, negative_mask); |
| 767 | |
| 768 | // positive_fix = ((x > -0.0f) && (x > truncated_with_sign) ? -1.0f : 0.0f); |
| 769 | __m128 positive_fix = _mm_and_ps( |
| 770 | _mm_and_ps( |
| 771 | _mm_cmpgt_ps(x, magic_negative_zero), |
| 772 | _mm_cmpgt_ps(x, truncated_with_sign)), |
| 773 | _mm_set_ps1(-1.0f)); |
| 774 | |
| 775 | // fixed_result = truncated_with_sign - positive_fix; |
| 776 | __m128 fixed_result = _mm_sub_ps(truncated_with_sign, positive_fix); |
| 777 | |
| 778 | // return ((x && no_fraction) || (!no_fraction && fixed_result)); |
| 779 | return _mm_or_ps( |
| 780 | _mm_and_ps(x, no_fraction), |
| 781 | _mm_andnot_ps(no_fraction, fixed_result)); |
| 782 | } |
| 783 | |
| 784 | static NCNN_FORCEINLINE __m128 floor_ps(__m128 x) |
| 785 | { |
| 786 | #if __SSE4_1__ |
| 787 | return _mm_floor_ps(x); |
| 788 | #endif // __SSE4_1__ |
| 789 | |
| 790 | // Use negative zero as the sign bit mask. |
| 791 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 792 | |
| 793 | // The smallest float number that have no fractional part. (2^23) |
| 794 | const __m128 magic_smallest_no_fraction = _mm_set_ps1(8388608.0f); |
| 795 | |
| 796 | // absolute = abs(x); |
| 797 | __m128 absolute = _mm_andnot_ps(magic_negative_zero, x); |
| 798 | |
| 799 | // negative_mask = magic_negative_zero && x; |
| 800 | __m128 negative_mask = _mm_and_ps(magic_negative_zero, x); |
| 801 | |
| 802 | // no_fraction = (magic_smallest_no_fraction < absolute); |
| 803 | __m128 no_fraction = _mm_cmplt_ps(magic_smallest_no_fraction, absolute); |
| 804 | |
| 805 | // truncated = static_cast<float>(static_cast<uint32_t>(absolute)); |
| 806 | __m128 truncated = _mm_cvtepi32_ps(_mm_cvttps_epi32(absolute)); |
| 807 | |
| 808 | // truncated_with_sign = (truncated || negative_mask); |
| 809 | __m128 truncated_with_sign = _mm_or_ps(truncated, negative_mask); |
| 810 | |
| 811 | // negative_fix = ((x < truncated_with_sign) ? 1.0f : 0.0f); |
| 812 | __m128 negative_fix = _mm_and_ps( |
| 813 | _mm_cmplt_ps(x, truncated_with_sign), |
| 814 | _mm_set_ps1(1.0f)); |
| 815 | |
| 816 | // fixed_result = truncated_with_sign - negative_fix; |
| 817 | __m128 fixed_result = _mm_sub_ps(truncated_with_sign, negative_fix); |
| 818 | |
| 819 | // return ((x && no_fraction) || (!no_fraction && fixed_result)); |
| 820 | return _mm_or_ps( |
| 821 | _mm_and_ps(x, no_fraction), |
| 822 | _mm_andnot_ps(no_fraction, fixed_result)); |
| 823 | } |
| 824 | |
| 825 | static NCNN_FORCEINLINE __m128 asin_ps(__m128 x) |
| 826 | { |
| 827 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 828 | const __m128 magic_half_one = _mm_set_ps1(0.5f); |
| 829 | const __m128 magic_one = _mm_set_ps1(1.0f); |
| 830 | const __m128 magic_a4 = _mm_set_ps1(0.023994016f); |
| 831 | const __m128 magic_a5 = _mm_set_ps1(0.042417344f); |
| 832 | const __m128 magic_a2 = _mm_set_ps1(0.07494697f); |
| 833 | const __m128 magic_a3 = _mm_set_ps1(0.045520633f); |
| 834 | const __m128 magic_a0 = _mm_set_ps1(1.0f); |
| 835 | const __m128 magic_a1 = _mm_set_ps1(0.166667819f); |
| 836 | const __m128 magic_half_pi = _mm_set_ps1(1.5707964f); |
| 837 | const __m128 magic_three = _mm_set_ps1(3.0f); |
| 838 | |
| 839 | // negative_mask = magic_negative_zero && x; |
| 840 | __m128 negative_mask = _mm_and_ps(magic_negative_zero, x); |
| 841 | |
| 842 | // absolute = abs(x); |
| 843 | __m128 absolute = _mm_andnot_ps(magic_negative_zero, x); |
| 844 | |
| 845 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 846 | |
| 847 | // is_small_input = (absolute <= 0.5f); |
| 848 | __m128 is_small_input = _mm_cmple_ps(absolute, magic_half_one); |
| 849 | |
| 850 | // is_big_input = (is_small_input ? 0.0f : 1.0f); |
| 851 | __m128 is_big_input = _mm_andnot_ps(is_small_input, magic_one); |
| 852 | |
| 853 | // big_input_approx = sqrt(0.5f * (1 - absolute)); |
| 854 | __m128 big_input_approx = _mm_sqrt_ps(_mm_mul_ps( |
| 855 | magic_half_one, |
| 856 | _mm_sub_ps(magic_one, absolute))); |
| 857 | |
| 858 | // input_approx = (is_small_input ? absolute : big_input_approx); |
| 859 | __m128 input_approx = _mm_or_ps( |
| 860 | _mm_and_ps(is_small_input, absolute), |
| 861 | _mm_andnot_ps(is_small_input, big_input_approx)); |
| 862 | |
| 863 | // square_of_input_approx = input_approx * input_approx; |
| 864 | __m128 square_of_input_approx = _mm_mul_ps(input_approx, input_approx); |
| 865 | |
| 866 | // fourth_power_of_input_approx = |
| 867 | // square_of_input_approx * square_of_input_approx; |
| 868 | __m128 fourth_power_of_input_approx = _mm_mul_ps( |
| 869 | square_of_input_approx, square_of_input_approx); |
| 870 | |
| 871 | // TODO: Need more explanations. |
| 872 | // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2); |
| 873 | // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3); |
| 874 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a0); |
| 875 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a1); |
| 876 | // output_approx = ((square_of_input_approx * x4) + x3); |
| 877 | __m128 output_approx = _mm_comp_fmadd_ps( |
| 878 | square_of_input_approx, |
| 879 | _mm_comp_fmadd_ps( |
| 880 | fourth_power_of_input_approx, |
| 881 | _mm_comp_fmadd_ps( |
| 882 | fourth_power_of_input_approx, |
| 883 | magic_a5, |
| 884 | magic_a3), |
| 885 | magic_a1), |
| 886 | _mm_comp_fmadd_ps( |
| 887 | fourth_power_of_input_approx, |
| 888 | _mm_comp_fmadd_ps( |
| 889 | fourth_power_of_input_approx, |
| 890 | magic_a4, |
| 891 | magic_a2), |
| 892 | magic_a0)); |
| 893 | |
| 894 | // TODO: Need more explanations. |
| 895 | // x1 = ((0.5 * PI) * is_big_input); |
| 896 | // x2 = (output_approx * input_approx); |
| 897 | // x3 = (-(3.0f * is_big_input) + 1.0f); |
| 898 | // final_approx = ((x2 * x3) + x1); |
| 899 | __m128 final_approx = _mm_comp_fmadd_ps( |
| 900 | _mm_mul_ps(output_approx, input_approx), |
| 901 | _mm_comp_fnmadd_ps(magic_three, is_big_input, magic_one), |
| 902 | _mm_mul_ps(magic_half_pi, is_big_input)); |
| 903 | |
| 904 | // return (final_approx || negative_mask); |
| 905 | return _mm_or_ps(final_approx, negative_mask); |
| 906 | } |
| 907 | |
| 908 | static NCNN_FORCEINLINE __m128 acos_ps(__m128 x) |
| 909 | { |
| 910 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 911 | const __m128 magic_zero = _mm_set_ps1(0.0f); |
| 912 | const __m128 magic_half_one = _mm_set_ps1(0.5f); |
| 913 | const __m128 magic_one = _mm_set_ps1(1.0f); |
| 914 | const __m128 magic_a4 = _mm_set_ps1(0.023994016f); |
| 915 | const __m128 magic_a5 = _mm_set_ps1(0.042417344f); |
| 916 | const __m128 magic_a2 = _mm_set_ps1(0.07494697f); |
| 917 | const __m128 magic_a3 = _mm_set_ps1(0.045520633f); |
| 918 | const __m128 magic_a0 = _mm_set_ps1(1.0f); |
| 919 | const __m128 magic_a1 = _mm_set_ps1(0.166667819f); |
| 920 | const __m128 magic_half_pi = _mm_set_ps1(1.5707964f); |
| 921 | const __m128 magic_pi = _mm_set_ps1(3.1415927f); |
| 922 | |
| 923 | // negative_mask = magic_negative_zero && x; |
| 924 | __m128 negative_mask = _mm_and_ps(magic_negative_zero, x); |
| 925 | |
| 926 | // absolute = abs(x); |
| 927 | __m128 absolute = _mm_andnot_ps(magic_negative_zero, x); |
| 928 | |
| 929 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 930 | |
| 931 | // is_small_input = (absolute <= 0.5f); |
| 932 | __m128 is_small_input = _mm_cmple_ps(absolute, magic_half_one); |
| 933 | |
| 934 | // big_input_approx = sqrt(0.5f * (1 - absolute)); |
| 935 | __m128 big_input_approx = _mm_sqrt_ps(_mm_mul_ps( |
| 936 | magic_half_one, |
| 937 | _mm_sub_ps(magic_one, absolute))); |
| 938 | |
| 939 | // input_approx = (is_small_input ? absolute : big_input_approx); |
| 940 | __m128 input_approx = _mm_or_ps( |
| 941 | _mm_and_ps(is_small_input, absolute), |
| 942 | _mm_andnot_ps(is_small_input, big_input_approx)); |
| 943 | |
| 944 | // square_of_input_approx = input_approx * input_approx; |
| 945 | __m128 square_of_input_approx = _mm_mul_ps(input_approx, input_approx); |
| 946 | |
| 947 | // fourth_power_of_input_approx = |
| 948 | // square_of_input_approx * square_of_input_approx; |
| 949 | __m128 fourth_power_of_input_approx = _mm_mul_ps( |
| 950 | square_of_input_approx, square_of_input_approx); |
| 951 | |
| 952 | // TODO: Need more explanations. |
| 953 | // x1 = ((fourth_power_of_input_approx * magic_a4) + magic_a2); |
| 954 | // x2 = ((fourth_power_of_input_approx * magic_a5) + magic_a3); |
| 955 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a0); |
| 956 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a1); |
| 957 | // output_approx = ((square_of_input_approx * x4) + x3); |
| 958 | __m128 output_approx = _mm_comp_fmadd_ps( |
| 959 | square_of_input_approx, |
| 960 | _mm_comp_fmadd_ps( |
| 961 | fourth_power_of_input_approx, |
| 962 | _mm_comp_fmadd_ps( |
| 963 | fourth_power_of_input_approx, |
| 964 | magic_a5, |
| 965 | magic_a3), |
| 966 | magic_a1), |
| 967 | _mm_comp_fmadd_ps( |
| 968 | fourth_power_of_input_approx, |
| 969 | _mm_comp_fmadd_ps( |
| 970 | fourth_power_of_input_approx, |
| 971 | magic_a4, |
| 972 | magic_a2), |
| 973 | magic_a0)); |
| 974 | |
| 975 | // TODO: Need more explanations. |
| 976 | // x1 = (output_approx * input_approx); |
| 977 | __m128 x1 = _mm_mul_ps(output_approx, input_approx); |
| 978 | |
| 979 | // TODO: Need more explanations. |
| 980 | // small_final_approx = ((0.5 * PI) - (x1 | negative_mask)); |
| 981 | __m128 small_final_approx = _mm_sub_ps( |
| 982 | magic_half_pi, |
| 983 | _mm_or_ps(x1, negative_mask)); |
| 984 | |
| 985 | // TODO: Need more explanations. |
| 986 | // big_final_approx = (((x < 0.0f) & PI) + ((x1 * 2) | negative_mask)); |
| 987 | __m128 big_final_approx = _mm_add_ps( |
| 988 | _mm_and_ps(_mm_cmplt_ps(x, magic_zero), magic_pi), |
| 989 | _mm_or_ps(_mm_add_ps(x1, x1), negative_mask)); |
| 990 | |
| 991 | // return (is_small_input ? small_final_approx : big_final_approx); |
| 992 | return _mm_or_ps( |
| 993 | _mm_and_ps(is_small_input, small_final_approx), |
| 994 | _mm_andnot_ps(is_small_input, big_final_approx)); |
| 995 | } |
| 996 | |
| 997 | static NCNN_FORCEINLINE __m128 atan_ps(__m128 x) |
| 998 | { |
| 999 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 1000 | const __m128 magic_one = _mm_set_ps1(1.0f); |
| 1001 | const __m128 magic_negative_one = _mm_set_ps1(-1.0f); |
| 1002 | const __m128 magic_half_pi = _mm_set_ps1(1.5707964f); |
| 1003 | const __m128 magic_a0 = _mm_set_ps1(1.0f); |
| 1004 | const __m128 magic_a1 = _mm_set_ps1(-0.33333072f); |
| 1005 | const __m128 magic_a2 = _mm_set_ps1(0.1999262f); |
| 1006 | const __m128 magic_a3 = _mm_set_ps1(-0.14203644f); |
| 1007 | const __m128 magic_a4 = _mm_set_ps1(0.10640934f); |
| 1008 | const __m128 magic_a5 = _mm_set_ps1(-0.07504295f); |
| 1009 | const __m128 magic_a6 = _mm_set_ps1(0.04269152f); |
| 1010 | const __m128 magic_a7 = _mm_set_ps1(-0.01606863f); |
| 1011 | const __m128 magic_a8 = _mm_set_ps1(0.0028498897f); |
| 1012 | |
| 1013 | // negative_mask = magic_negative_zero && x; |
| 1014 | __m128 negative_mask = _mm_and_ps(magic_negative_zero, x); |
| 1015 | |
| 1016 | // absolute = abs(x); |
| 1017 | __m128 absolute = _mm_andnot_ps(magic_negative_zero, x); |
| 1018 | |
| 1019 | // Reference: https://en.wikipedia.org/wiki/Small-angle_approximation |
| 1020 | |
| 1021 | // is_small_input = (1.0f < absolute); |
| 1022 | __m128 is_small_input = _mm_cmplt_ps(magic_one, absolute); |
| 1023 | |
| 1024 | // x1 = (is_small_input ? -1.0f : absolute); |
| 1025 | // x2 = (is_small_input ? absolute : 1.0f) |
| 1026 | // input_approx = x1 / x2; |
| 1027 | __m128 input_approx = _mm_div_ps( |
| 1028 | _mm_or_ps( |
| 1029 | _mm_and_ps(is_small_input, magic_negative_one), |
| 1030 | _mm_andnot_ps(is_small_input, absolute)), |
| 1031 | _mm_or_ps( |
| 1032 | _mm_and_ps(is_small_input, absolute), |
| 1033 | _mm_andnot_ps(is_small_input, magic_one))); |
| 1034 | |
| 1035 | // square_of_input_approx = input_approx * input_approx; |
| 1036 | __m128 square_of_input_approx = _mm_mul_ps(input_approx, input_approx); |
| 1037 | |
| 1038 | // fourth_power_of_input_approx = |
| 1039 | // square_of_input_approx * square_of_input_approx; |
| 1040 | __m128 fourth_power_of_input_approx = _mm_mul_ps( |
| 1041 | square_of_input_approx, square_of_input_approx); |
| 1042 | |
| 1043 | // TODO: Need more explanations. |
| 1044 | // x1 = ((fourth_power_of_input_approx * magic_a7) + magic_a5); |
| 1045 | // x2 = ((fourth_power_of_input_approx * magic_a8) + magic_a6); |
| 1046 | // x3 = ((fourth_power_of_input_approx * x1) + magic_a3); |
| 1047 | // x4 = ((fourth_power_of_input_approx * x2) + magic_a4); |
| 1048 | // x5 = ((fourth_power_of_input_approx * x3) + magic_a1); |
| 1049 | // x6 = ((fourth_power_of_input_approx * x4) + magic_a2); |
| 1050 | // x7 = ((fourth_power_of_input_approx * x6) + magic_a0); |
| 1051 | // output_approx = ((square_of_input_approx * x5) + x7); |
| 1052 | __m128 output_approx = _mm_comp_fmadd_ps( |
| 1053 | square_of_input_approx, |
| 1054 | _mm_comp_fmadd_ps( |
| 1055 | fourth_power_of_input_approx, |
| 1056 | _mm_comp_fmadd_ps( |
| 1057 | fourth_power_of_input_approx, |
| 1058 | _mm_comp_fmadd_ps( |
| 1059 | fourth_power_of_input_approx, |
| 1060 | magic_a7, |
| 1061 | magic_a5), |
| 1062 | magic_a3), |
| 1063 | magic_a1), |
| 1064 | _mm_comp_fmadd_ps( |
| 1065 | fourth_power_of_input_approx, |
| 1066 | _mm_comp_fmadd_ps( |
| 1067 | fourth_power_of_input_approx, |
| 1068 | _mm_comp_fmadd_ps( |
| 1069 | fourth_power_of_input_approx, |
| 1070 | _mm_comp_fmadd_ps( |
| 1071 | fourth_power_of_input_approx, |
| 1072 | magic_a8, |
| 1073 | magic_a6), |
| 1074 | magic_a4), |
| 1075 | magic_a2), |
| 1076 | magic_a0)); |
| 1077 | |
| 1078 | // TODO: Need more explanations. |
| 1079 | // x1 = (output_approx * input_approx); |
| 1080 | // if (is_small_input) x1 += (0.5 * PI); |
| 1081 | // return (negative_mask ? -x1 : x1); |
| 1082 | return _mm_or_ps( |
| 1083 | _mm_add_ps( |
| 1084 | _mm_mul_ps(output_approx, input_approx), |
| 1085 | _mm_and_ps(is_small_input, magic_half_pi)), |
| 1086 | negative_mask); |
| 1087 | } |
| 1088 | |
| 1089 | static NCNN_FORCEINLINE __m128 atan2_ps(__m128 y, __m128 x) |
| 1090 | { |
| 1091 | // Reference: https://mazzo.li/posts/vectorized-atan2.html |
| 1092 | |
| 1093 | const __m128 magic_zero = _mm_set_ps1(0.0f); |
| 1094 | const __m128 magic_negative_zero = _mm_set_ps1(-0.0f); |
| 1095 | const __m128 magic_pi = _mm_set_ps1(3.1415927f); |
| 1096 | const __m128 magic_half_pi = _mm_set_ps1(1.5707964f); |
| 1097 | |
| 1098 | // not_equal_zero_x = (x != 0.0f); |
| 1099 | __m128 not_equal_zero_x = _mm_cmpneq_ps(x, magic_zero); |
| 1100 | |
| 1101 | // not_equal_zero_y = (y != 0.0f); |
| 1102 | __m128 not_equal_zero_y = _mm_cmpneq_ps(y, magic_zero); |
| 1103 | |
| 1104 | // normal_mode = ((x != 0.0f) & (y != 0.0f)); |
| 1105 | __m128 normal_mode = _mm_and_ps(not_equal_zero_x, not_equal_zero_y); |
| 1106 | |
| 1107 | // negative_mask_x = magic_negative_zero && x; |
| 1108 | __m128 negative_mask_x = _mm_and_ps(magic_negative_zero, x); |
| 1109 | |
| 1110 | // negative_mask_y = magic_negative_zero && y; |
| 1111 | __m128 negative_mask_y = _mm_and_ps(magic_negative_zero, y); |
| 1112 | |
| 1113 | // pi_additions = ((x < 0.0f) ? ((y < 0.0f) ? -PI : PI) : 0.0f); |
| 1114 | __m128 pi_additions = _mm_and_ps( |
| 1115 | _mm_cmplt_ps(x, magic_zero), |
| 1116 | _mm_or_ps( |
| 1117 | _mm_and_ps( |
| 1118 | _mm_cmplt_ps(y, magic_zero), |
| 1119 | magic_negative_zero), |
| 1120 | magic_pi)); |
| 1121 | |
| 1122 | // normal_result = (atan(y / x) + pi_additions); |
| 1123 | __m128 normal_result = _mm_add_ps( |
| 1124 | atan_ps(_mm_div_ps(y, x)), |
| 1125 | pi_additions); |
| 1126 | |
| 1127 | // negative_mask_full_x = ((negative_mask_x | PI) < 0.0f); |
| 1128 | __m128 negative_mask_full_x = _mm_cmplt_ps( |
| 1129 | _mm_or_ps(negative_mask_x, magic_pi), |
| 1130 | magic_zero); |
| 1131 | |
| 1132 | // x1 = (negative_mask_y ? -(0.5 * PI) : (0.5 * PI)); |
| 1133 | // x2 = (negative_mask_full_x ? PI : 0.0f); |
| 1134 | // special_result = ((y != 0.0f) ? x1 : x2); |
| 1135 | __m128 special_result = _mm_or_ps( |
| 1136 | _mm_and_ps( |
| 1137 | not_equal_zero_y, |
| 1138 | _mm_or_ps(negative_mask_y, magic_half_pi)), |
| 1139 | _mm_andnot_ps( |
| 1140 | not_equal_zero_y, |
| 1141 | _mm_or_ps( |
| 1142 | _mm_and_ps(negative_mask_full_x, magic_pi), |
| 1143 | _mm_andnot_ps(negative_mask_full_x, magic_zero)))); |
| 1144 | |
| 1145 | // return (normal_mode ? normal_result : special_result); |
| 1146 | return _mm_or_ps( |
| 1147 | _mm_and_ps(normal_mode, normal_result), |
| 1148 | _mm_andnot_ps(normal_mode, special_result)); |
| 1149 | } |
| 1150 | |
| 1151 | static NCNN_FORCEINLINE __m128 abs_ps(__m128 x) |
| 1152 | { |
| 1153 | const __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)); |
| 1154 | return _mm_and_ps(abs_mask, x); |
| 1155 | } |
| 1156 | |
| 1157 | #endif // SSE_MATHFUN_H |