File indexing completed on 2025-08-28 09:11:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef XSIMD_GENERIC_MATH_HPP
0013 #define XSIMD_GENERIC_MATH_HPP
0014
0015 #include "../xsimd_scalar.hpp"
0016 #include "./xsimd_generic_details.hpp"
0017 #include "./xsimd_generic_trigo.hpp"
0018
0019 #include <type_traits>
0020
0021 namespace xsimd
0022 {
0023
0024 namespace kernel
0025 {
0026
0027 using namespace types;
0028
0029 template <class A, class T, class>
0030 XSIMD_INLINE batch<T, A> abs(batch<T, A> const& self, requires_arch<generic>) noexcept
0031 {
0032 if (std::is_unsigned<T>::value)
0033 return self;
0034 else
0035 {
0036 auto sign = bitofsign(self);
0037 auto inv = self ^ sign;
0038 return inv - sign;
0039 }
0040 }
0041
0042 template <class A, class T>
0043 XSIMD_INLINE batch<T, A> abs(batch<std::complex<T>, A> const& z, requires_arch<generic>) noexcept
0044 {
0045 return hypot(z.real(), z.imag());
0046 }
0047
0048
0049 namespace detail
0050 {
0051 template <class A, class T>
0052 XSIMD_INLINE batch<T, A> avg(batch<T, A> const& x, batch<T, A> const& y, std::true_type, std::false_type) noexcept
0053 {
0054 return (x & y) + ((x ^ y) >> 1);
0055 }
0056
0057 template <class A, class T>
0058 XSIMD_INLINE batch<T, A> avg(batch<T, A> const& x, batch<T, A> const& y, std::true_type, std::true_type) noexcept
0059 {
0060
0061
0062 auto t = (x & y) + ((x ^ y) >> 1);
0063 auto t_u = bitwise_cast<typename std::make_unsigned<T>::type>(t);
0064 auto avg = t + (bitwise_cast<T>(t_u >> (8 * sizeof(T) - 1)) & (x ^ y));
0065 return avg;
0066 }
0067
0068 template <class A, class T>
0069 XSIMD_INLINE batch<T, A> avg(batch<T, A> const& x, batch<T, A> const& y, std::false_type, std::true_type) noexcept
0070 {
0071 return (x + y) / 2;
0072 }
0073 }
0074
0075 template <class A, class T>
0076 XSIMD_INLINE batch<T, A> avg(batch<T, A> const& x, batch<T, A> const& y, requires_arch<generic>) noexcept
0077 {
0078 return detail::avg(x, y, typename std::is_integral<T>::type {}, typename std::is_signed<T>::type {});
0079 }
0080
0081
0082 namespace detail
0083 {
0084 template <class A, class T>
0085 XSIMD_INLINE batch<T, A> avgr(batch<T, A> const& x, batch<T, A> const& y, std::true_type) noexcept
0086 {
0087 constexpr unsigned shift = 8 * sizeof(T) - 1;
0088 auto adj = std::is_signed<T>::value ? ((x ^ y) & 0x1) : (((x ^ y) << shift) >> shift);
0089 return ::xsimd::kernel::avg(x, y, A {}) + adj;
0090 }
0091
0092 template <class A, class T>
0093 XSIMD_INLINE batch<T, A> avgr(batch<T, A> const& x, batch<T, A> const& y, std::false_type) noexcept
0094 {
0095 return ::xsimd::kernel::avg(x, y, A {});
0096 }
0097 }
0098
0099 template <class A, class T>
0100 XSIMD_INLINE batch<T, A> avgr(batch<T, A> const& x, batch<T, A> const& y, requires_arch<generic>) noexcept
0101 {
0102 return detail::avgr(x, y, typename std::is_integral<T>::type {});
0103 }
0104
0105
0106 template <class A, class T>
0107 XSIMD_INLINE batch<T, A> batch_cast(batch<T, A> const& self, batch<T, A> const&, requires_arch<generic>) noexcept
0108 {
0109 return self;
0110 }
0111
0112 namespace detail
0113 {
0114 template <class A, class T_out, class T_in>
0115 XSIMD_INLINE batch<T_out, A> batch_cast(batch<T_in, A> const& self, batch<T_out, A> const& out, requires_arch<generic>, with_fast_conversion) noexcept
0116 {
0117 return fast_cast(self, out, A {});
0118 }
0119 template <class A, class T_out, class T_in>
0120 XSIMD_INLINE batch<T_out, A> batch_cast(batch<T_in, A> const& self, batch<T_out, A> const&, requires_arch<generic>, with_slow_conversion) noexcept
0121 {
0122 static_assert(!std::is_same<T_in, T_out>::value, "there should be no conversion for this type combination");
0123 using batch_type_in = batch<T_in, A>;
0124 using batch_type_out = batch<T_out, A>;
0125 static_assert(batch_type_in::size == batch_type_out::size, "compatible sizes");
0126 alignas(A::alignment()) T_in buffer_in[batch_type_in::size];
0127 alignas(A::alignment()) T_out buffer_out[batch_type_out::size];
0128 self.store_aligned(&buffer_in[0]);
0129 std::copy(std::begin(buffer_in), std::end(buffer_in), std::begin(buffer_out));
0130 return batch_type_out::load_aligned(buffer_out);
0131 }
0132
0133 }
0134
0135 template <class A, class T_out, class T_in>
0136 XSIMD_INLINE batch<T_out, A> batch_cast(batch<T_in, A> const& self, batch<T_out, A> const& out, requires_arch<generic>) noexcept
0137 {
0138 return detail::batch_cast(self, out, A {}, detail::conversion_type<A, T_in, T_out> {});
0139 }
0140
0141
0142 template <class A, class T>
0143 XSIMD_INLINE batch<T, A> bitofsign(batch<T, A> const& self, requires_arch<generic>) noexcept
0144 {
0145 static_assert(std::is_integral<T>::value, "int type implementation");
0146 if (std::is_unsigned<T>::value)
0147 return batch<T, A>(0);
0148 else
0149 return self >> (T)(8 * sizeof(T) - 1);
0150 }
0151
0152 template <class A>
0153 XSIMD_INLINE batch<float, A> bitofsign(batch<float, A> const& self, requires_arch<generic>) noexcept
0154 {
0155 return self & constants::signmask<batch<float, A>>();
0156 }
0157 template <class A>
0158 XSIMD_INLINE batch<double, A> bitofsign(batch<double, A> const& self, requires_arch<generic>) noexcept
0159 {
0160 return self & constants::signmask<batch<double, A>>();
0161 }
0162
0163
0164 template <class A, class T>
0165 XSIMD_INLINE batch<T, A> bitwise_cast(batch<T, A> const& self, batch<T, A> const&, requires_arch<generic>) noexcept
0166 {
0167 return self;
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 template <class A>
0181 XSIMD_INLINE batch<float, A> cbrt(batch<float, A> const& self, requires_arch<generic>) noexcept
0182 {
0183 using batch_type = batch<float, A>;
0184 batch_type z = abs(self);
0185 #ifndef XSIMD_NO_DENORMALS
0186 auto denormal = z < constants::smallestposval<batch_type>();
0187 z = select(denormal, z * constants::twotonmb<batch_type>(), z);
0188 batch_type f = select(denormal, constants::twotonmbo3<batch_type>(), batch_type(1.));
0189 #endif
0190 const batch_type CBRT2(bit_cast<float>(0x3fa14518));
0191 const batch_type CBRT4(bit_cast<float>(0x3fcb2ff5));
0192 const batch_type CBRT2I(bit_cast<float>(0x3f4b2ff5));
0193 const batch_type CBRT4I(bit_cast<float>(0x3f214518));
0194 using i_type = as_integer_t<batch_type>;
0195 i_type e;
0196 batch_type x = frexp(z, e);
0197 x = detail::horner<batch_type,
0198 0x3ece0609,
0199 0x3f91eb77,
0200 0xbf745265,
0201 0x3f0bf0fe,
0202 0xbe09e49a>(x);
0203 auto flag = e >= i_type(0);
0204 i_type e1 = abs(e);
0205 i_type rem = e1;
0206 e1 /= i_type(3);
0207 rem -= e1 * i_type(3);
0208 e = e1 * sign(e);
0209 const batch_type cbrt2 = select(batch_bool_cast<float>(flag), CBRT2, CBRT2I);
0210 const batch_type cbrt4 = select(batch_bool_cast<float>(flag), CBRT4, CBRT4I);
0211 batch_type fact = select(batch_bool_cast<float>(rem == i_type(1)), cbrt2, batch_type(1.));
0212 fact = select(batch_bool_cast<float>(rem == i_type(2)), cbrt4, fact);
0213 x = ldexp(x * fact, e);
0214 x -= (x - z / (x * x)) * batch_type(1.f / 3.f);
0215 #ifndef XSIMD_NO_DENORMALS
0216 x = (x | bitofsign(self)) * f;
0217 #else
0218 x = x | bitofsign(self);
0219 #endif
0220 #ifndef XSIMD_NO_INFINITIES
0221 return select(self == batch_type(0.) || isinf(self), self, x);
0222 #else
0223 return select(self == batch_type(0.), self, x);
0224 #endif
0225 }
0226
0227 template <class A>
0228 XSIMD_INLINE batch<double, A> cbrt(batch<double, A> const& self, requires_arch<generic>) noexcept
0229 {
0230 using batch_type = batch<double, A>;
0231 batch_type z = abs(self);
0232 #ifndef XSIMD_NO_DENORMALS
0233 auto denormal = z < constants::smallestposval<batch_type>();
0234 z = select(denormal, z * constants::twotonmb<batch_type>(), z);
0235 batch_type f = select(denormal, constants::twotonmbo3<batch_type>(), batch_type(1.));
0236 #endif
0237 const batch_type CBRT2(bit_cast<double>(int64_t(0x3ff428a2f98d728b)));
0238 const batch_type CBRT4(bit_cast<double>(int64_t(0x3ff965fea53d6e3d)));
0239 const batch_type CBRT2I(bit_cast<double>(int64_t(0x3fe965fea53d6e3d)));
0240 const batch_type CBRT4I(bit_cast<double>(int64_t(0x3fe428a2f98d728b)));
0241 using i_type = as_integer_t<batch_type>;
0242 i_type e;
0243 batch_type x = frexp(z, e);
0244 x = detail::horner<batch_type,
0245 0x3fd9c0c12122a4feull,
0246 0x3ff23d6ee505873aull,
0247 0xbfee8a4ca3ba37b8ull,
0248 0x3fe17e1fc7e59d58ull,
0249 0xbfc13c93386fdff6ull>(x);
0250 auto flag = e >= typename i_type::value_type(0);
0251 i_type e1 = abs(e);
0252 i_type rem = e1;
0253 e1 /= i_type(3);
0254 rem -= e1 * i_type(3);
0255 e = e1 * sign(e);
0256 const batch_type cbrt2 = select(batch_bool_cast<double>(flag), CBRT2, CBRT2I);
0257 const batch_type cbrt4 = select(batch_bool_cast<double>(flag), CBRT4, CBRT4I);
0258 batch_type fact = select(batch_bool_cast<double>(rem == i_type(1)), cbrt2, batch_type(1.));
0259 fact = select(batch_bool_cast<double>(rem == i_type(2)), cbrt4, fact);
0260 x = ldexp(x * fact, e);
0261 x -= (x - z / (x * x)) * batch_type(1. / 3.);
0262 x -= (x - z / (x * x)) * batch_type(1. / 3.);
0263 #ifndef XSIMD_NO_DENORMALS
0264 x = (x | bitofsign(self)) * f;
0265 #else
0266 x = x | bitofsign(self);
0267 #endif
0268 #ifndef XSIMD_NO_INFINITIES
0269 return select(self == batch_type(0.) || isinf(self), self, x);
0270 #else
0271 return select(self == batch_type(0.), self, x);
0272 #endif
0273 }
0274
0275
0276 template <class A, class T>
0277 XSIMD_INLINE batch<T, A> clip(batch<T, A> const& self, batch<T, A> const& lo, batch<T, A> const& hi, requires_arch<generic>) noexcept
0278 {
0279 return min(hi, max(self, lo));
0280 }
0281
0282
0283 template <class A, class T, class _ = typename std::enable_if<std::is_floating_point<T>::value, void>::type>
0284 XSIMD_INLINE batch<T, A> copysign(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
0285 {
0286 return abs(self) | bitofsign(other);
0287 }
0288
0289
0290
0291 namespace detail
0292 {
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302 template <class B>
0303 struct erf_kernel;
0304
0305 template <class A>
0306 struct erf_kernel<batch<float, A>>
0307 {
0308 using batch_type = batch<float, A>;
0309
0310
0311 static XSIMD_INLINE batch_type erf1(const batch_type& x) noexcept
0312 {
0313 return detail::horner<batch_type,
0314 0x3f906eba,
0315 0xbec0937e,
0316 0x3de70f22,
0317 0xbcdb61f4,
0318 0x3ba4468d,
0319 0xba1fc83b
0320 >(x);
0321 }
0322
0323
0324
0325 static XSIMD_INLINE batch_type erfc2(const batch_type& x) noexcept
0326 {
0327 return detail::horner<batch_type,
0328 0x3f0a0e8b,
0329 0xbf918a62,
0330 0x3e243828,
0331 0x3ec4ca6e,
0332 0x3e1175c7,
0333 0x3e2006f0,
0334 0xbfaea865,
0335 0x4050b063,
0336 0xc0cd1a85,
0337 0x40d67e3b,
0338 0xc0283611
0339 >(x);
0340 }
0341
0342 static XSIMD_INLINE batch_type erfc3(const batch_type& x) noexcept
0343 {
0344 return (batch_type(1.) - x) * detail::horner<batch_type,
0345 0x3f7ffffe,
0346 0xbe036d7e,
0347 0xbfa11698,
0348 0xbffc9284,
0349 0xc016c985,
0350 0x3f2cff3b,
0351 0xc010d956,
0352 0x401b5680,
0353 0x41aa8e55
0354 >(x);
0355 }
0356 };
0357
0358 template <class A>
0359 struct erf_kernel<batch<double, A>>
0360 {
0361 using batch_type = batch<double, A>;
0362
0363
0364 static XSIMD_INLINE batch_type erf1(const batch_type& x) noexcept
0365 {
0366 return detail::horner<batch_type,
0367 0x3ff20dd750429b61ull,
0368 0x3fc16500f106c0a5ull,
0369 0x3fa4a59a4f02579cull,
0370 0x3f53b7664358865aull,
0371 0x3f110512d5b20332ull
0372 >(x)
0373 / detail::horner<batch_type,
0374 0x3ff0000000000000ull,
0375 0x3fdd0a84eb1ca867ull,
0376 0x3fb64536ca92ea2full,
0377 0x3f8166f75999dbd1ull,
0378 0x3f37ea4332348252ull
0379 >(x);
0380 }
0381
0382
0383
0384 static XSIMD_INLINE batch_type erfc2(const batch_type& x) noexcept
0385 {
0386 return detail::horner<batch_type,
0387 0x3feffffffbbb552bull,
0388 0x3ff54dfe9b258a60ull,
0389 0x3fec1986509e687bull,
0390 0x3fd53dd7a67c7e9full,
0391 0x3fb2488a6b5cb5e5ull,
0392 0x3f7cf4cfe0aacbb4ull,
0393 0x0ull
0394 >(x)
0395 / detail::horner<batch_type,
0396 0x3ff0000000000000ull,
0397 0x4003adeae79b9708ull,
0398 0x40053b1052dca8bdull,
0399 0x3ff9e677c2777c3cull,
0400 0x3fe307622fcff772ull,
0401 0x3fc033c113a7deeeull,
0402 0x3f89a996639b0d00ull
0403 >(x);
0404 }
0405
0406
0407
0408 static XSIMD_INLINE batch_type erfc3(const batch_type& x) noexcept
0409 {
0410 return detail::horner<batch_type,
0411 0x3fefff5a9e697ae2ull,
0412 0x3ff9fa202deb88e5ull,
0413 0x3ff44744306832aeull,
0414 0x3fe29be1cff90d94ull,
0415 0x3fc42210f88b9d43ull,
0416 0x3f971d0907ea7a92ull,
0417 0x0ll
0418 >(x)
0419 / detail::horner<batch_type,
0420 0x3ff0000000000000ull,
0421 0x400602f24bf3fdb6ull,
0422 0x400afd487397568full,
0423 0x400315ffdfd5ce91ull,
0424 0x3ff0cfd4cb6cde9full,
0425 0x3fd1d7ab774bb837ull,
0426 0x3fa47bd61bbb3843ull
0427 >(x);
0428 }
0429
0430
0431
0432 static XSIMD_INLINE batch_type erfc4(const batch_type& x) noexcept
0433 {
0434 return detail::horner<batch_type,
0435 0xbc7e4ad1ec7d0000ll,
0436 0x3fe20dd750429a16ll,
0437 0x3db60000e984b501ll,
0438 0xbfd20dd753ae5dfdll,
0439 0x3e907e71e046a820ll,
0440 0x3fdb1494cac06d39ll,
0441 0x3f34a451701654f1ll,
0442 0xbff105e6b8ef1a63ll,
0443 0x3fb505a857e9ccc8ll,
0444 0x40074fbabc514212ll,
0445 0x4015ac7631f7ac4fll,
0446 0xc0457e03041e9d8bll,
0447 0x4055803d26c4ec4fll,
0448 0xc0505fce04ec4ec5ll
0449 >(x);
0450 }
0451 };
0452 }
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463 template <class A>
0464 XSIMD_INLINE batch<float, A> erf(batch<float, A> const& self, requires_arch<generic>) noexcept
0465 {
0466 using batch_type = batch<float, A>;
0467 batch_type x = abs(self);
0468 batch_type r1(0.);
0469 auto test1 = x < batch_type(2.f / 3.f);
0470 if (any(test1))
0471 {
0472 r1 = self * detail::erf_kernel<batch_type>::erf1(x * x);
0473 if (all(test1))
0474 return r1;
0475 }
0476 batch_type z = x / (batch_type(1.) + x);
0477 z -= batch_type(0.4f);
0478 batch_type r2 = batch_type(1.) - exp(-x * x) * detail::erf_kernel<batch_type>::erfc2(z);
0479 r2 = select(self < batch_type(0.), -r2, r2);
0480 r1 = select(test1, r1, r2);
0481 #ifndef XSIMD_NO_INFINITIES
0482 r1 = select(xsimd::isinf(self), sign(self), r1);
0483 #endif
0484 return r1;
0485 }
0486
0487 template <class A>
0488 XSIMD_INLINE batch<double, A> erf(batch<double, A> const& self, requires_arch<generic>) noexcept
0489 {
0490 using batch_type = batch<double, A>;
0491 batch_type x = abs(self);
0492 batch_type xx = x * x;
0493 batch_type lim1(0.65);
0494 batch_type lim2(2.2);
0495 auto test1 = x < lim1;
0496 batch_type r1(0.);
0497 if (any(test1))
0498 {
0499 r1 = self * detail::erf_kernel<batch_type>::erf1(xx);
0500 if (all(test1))
0501 return r1;
0502 }
0503 auto test2 = x < lim2;
0504 auto test3 = test2 && !test1;
0505 batch_type ex = exp(-xx);
0506 if (any(test3))
0507 {
0508 batch_type z = batch_type(1.) - ex * detail::erf_kernel<batch_type>::erfc2(x);
0509 batch_type r2 = select(self < batch_type(0.), -z, z);
0510 r1 = select(test1, r1, r2);
0511 if (all(test1 || test3))
0512 return r1;
0513 }
0514 batch_type z = batch_type(1.) - ex * detail::erf_kernel<batch_type>::erfc3(x);
0515 z = select(self < batch_type(0.), -z, z);
0516 #ifndef XSIMD_NO_INFINITIES
0517 z = select(xsimd::isinf(self), sign(self), z);
0518 #endif
0519 return select(test2, r1, z);
0520 }
0521
0522
0523 template <class A>
0524 XSIMD_INLINE batch<float, A> erfc(batch<float, A> const& self, requires_arch<generic>) noexcept
0525 {
0526 using batch_type = batch<float, A>;
0527 batch_type x = abs(self);
0528 auto test0 = self < batch_type(0.);
0529 batch_type r1(0.);
0530 auto test1 = 3.f * x < 2.f;
0531 batch_type z = x / (batch_type(1.) + x);
0532 if (any(test1))
0533 {
0534 r1 = detail::erf_kernel<batch_type>::erfc3(z);
0535 if (all(test1))
0536 return select(test0, batch_type(2.) - r1, r1);
0537 }
0538
0539 z -= batch_type(0.4f);
0540 batch_type r2 = exp(-x * x) * detail::erf_kernel<batch_type>::erfc2(z);
0541 r1 = select(test1, r1, r2);
0542 #ifndef XSIMD_NO_INFINITIES
0543 r1 = select(x == constants::infinity<batch_type>(), batch_type(0.), r1);
0544 #endif
0545 return select(test0, batch_type(2.) - r1, r1);
0546 }
0547
0548 template <class A>
0549 XSIMD_INLINE batch<double, A> erfc(batch<double, A> const& self, requires_arch<generic>) noexcept
0550 {
0551 using batch_type = batch<double, A>;
0552 batch_type x = abs(self);
0553 batch_type xx = x * x;
0554 batch_type lim1(0.65);
0555 batch_type lim2(2.2);
0556 auto test0 = self < batch_type(0.);
0557 auto test1 = x < lim1;
0558 batch_type r1(0.);
0559 if (any(test1))
0560 {
0561 r1 = batch_type(1.) - x * detail::erf_kernel<batch_type>::erf1(xx);
0562 if (all(test1))
0563 return select(test0, batch_type(2.) - r1, r1);
0564 }
0565 auto test2 = x < lim2;
0566 auto test3 = test2 && !test1;
0567 batch_type ex = exp(-xx);
0568 if (any(test3))
0569 {
0570 batch_type z = ex * detail::erf_kernel<batch_type>::erfc2(x);
0571 r1 = select(test1, r1, z);
0572 if (all(test1 || test3))
0573 return select(test0, batch_type(2.) - r1, r1);
0574 }
0575 batch_type z = ex * detail::erf_kernel<batch_type>::erfc3(x);
0576 r1 = select(test2, r1, z);
0577 #ifndef XSIMD_NO_INFINITIES
0578 r1 = select(x == constants::infinity<batch_type>(), batch_type(0.), r1);
0579 #endif
0580 return select(test0, batch_type(2.) - r1, r1);
0581 }
0582
0583
0584 namespace detail
0585 {
0586
0587 template <class B>
0588 struct estrin
0589 {
0590 B x;
0591
0592 template <typename... Ts>
0593 XSIMD_INLINE B operator()(const Ts&... coefs) noexcept
0594 {
0595 return eval(coefs...);
0596 }
0597
0598 private:
0599 XSIMD_INLINE B eval(const B& c0) noexcept
0600 {
0601 return c0;
0602 }
0603
0604 XSIMD_INLINE B eval(const B& c0, const B& c1) noexcept
0605 {
0606 return fma(x, c1, c0);
0607 }
0608
0609 template <size_t... Is, class Tuple>
0610 XSIMD_INLINE B eval(::xsimd::detail::index_sequence<Is...>, const Tuple& tuple)
0611 {
0612 return estrin { x * x }(std::get<Is>(tuple)...);
0613 }
0614
0615 template <class... Args>
0616 XSIMD_INLINE B eval(const std::tuple<Args...>& tuple) noexcept
0617 {
0618 return eval(::xsimd::detail::make_index_sequence<sizeof...(Args)>(), tuple);
0619 }
0620
0621 template <class... Args>
0622 XSIMD_INLINE B eval(const std::tuple<Args...>& tuple, const B& c0) noexcept
0623 {
0624 return eval(std::tuple_cat(tuple, std::make_tuple(eval(c0))));
0625 }
0626
0627 template <class... Args>
0628 XSIMD_INLINE B eval(const std::tuple<Args...>& tuple, const B& c0, const B& c1) noexcept
0629 {
0630 return eval(std::tuple_cat(tuple, std::make_tuple(eval(c0, c1))));
0631 }
0632
0633 template <class... Args, class... Ts>
0634 XSIMD_INLINE B eval(const std::tuple<Args...>& tuple, const B& c0, const B& c1, const Ts&... coefs) noexcept
0635 {
0636 return eval(std::tuple_cat(tuple, std::make_tuple(eval(c0, c1))), coefs...);
0637 }
0638
0639 template <class... Ts>
0640 XSIMD_INLINE B eval(const B& c0, const B& c1, const Ts&... coefs) noexcept
0641 {
0642 return eval(std::make_tuple(eval(c0, c1)), coefs...);
0643 }
0644 };
0645 }
0646
0647 template <class T, class A, uint64_t... Coefs>
0648 XSIMD_INLINE batch<T, A> estrin(const batch<T, A>& self) noexcept
0649 {
0650 using batch_type = batch<T, A>;
0651 return detail::estrin<batch_type> { self }(detail::coef<batch_type, Coefs>()...);
0652 }
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664 namespace detail
0665 {
0666 enum exp_reduction_tag
0667 {
0668 exp_tag,
0669 exp2_tag,
0670 exp10_tag
0671 };
0672
0673 template <class B, exp_reduction_tag Tag>
0674 struct exp_reduction_base;
0675
0676 template <class B>
0677 struct exp_reduction_base<B, exp_tag>
0678 {
0679 static constexpr B maxlog() noexcept
0680 {
0681 return constants::maxlog<B>();
0682 }
0683
0684 static constexpr B minlog() noexcept
0685 {
0686 return constants::minlog<B>();
0687 }
0688 };
0689
0690 template <class B>
0691 struct exp_reduction_base<B, exp10_tag>
0692 {
0693 static constexpr B maxlog() noexcept
0694 {
0695 return constants::maxlog10<B>();
0696 }
0697
0698 static constexpr B minlog() noexcept
0699 {
0700 return constants::minlog10<B>();
0701 }
0702 };
0703
0704 template <class B>
0705 struct exp_reduction_base<B, exp2_tag>
0706 {
0707 static constexpr B maxlog() noexcept
0708 {
0709 return constants::maxlog2<B>();
0710 }
0711
0712 static constexpr B minlog() noexcept
0713 {
0714 return constants::minlog2<B>();
0715 }
0716 };
0717
0718 template <class T, class A, exp_reduction_tag Tag>
0719 struct exp_reduction;
0720
0721 template <class A>
0722 struct exp_reduction<float, A, exp_tag> : exp_reduction_base<batch<float, A>, exp_tag>
0723 {
0724 using batch_type = batch<float, A>;
0725 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0726 {
0727 batch_type y = detail::horner<batch_type,
0728 0x3f000000,
0729 0x3e2aa9a5,
0730 0x3d2aa957,
0731 0x3c098d8b,
0732 0x3ab778cf
0733 >(x);
0734 return ++fma(y, x * x, x);
0735 }
0736
0737 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type& x) noexcept
0738 {
0739 batch_type k = nearbyint(constants::invlog_2<batch_type>() * a);
0740 x = fnma(k, constants::log_2hi<batch_type>(), a);
0741 x = fnma(k, constants::log_2lo<batch_type>(), x);
0742 return k;
0743 }
0744 };
0745
0746 template <class A>
0747 struct exp_reduction<float, A, exp10_tag> : exp_reduction_base<batch<float, A>, exp10_tag>
0748 {
0749 using batch_type = batch<float, A>;
0750 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0751 {
0752 return ++(detail::horner<batch_type,
0753 0x40135d8e,
0754 0x4029a926,
0755 0x400237da,
0756 0x3f95eb4c,
0757 0x3f0aacef,
0758 0x3e54dff1
0759 >(x)
0760 * x);
0761 }
0762
0763 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type& x) noexcept
0764 {
0765 batch_type k = nearbyint(constants::invlog10_2<batch_type>() * a);
0766 x = fnma(k, constants::log10_2hi<batch_type>(), a);
0767 x -= k * constants::log10_2lo<batch_type>();
0768 return k;
0769 }
0770 };
0771
0772 template <class A>
0773 struct exp_reduction<float, A, exp2_tag> : exp_reduction_base<batch<float, A>, exp2_tag>
0774 {
0775 using batch_type = batch<float, A>;
0776 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0777 {
0778 batch_type y = detail::horner<batch_type,
0779 0x3e75fdf1,
0780 0x3d6356eb,
0781 0x3c1d9422,
0782 0x3ab01218,
0783 0x3922c8c4
0784 >(x);
0785 return ++fma(y, x * x, x * constants::log_2<batch_type>());
0786 }
0787
0788 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type& x) noexcept
0789 {
0790 batch_type k = nearbyint(a);
0791 x = (a - k);
0792 return k;
0793 }
0794 };
0795
0796 template <class A>
0797 struct exp_reduction<double, A, exp_tag> : exp_reduction_base<batch<double, A>, exp_tag>
0798 {
0799 using batch_type = batch<double, A>;
0800 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0801 {
0802 batch_type t = x * x;
0803 return fnma(t,
0804 detail::horner<batch_type,
0805 0x3fc555555555553eull,
0806 0xbf66c16c16bebd93ull,
0807 0x3f11566aaf25de2cull,
0808 0xbebbbd41c5d26bf1ull,
0809 0x3e66376972bea4d0ull>(t),
0810 x);
0811 }
0812
0813 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type& hi, batch_type& lo, batch_type& x) noexcept
0814 {
0815 batch_type k = nearbyint(constants::invlog_2<batch_type>() * a);
0816 hi = fnma(k, constants::log_2hi<batch_type>(), a);
0817 lo = k * constants::log_2lo<batch_type>();
0818 x = hi - lo;
0819 return k;
0820 }
0821
0822 static XSIMD_INLINE batch_type finalize(const batch_type& x, const batch_type& c, const batch_type& hi, const batch_type& lo) noexcept
0823 {
0824 return batch_type(1.) - (((lo - (x * c) / (batch_type(2.) - c)) - hi));
0825 }
0826 };
0827
0828 template <class A>
0829 struct exp_reduction<double, A, exp10_tag> : exp_reduction_base<batch<double, A>, exp10_tag>
0830 {
0831 using batch_type = batch<double, A>;
0832 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0833 {
0834 batch_type xx = x * x;
0835 batch_type px = x * detail::horner<batch_type, 0x40a2b4798e134a01ull, 0x40796b7a050349e4ull, 0x40277d9474c55934ull, 0x3fa4fd75f3062dd4ull>(xx);
0836 batch_type x2 = px / (detail::horner1<batch_type, 0x40a03f37650df6e2ull, 0x4093e05eefd67782ull, 0x405545fdce51ca08ull>(xx) - px);
0837 return ++(x2 + x2);
0838 }
0839
0840 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type&, batch_type&, batch_type& x) noexcept
0841 {
0842 batch_type k = nearbyint(constants::invlog10_2<batch_type>() * a);
0843 x = fnma(k, constants::log10_2hi<batch_type>(), a);
0844 x = fnma(k, constants::log10_2lo<batch_type>(), x);
0845 return k;
0846 }
0847
0848 static XSIMD_INLINE batch_type finalize(const batch_type&, const batch_type& c, const batch_type&, const batch_type&) noexcept
0849 {
0850 return c;
0851 }
0852 };
0853
0854 template <class A>
0855 struct exp_reduction<double, A, exp2_tag> : exp_reduction_base<batch<double, A>, exp2_tag>
0856 {
0857 using batch_type = batch<double, A>;
0858 static XSIMD_INLINE batch_type approx(const batch_type& x) noexcept
0859 {
0860 batch_type t = x * x;
0861 return fnma(t,
0862 detail::horner<batch_type,
0863 0x3fc555555555553eull,
0864 0xbf66c16c16bebd93ull,
0865 0x3f11566aaf25de2cull,
0866 0xbebbbd41c5d26bf1ull,
0867 0x3e66376972bea4d0ull>(t),
0868 x);
0869 }
0870
0871 static XSIMD_INLINE batch_type reduce(const batch_type& a, batch_type&, batch_type&, batch_type& x) noexcept
0872 {
0873 batch_type k = nearbyint(a);
0874 x = (a - k) * constants::log_2<batch_type>();
0875 return k;
0876 }
0877
0878 static XSIMD_INLINE batch_type finalize(const batch_type& x, const batch_type& c, const batch_type&, const batch_type&) noexcept
0879 {
0880 return batch_type(1.) + x + x * c / (batch_type(2.) - c);
0881 }
0882 };
0883
0884 template <exp_reduction_tag Tag, class A>
0885 XSIMD_INLINE batch<float, A> exp(batch<float, A> const& self) noexcept
0886 {
0887 using batch_type = batch<float, A>;
0888 using reducer_t = exp_reduction<float, A, Tag>;
0889 batch_type x;
0890 batch_type k = reducer_t::reduce(self, x);
0891 x = reducer_t::approx(x);
0892 x = select(self <= reducer_t::minlog(), batch_type(0.), ldexp(x, to_int(k)));
0893 x = select(self >= reducer_t::maxlog(), constants::infinity<batch_type>(), x);
0894 return x;
0895 }
0896
0897 template <exp_reduction_tag Tag, class A>
0898 XSIMD_INLINE batch<double, A> exp(batch<double, A> const& self) noexcept
0899 {
0900 using batch_type = batch<double, A>;
0901 using reducer_t = exp_reduction<double, A, Tag>;
0902 batch_type hi, lo, x;
0903 batch_type k = reducer_t::reduce(self, hi, lo, x);
0904 batch_type c = reducer_t::approx(x);
0905 c = reducer_t::finalize(x, c, hi, lo);
0906 c = select(self <= reducer_t::minlog(), batch_type(0.), ldexp(c, to_int(k)));
0907 c = select(self >= reducer_t::maxlog(), constants::infinity<batch_type>(), c);
0908 return c;
0909 }
0910 }
0911
0912 template <class A, class T>
0913 XSIMD_INLINE batch<T, A> exp(batch<T, A> const& self, requires_arch<generic>) noexcept
0914 {
0915 return detail::exp<detail::exp_tag>(self);
0916 }
0917
0918 template <class A, class T>
0919 XSIMD_INLINE batch<std::complex<T>, A> exp(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
0920 {
0921 using batch_type = batch<std::complex<T>, A>;
0922 auto isincos = sincos(self.imag());
0923 return exp(self.real()) * batch_type(std::get<1>(isincos), std::get<0>(isincos));
0924 }
0925
0926
0927 template <class A, class T>
0928 XSIMD_INLINE batch<T, A> exp10(batch<T, A> const& self, requires_arch<generic>) noexcept
0929 {
0930 return detail::exp<detail::exp10_tag>(self);
0931 }
0932
0933
0934 template <class A, class T>
0935 XSIMD_INLINE batch<T, A> exp2(batch<T, A> const& self, requires_arch<generic>) noexcept
0936 {
0937 return detail::exp<detail::exp2_tag>(self);
0938 }
0939
0940
0941 namespace detail
0942 {
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952 template <class A>
0953 static XSIMD_INLINE batch<float, A> expm1(const batch<float, A>& a) noexcept
0954 {
0955 using batch_type = batch<float, A>;
0956 batch_type k = nearbyint(constants::invlog_2<batch_type>() * a);
0957 batch_type x = fnma(k, constants::log_2hi<batch_type>(), a);
0958 x = fnma(k, constants::log_2lo<batch_type>(), x);
0959 batch_type hx = x * batch_type(0.5);
0960 batch_type hxs = x * hx;
0961 batch_type r = detail::horner<batch_type,
0962 0X3F800000UL,
0963 0XBD08887FUL,
0964 0X3ACF6DB4UL
0965 >(hxs);
0966 batch_type t = fnma(r, hx, batch_type(3.));
0967 batch_type e = hxs * ((r - t) / (batch_type(6.) - x * t));
0968 e = fms(x, e, hxs);
0969 using i_type = as_integer_t<batch_type>;
0970 i_type ik = to_int(k);
0971 batch_type two2mk = ::xsimd::bitwise_cast<float>((constants::maxexponent<batch_type>() - ik) << constants::nmb<batch_type>());
0972 batch_type y = batch_type(1.) - two2mk - (e - x);
0973 return ldexp(y, ik);
0974 }
0975
0976 template <class A>
0977 static XSIMD_INLINE batch<double, A> expm1(const batch<double, A>& a) noexcept
0978 {
0979 using batch_type = batch<double, A>;
0980 batch_type k = nearbyint(constants::invlog_2<batch_type>() * a);
0981 batch_type hi = fnma(k, constants::log_2hi<batch_type>(), a);
0982 batch_type lo = k * constants::log_2lo<batch_type>();
0983 batch_type x = hi - lo;
0984 batch_type hxs = x * x * batch_type(0.5);
0985 batch_type r = detail::horner<batch_type,
0986 0X3FF0000000000000ULL,
0987 0XBFA11111111110F4ULL,
0988 0X3F5A01A019FE5585ULL,
0989 0XBF14CE199EAADBB7ULL,
0990 0X3ED0CFCA86E65239ULL,
0991 0XBE8AFDB76E09C32DULL>(hxs);
0992 batch_type t = batch_type(3.) - r * batch_type(0.5) * x;
0993 batch_type e = hxs * ((r - t) / (batch_type(6) - x * t));
0994 batch_type c = (hi - x) - lo;
0995 e = (x * (e - c) - c) - hxs;
0996 using i_type = as_integer_t<batch_type>;
0997 i_type ik = to_int(k);
0998 batch_type two2mk = ::xsimd::bitwise_cast<double>((constants::maxexponent<batch_type>() - ik) << constants::nmb<batch_type>());
0999 batch_type ct1 = batch_type(1.) - two2mk - (e - x);
1000 batch_type ct2 = ++(x - (e + two2mk));
1001 batch_type y = select(k < batch_type(20.), ct1, ct2);
1002 return ldexp(y, ik);
1003 }
1004
1005 }
1006
1007 template <class A, class T>
1008 XSIMD_INLINE batch<T, A> expm1(batch<T, A> const& self, requires_arch<generic>) noexcept
1009 {
1010 using batch_type = batch<T, A>;
1011 return select(self < constants::logeps<batch_type>(),
1012 batch_type(-1.),
1013 select(self > constants::maxlog<batch_type>(),
1014 constants::infinity<batch_type>(),
1015 detail::expm1(self)));
1016 }
1017
1018 template <class A, class T>
1019 XSIMD_INLINE batch<std::complex<T>, A> expm1(const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
1020 {
1021 using batch_type = batch<std::complex<T>, A>;
1022 using real_batch = typename batch_type::real_batch;
1023 real_batch isin = sin(z.imag());
1024 real_batch rem1 = expm1(z.real());
1025 real_batch re = rem1 + 1.;
1026 real_batch si = sin(z.imag() * 0.5);
1027 return { rem1 - 2. * re * si * si, re * isin };
1028 }
1029
1030
1031 template <class A, class T>
1032 XSIMD_INLINE batch<std::complex<T>, A> polar(const batch<T, A>& r, const batch<T, A>& theta, requires_arch<generic>) noexcept
1033 {
1034 auto sincosTheta = sincos(theta);
1035 return { r * sincosTheta.second, r * sincosTheta.first };
1036 }
1037
1038
1039 template <class A, class T>
1040 XSIMD_INLINE batch<T, A> fdim(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
1041 {
1042 return fmax(batch<T, A>(0), self - other);
1043 }
1044
1045
1046 template <class A, class T>
1047 XSIMD_INLINE batch<T, A> fmod(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
1048 {
1049 return fnma(trunc(self / other), other, self);
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062 template <class A, class T>
1063 XSIMD_INLINE batch<T, A> frexp(const batch<T, A>& self, batch<as_integer_t<T>, A>& exp, requires_arch<generic>) noexcept
1064 {
1065 using batch_type = batch<T, A>;
1066 using int_type = as_integer_t<T>;
1067 using i_type = batch<int_type, A>;
1068 i_type m1f = constants::mask1frexp<batch_type>();
1069 i_type r1 = m1f & ::xsimd::bitwise_cast<int_type>(self);
1070 batch_type x = self & ::xsimd::bitwise_cast<T>(~m1f);
1071 exp = (r1 >> constants::nmb<batch_type>()) - constants::maxexponentm1<batch_type>();
1072 exp = select(batch_bool_cast<typename i_type::value_type>(self != batch_type(0.)), exp, i_type(typename i_type::value_type(0)));
1073 return select((self != batch_type(0.)), x | ::xsimd::bitwise_cast<T>(constants::mask2frexp<batch_type>()), batch_type(0.));
1074 }
1075
1076
1077 template <class A, class T>
1078 XSIMD_INLINE batch<T, A> from_bool(batch_bool<T, A> const& self, requires_arch<generic>) noexcept
1079 {
1080 return batch<T, A>(self.data) & batch<T, A>(1);
1081 }
1082
1083
1084 template <class T, class A, uint64_t... Coefs>
1085 XSIMD_INLINE batch<T, A> horner(const batch<T, A>& self) noexcept
1086 {
1087 return detail::horner<batch<T, A>, Coefs...>(self);
1088 }
1089
1090
1091 template <class A, class T>
1092 XSIMD_INLINE batch<T, A> hypot(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
1093 {
1094 return sqrt(fma(self, self, other * other));
1095 }
1096
1097
1098 template <class A, class T, class ITy>
1099 XSIMD_INLINE batch<T, A> ipow(batch<T, A> const& self, ITy other, requires_arch<generic>) noexcept
1100 {
1101 return ::xsimd::detail::ipow(self, other);
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114 template <class A, class T>
1115 XSIMD_INLINE batch<T, A> ldexp(const batch<T, A>& self, const batch<as_integer_t<T>, A>& other, requires_arch<generic>) noexcept
1116 {
1117 using batch_type = batch<T, A>;
1118 using itype = as_integer_t<batch_type>;
1119 itype ik = other + constants::maxexponent<T>();
1120 ik = ik << constants::nmb<T>();
1121 return self * ::xsimd::bitwise_cast<T>(ik);
1122 }
1123
1124
1125 template <class A, class T>
1126 XSIMD_INLINE batch<T, A> lgamma(batch<T, A> const& self, requires_arch<generic>) noexcept;
1127
1128 namespace detail
1129 {
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 template <class A>
1140 static XSIMD_INLINE batch<float, A> gammalnB(const batch<float, A>& x) noexcept
1141 {
1142 return horner<batch<float, A>,
1143 0x3ed87730,
1144 0x3ea51a64,
1145 0xbd89f07e,
1146 0x3ca89ed8,
1147 0xbbf164fd,
1148 0x3b3ba883,
1149 0xbaabeab1,
1150 0x3a1ebb94
1151 >(x);
1152 }
1153
1154 template <class A>
1155 static XSIMD_INLINE batch<float, A> gammalnC(const batch<float, A>& x) noexcept
1156 {
1157 return horner<batch<float, A>,
1158 0xbf13c468,
1159 0x3f528d34,
1160 0xbecd27a8,
1161 0x3e8a898b,
1162 0xbe53c04f,
1163 0x3e2d4dab,
1164 0xbe22d329,
1165 0x3e0c3c4f
1166 >(x);
1167 }
1168
1169 template <class A>
1170 static XSIMD_INLINE batch<float, A> gammaln2(const batch<float, A>& x) noexcept
1171 {
1172 return horner<batch<float, A>,
1173 0x3daaaa94,
1174 0xbb358701,
1175 0x3a31fd69
1176 >(x);
1177 }
1178
1179 template <class A>
1180 static XSIMD_INLINE batch<double, A> gammaln1(const batch<double, A>& x) noexcept
1181 {
1182 return horner<batch<double, A>,
1183 0xc12a0c675418055eull,
1184 0xc13a45890219f20bull,
1185 0xc131bc82f994db51ull,
1186 0xc1143d73f89089e5ull,
1187 0xc0e2f234355bb93eull,
1188 0xc09589018ff36761ull
1189 >(x)
1190 / horner<batch<double, A>,
1191 0xc13ece4b6a11e14aull,
1192 0xc1435255892ff34cull,
1193 0xc131628671950043ull,
1194 0xc10aeb84b9744c9bull,
1195 0xc0d0aa0d7b89d757ull,
1196 0xc075fd0d1cf312b2ull,
1197 0x3ff0000000000000ull
1198 >(x);
1199 }
1200
1201 template <class A>
1202 static XSIMD_INLINE batch<double, A> gammalnA(const batch<double, A>& x) noexcept
1203 {
1204 return horner<batch<double, A>,
1205 0x3fb555555555554bull,
1206 0xbf66c16c16b0a5a1ull,
1207 0x3f4a019f20dc5ebbull,
1208 0xbf437fbdb580e943ull,
1209 0x3f4a985027336661ull
1210 >(x);
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222 template <class B>
1223 struct lgamma_impl;
1224
1225 template <class A>
1226 struct lgamma_impl<batch<float, A>>
1227 {
1228 using batch_type = batch<float, A>;
1229 static XSIMD_INLINE batch_type compute(const batch_type& a) noexcept
1230 {
1231 auto inf_result = (a <= batch_type(0.)) && is_flint(a);
1232 batch_type x = select(inf_result, constants::nan<batch_type>(), a);
1233 batch_type q = abs(x);
1234 #ifndef XSIMD_NO_INFINITIES
1235 inf_result = (x == constants::infinity<batch_type>()) || inf_result;
1236 #endif
1237 auto ltza = a < batch_type(0.);
1238 batch_type r(0);
1239 batch_type r1 = other(q);
1240 if (any(ltza))
1241 {
1242 r = select(inf_result, constants::infinity<batch_type>(), negative(q, r1));
1243 if (all(ltza))
1244 return r;
1245 }
1246 batch_type r2 = select(ltza, r, r1);
1247 return select(a == constants::minusinfinity<batch_type>(), constants::nan<batch_type>(), select(inf_result, constants::infinity<batch_type>(), r2));
1248 }
1249
1250 private:
1251 static XSIMD_INLINE batch_type negative(const batch_type& q, const batch_type& w) noexcept
1252 {
1253 batch_type p = floor(q);
1254 batch_type z = q - p;
1255 auto test2 = z < batch_type(0.5);
1256 z = select(test2, z - batch_type(1.), z);
1257 z = q * sin(z, trigo_pi_tag());
1258 return -log(constants::invpi<batch_type>() * abs(z)) - w;
1259 }
1260
1261 static XSIMD_INLINE batch_type other(const batch_type& x) noexcept
1262 {
1263 auto xlt650 = (x < batch_type(6.5));
1264 batch_type r0x = x;
1265 batch_type r0z = x;
1266 batch_type r0s = batch_type(1.);
1267 batch_type r1 = batch_type(0.);
1268 batch_type p = constants::nan<batch_type>();
1269 if (any(xlt650))
1270 {
1271 batch_type z = batch_type(1.);
1272 batch_type tx = select(xlt650, x, batch_type(0.));
1273 batch_type nx = batch_type(0.);
1274 const batch_type _075 = batch_type(0.75);
1275 const batch_type _150 = batch_type(1.50);
1276 const batch_type _125 = batch_type(1.25);
1277 const batch_type _250 = batch_type(2.50);
1278 auto xge150 = (x >= _150);
1279 auto txgt250 = (tx > _250);
1280
1281
1282 while (any(xge150 && txgt250))
1283 {
1284 nx = select(txgt250, nx - batch_type(1.), nx);
1285 tx = select(txgt250, x + nx, tx);
1286 z = select(txgt250, z * tx, z);
1287 txgt250 = (tx > _250);
1288 }
1289 r0x = select(xge150, x + nx - batch_type(2.), x);
1290 r0z = select(xge150, z, r0z);
1291 r0s = select(xge150, batch_type(1.), r0s);
1292
1293
1294 auto xge125 = (x >= _125);
1295 auto xge125t = xge125 && !xge150;
1296 if (any(xge125))
1297 {
1298 r0x = select(xge125t, x - batch_type(1.), r0x);
1299 r0z = select(xge125t, z * x, r0z);
1300 r0s = select(xge125t, batch_type(-1.), r0s);
1301 }
1302
1303
1304 batch_bool<float, A> kernelC(false);
1305 auto xge075 = (x >= _075);
1306 auto xge075t = xge075 && !xge125;
1307 if (any(xge075t))
1308 {
1309 kernelC = xge075t;
1310 r0x = select(xge075t, x - batch_type(1.), x);
1311 r0z = select(xge075t, batch_type(1.), r0z);
1312 r0s = select(xge075t, batch_type(-1.), r0s);
1313 p = gammalnC(r0x);
1314 }
1315
1316
1317 auto txlt150 = (tx < _150) && !xge075;
1318 if (any(txlt150))
1319 {
1320 auto orig = txlt150;
1321 while (any(txlt150))
1322 {
1323 z = select(txlt150, z * tx, z);
1324 nx = select(txlt150, nx + batch_type(1.), nx);
1325 tx = select(txlt150, x + nx, tx);
1326 txlt150 = (tx < _150) && !xge075;
1327 }
1328 r0x = select(orig, r0x + nx - batch_type(2.), r0x);
1329 r0z = select(orig, z, r0z);
1330 r0s = select(orig, batch_type(-1.), r0s);
1331 }
1332 p = select(kernelC, p, gammalnB(r0x));
1333 if (all(xlt650))
1334 return fma(r0x, p, r0s * log(abs(r0z)));
1335 }
1336 r0z = select(xlt650, abs(r0z), x);
1337 batch_type m = log(r0z);
1338 r1 = fma(r0x, p, r0s * m);
1339 batch_type r2 = fma(x - batch_type(0.5), m, constants::logsqrt2pi<batch_type>() - x);
1340 r2 += gammaln2(batch_type(1.) / (x * x)) / x;
1341 return select(xlt650, r1, r2);
1342 }
1343 };
1344
1345 template <class A>
1346 struct lgamma_impl<batch<double, A>>
1347 {
1348 using batch_type = batch<double, A>;
1349
1350 static XSIMD_INLINE batch_type compute(const batch_type& a) noexcept
1351 {
1352 auto inf_result = (a <= batch_type(0.)) && is_flint(a);
1353 batch_type x = select(inf_result, constants::nan<batch_type>(), a);
1354 batch_type q = abs(x);
1355 #ifndef XSIMD_NO_INFINITIES
1356 inf_result = (q == constants::infinity<batch_type>());
1357 #endif
1358 auto test = (a < batch_type(-34.));
1359 batch_type r = constants::nan<batch_type>();
1360 if (any(test))
1361 {
1362 r = large_negative(q);
1363 if (all(test))
1364 return select(inf_result, constants::nan<batch_type>(), r);
1365 }
1366 batch_type r1 = other(a);
1367 batch_type r2 = select(test, r, r1);
1368 return select(a == constants::minusinfinity<batch_type>(), constants::nan<batch_type>(), select(inf_result, constants::infinity<batch_type>(), r2));
1369 }
1370
1371 private:
1372
1373
1374 static inline batch_type large_negative(const batch_type& q) noexcept
1375 {
1376 batch_type w = lgamma(q);
1377 batch_type p = floor(q);
1378 batch_type z = q - p;
1379 auto test2 = (z < batch_type(0.5));
1380 z = select(test2, z - batch_type(1.), z);
1381 z = q * sin(z, trigo_pi_tag());
1382 z = abs(z);
1383 return constants::logpi<batch_type>() - log(z) - w;
1384 }
1385
1386 static XSIMD_INLINE batch_type other(const batch_type& xx) noexcept
1387 {
1388 batch_type x = xx;
1389 auto test = (x < batch_type(13.));
1390 batch_type r1 = batch_type(0.);
1391 if (any(test))
1392 {
1393 batch_type z = batch_type(1.);
1394 batch_type p = batch_type(0.);
1395 batch_type u = select(test, x, batch_type(0.));
1396 auto test1 = (u >= batch_type(3.));
1397 while (any(test1))
1398 {
1399 p = select(test1, p - batch_type(1.), p);
1400 u = select(test1, x + p, u);
1401 z = select(test1, z * u, z);
1402 test1 = (u >= batch_type(3.));
1403 }
1404
1405 auto test2 = (u < batch_type(2.));
1406 while (any(test2))
1407 {
1408 z = select(test2, z / u, z);
1409 p = select(test2, p + batch_type(1.), p);
1410 u = select(test2, x + p, u);
1411 test2 = (u < batch_type(2.));
1412 }
1413
1414 z = abs(z);
1415 x += p - batch_type(2.);
1416 r1 = x * gammaln1(x) + log(z);
1417 if (all(test))
1418 return r1;
1419 }
1420 batch_type r2 = fma(xx - batch_type(0.5), log(xx), constants::logsqrt2pi<batch_type>() - xx);
1421 batch_type p = batch_type(1.) / (xx * xx);
1422 r2 += gammalnA(p) / xx;
1423 return select(test, r1, r2);
1424 }
1425 };
1426 }
1427
1428 template <class A, class T>
1429 XSIMD_INLINE batch<T, A> lgamma(batch<T, A> const& self, requires_arch<generic>) noexcept
1430 {
1431 return detail::lgamma_impl<batch<T, A>>::compute(self);
1432 }
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444 template <class A>
1445 XSIMD_INLINE batch<float, A> log(batch<float, A> const& self, requires_arch<generic>) noexcept
1446 {
1447 using batch_type = batch<float, A>;
1448 using int_type = as_integer_t<float>;
1449 using i_type = batch<int_type, A>;
1450 batch_type x = self;
1451 i_type k(0);
1452 auto isnez = (self != batch_type(0.));
1453 #ifndef XSIMD_NO_DENORMALS
1454 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1455 if (any(test))
1456 {
1457 k = select(batch_bool_cast<int_type>(test), k - i_type(23), k);
1458 x = select(test, x * batch_type(8388608ul), x);
1459 }
1460 #endif
1461 i_type ix = ::xsimd::bitwise_cast<int_type>(x);
1462 ix += 0x3f800000 - 0x3f3504f3;
1463 k += (ix >> 23) - 0x7f;
1464 ix = (ix & i_type(0x007fffff)) + 0x3f3504f3;
1465 x = ::xsimd::bitwise_cast<float>(ix);
1466 batch_type f = --x;
1467 batch_type s = f / (batch_type(2.) + f);
1468 batch_type z = s * s;
1469 batch_type w = z * z;
1470 batch_type t1 = w * detail::horner<batch_type, 0x3eccce13, 0x3e789e26>(w);
1471 batch_type t2 = z * detail::horner<batch_type, 0x3f2aaaaa, 0x3e91e9ee>(w);
1472 batch_type R = t2 + t1;
1473 batch_type hfsq = batch_type(0.5) * f * f;
1474 batch_type dk = to_float(k);
1475 batch_type r = fma(dk, constants::log_2hi<batch_type>(), fma(s, (hfsq + R), dk * constants::log_2lo<batch_type>()) - hfsq + f);
1476 #ifndef XSIMD_NO_INFINITIES
1477 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1478 #else
1479 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1480 #endif
1481 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1482 }
1483
1484 template <class A>
1485 XSIMD_INLINE batch<double, A> log(batch<double, A> const& self, requires_arch<generic>) noexcept
1486 {
1487 using batch_type = batch<double, A>;
1488 using int_type = as_integer_t<double>;
1489 using i_type = batch<int_type, A>;
1490
1491 batch_type x = self;
1492 i_type hx = ::xsimd::bitwise_cast<int_type>(x) >> 32;
1493 i_type k(0);
1494 auto isnez = (self != batch_type(0.));
1495 #ifndef XSIMD_NO_DENORMALS
1496 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1497 if (any(test))
1498 {
1499 k = select(batch_bool_cast<int_type>(test), k - i_type(54), k);
1500 x = select(test, x * batch_type(18014398509481984ull), x);
1501 }
1502 #endif
1503 hx += 0x3ff00000 - 0x3fe6a09e;
1504 k += (hx >> 20) - 0x3ff;
1505 batch_type dk = to_float(k);
1506 hx = (hx & i_type(0x000fffff)) + 0x3fe6a09e;
1507 x = ::xsimd::bitwise_cast<double>(hx << 32 | (i_type(0xffffffff) & ::xsimd::bitwise_cast<int_type>(x)));
1508
1509 batch_type f = --x;
1510 batch_type hfsq = batch_type(0.5) * f * f;
1511 batch_type s = f / (batch_type(2.) + f);
1512 batch_type z = s * s;
1513 batch_type w = z * z;
1514
1515 batch_type t1 = w * detail::horner<batch_type, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll>(w);
1516 batch_type t2 = z * detail::horner<batch_type, 0x3fe5555555555593ll, 0x3fd2492494229359ll, 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll>(w);
1517 batch_type R = t2 + t1;
1518 batch_type r = fma(dk, constants::log_2hi<batch_type>(), fma(s, (hfsq + R), dk * constants::log_2lo<batch_type>()) - hfsq + f);
1519 #ifndef XSIMD_NO_INFINITIES
1520 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1521 #else
1522 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1523 #endif
1524 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1525 }
1526
1527 template <class A, class T>
1528 XSIMD_INLINE batch<std::complex<T>, A> log(const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
1529 {
1530 return batch<std::complex<T>, A>(log(abs(z)), atan2(z.imag(), z.real()));
1531 }
1532
1533
1534 template <class A>
1535 XSIMD_INLINE batch<float, A> log2(batch<float, A> const& self, requires_arch<generic>) noexcept
1536 {
1537 using batch_type = batch<float, A>;
1538 using int_type = as_integer_t<float>;
1539 using i_type = batch<int_type, A>;
1540 batch_type x = self;
1541 i_type k(0);
1542 auto isnez = (self != batch_type(0.));
1543 #ifndef XSIMD_NO_DENORMALS
1544 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1545 if (any(test))
1546 {
1547 k = select(batch_bool_cast<int_type>(test), k - i_type(25), k);
1548 x = select(test, x * batch_type(33554432ul), x);
1549 }
1550 #endif
1551 i_type ix = ::xsimd::bitwise_cast<int_type>(x);
1552 ix += 0x3f800000 - 0x3f3504f3;
1553 k += (ix >> 23) - 0x7f;
1554 ix = (ix & i_type(0x007fffff)) + 0x3f3504f3;
1555 x = ::xsimd::bitwise_cast<float>(ix);
1556 batch_type f = --x;
1557 batch_type s = f / (batch_type(2.) + f);
1558 batch_type z = s * s;
1559 batch_type w = z * z;
1560 batch_type t1 = w * detail::horner<batch_type, 0x3eccce13, 0x3e789e26>(w);
1561 batch_type t2 = z * detail::horner<batch_type, 0x3f2aaaaa, 0x3e91e9ee>(w);
1562 batch_type R = t1 + t2;
1563 batch_type hfsq = batch_type(0.5) * f * f;
1564 batch_type dk = to_float(k);
1565 batch_type r = fma(fms(s, hfsq + R, hfsq) + f, constants::invlog_2<batch_type>(), dk);
1566 #ifndef XSIMD_NO_INFINITIES
1567 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1568 #else
1569 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1570 #endif
1571 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1572 }
1573
1574 template <class A>
1575 XSIMD_INLINE batch<double, A> log2(batch<double, A> const& self, requires_arch<generic>) noexcept
1576 {
1577 using batch_type = batch<double, A>;
1578 using int_type = as_integer_t<double>;
1579 using i_type = batch<int_type, A>;
1580 batch_type x = self;
1581 i_type hx = ::xsimd::bitwise_cast<int_type>(x) >> 32;
1582 i_type k(0);
1583 auto isnez = (self != batch_type(0.));
1584 #ifndef XSIMD_NO_DENORMALS
1585 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1586 if (any(test))
1587 {
1588 k = select(batch_bool_cast<typename i_type::value_type>(test), k - i_type(54), k);
1589 x = select(test, x * batch_type(18014398509481984ull), x);
1590 }
1591 #endif
1592 hx += 0x3ff00000 - 0x3fe6a09e;
1593 k += (hx >> 20) - 0x3ff;
1594 hx = (hx & i_type(0x000fffff)) + 0x3fe6a09e;
1595 x = ::xsimd::bitwise_cast<double>(hx << 32 | (i_type(0xffffffff) & ::xsimd::bitwise_cast<int_type>(x)));
1596 batch_type f = --x;
1597 batch_type s = f / (batch_type(2.) + f);
1598 batch_type z = s * s;
1599 batch_type w = z * z;
1600 batch_type t1 = w * detail::horner<batch_type, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll>(w);
1601 batch_type t2 = z * detail::horner<batch_type, 0x3fe5555555555593ll, 0x3fd2492494229359ll, 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll>(w);
1602 batch_type R = t2 + t1;
1603 batch_type hfsq = batch_type(0.5) * f * f;
1604 batch_type hi = f - hfsq;
1605 hi = hi & ::xsimd::bitwise_cast<double>((constants::allbits<i_type>() << 32));
1606 batch_type lo = fma(s, hfsq + R, f - hi - hfsq);
1607 batch_type val_hi = hi * constants::invlog_2hi<batch_type>();
1608 batch_type val_lo = fma(lo + hi, constants::invlog_2lo<batch_type>(), lo * constants::invlog_2hi<batch_type>());
1609 batch_type dk = to_float(k);
1610 batch_type w1 = dk + val_hi;
1611 val_lo += (dk - w1) + val_hi;
1612 val_hi = w1;
1613 batch_type r = val_lo + val_hi;
1614 #ifndef XSIMD_NO_INFINITIES
1615 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1616 #else
1617 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1618 #endif
1619 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1620 }
1621
1622 namespace detail
1623 {
1624 template <class T, class A>
1625 XSIMD_INLINE batch<T, A> logN_complex_impl(const batch<T, A>& z, typename batch<T, A>::value_type base) noexcept
1626 {
1627 using batch_type = batch<T, A>;
1628 using rv_type = typename batch_type::value_type;
1629 return log(z) / batch_type(rv_type(base));
1630 }
1631 }
1632
1633 template <class A, class T>
1634 XSIMD_INLINE batch<std::complex<T>, A> log2(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
1635 {
1636 return detail::logN_complex_impl(self, std::log(2));
1637 }
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651 template <class A>
1652 XSIMD_INLINE batch<float, A> log10(batch<float, A> const& self, requires_arch<generic>) noexcept
1653 {
1654 using batch_type = batch<float, A>;
1655 const batch_type
1656 ivln10hi(4.3432617188e-01f),
1657 ivln10lo(-3.1689971365e-05f),
1658 log10_2hi(3.0102920532e-01f),
1659 log10_2lo(7.9034151668e-07f);
1660 using int_type = as_integer_t<float>;
1661 using i_type = batch<int_type, A>;
1662 batch_type x = self;
1663 i_type k(0);
1664 auto isnez = (self != batch_type(0.));
1665 #ifndef XSIMD_NO_DENORMALS
1666 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1667 if (any(test))
1668 {
1669 k = select(batch_bool_cast<int_type>(test), k - i_type(25), k);
1670 x = select(test, x * batch_type(33554432ul), x);
1671 }
1672 #endif
1673 i_type ix = ::xsimd::bitwise_cast<int_type>(x);
1674 ix += 0x3f800000 - 0x3f3504f3;
1675 k += (ix >> 23) - 0x7f;
1676 ix = (ix & i_type(0x007fffff)) + 0x3f3504f3;
1677 x = ::xsimd::bitwise_cast<float>(ix);
1678 batch_type f = --x;
1679 batch_type s = f / (batch_type(2.) + f);
1680 batch_type z = s * s;
1681 batch_type w = z * z;
1682 batch_type t1 = w * detail::horner<batch_type, 0x3eccce13, 0x3e789e26>(w);
1683 batch_type t2 = z * detail::horner<batch_type, 0x3f2aaaaa, 0x3e91e9ee>(w);
1684 batch_type R = t2 + t1;
1685 batch_type dk = to_float(k);
1686 batch_type hfsq = batch_type(0.5) * f * f;
1687 batch_type hibits = f - hfsq;
1688 hibits &= ::xsimd::bitwise_cast<float>(i_type(0xfffff000));
1689 batch_type lobits = fma(s, hfsq + R, f - hibits - hfsq);
1690 batch_type r = fma(dk, log10_2hi,
1691 fma(hibits, ivln10hi,
1692 fma(lobits, ivln10hi,
1693 fma(lobits + hibits, ivln10lo, dk * log10_2lo))));
1694 #ifndef XSIMD_NO_INFINITIES
1695 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1696 #else
1697 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1698 #endif
1699 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1700 }
1701
1702 template <class A>
1703 XSIMD_INLINE batch<double, A> log10(batch<double, A> const& self, requires_arch<generic>) noexcept
1704 {
1705 using batch_type = batch<double, A>;
1706 const batch_type
1707 ivln10hi(4.34294481878168880939e-01),
1708 ivln10lo(2.50829467116452752298e-11),
1709 log10_2hi(3.01029995663611771306e-01),
1710 log10_2lo(3.69423907715893078616e-13);
1711 using int_type = as_integer_t<double>;
1712 using i_type = batch<int_type, A>;
1713 batch_type x = self;
1714 i_type hx = ::xsimd::bitwise_cast<int_type>(x) >> 32;
1715 i_type k(0);
1716 auto isnez = (self != batch_type(0.));
1717 #ifndef XSIMD_NO_DENORMALS
1718 auto test = (self < constants::smallestposval<batch_type>()) && isnez;
1719 if (any(test))
1720 {
1721 k = select(batch_bool_cast<int_type>(test), k - i_type(54), k);
1722 x = select(test, x * batch_type(18014398509481984ull), x);
1723 }
1724 #endif
1725 hx += 0x3ff00000 - 0x3fe6a09e;
1726 k += (hx >> 20) - 0x3ff;
1727 hx = (hx & i_type(0x000fffff)) + 0x3fe6a09e;
1728 x = ::xsimd::bitwise_cast<double>(hx << 32 | (i_type(0xffffffff) & ::xsimd::bitwise_cast<int_type>(x)));
1729 batch_type f = --x;
1730 batch_type dk = to_float(k);
1731 batch_type s = f / (batch_type(2.) + f);
1732 batch_type z = s * s;
1733 batch_type w = z * z;
1734 batch_type t1 = w * detail::horner<batch_type, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll>(w);
1735 batch_type t2 = z * detail::horner<batch_type, 0x3fe5555555555593ll, 0x3fd2492494229359ll, 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll>(w);
1736 batch_type R = t2 + t1;
1737 batch_type hfsq = batch_type(0.5) * f * f;
1738 batch_type hi = f - hfsq;
1739 hi = hi & ::xsimd::bitwise_cast<double>(constants::allbits<i_type>() << 32);
1740 batch_type lo = f - hi - hfsq + s * (hfsq + R);
1741 batch_type val_hi = hi * ivln10hi;
1742 batch_type y = dk * log10_2hi;
1743 batch_type val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;
1744 batch_type w1 = y + val_hi;
1745 val_lo += (y - w1) + val_hi;
1746 val_hi = w1;
1747 batch_type r = val_lo + val_hi;
1748 #ifndef XSIMD_NO_INFINITIES
1749 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1750 #else
1751 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1752 #endif
1753 return select(!(self >= batch_type(0.)), constants::nan<batch_type>(), zz);
1754 }
1755
1756 template <class A, class T>
1757 XSIMD_INLINE batch<std::complex<T>, A> log10(const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
1758 {
1759 return detail::logN_complex_impl(z, std::log(10));
1760 }
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772 template <class A>
1773 XSIMD_INLINE batch<float, A> log1p(batch<float, A> const& self, requires_arch<generic>) noexcept
1774 {
1775 using batch_type = batch<float, A>;
1776 using int_type = as_integer_t<float>;
1777 using i_type = batch<int_type, A>;
1778 const batch_type uf = self + batch_type(1.);
1779 auto isnez = (uf != batch_type(0.));
1780 i_type iu = ::xsimd::bitwise_cast<int_type>(uf);
1781 iu += 0x3f800000 - 0x3f3504f3;
1782 i_type k = (iu >> 23) - 0x7f;
1783 iu = (iu & i_type(0x007fffff)) + 0x3f3504f3;
1784 batch_type f = --(::xsimd::bitwise_cast<float>(iu));
1785 batch_type s = f / (batch_type(2.) + f);
1786 batch_type z = s * s;
1787 batch_type w = z * z;
1788 batch_type t1 = w * detail::horner<batch_type, 0x3eccce13, 0x3e789e26>(w);
1789 batch_type t2 = z * detail::horner<batch_type, 0x3f2aaaaa, 0x3e91e9ee>(w);
1790 batch_type R = t2 + t1;
1791 batch_type hfsq = batch_type(0.5) * f * f;
1792 batch_type dk = to_float(k);
1793
1794 batch_type c = select(batch_bool_cast<float>(k >= i_type(2)), batch_type(1.) - (uf - self), self - (uf - batch_type(1.))) / uf;
1795 batch_type r = fma(dk, constants::log_2hi<batch_type>(), fma(s, (hfsq + R), dk * constants::log_2lo<batch_type>() + c) - hfsq + f);
1796 #ifndef XSIMD_NO_INFINITIES
1797 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1798 #else
1799 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1800 #endif
1801 return select(!(uf >= batch_type(0.)), constants::nan<batch_type>(), zz);
1802 }
1803
1804 template <class A>
1805 XSIMD_INLINE batch<double, A> log1p(batch<double, A> const& self, requires_arch<generic>) noexcept
1806 {
1807 using batch_type = batch<double, A>;
1808 using int_type = as_integer_t<double>;
1809 using i_type = batch<int_type, A>;
1810 const batch_type uf = self + batch_type(1.);
1811 auto isnez = (uf != batch_type(0.));
1812 i_type hu = ::xsimd::bitwise_cast<int_type>(uf) >> 32;
1813 hu += 0x3ff00000 - 0x3fe6a09e;
1814 i_type k = (hu >> 20) - 0x3ff;
1815
1816 batch_type c = select(batch_bool_cast<double>(k >= i_type(2)), batch_type(1.) - (uf - self), self - (uf - batch_type(1.))) / uf;
1817 hu = (hu & i_type(0x000fffff)) + 0x3fe6a09e;
1818 batch_type f = ::xsimd::bitwise_cast<double>((hu << 32) | (i_type(0xffffffff) & ::xsimd::bitwise_cast<int_type>(uf)));
1819 f = --f;
1820 batch_type hfsq = batch_type(0.5) * f * f;
1821 batch_type s = f / (batch_type(2.) + f);
1822 batch_type z = s * s;
1823 batch_type w = z * z;
1824 batch_type t1 = w * detail::horner<batch_type, 0x3fd999999997fa04ll, 0x3fcc71c51d8e78afll, 0x3fc39a09d078c69fll>(w);
1825 batch_type t2 = z * detail::horner<batch_type, 0x3fe5555555555593ll, 0x3fd2492494229359ll, 0x3fc7466496cb03dell, 0x3fc2f112df3e5244ll>(w);
1826 batch_type R = t2 + t1;
1827 batch_type dk = to_float(k);
1828 batch_type r = fma(dk, constants::log_2hi<batch_type>(), fma(s, hfsq + R, dk * constants::log_2lo<batch_type>() + c) - hfsq + f);
1829 #ifndef XSIMD_NO_INFINITIES
1830 batch_type zz = select(isnez, select(self == constants::infinity<batch_type>(), constants::infinity<batch_type>(), r), constants::minusinfinity<batch_type>());
1831 #else
1832 batch_type zz = select(isnez, r, constants::minusinfinity<batch_type>());
1833 #endif
1834 return select(!(uf >= batch_type(0.)), constants::nan<batch_type>(), zz);
1835 }
1836
1837 template <class A, class T>
1838 XSIMD_INLINE batch<std::complex<T>, A> log1p(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
1839 {
1840 using batch_type = batch<std::complex<T>, A>;
1841 using real_batch = typename batch_type::real_batch;
1842 batch_type u = 1 + self;
1843 batch_type logu = log(u);
1844 return select(u == batch_type(1.),
1845 self,
1846 select(u.real() <= real_batch(0.),
1847 logu,
1848 logu * self / (u - batch_type(1.))));
1849 }
1850
1851
1852 template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
1853 XSIMD_INLINE batch<T, A> mod(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
1854 {
1855 return detail::apply([](T x, T y) noexcept -> T
1856 { return x % y; },
1857 self, other);
1858 }
1859
1860
1861 template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
1862 XSIMD_INLINE batch<T, A> nearbyint(batch<T, A> const& self, requires_arch<generic>) noexcept
1863 {
1864 return self;
1865 }
1866 namespace detail
1867 {
1868 template <class A, class T>
1869 XSIMD_INLINE batch<T, A> nearbyintf(batch<T, A> const& self) noexcept
1870 {
1871 using batch_type = batch<T, A>;
1872 batch_type s = bitofsign(self);
1873 batch_type v = self ^ s;
1874 batch_type t2n = constants::twotonmb<batch_type>();
1875
1876
1877
1878 #ifdef __FAST_MATH__
1879 volatile batch_type d0 = v + t2n;
1880 batch_type d = *(batch_type*)(void*)(&d0) - t2n;
1881 #else
1882 batch_type d0 = v + t2n;
1883 batch_type d = d0 - t2n;
1884 #endif
1885 return s ^ select(v < t2n, d, v);
1886 }
1887 }
1888 template <class A>
1889 XSIMD_INLINE batch<float, A> nearbyint(batch<float, A> const& self, requires_arch<generic>) noexcept
1890 {
1891 return detail::nearbyintf(self);
1892 }
1893 template <class A>
1894 XSIMD_INLINE batch<double, A> nearbyint(batch<double, A> const& self, requires_arch<generic>) noexcept
1895 {
1896 return detail::nearbyintf(self);
1897 }
1898
1899
1900 template <class T, class A, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
1901 XSIMD_INLINE batch<T, A> nearbyint_as_int(batch<T, A> const& self, requires_arch<generic>) noexcept
1902 {
1903 return self;
1904 }
1905
1906
1907 template <class A>
1908 XSIMD_INLINE batch<as_integer_t<float>, A>
1909 nearbyint_as_int(batch<float, A> const& self, requires_arch<generic>) noexcept
1910 {
1911 using U = as_integer_t<float>;
1912 return kernel::detail::apply_transform<U>([](float x) noexcept -> U
1913 { return std::nearbyintf(x); },
1914 self);
1915 }
1916
1917 template <class A>
1918 XSIMD_INLINE batch<as_integer_t<double>, A>
1919 nearbyint_as_int(batch<double, A> const& self, requires_arch<generic>) noexcept
1920 {
1921 using U = as_integer_t<double>;
1922 return kernel::detail::apply_transform<U>([](double x) noexcept -> U
1923 { return std::nearbyint(x); },
1924 self);
1925 }
1926
1927
1928 namespace detail
1929 {
1930 template <class T, class A, bool is_int = std::is_integral<T>::value>
1931 struct nextafter_kernel
1932 {
1933 using batch_type = batch<T, A>;
1934
1935 static XSIMD_INLINE batch_type next(batch_type const& b) noexcept
1936 {
1937 return b;
1938 }
1939
1940 static XSIMD_INLINE batch_type prev(batch_type const& b) noexcept
1941 {
1942 return b;
1943 }
1944 };
1945
1946 template <class T, class A>
1947 struct bitwise_cast_batch;
1948
1949 template <class A>
1950 struct bitwise_cast_batch<float, A>
1951 {
1952 using type = batch<int32_t, A>;
1953 };
1954
1955 template <class A>
1956 struct bitwise_cast_batch<double, A>
1957 {
1958 using type = batch<int64_t, A>;
1959 };
1960
1961 template <class T, class A>
1962 struct nextafter_kernel<T, A, false>
1963 {
1964 using batch_type = batch<T, A>;
1965 using int_batch = typename bitwise_cast_batch<T, A>::type;
1966 using int_type = typename int_batch::value_type;
1967
1968 static XSIMD_INLINE batch_type next(const batch_type& b) noexcept
1969 {
1970 batch_type n = ::xsimd::bitwise_cast<T>(::xsimd::bitwise_cast<int_type>(b) + int_type(1));
1971 return select(b == constants::infinity<batch_type>(), b, n);
1972 }
1973
1974 static XSIMD_INLINE batch_type prev(const batch_type& b) noexcept
1975 {
1976 batch_type p = ::xsimd::bitwise_cast<T>(::xsimd::bitwise_cast<int_type>(b) - int_type(1));
1977 return select(b == constants::minusinfinity<batch_type>(), b, p);
1978 }
1979 };
1980 }
1981 template <class A, class T>
1982 XSIMD_INLINE batch<T, A> nextafter(batch<T, A> const& from, batch<T, A> const& to, requires_arch<generic>) noexcept
1983 {
1984 using kernel = detail::nextafter_kernel<T, A>;
1985 return select(from == to, from,
1986 select(to > from, kernel::next(from), kernel::prev(from)));
1987 }
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999 template <class A, class T>
2000 XSIMD_INLINE batch<T, A> pow(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
2001 {
2002 using batch_type = batch<T, A>;
2003 const auto zero = batch_type(0.);
2004 auto negself = self < zero;
2005 auto iszeropowpos = self == zero && other >= zero;
2006 auto adj_self = select(iszeropowpos, batch_type(1), abs(self));
2007 batch_type z = exp(other * log(adj_self));
2008 z = select(iszeropowpos, zero, z);
2009 z = select(is_odd(other) && negself, -z, z);
2010 auto invalid = negself && !(is_flint(other) || isinf(other));
2011 return select(invalid, constants::nan<batch_type>(), z);
2012 }
2013
2014 template <class A, class T>
2015 XSIMD_INLINE batch<std::complex<T>, A> pow(const batch<std::complex<T>, A>& a, const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
2016 {
2017 using cplx_batch = batch<std::complex<T>, A>;
2018 using real_batch = typename cplx_batch::real_batch;
2019 real_batch absa = abs(a);
2020 real_batch arga = arg(a);
2021 real_batch x = z.real();
2022 real_batch y = z.imag();
2023 real_batch r = pow(absa, x);
2024 real_batch theta = x * arga;
2025 real_batch ze(0);
2026 auto cond = (y == ze);
2027 r = select(cond, r, r * exp(-y * arga));
2028 theta = select(cond, theta, theta + y * log(absa));
2029 auto sincosTheta = xsimd::sincos(theta);
2030 return select(absa == ze, cplx_batch(ze), cplx_batch(r * sincosTheta.second, r * sincosTheta.first));
2031 }
2032
2033 template <class A, class T>
2034 inline batch<std::complex<T>, A> pow(const batch<std::complex<T>, A>& a, const batch<T, A>& z, requires_arch<generic>) noexcept
2035 {
2036 using cplx_batch = batch<std::complex<T>, A>;
2037
2038 auto absa = abs(a);
2039 auto arga = arg(a);
2040 auto r = pow(absa, z);
2041
2042 auto theta = z * arga;
2043 auto sincosTheta = xsimd::sincos(theta);
2044 return select(absa == 0, cplx_batch(0), cplx_batch(r * sincosTheta.second, r * sincosTheta.first));
2045 }
2046
2047 template <class A, class T>
2048 inline batch<std::complex<T>, A> pow(const batch<T, A>& a, const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
2049 {
2050 return pow(batch<std::complex<T>, A> { a, batch<T, A> {} }, z);
2051 }
2052
2053
2054 template <class T, class A, class = typename std::enable_if<std::is_floating_point<T>::value, void>::type>
2055 XSIMD_INLINE batch<T, A> reciprocal(batch<T, A> const& self,
2056 requires_arch<generic>) noexcept
2057 {
2058 using batch_type = batch<T, A>;
2059 return div(batch_type(1), self);
2060 }
2061
2062
2063 template <class A, class T>
2064 XSIMD_INLINE std::complex<T> reduce_add(batch<std::complex<T>, A> const& self, requires_arch<generic>) noexcept
2065 {
2066 return { reduce_add(self.real()), reduce_add(self.imag()) };
2067 }
2068
2069 namespace detail
2070 {
2071 template <class T, T N>
2072 struct split_high
2073 {
2074 static constexpr T get(T i, T)
2075 {
2076 return i >= N ? (i % 2) : i + N;
2077 }
2078 };
2079
2080 template <class Op, class A, class T>
2081 XSIMD_INLINE T reduce(Op, batch<T, A> const& self, std::integral_constant<unsigned, 1>) noexcept
2082 {
2083 return self.get(0);
2084 }
2085
2086 template <class Op, class A, class T, unsigned Lvl>
2087 XSIMD_INLINE T reduce(Op op, batch<T, A> const& self, std::integral_constant<unsigned, Lvl>) noexcept
2088 {
2089 using index_type = as_unsigned_integer_t<T>;
2090 batch<T, A> split = swizzle(self, make_batch_constant<index_type, A, split_high<index_type, Lvl / 2>>());
2091 return reduce(op, op(split, self), std::integral_constant<unsigned, Lvl / 2>());
2092 }
2093 }
2094
2095
2096 template <class A, class T>
2097 XSIMD_INLINE T reduce_max(batch<T, A> const& self, requires_arch<generic>) noexcept
2098 {
2099 return detail::reduce([](batch<T, A> const& x, batch<T, A> const& y)
2100 { return max(x, y); },
2101 self, std::integral_constant<unsigned, batch<T, A>::size>());
2102 }
2103
2104
2105 template <class A, class T>
2106 XSIMD_INLINE T reduce_min(batch<T, A> const& self, requires_arch<generic>) noexcept
2107 {
2108 return detail::reduce([](batch<T, A> const& x, batch<T, A> const& y)
2109 { return min(x, y); },
2110 self, std::integral_constant<unsigned, batch<T, A>::size>());
2111 }
2112
2113
2114 template <class A>
2115 XSIMD_INLINE batch<float, A> remainder(batch<float, A> const& self, batch<float, A> const& other, requires_arch<generic>) noexcept
2116 {
2117 return fnma(nearbyint(self / other), other, self);
2118 }
2119 template <class A>
2120 XSIMD_INLINE batch<double, A> remainder(batch<double, A> const& self, batch<double, A> const& other, requires_arch<generic>) noexcept
2121 {
2122 return fnma(nearbyint(self / other), other, self);
2123 }
2124 template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
2125 XSIMD_INLINE batch<T, A> remainder(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) noexcept
2126 {
2127 auto mod = self % other;
2128 return select(mod <= other / 2, mod, mod - other);
2129 }
2130
2131
2132 template <class A, class T>
2133 XSIMD_INLINE batch<std::complex<T>, A> select(batch_bool<T, A> const& cond, batch<std::complex<T>, A> const& true_br, batch<std::complex<T>, A> const& false_br, requires_arch<generic>) noexcept
2134 {
2135 return { select(cond, true_br.real(), false_br.real()), select(cond, true_br.imag(), false_br.imag()) };
2136 }
2137
2138
2139 template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
2140 XSIMD_INLINE batch<T, A> sign(batch<T, A> const& self, requires_arch<generic>) noexcept
2141 {
2142 using batch_type = batch<T, A>;
2143 batch_type res = select(self > batch_type(0), batch_type(1), batch_type(0)) - select(self < batch_type(0), batch_type(1), batch_type(0));
2144 return res;
2145 }
2146
2147 namespace detail
2148 {
2149 template <class T, class A>
2150 XSIMD_INLINE batch<T, A> signf(batch<T, A> const& self) noexcept
2151 {
2152 using batch_type = batch<T, A>;
2153 batch_type res = select(self > batch_type(0.f), batch_type(1.f), batch_type(0.f)) - select(self < batch_type(0.f), batch_type(1.f), batch_type(0.f));
2154 #ifdef XSIMD_NO_NANS
2155 return res;
2156 #else
2157 return select(isnan(self), constants::nan<batch_type>(), res);
2158 #endif
2159 }
2160 }
2161
2162 template <class A>
2163 XSIMD_INLINE batch<float, A> sign(batch<float, A> const& self, requires_arch<generic>) noexcept
2164 {
2165 return detail::signf(self);
2166 }
2167 template <class A>
2168 XSIMD_INLINE batch<double, A> sign(batch<double, A> const& self, requires_arch<generic>) noexcept
2169 {
2170 return detail::signf(self);
2171 }
2172 template <class A, class T>
2173 XSIMD_INLINE batch<std::complex<T>, A> sign(const batch<std::complex<T>, A>& z, requires_arch<generic>) noexcept
2174 {
2175 using batch_type = batch<std::complex<T>, A>;
2176 using real_batch = typename batch_type::real_batch;
2177 auto rz = z.real();
2178 auto iz = z.imag();
2179 return select(rz != real_batch(0.),
2180 batch_type(sign(rz)),
2181 batch_type(sign(iz)));
2182 }
2183
2184
2185 template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
2186 XSIMD_INLINE batch<T, A> signnz(batch<T, A> const& self, requires_arch<generic>) noexcept
2187 {
2188 using batch_type = batch<T, A>;
2189 return (self >> (sizeof(T) * 8 - 1)) | batch_type(1.);
2190 }
2191
2192 namespace detail
2193 {
2194 template <class T, class A>
2195 XSIMD_INLINE batch<T, A> signnzf(batch<T, A> const& self) noexcept
2196 {
2197 using batch_type = batch<T, A>;
2198 #ifndef XSIMD_NO_NANS
2199 return select(isnan(self), constants::nan<batch_type>(), batch_type(1.) | (constants::signmask<batch_type>() & self));
2200 #else
2201 return batch_type(1.) | (constants::signmask<batch_type>() & self);
2202 #endif
2203 }
2204 }
2205
2206 template <class A>
2207 XSIMD_INLINE batch<float, A> signnz(batch<float, A> const& self, requires_arch<generic>) noexcept
2208 {
2209 return detail::signnzf(self);
2210 }
2211 template <class A>
2212 XSIMD_INLINE batch<double, A> signnz(batch<double, A> const& self, requires_arch<generic>) noexcept
2213 {
2214 return detail::signnzf(self);
2215 }
2216
2217
2218 template <class A, class T>
2219 XSIMD_INLINE batch<std::complex<T>, A> sqrt(batch<std::complex<T>, A> const& z, requires_arch<generic>) noexcept
2220 {
2221
2222 constexpr T csqrt_scale_factor = std::is_same<T, float>::value ? 6.7108864e7f : 1.8014398509481984e16;
2223 constexpr T csqrt_scale = std::is_same<T, float>::value ? 1.220703125e-4f : 7.450580596923828125e-9;
2224 using batch_type = batch<std::complex<T>, A>;
2225 using real_batch = batch<T, A>;
2226 real_batch x = z.real();
2227 real_batch y = z.imag();
2228 real_batch sqrt_x = sqrt(fabs(x));
2229 real_batch sqrt_hy = sqrt(0.5 * fabs(y));
2230 auto cond = (fabs(x) > real_batch(4.) || fabs(y) > real_batch(4.));
2231 x = select(cond, x * 0.25, x * csqrt_scale_factor);
2232 y = select(cond, y * 0.25, y * csqrt_scale_factor);
2233 real_batch scale = select(cond, real_batch(2.), real_batch(csqrt_scale));
2234 real_batch r = abs(batch_type(x, y));
2235
2236 auto condxp = x > real_batch(0.);
2237 real_batch t0 = select(condxp, xsimd::sqrt(0.5 * (r + x)), xsimd::sqrt(0.5 * (r - x)));
2238 real_batch r0 = scale * fabs((0.5 * y) / t0);
2239 t0 *= scale;
2240 real_batch t = select(condxp, t0, r0);
2241 r = select(condxp, r0, t0);
2242 batch_type resg = select(y < real_batch(0.), batch_type(t, -r), batch_type(t, r));
2243 real_batch ze(0.);
2244
2245 return select(y == ze,
2246 select(x == ze,
2247 batch_type(ze, ze),
2248 select(x < ze, batch_type(ze, sqrt_x), batch_type(sqrt_x, ze))),
2249 select(x == ze,
2250 select(y > ze, batch_type(sqrt_hy, sqrt_hy), batch_type(sqrt_hy, -sqrt_hy)),
2251 resg));
2252 }
2253
2254
2255
2256 namespace detail
2257 {
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267 template <class B>
2268 struct stirling_kernel;
2269
2270 template <class A>
2271 struct stirling_kernel<batch<float, A>>
2272 {
2273 using batch_type = batch<float, A>;
2274 static XSIMD_INLINE batch_type compute(const batch_type& x) noexcept
2275 {
2276 return horner<batch_type,
2277 0x3daaaaab,
2278 0x3b638e39,
2279 0xbb2fb930,
2280 0xb970b359>(x);
2281 }
2282
2283 static XSIMD_INLINE batch_type split_limit() noexcept
2284 {
2285 return batch_type(bit_cast<float>(uint32_t(0x41d628f6)));
2286 }
2287
2288 static XSIMD_INLINE batch_type large_limit() noexcept
2289 {
2290 return batch_type(bit_cast<float>(uint32_t(0x420c28f3)));
2291 }
2292 };
2293
2294 template <class A>
2295 struct stirling_kernel<batch<double, A>>
2296 {
2297 using batch_type = batch<double, A>;
2298 static XSIMD_INLINE batch_type compute(const batch_type& x) noexcept
2299 {
2300 return horner<batch_type,
2301 0x3fb5555555555986ull,
2302 0x3f6c71c71b98c5fdull,
2303 0xbf65f72607d44fd7ull,
2304 0xbf2e166b27e61d7cull,
2305 0x3f49cc72592d7293ull
2306 >(x);
2307 }
2308
2309 static XSIMD_INLINE batch_type split_limit() noexcept
2310 {
2311 return batch_type(bit_cast<double>(uint64_t(0x4061e083ba3443d4)));
2312 }
2313
2314 static XSIMD_INLINE batch_type large_limit() noexcept
2315 {
2316 return batch_type(bit_cast<double>(uint64_t(0x4065800000000000)));
2317 }
2318 };
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329 template <class T, class A>
2330 XSIMD_INLINE batch<T, A> stirling(const batch<T, A>& a) noexcept
2331 {
2332 using batch_type = batch<T, A>;
2333 const batch_type stirlingsplitlim = stirling_kernel<batch_type>::split_limit();
2334 const batch_type stirlinglargelim = stirling_kernel<batch_type>::large_limit();
2335 batch_type x = select(a >= batch_type(0.), a, constants::nan<batch_type>());
2336 batch_type w = batch_type(1.) / x;
2337 w = fma(w, stirling_kernel<batch_type>::compute(w), batch_type(1.));
2338 batch_type y = exp(-x);
2339 auto test = (x < stirlingsplitlim);
2340 batch_type z = x - batch_type(0.5);
2341 z = select(test, z, batch_type(0.5) * z);
2342 batch_type v = exp(z * log(abs(x)));
2343 y *= v;
2344 y = select(test, y, y * v);
2345 y *= constants::sqrt_2pi<batch_type>() * w;
2346 #ifndef XSIMD_NO_INFINITIES
2347 y = select(isinf(x), x, y);
2348 #endif
2349 return select(x > stirlinglargelim, constants::infinity<batch_type>(), y);
2350 }
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361 template <class B>
2362 struct tgamma_kernel;
2363
2364 template <class A>
2365 struct tgamma_kernel<batch<float, A>>
2366 {
2367 using batch_type = batch<float, A>;
2368 static XSIMD_INLINE batch_type compute(const batch_type& x) noexcept
2369 {
2370 return horner<batch_type,
2371 0x3f800000UL,
2372 0x3ed87799UL,
2373 0x3ed2d411UL,
2374 0x3da82a34UL,
2375 0x3d93ae7cUL,
2376 0x3b91db14UL,
2377 0x3ba90c99UL,
2378 0x3ad28b22UL
2379 >(x);
2380 }
2381 };
2382
2383 template <class A>
2384 struct tgamma_kernel<batch<double, A>>
2385 {
2386 using batch_type = batch<double, A>;
2387 static XSIMD_INLINE batch_type compute(const batch_type& x) noexcept
2388 {
2389 return horner<batch_type,
2390 0x3ff0000000000000ULL,
2391 0x3fdfa1373993e312ULL,
2392 0x3fca8da9dcae7d31ULL,
2393 0x3fa863d918c423d3ULL,
2394 0x3f8557cde9db14b0ULL,
2395 0x3f5384e3e686bfabULL,
2396 0x3f24fcb839982153ULL
2397 >(x)
2398 / horner<batch_type,
2399 0x3ff0000000000000ULL,
2400 0x3fb24944c9cd3c51ULL,
2401 0xbfce071a9d4287c2ULL,
2402 0x3fa25779e33fde67ULL,
2403 0x3f8831ed5b1bb117ULL,
2404 0xBf7240e4e750b44aULL,
2405 0x3f41ae8a29152573ULL,
2406 0xbef8487a8400d3aFULL
2407 >(x);
2408 }
2409 };
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420 template <class B>
2421 XSIMD_INLINE B tgamma_large_negative(const B& a) noexcept
2422 {
2423 B st = stirling(a);
2424 B p = floor(a);
2425 B sgngam = select(is_even(p), -B(1.), B(1.));
2426 B z = a - p;
2427 auto test2 = z < B(0.5);
2428 z = select(test2, z - B(1.), z);
2429 z = a * sin(z, trigo_pi_tag());
2430 z = abs(z);
2431 return sgngam * constants::pi<B>() / (z * st);
2432 }
2433
2434 template <class B, class BB>
2435 XSIMD_INLINE B tgamma_other(const B& a, const BB& test) noexcept
2436 {
2437 B x = select(test, B(2.), a);
2438 #ifndef XSIMD_NO_INFINITIES
2439 auto inf_result = (a == constants::infinity<B>());
2440 x = select(inf_result, B(2.), x);
2441 #endif
2442 B z = B(1.);
2443 auto test1 = (x >= B(3.));
2444 while (any(test1))
2445 {
2446 x = select(test1, x - B(1.), x);
2447 z = select(test1, z * x, z);
2448 test1 = (x >= B(3.));
2449 }
2450 test1 = (x < B(0.));
2451 while (any(test1))
2452 {
2453 z = select(test1, z / x, z);
2454 x = select(test1, x + B(1.), x);
2455 test1 = (x < B(0.));
2456 }
2457 auto test2 = (x < B(2.));
2458 while (any(test2))
2459 {
2460 z = select(test2, z / x, z);
2461 x = select(test2, x + B(1.), x);
2462 test2 = (x < B(2.));
2463 }
2464 x = z * tgamma_kernel<B>::compute(x - B(2.));
2465 #ifndef XSIMD_NO_INFINITIES
2466 return select(inf_result, a, x);
2467 #else
2468 return x;
2469 #endif
2470 }
2471 }
2472
2473 template <class A, class T>
2474 XSIMD_INLINE batch<T, A> tgamma(batch<T, A> const& self, requires_arch<generic>) noexcept
2475 {
2476 using batch_type = batch<T, A>;
2477 auto nan_result = (self < batch_type(0.) && is_flint(self));
2478 #ifndef XSIMD_NO_INVALIDS
2479 nan_result = isnan(self) || nan_result;
2480 #endif
2481 batch_type q = abs(self);
2482 auto test = (self < batch_type(-33.));
2483 batch_type r = constants::nan<batch_type>();
2484 if (any(test))
2485 {
2486 r = detail::tgamma_large_negative(q);
2487 if (all(test))
2488 return select(nan_result, constants::nan<batch_type>(), r);
2489 }
2490 batch_type r1 = detail::tgamma_other(self, test);
2491 batch_type r2 = select(test, r, r1);
2492 return select(self == batch_type(0.), copysign(constants::infinity<batch_type>(), self), select(nan_result, constants::nan<batch_type>(), r2));
2493 }
2494
2495 }
2496
2497 }
2498
2499 #endif