12#ifndef EIGEN_MATHFUNCTIONS_H
13#define EIGEN_MATHFUNCTIONS_H
17#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
18#define EIGEN_LOG2E 1.442695040888963407359924681001892137426645954152985934135449406931109219L
19#define EIGEN_LN2 0.693147180559945309417232121458176568075500134360255254120680009493393621L
22#include "./InternalHeaderCheck.h"
49template <
typename T,
typename dummy =
void>
50struct global_math_functions_filtering_base {
55struct global_math_functions_filtering_base<T,
56 void_t<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>> {
57 using type =
typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
60#define EIGEN_MATHFUNC_IMPL(func, scalar) \
61 Eigen::internal::func##_impl<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>
67template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
68struct real_default_impl {
69 using RealScalar =
typename NumTraits<Scalar>::Real;
70 EIGEN_DEVICE_FUNC
static constexpr RealScalar run(
const Scalar& x) {
return x; }
73template <
typename Scalar>
74struct real_default_impl<Scalar, true> {
75 using RealScalar =
typename NumTraits<Scalar>::Real;
76 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
82template <
typename Scalar>
83struct real_impl : real_default_impl<Scalar> {};
85#if defined(EIGEN_GPU_COMPILE_PHASE)
87struct real_impl<std::complex<T>> {
89 EIGEN_DEVICE_FUNC
static inline T run(
const std::complex<T>& x) {
return x.real(); }
97template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
98struct imag_default_impl {
99 using RealScalar =
typename NumTraits<Scalar>::Real;
100 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar&) {
return RealScalar(0); }
103template <
typename Scalar>
104struct imag_default_impl<Scalar, true> {
105 using RealScalar =
typename NumTraits<Scalar>::Real;
106 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
112template <
typename Scalar>
113struct imag_impl : imag_default_impl<Scalar> {};
115#if defined(EIGEN_GPU_COMPILE_PHASE)
117struct imag_impl<std::complex<T>> {
118 using RealScalar = T;
119 EIGEN_DEVICE_FUNC
static inline T run(
const std::complex<T>& x) {
return x.imag(); }
127template <
typename Scalar>
128struct real_ref_impl {
129 using RealScalar =
typename NumTraits<Scalar>::Real;
130 EIGEN_DEVICE_FUNC
static inline RealScalar& run(Scalar& x) {
return reinterpret_cast<RealScalar*
>(&x)[0]; }
131 EIGEN_DEVICE_FUNC
static inline const RealScalar& run(
const Scalar& x) {
132 return reinterpret_cast<const RealScalar*
>(&x)[0];
140template <
typename Scalar,
bool IsComplex>
141struct imag_ref_default_impl {
142 using RealScalar =
typename NumTraits<Scalar>::Real;
143 EIGEN_DEVICE_FUNC
static inline RealScalar& run(Scalar& x) {
return reinterpret_cast<RealScalar*
>(&x)[1]; }
144 EIGEN_DEVICE_FUNC
static inline const RealScalar& run(
const Scalar& x) {
145 return reinterpret_cast<const RealScalar*
>(&x)[1];
149template <
typename Scalar>
150struct imag_ref_default_impl<Scalar, false> {
151 using RealScalar =
typename NumTraits<Scalar>::Real;
152 EIGEN_DEVICE_FUNC
constexpr static inline RealScalar run(Scalar&) {
return RealScalar(0); }
153 EIGEN_DEVICE_FUNC
constexpr static inline RealScalar run(
const Scalar&) {
return RealScalar(0); }
156template <
typename Scalar>
157struct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
163template <
typename Scalar>
164EIGEN_DEVICE_FUNC
inline decltype(
auto) real(
const Scalar& x) {
165 return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
168template <
typename Scalar>
169EIGEN_DEVICE_FUNC
inline auto real_ref(
const Scalar& x)
170 -> internal::add_const_on_value_type_t<
decltype(internal::real_ref_impl<Scalar>::run(x))> {
171 return internal::real_ref_impl<Scalar>::run(x);
174template <
typename Scalar>
175EIGEN_DEVICE_FUNC
inline auto real_ref(Scalar& x)
176 ->
decltype(EIGEN_MATHFUNC_IMPL(real_ref, std::remove_const_t<Scalar>)::run(x)) {
177 return EIGEN_MATHFUNC_IMPL(real_ref, std::remove_const_t<Scalar>)::run(x);
180template <
typename Scalar>
181EIGEN_DEVICE_FUNC
inline decltype(
auto) imag(
const Scalar& x) {
182 return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
185template <
typename Scalar>
186EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar select(
const Scalar& mask,
const Scalar& a,
const Scalar& b) {
187 return numext::is_exactly_zero(mask) ? b : a;
198template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
199struct conj_default_impl {
200 EIGEN_DEVICE_FUNC
static constexpr Scalar run(
const Scalar& x) {
return x; }
203template <
typename Scalar>
204struct conj_default_impl<Scalar, true> {
205 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
211template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
212struct conj_impl : conj_default_impl<Scalar, IsComplex> {};
221template <
typename Lhs,
typename Rhs,
222 bool NarrowUnsigned = std::is_same<Lhs, Rhs>::value && std::is_unsigned<Lhs>::value &&
223 !std::is_same<Lhs, bool>::value && (
sizeof(Lhs) <
sizeof(int))>
225 EIGEN_DEVICE_FUNC
static constexpr EIGEN_ALWAYS_INLINE
auto run(
const Lhs& a,
const Rhs& b) {
return a * b; }
228template <
typename Scalar>
229struct mul_impl<Scalar, Scalar, true> {
230 EIGEN_DEVICE_FUNC
static constexpr EIGEN_ALWAYS_INLINE Scalar run(
const Scalar& a,
const Scalar& b) {
231 return static_cast<Scalar
>(
static_cast<unsigned int>(a) *
static_cast<unsigned int>(b));
237template <
typename RealScalar>
238struct mul_impl<RealScalar, std::complex<RealScalar>,
false> {
239 EIGEN_DEVICE_FUNC
static constexpr EIGEN_ALWAYS_INLINE std::complex<RealScalar> run(
240 const RealScalar& a,
const std::complex<RealScalar>& b) {
241 return std::complex<RealScalar>(a * b.real(), a * b.imag());
248template <
typename Lhs,
typename Rhs>
249EIGEN_DEVICE_FUNC
constexpr EIGEN_ALWAYS_INLINE
auto mul(
const Lhs& a,
const Rhs& b) {
250 return mul_impl<Lhs, Rhs>::run(a, b);
257template <
typename Scalar,
bool IsComplex>
258struct abs2_impl_default {
259 using RealScalar =
typename NumTraits<Scalar>::Real;
260 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
return internal::mul(x, x); }
263template <
typename Scalar>
264struct abs2_impl_default<Scalar, true>
266 using RealScalar =
typename NumTraits<Scalar>::Real;
267 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
268 return numext::real(x) * numext::real(x) + numext::imag(x) * numext::imag(x);
272template <
typename Scalar>
274 using RealScalar =
typename NumTraits<Scalar>::Real;
275 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
276 return abs2_impl_default<Scalar, NumTraits<Scalar>::IsComplex>::run(x);
284template <
typename Scalar>
286 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE Scalar run(
const Scalar& x) {
287 EIGEN_USING_STD(sqrt);
293template <
typename ComplexT>
294EIGEN_DEVICE_FUNC
constexpr ComplexT complex_sqrt(
const ComplexT& a_x);
299struct sqrt_impl<std::complex<T>> {
300 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE std::complex<T> run(
const std::complex<T>& x) {
return complex_sqrt(x); }
308template <
typename ComplexT>
309EIGEN_DEVICE_FUNC
constexpr ComplexT complex_rsqrt(
const ComplexT& a_x);
312struct rsqrt_impl<std::complex<T>> {
313 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE std::complex<T> run(
const std::complex<T>& x) {
314 return complex_rsqrt(x);
322template <
typename Scalar,
bool IsComplex>
323struct norm1_default_impl;
325template <
typename Scalar>
326struct norm1_default_impl<Scalar, true> {
327 using RealScalar =
typename NumTraits<Scalar>::Real;
328 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
329 EIGEN_USING_STD(abs);
330 return abs(numext::real(x)) + abs(numext::imag(x));
334template <
typename Scalar>
335struct norm1_default_impl<Scalar, false> {
336 using RealScalar =
typename NumTraits<Scalar>::Real;
337 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
338 EIGEN_USING_STD(abs);
343template <
typename Scalar>
344struct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
350template <
typename Scalar>
357template <
typename OldType,
typename NewType,
typename EnableIf =
void>
359 EIGEN_DEVICE_FUNC
static inline NewType run(
const OldType& x) {
return static_cast<NewType
>(x); }
362template <
typename OldType>
363struct cast_impl<OldType, bool> {
364 EIGEN_DEVICE_FUNC
static inline bool run(
const OldType& x) {
return x != OldType(0); }
369template <
typename OldType,
typename NewType>
370struct cast_impl<OldType, NewType, std::enable_if_t<!NumTraits<OldType>::IsComplex && NumTraits<NewType>::IsComplex>> {
371 EIGEN_DEVICE_FUNC
static inline NewType run(
const OldType& x) {
372 using NewReal =
typename NumTraits<NewType>::Real;
373 return static_cast<NewType
>(cast_impl<OldType, NewReal>::run(x));
377#if EIGEN_ARCH_ARM64 && EIGEN_COMP_CLANG && !EIGEN_CLANG_STRICT_AT_LEAST(21, 0, 0)
381template <
typename OldType>
382struct cast_impl<OldType, float, std::enable_if_t<std::is_integral<OldType>::value && sizeof(OldType) == 8>> {
383 EIGEN_DEVICE_FUNC
static inline float run(
const OldType& x) {
384 using U = std::make_unsigned_t<OldType>;
385 const U u =
static_cast<U
>(x);
389 const U odd = (u & ~low) | ((u & low) != 0 ? U(0x800) : U(0));
390 const U magnitude = std::is_signed<OldType>::value && (u >> 63) != 0 ? U(0) - u : u;
391 return static_cast<float>(magnitude >= (U(1) << 53) ? numext::bit_cast<OldType>(odd) : x);
398template <
typename OldType,
typename NewType>
399EIGEN_DEVICE_FUNC
inline NewType cast(
const OldType& x) {
400 return cast_impl<OldType, NewType>::run(x);
409#if (!EIGEN_COMP_MSVC || EIGEN_COMP_MSVC >= 1920)
411template <typename Scalar, bool HasStdImpl = NumTraits<Scalar>::IsComplex || std::is_integral<Scalar>::value ||
412 std::is_same<Scalar, float>::value ||
413 std::is_same<Scalar, double>::value ||
414 std::is_same<Scalar, long double>::value>
415struct arg_default_impl;
417template <
typename Scalar>
418struct arg_default_impl<Scalar, true> {
419 using RealScalar =
typename NumTraits<Scalar>::Real;
420 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
423 return static_cast<RealScalar
>(arg(x));
428template <
typename Scalar>
429struct arg_default_impl<Scalar, false> {
430 using RealScalar =
typename NumTraits<Scalar>::Real;
431 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
432 return (x < Scalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
436template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
437struct arg_default_impl {
438 using RealScalar =
typename NumTraits<Scalar>::Real;
439 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
440 return (x < RealScalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
444template <
typename Scalar>
445struct arg_default_impl<Scalar, true> {
446 using RealScalar =
typename NumTraits<Scalar>::Real;
447 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
448 EIGEN_USING_STD(arg);
453template <
typename Scalar>
454struct arg_impl : arg_default_impl<Scalar> {};
461namespace std_fallback {
465template <
typename Scalar>
466EIGEN_DEVICE_FUNC
inline Scalar expm1(
const Scalar& x) {
467 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
468 using RealScalar =
typename NumTraits<Scalar>::Real;
470 EIGEN_USING_STD(exp);
472 if (numext::equal_strict(u, Scalar(1))) {
475 Scalar um1 = u - RealScalar(1);
476 if (numext::equal_strict(um1, Scalar(-1))) {
477 return RealScalar(-1);
480 EIGEN_USING_STD(log);
481 Scalar logu = log(u);
482 return numext::equal_strict(u, logu) ? u : (u - RealScalar(1)) * x / logu;
486template <
typename Scalar>
488 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
489 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
490 EIGEN_USING_STD(expm1);
500template <
typename ComplexT>
501EIGEN_DEVICE_FUNC
constexpr ComplexT complex_log(
const ComplexT& z);
503template <
typename Scalar>
505 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
506 EIGEN_USING_STD(log);
507 return static_cast<Scalar
>(log(x));
511template <
typename Scalar>
512struct log_impl<std::complex<Scalar>> {
513 EIGEN_DEVICE_FUNC
static inline std::complex<Scalar> run(
const std::complex<Scalar>& z) {
return complex_log(z); }
520namespace std_fallback {
523template <
typename Scalar>
524EIGEN_DEVICE_FUNC
inline Scalar log1p(
const Scalar& x) {
525 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
526 using RealScalar =
typename NumTraits<Scalar>::Real;
527 EIGEN_USING_STD(log);
528 Scalar x1p = RealScalar(1) + x;
529 Scalar log_1p = log_impl<Scalar>::run(x1p);
530 const bool is_small = numext::equal_strict(x1p, Scalar(1));
532 EIGEN_USING_STD(isfinite);
533 if (!((isfinite)(numext::real(log_1p)) && (isfinite)(numext::imag(log_1p))))
return log_1p;
534 return is_small ? x : x * (log_1p / (x1p - RealScalar(1)));
538template <
typename Scalar>
540 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
542 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
543 EIGEN_USING_STD(log1p);
550template <
typename RealScalar>
551struct log1p_impl<std::complex<RealScalar>>;
554template <typename RealScalar>
555struct complex_exp2_impl;
561template <typename ScalarX, typename ScalarY,
562 bool IsInteger = NumTraits<ScalarX>::IsInteger && NumTraits<ScalarY>::IsInteger>
565 typename ScalarBinaryOpTraits<ScalarX, ScalarY, internal::scalar_pow_op<ScalarX, ScalarY>>::ReturnType;
566 static EIGEN_DEVICE_FUNC
inline result_type run(
const ScalarX& x,
const ScalarY& y) {
567 EIGEN_USING_STD(pow);
572template <
typename ScalarX,
typename ScalarY>
573struct pow_impl<ScalarX, ScalarY, true> {
574 using result_type = ScalarX;
575 static EIGEN_DEVICE_FUNC
inline ScalarX run(ScalarX x, ScalarY y) {
577 eigen_assert(!NumTraits<ScalarY>::IsSigned || y >= 0);
578 if (y & 1) res = internal::mul(res, x);
581 x = internal::mul(x, x);
582 if (y & 1) res = internal::mul(res, x);
589constexpr int floor_log2(
unsigned int value) {
598template <
typename BitsType,
typename EnableIf =
void>
599struct count_bits_impl {
600 static_assert(std::is_integral<BitsType>::value && std::is_unsigned<BitsType>::value,
601 "BitsType must be an unsigned integer");
602 static EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
603 int n = CHAR_BIT *
sizeof(BitsType);
605 while (bits > 0 && shift > 0) {
606 BitsType y = bits >> shift;
619 static EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
620 int n = CHAR_BIT *
sizeof(BitsType);
622 while (bits > 0 && shift > 0) {
623 BitsType y = bits << shift;
636 static EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
647template <
typename BitsType>
648EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
649 return count_bits_impl<BitsType>::clz(bits);
653template <
typename BitsType>
654EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
655 return count_bits_impl<BitsType>::ctz(bits);
659template <
typename BitsType>
660EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
661 return count_bits_impl<BitsType>::popcount(bits);
664#if defined(EIGEN_CUDA_ARCH)
671template <
typename BitsType>
672struct count_bits_impl<
673 BitsType, std::enable_if_t<std::is_integral<BitsType>::value && sizeof(BitsType) <= sizeof(unsigned int)>> {
674 static constexpr int kNumBits =
static_cast<int>(
sizeof(BitsType) * CHAR_BIT);
675 static EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
676 static constexpr int kLeadingBitsOffset = (
sizeof(
unsigned int) -
sizeof(BitsType)) * CHAR_BIT;
677 return static_cast<int>(__clz(
static_cast<unsigned int>(bits))) - kLeadingBitsOffset;
680 static EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
681 return bits == 0 ? kNumBits : __ffs(
static_cast<int>(
static_cast<unsigned int>(bits))) - 1;
684 static EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
return __popc(
static_cast<unsigned int>(bits)); }
687template <
typename BitsType>
688struct count_bits_impl<BitsType,
689 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned int) < sizeof(BitsType) &&
690 sizeof(BitsType) <= sizeof(unsigned long long)>> {
691 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
692 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
693 static constexpr int kLeadingBitsOffset = (sizeof(unsigned long long) - sizeof(BitsType)) * CHAR_BIT;
694 return static_cast<int>(__clzll(static_cast<unsigned long long>(bits))) - kLeadingBitsOffset;
697 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
698 return bits == 0 ? kNumBits : __ffsll(static_cast<long long>(static_cast<unsigned long long>(bits))) - 1;
701 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
702 return __popcll(static_cast<unsigned long long>(bits));
706#elif EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
708template <typename BitsType>
709struct count_bits_impl<
710 BitsType, std::enable_if_t<std::is_integral<BitsType>::value && sizeof(BitsType) <= sizeof(unsigned int)>> {
711 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
712 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
713 static constexpr int kLeadingBitsOffset = (sizeof(unsigned int) - sizeof(BitsType)) * CHAR_BIT;
714 return bits == 0 ? kNumBits : __builtin_clz(static_cast<unsigned int>(bits)) - kLeadingBitsOffset;
717 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
718 return bits == 0 ? kNumBits : __builtin_ctz(static_cast<unsigned int>(bits));
721 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
722 return __builtin_popcount(static_cast<unsigned int>(bits));
726template <typename BitsType>
727struct count_bits_impl<BitsType,
728 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned int) < sizeof(BitsType) &&
729 sizeof(BitsType) <= sizeof(unsigned long)>> {
730 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
731 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
732 static constexpr int kLeadingBitsOffset = (sizeof(unsigned long) - sizeof(BitsType)) * CHAR_BIT;
733 return bits == 0 ? kNumBits : __builtin_clzl(static_cast<unsigned long>(bits)) - kLeadingBitsOffset;
736 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
737 return bits == 0 ? kNumBits : __builtin_ctzl(static_cast<unsigned long>(bits));
740 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
741 return __builtin_popcountl(static_cast<unsigned long>(bits));
745template <typename BitsType>
746struct count_bits_impl<BitsType,
747 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned long) < sizeof(BitsType) &&
748 sizeof(BitsType) <= sizeof(unsigned long long)>> {
749 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
750 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
751 static constexpr int kLeadingBitsOffset = (sizeof(unsigned long long) - sizeof(BitsType)) * CHAR_BIT;
752 return bits == 0 ? kNumBits : __builtin_clzll(static_cast<unsigned long long>(bits)) - kLeadingBitsOffset;
755 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
756 return bits == 0 ? kNumBits : __builtin_ctzll(static_cast<unsigned long long>(bits));
759 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
760 return __builtin_popcountll(static_cast<unsigned long long>(bits));
770template <typename BitsType>
771EIGEN_DEVICE_FUNC inline int popcount_fallback(BitsType bits) {
780template <typename BitsType>
781struct count_bits_impl<
782 BitsType, std::enable_if_t<std::is_integral<BitsType>::value && sizeof(BitsType) <= sizeof(unsigned long)>> {
783 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
784 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
786 _BitScanReverse(&out, static_cast<unsigned long>(bits));
787 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
790 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
792 _BitScanForward(&out, static_cast<unsigned long>(bits));
793 return bits == 0 ? kNumBits : static_cast<int>(out);
796 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
797#if defined(EIGEN_VECTORIZE_SSE4_2)
798 return static_cast<int>(__popcnt(static_cast<unsigned int>(bits)));
800 return popcount_fallback(bits);
807template <typename BitsType>
808struct count_bits_impl<BitsType,
809 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned long) < sizeof(BitsType) &&
810 sizeof(BitsType) <= sizeof(__int64)>> {
811 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
812 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
814 _BitScanReverse64(&out, static_cast<unsigned __int64>(bits));
815 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
818 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
820 _BitScanForward64(&out, static_cast<unsigned __int64>(bits));
821 return bits == 0 ? kNumBits : static_cast<int>(out);
824 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
825#if defined(EIGEN_VECTORIZE_SSE4_2)
826 return static_cast<int>(__popcnt64(static_cast<unsigned __int64>(bits)));
828 return popcount_fallback(bits);
837template <typename BitsType>
839 static constexpr int kTotalBits = sizeof(BitsType) * CHAR_BIT;
840 static EIGEN_DEVICE_FUNC inline int run_ceil(const BitsType& x) {
841 const int n = kTotalBits - clz(x);
842 bool power_of_two = (x & (x - 1)) == 0;
843 return x == 0 ? 0 : power_of_two ? (n - 1) : n;
845 static EIGEN_DEVICE_FUNC inline int run_floor(const BitsType& x) {
846 const int n = kTotalBits - clz(x);
847 return x == 0 ? 0 : n - 1;
851template <typename BitsType>
852int log2_ceil(const BitsType& x) {
853 return log_2_impl<BitsType>::run_ceil(x);
856template <typename BitsType>
857int log2_floor(const BitsType& x) {
858 return log_2_impl<BitsType>::run_floor(x);
864EIGEN_DEVICE_FUNC std::enable_if_t<!(std::numeric_limits<T>::has_infinity || std::numeric_limits<T>::has_quiet_NaN ||
865 std::numeric_limits<T>::has_signaling_NaN),
867isfinite_impl(const T&) {
872EIGEN_DEVICE_FUNC std::enable_if_t<(std::numeric_limits<T>::has_infinity || std::numeric_limits<T>::has_quiet_NaN ||
873 std::numeric_limits<T>::has_signaling_NaN) &&
874 (!NumTraits<T>::IsComplex),
876isfinite_impl(const T& x) {
877 EIGEN_USING_STD(isfinite);
878 return isfinite EIGEN_NOT_A_MACRO(x);
882EIGEN_DEVICE_FUNC std::enable_if_t<!std::numeric_limits<T>::has_infinity, bool> isinf_impl(const T&) {
887EIGEN_DEVICE_FUNC std::enable_if_t<(std::numeric_limits<T>::has_infinity && !NumTraits<T>::IsComplex), bool> isinf_impl(
889 EIGEN_USING_STD(isinf);
890 return isinf EIGEN_NOT_A_MACRO(x);
895 std::enable_if_t<!(std::numeric_limits<T>::has_quiet_NaN || std::numeric_limits<T>::has_signaling_NaN), bool>
896 isnan_impl(const T&) {
901EIGEN_DEVICE_FUNC std::enable_if_t<
902 (std::numeric_limits<T>::has_quiet_NaN || std::numeric_limits<T>::has_signaling_NaN) && (!NumTraits<T>::IsComplex),
904isnan_impl(const T& x) {
905 EIGEN_USING_STD(isnan);
906 return isnan EIGEN_NOT_A_MACRO(x);
911EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x);
913EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x);
915EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x);
917EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS T ptanh_float(const T& a_x);
922template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
923 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0)>
925 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) { return Scalar((a > Scalar(0)) - (a < Scalar(0))); }
928template <typename Scalar>
929struct sign_impl<Scalar, false, false> {
930 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) {
931 return (isnan_impl<Scalar>)(a) ? a : Scalar((a > Scalar(0)) - (a < Scalar(0)));
935template <typename Scalar, bool IsInteger>
936struct sign_impl<Scalar, true, IsInteger> {
937 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) {
938 using real_type = typename NumTraits<Scalar>::Real;
939 EIGEN_USING_STD(abs);
940 real_type aa = abs(a);
941 if (aa == real_type(0)) return Scalar(0);
945 return Scalar(numext::real(a) / aa, numext::imag(a) / aa);
951struct sign_impl<bool, false, true> {
952 EIGEN_DEVICE_FUNC static inline bool run(const bool& a) { return a; }
956namespace has_copysign_detail {
958template <typename T, typename EnableIf = void>
959struct has_copysign_impl : public std::false_type {};
961EIGEN_USING_STD(copysign)
964struct has_copysign_impl<T, std::enable_if_t<std::is_constructible<
965 T, decltype(copysign(std::declval<const T&>(), std::declval<const T&>()))>::value>>
966 : public std::true_type {};
975struct has_copysign : public has_copysign_detail::has_copysign_impl<T> {};
977template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
978 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0),
979 bool HasCopysign = (!IsComplex && !IsInteger && has_copysign<Scalar>::value)>
980struct copysign_impl {
981 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
982 EIGEN_USING_STD(copysign);
983 return Scalar(copysign(a, b));
987template <typename Scalar, bool IsInteger>
988struct copysign_impl<Scalar, true, IsInteger, false> {
989 using RealScalar = typename NumTraits<Scalar>::Real;
990 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
991 return Scalar(copysign_impl<RealScalar>::run(numext::real(a), numext::real(b)),
992 copysign_impl<RealScalar>::run(numext::imag(a), numext::imag(b)));
996template <typename Scalar, bool IsInteger>
997struct copysign_impl<Scalar, false, IsInteger, false> {
998 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
999 EIGEN_IF_CONSTEXPR (IsInteger && !NumTraits<Scalar>::IsSigned) return a;
1000 const Scalar abs_a = a < Scalar(0) ? -a : a;
1001 return b < Scalar(0) ? -abs_a : abs_a;
1006struct copysign_impl<bool, false, true, false> {
1007 EIGEN_DEVICE_FUNC static inline bool run(const bool& a, const bool&) { return a; }
1013template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger>
1015 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return -a; }
1018template <typename Scalar>
1019struct negate_impl<Scalar, true> {
1020 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), NEGATE IS NOT DEFINED FOR BOOLEAN TYPES)
1021 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return Scalar(0) - a; }
1024template <typename Scalar, bool IsInteger = NumTraits<typename unpacket_traits<Scalar>::type>::IsInteger>
1025struct nearest_integer_impl {
1026 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) {
1027 EIGEN_USING_STD(floor) return floor(x);
1029 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) {
1030 EIGEN_USING_STD(ceil) return ceil(x);
1032 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) {
1033 EIGEN_USING_STD(rint) return rint(x);
1035 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) {
1036 EIGEN_USING_STD(round) return round(x);
1038 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) {
1039 EIGEN_USING_STD(trunc) return trunc(x);
1042template <typename Scalar>
1043struct nearest_integer_impl<Scalar, true> {
1044 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) { return x; }
1045 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) { return x; }
1046 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) { return x; }
1047 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) { return x; }
1048 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) { return x; }
1052namespace has_fma_detail {
1054template <typename T, typename EnableIf = void>
1055struct has_fma_impl : public std::false_type {};
1059template <typename T>
1061 T, std::enable_if_t<std::is_same<T, decltype(fma(std::declval<T>(), std::declval<T>(), std::declval<T>()))>::value>>
1062 : public std::true_type {};
1066template <typename T>
1067struct has_fma : public has_fma_detail::has_fma_impl<T> {};
1070template <typename T, typename Enable = void>
1072 static_assert(has_fma<T>::value, "No function fma(...) for type. Please provide an implementation.");
1076template <typename T>
1077struct fma_impl<T, std::enable_if_t<has_fma<T>::value>> {
1078 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T run(const T& a, const T& b, const T& c) {
1080 return fma(a, b, c);
1084#if defined(EIGEN_GPUCC)
1086struct has_fma<float> : public std::true_type {};
1089struct fma_impl<float, void> {
1090 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float run(const float& a, const float& b, const float& c) {
1091 return ::fmaf(a, b, c);
1096struct has_fma<double> : public std::true_type {};
1099struct fma_impl<double, void> {
1100 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double run(const double& a, const double& b, const double& c) {
1101 return ::fma(a, b, c);
1107template <typename Scalar, typename EnableIf = void>
1109 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1111 const Scalar xy = x * y;
1118template <typename Scalar>
1119struct has_fast_fma : std::false_type {};
1120#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMAF) || defined(__FP_FAST_FMAF)
1122struct has_fast_fma<float> : std::true_type {};
1124#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMA) || defined(__FP_FAST_FMA)
1126struct has_fast_fma<double> : std::true_type {};
1128#if defined(FP_FAST_FMAL) || defined(__FP_FAST_FMAL)
1130struct has_fast_fma<long double> : std::true_type {};
1134struct has_fast_fma<long double>
1135 : bool_constant<std::numeric_limits<long double>::digits == std::numeric_limits<double>::digits &&
1136 has_fast_fma<double>::value> {};
1143template <typename Scalar>
1144struct scalar_madd_uses_fma
1145 : std::conditional_t<bool(EIGEN_SCALAR_MADD_USE_FMA) &&
1146 (!std::is_same<Scalar, long double>::value || has_fast_fma<Scalar>::value),
1147 has_fma<Scalar>, std::false_type> {};
1149template <typename Scalar>
1150struct madd_impl<Scalar, std::enable_if_t<scalar_madd_uses_fma<Scalar>::value>> {
1151 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1152 return fma_impl<Scalar>::run(x, y, z);
1157template <typename Scalar>
1158struct binary_floating_point_traits {
1159 using Bits = typename numext::get_integer_by_size<sizeof(Scalar)>::unsigned_type;
1160 static constexpr int kFractionBits = std::numeric_limits<Scalar>::digits - 1;
1161 static constexpr int kExponentBits = int(sizeof(Scalar) * CHAR_BIT) - std::numeric_limits<Scalar>::digits;
1162 static constexpr int kExponentBias = std::numeric_limits<Scalar>::max_exponent - 1;
1163 static constexpr Bits kSignBit = Bits(1) << (sizeof(Scalar) * CHAR_BIT - 1);
1164 static constexpr Bits kExponentUnit = Bits(1) << kFractionBits;
1165 static constexpr Bits kFractionMask = kExponentUnit - 1;
1166 static constexpr Bits kExponentMask = ((Bits(1) << kExponentBits) - 1) << kFractionBits;
1167 static constexpr Bits kMaxFiniteExponentBits = kExponentMask - kExponentUnit;
1169 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits bits(const Scalar& value) { return numext::bit_cast<Bits>(value); }
1171 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits magnitude(const Scalar& value) { return bits(value) & ~kSignBit; }
1175#if EIGEN_COMP_CXXVER < 17
1176template <typename Scalar>
1177constexpr int binary_floating_point_traits<Scalar>::kFractionBits;
1178template <typename Scalar>
1179constexpr int binary_floating_point_traits<Scalar>::kExponentBits;
1180template <typename Scalar>
1181constexpr int binary_floating_point_traits<Scalar>::kExponentBias;
1182template <typename Scalar>
1183constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kSignBit;
1184template <typename Scalar>
1185constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kExponentUnit;
1186template <typename Scalar>
1187constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kFractionMask;
1188template <typename Scalar>
1189constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kExponentMask;
1190template <typename Scalar>
1192 typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kMaxFiniteExponentBits;
1195template <typename Scalar>
1196EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, false_type) {
1197 return numext::is_exactly_zero(value);
1200template <typename Scalar>
1201EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE bool is_zero_magnitude_bits(
1202 typename binary_floating_point_traits<Scalar>::Bits magnitude) {
1205 return magnitude == 0;
1209template <typename Scalar>
1210struct sign_impl_binary {
1211 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar& a) {
1212 using Binary = binary_floating_point_traits<Scalar>;
1213 using Bits = typename Binary::Bits;
1214 const Bits bits = Binary::bits(a);
1215 const Bits magnitude = bits & ~Binary::kSignBit;
1217 const Bits nonzero = (magnitude | (Bits(0) - magnitude)) >> (sizeof(Bits) * CHAR_BIT - 1);
1218 const Bits nonzero_mask = Bits(0) - nonzero;
1219 const Bits nan_mask = Bits(0) - Bits(magnitude > Binary::kExponentMask);
1220 const Bits signed_one = (bits & Binary::kSignBit) | Binary::bits(Scalar(1));
1221 return numext::bit_cast<Scalar>((bits & nan_mask) | (signed_one & nonzero_mask & ~nan_mask));
1226struct sign_impl<float, false, false> : sign_impl_binary<float> {};
1229struct sign_impl<double, false, false> : sign_impl_binary<double> {};
1231template <typename Scalar>
1232EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, true_type) {
1233 return is_zero_magnitude_bits<Scalar>(binary_floating_point_traits<Scalar>::magnitude(value));
1237template <typename T, bool IsInteger = NumTraits<T>::IsInteger>
1238struct absdiff_impl {
1239 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) {
1240 EIGEN_USING_STD(abs);
1246template <typename T>
1247struct absdiff_impl<T, true> {
1248 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) { return x > y ? x - y : y - x; }
1259template <typename X>
1260EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush(const X& x) {
1261 constexpr bool HasBinaryEncoding = (std::is_same<X, float>::value || std::is_same<X, double>::value) &&
1262 std::numeric_limits<X>::is_iec559 && std::numeric_limits<X>::radix == 2 &&
1263 (sizeof(X) == sizeof(uint32_t) || sizeof(X) == sizeof(uint64_t));
1264 return internal::is_exactly_zero_no_flush_impl(x, internal::bool_constant<HasBinaryEncoding>());
1267#if !defined(EIGEN_GPUCC)
1268template <typename T>
1269EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1270 EIGEN_USING_STD(min)
1271 return min EIGEN_NOT_A_MACRO(x, y);
1274template <typename T>
1275EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1276 EIGEN_USING_STD(max)
1277 return max EIGEN_NOT_A_MACRO(x, y);
1280template <typename T>
1281EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1282 return y < x ? y : x;
1284#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1289EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y) {
1293EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double mini(const double& x, const double& y) {
1297#ifndef EIGEN_GPU_COMPILE_PHASE
1299EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double mini(const long double& x, const long double& y) {
1300#if defined(EIGEN_HIPCC)
1302 return (x < y) ? x : y;
1310template <typename T>
1311EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1312 return x < y ? y : x;
1314#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1316EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y) {
1320EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double maxi(const double& x, const double& y) {
1323#ifndef EIGEN_GPU_COMPILE_PHASE
1325EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double maxi(const long double& x, const long double& y) {
1326#if defined(EIGEN_HIPCC)
1328 return (x > y) ? x : y;
1337#if defined(EIGEN_SYCL_DEVICE_ONLY)
1339#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1340 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1341 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1342 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1343 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1344#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1345 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1346 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1347 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1348 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1349#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1350 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1351 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1352 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1353 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1354#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1355 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1356 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1357 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1358 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1359#define SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(NAME, FUNC) \
1360 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1361 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC)
1362#define SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(NAME, FUNC) \
1363 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1364 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC)
1365#define SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(NAME, FUNC) \
1366 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1367 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1368#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(NAME, FUNC) \
1369 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1370 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1371#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(NAME, FUNC, RET_TYPE) \
1372 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_float) \
1373 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_double)
1375#define SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1377 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE& x) { \
1378 return cl::sycl::FUNC(x); \
1381#define SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, TYPE, TYPE)
1383#define SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE1, ARG_TYPE2) \
1385 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE1& x, const ARG_TYPE2& y) { \
1386 return cl::sycl::FUNC(x, y); \
1389#define SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1390 SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE, ARG_TYPE)
1392#define SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, TYPE, TYPE)
1394SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(mini, min)
1395SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(mini, fmin)
1396SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(maxi, max)
1397SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(maxi, fmax)
1401template <typename Scalar>
1402EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real arg(const Scalar& x) {
1403 return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
1406template <typename Scalar>
1407EIGEN_DEVICE_FUNC inline auto imag_ref(const Scalar& x)
1408 -> internal::add_const_on_value_type_t<decltype(internal::imag_ref_impl<Scalar>::run(x))> {
1409 return internal::imag_ref_impl<Scalar>::run(x);
1412template <typename Scalar>
1413EIGEN_DEVICE_FUNC inline auto imag_ref(Scalar& x)
1414 -> decltype(EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x)) {
1415 return EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x);
1418template <typename Scalar>
1419EIGEN_DEVICE_FUNC inline Scalar conj(const Scalar& x) {
1420 return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
1423template <typename Scalar>
1424EIGEN_DEVICE_FUNC inline Scalar sign(const Scalar& x) {
1425 return EIGEN_MATHFUNC_IMPL(sign, Scalar)::run(x);
1428template <typename Scalar>
1429EIGEN_DEVICE_FUNC inline Scalar copysign(const Scalar& x, const Scalar& y) {
1430 return EIGEN_MATHFUNC_IMPL(copysign, Scalar)::run(x, y);
1433#if defined(EIGEN_SYCL_DEVICE_ONLY)
1434SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(copysign, copysign)
1437template <typename Scalar>
1438EIGEN_DEVICE_FUNC inline Scalar negate(const Scalar& x) {
1439 return EIGEN_MATHFUNC_IMPL(negate, Scalar)::run(x);
1442template <typename Scalar>
1443EIGEN_DEVICE_FUNC inline decltype(auto) abs2(const Scalar& x) {
1444 return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
1447EIGEN_DEVICE_FUNC inline bool abs2(bool x) { return x; }
1449template <typename T>
1450EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T absdiff(const T& x, const T& y) {
1451 return internal::absdiff_impl<T>::run(x, y);
1454EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float absdiff(const float& x, const float& y) {
1455 return fabsf(x - y);
1458EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double absdiff(const double& x, const double& y) {
1463#ifndef EIGEN_GPU_COMPILE_PHASE
1465EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double absdiff(const long double& x, const long double& y) {
1466 return fabsl(x - y);
1470template <typename Scalar>
1471EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real norm1(const Scalar& x) {
1472 return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
1475template <typename Scalar>
1476EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real hypot(const Scalar& x, const Scalar& y) {
1477 return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
1480#if defined(EIGEN_SYCL_DEVICE_ONLY)
1481SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(hypot, hypot)
1484template <typename Scalar>
1485EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
1486 return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
1489#if defined(EIGEN_SYCL_DEVICE_ONLY)
1490SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log1p, log1p)
1493#if defined(EIGEN_GPUCC)
1495EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log1p(const float& x) {
1500EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log1p(const double& x) {
1505template <typename ScalarX, typename ScalarY>
1506EIGEN_DEVICE_FUNC inline typename internal::pow_impl<ScalarX, ScalarY>::result_type pow(const ScalarX& x,
1508 return internal::pow_impl<ScalarX, ScalarY>::run(x, y);
1511#if defined(EIGEN_SYCL_DEVICE_ONLY)
1512SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(pow, pow)
1515template <typename T>
1516EIGEN_DEVICE_FUNC bool(isnan)(const T& x) {
1517 return internal::isnan_impl(x);
1519template <typename T>
1520EIGEN_DEVICE_FUNC bool(isinf)(const T& x) {
1521 return internal::isinf_impl(x);
1523template <typename T>
1524EIGEN_DEVICE_FUNC bool(isfinite)(const T& x) {
1525 return internal::isfinite_impl(x);
1528#if defined(EIGEN_SYCL_DEVICE_ONLY)
1529SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isnan, isnan, bool)
1530SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isinf, isinf, bool)
1531SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isfinite, isfinite, bool)
1534template <typename Scalar>
1535EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar rint(const Scalar& x) {
1536 return internal::nearest_integer_impl<Scalar>::run_rint(x);
1539template <typename Scalar>
1540EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar round(const Scalar& x) {
1541 return internal::nearest_integer_impl<Scalar>::run_round(x);
1544template <typename Scalar>
1545EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(floor)(const Scalar& x) {
1546 return internal::nearest_integer_impl<Scalar>::run_floor(x);
1549template <typename Scalar>
1550EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(ceil)(const Scalar& x) {
1551 return internal::nearest_integer_impl<Scalar>::run_ceil(x);
1554template <typename Scalar>
1555EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(trunc)(const Scalar& x) {
1556 return internal::nearest_integer_impl<Scalar>::run_trunc(x);
1559#if defined(EIGEN_SYCL_DEVICE_ONLY)
1560SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(rint, rint)
1561SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(round, round)
1562SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(floor, floor)
1563SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(ceil, ceil)
1564SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(trunc, trunc)
1567#if defined(EIGEN_GPUCC)
1569EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float floor(const float& x) {
1573EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double floor(const double& x) {
1577EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float ceil(const float& x) {
1581EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double ceil(const double& x) {
1585EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float trunc(const float& x) {
1589EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double trunc(const double& x) {
1596template <typename T>
1597EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T div_ceil(T a, T b) {
1598 using UnsignedT = std::make_unsigned_t<T>;
1599 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1601 const UnsignedT ua = UnsignedT(a);
1602 const UnsignedT ub = UnsignedT(b);
1604 return ua == 0 ? 0 : (ua - 1) / ub + 1;
1609template <typename T, typename U>
1610EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T round_down(T a, U b) {
1611 using UnsignedT = std::make_unsigned_t<T>;
1612 using UnsignedU = std::make_unsigned_t<U>;
1613 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1614 EIGEN_STATIC_ASSERT((NumTraits<U>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1616 const UnsignedT ua = UnsignedT(a);
1617 const UnsignedU ub = UnsignedU(b);
1618 return ub * (ua / ub);
1621template <typename T>
1622EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log10(T x) {
1623 EIGEN_USING_STD(log10);
1627template <typename T>
1628EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log2(T x) {
1629 EIGEN_USING_STD(log2);
1633#if defined(EIGEN_SYCL_DEVICE_ONLY)
1635EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log10(float x) {
1636 return cl::sycl::log10(x);
1639EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log10(double x) {
1640 return cl::sycl::log10(x);
1643EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log2(float x) {
1644 return cl::sycl::log2(x);
1647EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log2(double x) {
1648 return cl::sycl::log2(x);
1654constexpr int log2(int x) {
1656 constexpr int table[32] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
1657 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
1663 return table[(v * 0x07C4ACDDU) >> 27];
1675template <typename Scalar>
1676EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar sqrt(const Scalar& x) {
1677 return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
1682EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_DEVICE_FUNC bool sqrt<bool>(const bool& x) {
1686#if defined(EIGEN_SYCL_DEVICE_ONLY)
1687SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sqrt, sqrt)
1690#if defined(EIGEN_VECTORIZE_SSE)
1693EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float& x);
1695EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double sqrt(const double& x);
1699template <typename T>
1700EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<!NumTraits<T>::IsComplex, T> cbrt(const T& x) {
1701 EIGEN_USING_STD(cbrt);
1702 return static_cast<T>(cbrt(x));
1705template <typename T>
1706EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, T> cbrt(const T& x) {
1707 EIGEN_USING_STD(pow);
1708 return pow(x, typename NumTraits<T>::Real(1.0 / 3.0));
1711#if defined(EIGEN_SYCL_DEVICE_ONLY)
1712SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cbrt, cbrt)
1716template <typename T>
1717EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T rsqrt(const T& x) {
1718 return internal::rsqrt_impl<T>::run(x);
1721template <typename T>
1722EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log(const T& x) {
1723 return internal::log_impl<T>::run(x);
1726#if defined(EIGEN_SYCL_DEVICE_ONLY)
1727SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log, log)
1730#if defined(EIGEN_GPUCC)
1732EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log(const float& x) {
1737EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log(const double& x) {
1742template <typename T>
1743EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<
1744 (NumTraits<T>::IsSigned || !NumTraits<T>::IsInteger) && !NumTraits<T>::IsComplex, typename NumTraits<T>::Real>
1746 EIGEN_USING_STD(abs);
1750template <typename T>
1751EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, typename NumTraits<T>::Real> abs(
1753 return numext::hypot(numext::real(x), numext::imag(x));
1756template <typename T>
1757EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<
1758 !NumTraits<T>::IsSigned && NumTraits<T>::IsInteger && !NumTraits<T>::IsComplex, typename NumTraits<T>::Real>
1763#if defined(EIGEN_SYCL_DEVICE_ONLY)
1764SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(abs, abs)
1765SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(abs, fabs)
1768#if defined(EIGEN_GPUCC)
1770EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const float& x) {
1775EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const double& x) {
1780EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const std::complex<float>& x) {
1781 return ::hypotf(x.real(), x.imag());
1785EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const std::complex<double>& x) {
1786 return ::hypot(x.real(), x.imag());
1790template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger, bool IsSigned = NumTraits<Scalar>::IsSigned>
1792template <typename Scalar>
1793struct signbit_impl<Scalar, false, true> {
1794 static constexpr size_t Size = sizeof(Scalar);
1795 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1796 using intSize_t = typename get_integer_by_size<Size>::signed_type;
1797 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static Scalar run(const Scalar& x) {
1798 intSize_t a = bit_cast<intSize_t, Scalar>(x);
1800 Scalar result = bit_cast<Scalar, intSize_t>(a);
1804template <typename Scalar>
1805struct signbit_impl<Scalar, true, true> {
1806 static constexpr size_t Size = sizeof(Scalar);
1807 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1808 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar run(const Scalar& x) { return x >> Shift; }
1810template <typename Scalar>
1811struct signbit_impl<Scalar, true, false> {
1812 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar run(
const Scalar&) {
return Scalar(0); }
1814template <
typename Scalar>
1815EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar signbit(
const Scalar& x) {
1816 return signbit_impl<Scalar>::run(x);
1819template <
typename T>
1820EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp(
const T& x) {
1821 EIGEN_USING_STD(exp);
1827template <
typename RealScalar>
1828EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp(
const std::complex<RealScalar>& x) {
1830 const std::complex<RealScalar> result = std::exp(x);
1833 if ((isfinite)(real_ref(x)) && !(isfinite)(imag_ref(x))) {
1834 return std::complex<RealScalar>(NumTraits<RealScalar>::quiet_NaN(), NumTraits<RealScalar>::quiet_NaN());
1836#if EIGEN_COMP_MSVC < 1931
1840 if ((real_ref(x) == NumTraits<RealScalar>::infinity() && !(isfinite)(imag_ref(x)))) {
1841 return std::complex<RealScalar>(NumTraits<RealScalar>::infinity(), NumTraits<RealScalar>::quiet_NaN());
1848#if defined(EIGEN_SYCL_DEVICE_ONLY)
1849SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp, exp)
1852#if defined(EIGEN_GPUCC)
1854EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp(
const float& x) {
1859EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp(
const double& x) {
1866EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<float> exp(
const std::complex<float>& x) {
1867 float com = ::expf(x.real());
1868 float res_real = com * ::cosf(x.imag());
1869 float res_imag = x.imag() == 0.f ? x.imag() : com * ::sinf(x.imag());
1870 return std::complex<float>(res_real, res_imag);
1874EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<double> exp(
const std::complex<double>& x) {
1875 double com = ::exp(x.real());
1876 double res_real = com * ::cos(x.imag());
1877 double res_imag = x.imag() == 0.0 ? x.imag() : com * ::sin(x.imag());
1878 return std::complex<double>(res_real, res_imag);
1882template <
typename T>
1883EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp2(
const T& x) {
1884 EIGEN_USING_STD(exp2);
1889template <
typename RealScalar>
1890EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp2(
const std::complex<RealScalar>& x) {
1891 return internal::complex_exp2_impl<RealScalar>::run(x);
1894#if defined(EIGEN_SYCL_DEVICE_ONLY)
1895SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp2, exp2)
1898#if defined(EIGEN_GPUCC)
1900EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp2(
const float& x) {
1905EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp2(
const double& x) {
1912template <
typename T>
1913EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T ldexp(
const T& x,
int exponent) {
1914 EIGEN_USING_STD(ldexp);
1915 return static_cast<T
>(ldexp(x, exponent));
1918#if defined(EIGEN_SYCL_DEVICE_ONLY)
1920EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_float ldexp(
const cl::sycl::cl_float& x,
int exponent) {
1921 return cl::sycl::ldexp(x, exponent);
1924EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_double ldexp(
const cl::sycl::cl_double& x,
int exponent) {
1925 return cl::sycl::ldexp(x, exponent);
1929#if defined(EIGEN_GPUCC)
1931EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float ldexp(
const float& x,
int exponent) {
1932 return ::ldexpf(x, exponent);
1936EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double ldexp(
const double& x,
int exponent) {
1937 return ::ldexp(x, exponent);
1941template <
typename Scalar>
1942EIGEN_DEVICE_FUNC
inline Scalar expm1(
const Scalar& x) {
1943 return EIGEN_MATHFUNC_IMPL(expm1, Scalar)::run(x);
1946#if defined(EIGEN_SYCL_DEVICE_ONLY)
1947SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(expm1, expm1)
1950#if defined(EIGEN_GPUCC)
1952EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float expm1(
const float& x) {
1957EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double expm1(
const double& x) {
1962template <
typename T>
1963EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cos(
const T& x) {
1964 EIGEN_USING_STD(cos);
1968#if defined(EIGEN_SYCL_DEVICE_ONLY)
1969SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cos, cos)
1972#if defined(EIGEN_GPUCC)
1974EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cos(
const float& x) {
1979EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cos(
const double& x) {
1984template <
typename T>
1985EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sin(
const T& x) {
1986 EIGEN_USING_STD(sin);
1990#if defined(EIGEN_SYCL_DEVICE_ONLY)
1991SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sin, sin)
1994#if defined(EIGEN_GPUCC)
1996EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sin(
const float& x) {
2001EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sin(
const double& x) {
2006template <
typename T>
2007EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tan(
const T& x) {
2008 EIGEN_USING_STD(tan);
2012#if defined(EIGEN_SYCL_DEVICE_ONLY)
2013SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tan, tan)
2016#if defined(EIGEN_GPUCC)
2018EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tan(
const float& x) {
2023EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tan(
const double& x) {
2028template <
typename T>
2029EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acos(
const T& x) {
2030 EIGEN_USING_STD(acos);
2034template <
typename T>
2035EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acosh(
const T& x) {
2036 EIGEN_USING_STD(acosh);
2037 return static_cast<T
>(acosh(x));
2040#if defined(EIGEN_SYCL_DEVICE_ONLY)
2041SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acos, acos)
2042SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acosh, acosh)
2045#if defined(EIGEN_GPUCC)
2047EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float acos(
const float& x) {
2052EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double acos(
const double& x) {
2057template <
typename T>
2058EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asin(
const T& x) {
2059 EIGEN_USING_STD(asin);
2063template <
typename T>
2064EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asinh(
const T& x) {
2065 EIGEN_USING_STD(asinh);
2066 return static_cast<T
>(asinh(x));
2069#if defined(EIGEN_SYCL_DEVICE_ONLY)
2070SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asin, asin)
2071SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asinh, asinh)
2074#if defined(EIGEN_GPUCC)
2076EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float asin(
const float& x) {
2081EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double asin(
const double& x) {
2086template <
typename T>
2087EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan(
const T& x) {
2088 EIGEN_USING_STD(atan);
2089 return static_cast<T
>(atan(x));
2092template <typename T, std::enable_if_t<!NumTraits<T>::IsComplex,
int> = 0>
2093EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan2(
const T& y,
const T& x) {
2094 EIGEN_USING_STD(atan2);
2095 return static_cast<T
>(atan2(y, x));
2098template <
typename T>
2099EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atanh(
const T& x) {
2100 EIGEN_USING_STD(atanh);
2101 return static_cast<T
>(atanh(x));
2104#if defined(EIGEN_SYCL_DEVICE_ONLY)
2105SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(atan2, atan2)
2106SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atan, atan)
2107SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atanh, atanh)
2110#if defined(EIGEN_GPUCC)
2112EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float atan(
const float& x) {
2117EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double atan(
const double& x) {
2122template <
typename T>
2123EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cosh(
const T& x) {
2124 EIGEN_USING_STD(cosh);
2125 return static_cast<T
>(cosh(x));
2128#if defined(EIGEN_SYCL_DEVICE_ONLY)
2129SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cosh, cosh)
2132#if defined(EIGEN_GPUCC)
2134EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cosh(
const float& x) {
2139EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cosh(
const double& x) {
2144template <
typename T>
2145EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sinh(
const T& x) {
2146 EIGEN_USING_STD(sinh);
2147 return static_cast<T
>(sinh(x));
2150#if defined(EIGEN_SYCL_DEVICE_ONLY)
2151SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sinh, sinh)
2154#if defined(EIGEN_GPUCC)
2156EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sinh(
const float& x) {
2161EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sinh(
const double& x) {
2166template <
typename T>
2167EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tanh(
const T& x) {
2168 EIGEN_USING_STD(tanh);
2172#if (!defined(EIGEN_GPUCC)) && EIGEN_FAST_MATH && !defined(EIGEN_SYCL_DEVICE_ONLY)
2173EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
float x) {
return internal::ptanh_float(x); }
2176#if defined(EIGEN_SYCL_DEVICE_ONLY)
2177SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tanh, tanh)
2180#if defined(EIGEN_GPUCC)
2182EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
const float& x) {
2187EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tanh(
const double& x) {
2192template <
typename T>
2193EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(
const T& a,
const T& b) {
2194 EIGEN_USING_STD(fmod);
2198#if defined(EIGEN_SYCL_DEVICE_ONLY)
2199SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(fmod, fmod)
2202#if defined(EIGEN_GPUCC)
2204EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float fmod(
const float& a,
const float& b) {
2205 return ::fmodf(a, b);
2209EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double fmod(
const double& a,
const double& b) {
2210 return ::fmod(a, b);
2214template <
typename T>
2215EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T nextafter(
const T& from,
const T& to) {
2216 EIGEN_USING_STD(nextafter);
2217 return nextafter(from, to);
2225template <
typename Scalar,
2226 std::enable_if_t<!std::is_same<Scalar, long double>::value && std::numeric_limits<Scalar>::is_iec559 &&
2227 std::numeric_limits<Scalar>::radix == 2 &&
2228 (
sizeof(Scalar) ==
sizeof(uint16_t) ||
sizeof(Scalar) ==
sizeof(uint32_t) ||
2229 sizeof(Scalar) ==
sizeof(uint64_t)),
2231EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar ceil_power_of_two(
const Scalar& value) {
2232 using Binary = internal::binary_floating_point_traits<Scalar>;
2233 using Bits =
typename Binary::Bits;
2234 const Bits valueBits = Binary::bits(value);
2235 const Bits resultBits = Bits((valueBits + Binary::kFractionMask) & Binary::kExponentMask);
2236 return bit_cast<Scalar>(resultBits);
2241#if !defined(EIGEN_GPU_COMPILE_PHASE)
2242EIGEN_STRONG_INLINE
long double ceil_power_of_two(
const long double& value) {
2243 EIGEN_USING_STD(frexp);
2244 EIGEN_USING_STD(ldexp);
2246 const long double fraction = frexp(value, &exponent);
2247 if (numext::is_exactly_zero(fraction))
return 0.0L;
2248 if (numext::equal_strict(fraction, 0.5L)) --exponent;
2249 if (exponent < std::numeric_limits<long double>::min_exponent - 1)
2250 exponent = std::numeric_limits<long double>::min_exponent - 1;
2251 return ldexp(1.0L, exponent);
2255#if defined(EIGEN_SYCL_DEVICE_ONLY)
2256SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(nextafter, nextafter)
2259#if defined(EIGEN_GPUCC)
2261EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float nextafter(
const float& from,
const float& to) {
2262 return ::nextafterf(from, to);
2266EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double nextafter(
const double& from,
const double& to) {
2267 return ::nextafter(from, to);
2271#if defined(EIGEN_SYCL_DEVICE_ONLY)
2272#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY
2273#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY
2274#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY
2275#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY
2276#undef SYCL_SPECIALIZE_INTEGER_TYPES_BINARY
2277#undef SYCL_SPECIALIZE_INTEGER_TYPES_UNARY
2278#undef SYCL_SPECIALIZE_FLOATING_TYPES_BINARY
2279#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY
2280#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE
2281#undef SYCL_SPECIALIZE_GEN_UNARY_FUNC
2282#undef SYCL_SPECIALIZE_UNARY_FUNC
2283#undef SYCL_SPECIALIZE_GEN1_BINARY_FUNC
2284#undef SYCL_SPECIALIZE_GEN2_BINARY_FUNC
2285#undef SYCL_SPECIALIZE_BINARY_FUNC
2292template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2293EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_left(
const Scalar& a,
int n) {
2294 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2295 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) << n));
2298template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2299EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_right(
const Scalar& a,
int n) {
2300 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2301 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) >> n));
2310template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2311EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar arithmetic_shift_right(
const Scalar& a,
int n) {
2312 return static_cast<Scalar
>(a >> n);
2315template <
typename Scalar>
2316EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar fma(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2317 return internal::fma_impl<Scalar>::run(x, y, z);
2321template <
typename Scalar>
2322EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar madd(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2323 return internal::madd_impl<Scalar>::run(x, y, z);
2331template <
typename Scalar>
2332EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar twoprod_low_fma(
const Scalar& x,
const Scalar& y,
const Scalar& xy) {
2333 return numext::fma(x, y, Scalar(-xy));
2339template <
typename Scalar>
2340EIGEN_DEVICE_FUNC
void twoprod_full_range(
const Scalar& x,
const Scalar& y, Scalar& p_hi, Scalar& p_lo);
2342template <
typename T>
2343EIGEN_DEVICE_FUNC
bool isfinite_impl(
const std::complex<T>& x) {
2344 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2346 return static_cast<unsigned int>((numext::isfinite)(numext::real(x))) & (numext::isfinite)(numext::imag(x));
2348 return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
2352template <
typename T>
2353EIGEN_DEVICE_FUNC
bool isnan_impl(
const std::complex<T>& x) {
2354 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2355 return static_cast<unsigned int>((numext::isnan)(numext::real(x))) | (numext::isnan)(numext::imag(x));
2357 return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
2361template <
typename T>
2362EIGEN_DEVICE_FUNC
bool isinf_impl(
const std::complex<T>& x) {
2363 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2364 const bool has_inf =
static_cast<unsigned int>((numext::isinf)(numext::real(x))) | (numext::isinf)(numext::imag(x));
2365 const bool has_nan = (numext::isnan)(x);
2366 return has_inf & !has_nan;
2368 return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
2376template <
typename Scalar,
bool IsComplex,
bool IsInteger>
2377struct scalar_fuzzy_default_impl {};
2379template <
typename Scalar>
2380struct scalar_fuzzy_default_impl<Scalar, false, false> {
2381 using RealScalar =
typename NumTraits<Scalar>::Real;
2382 template <
typename OtherScalar>
2383 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2384 const RealScalar& prec) {
2385 return numext::abs(x) <= numext::abs(y) * prec;
2387 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2388 return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
2390 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2391 return x <= y || isApprox(x, y, prec);
2395template <
typename Scalar>
2396struct scalar_fuzzy_default_impl<Scalar, false, true> {
2397 using RealScalar =
typename NumTraits<Scalar>::Real;
2398 template <
typename OtherScalar>
2399 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const Scalar&,
const RealScalar&) {
2400 return x == Scalar(0);
2402 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
return x == y; }
2403 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
2410template <
typename Scalar>
2411struct scalar_fuzzy_default_impl<Scalar, true, false> {
2412 using RealScalar =
typename NumTraits<Scalar>::Real;
2413 template <
typename OtherScalar>
2414 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2415 const RealScalar& prec) {
2416 return numext::abs(x) <= numext::abs(y) * prec;
2418 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2419 return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
2423template <
typename Scalar>
2424struct scalar_fuzzy_impl
2425 : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
2427template <
typename Scalar,
typename OtherScalar>
2428EIGEN_DEVICE_FUNC
inline bool isMuchSmallerThan(
2429 const Scalar& x,
const OtherScalar& y,
2430 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2431 return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
2434template <
typename Scalar>
2435EIGEN_DEVICE_FUNC
inline bool isApprox(
2436 const Scalar& x,
const Scalar& y,
2437 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2438 return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
2441template <
typename Scalar>
2442EIGEN_DEVICE_FUNC
inline bool isApproxOrLessThan(
2443 const Scalar& x,
const Scalar& y,
2444 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2445 return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
2453struct scalar_fuzzy_impl<bool> {
2454 using RealScalar = bool;
2456 template <
typename OtherScalar>
2457 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const bool& x,
const bool&,
const bool&) {
2461 EIGEN_DEVICE_FUNC
static inline bool isApprox(
bool x,
bool y,
bool) {
return x == y; }
2463 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const bool& x,
const bool& y,
const bool&) {
2474template <
typename RealScalar>
2475struct expm1_impl<std::complex<RealScalar>> {
2476 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2478 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2479 RealScalar xr = x.real();
2480 RealScalar xi = x.imag();
2490 RealScalar erm1 = numext::expm1<RealScalar>(xr);
2491 RealScalar er = erm1 + RealScalar(1.);
2494 if ((numext::isinf)(xr) && !(numext::isfinite)(xi)) {
2495 return xr > RealScalar(0) ? std::complex<RealScalar>(xr, xi * RealScalar(0))
2496 : std::complex<RealScalar>(RealScalar(-1), RealScalar(0));
2498 const RealScalar s = numext::sin(xi);
2499 const RealScalar c = numext::cos(xi);
2501 const RealScalar one_minus_c = c > RealScalar(0) ? s * s / (RealScalar(1) + c) : RealScalar(1) - c;
2504 if ((numext::isinf)(er) && (numext::isfinite)(xr)) {
2505 const RealScalar h = numext::exp(xr / RealScalar(2));
2506 if (!(numext::isinf)(h)) {
2507 return std::complex<RealScalar>((h * c) * h - RealScalar(1), numext::is_exactly_zero(xi) ? xi : (h * s) * h);
2509 const RealScalar h3 = numext::exp(xr / RealScalar(3));
2510 return std::complex<RealScalar>(((h3 * c) * h3) * h3, numext::is_exactly_zero(xi) ? xi : ((h3 * s) * h3) * h3);
2514 const RealScalar real_part = xr < RealScalar(0) ? erm1 - er * one_minus_c : erm1 * c - one_minus_c;
2516 return std::complex<RealScalar>(real_part, numext::is_exactly_zero(xi) ? xi : er * s);
2520template <
typename RealScalar>
2521struct log1p_impl<std::complex<RealScalar>> {
2522 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2524 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& z) {
2525 const RealScalar x = z.real();
2526 const RealScalar y = z.imag();
2529 if (numext::abs(x) < RealScalar(0.5) && numext::abs(y) < RealScalar(0.5) && !numext::is_exactly_zero(y)) {
2530 return std::complex<RealScalar>(numext::log1p(x * (RealScalar(2) + x) + y * y) / RealScalar(2),
2531 numext::atan2(y, RealScalar(1) + x));
2533 return std_fallback::log1p(z);
2542template <
typename RealScalar>
2543struct complex_exp2_impl {
2544 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2546 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2547 using ComplexScalar = std::complex<RealScalar>;
2548 const RealScalar a = x.real();
2549 const RealScalar b = x.imag();
2551 if (numext::is_exactly_zero(b))
return ComplexScalar(numext::exp2(a), b);
2554 if ((numext::isinf)(a) && !(numext::isfinite)(b)) {
2555 return a > RealScalar(0) ? ComplexScalar(a, b * RealScalar(0)) : ComplexScalar(RealScalar(0), RealScalar(0));
2559 constexpr bool kWiderThanDouble = NumTraits<RealScalar>::digits() > NumTraits<double>::digits();
2560 using Wide = std::conditional_t<kWiderThanDouble, RealScalar, double>;
2561 const RealScalar ln2_hi = RealScalar(EIGEN_LN2);
2562 const RealScalar ln2_lo = RealScalar((Wide(
static_cast<double>(EIGEN_LN2)) - Wide(ln2_hi)) +
2563 Wide(2.3190468138462996154948554638754786504e-17L));
2566 const bool negative = b < RealScalar(0);
2567 const RealScalar abs_b = numext::abs(b);
2568 RealScalar t_hi, t_lo;
2569 twoprod_full_range(abs_b, ln2_hi, t_hi, t_lo);
2570 t_lo += abs_b * ln2_lo;
2573 RealScalar sin_lo = t_lo;
2574 RealScalar cos_lo_m1 = -t_lo * t_lo / RealScalar(2);
2575 if (numext::abs(t_lo) > numext::sqrt(NumTraits<RealScalar>::epsilon())) {
2576 sin_lo = numext::sin(t_lo);
2577 const RealScalar sin_half = numext::sin(t_lo / RealScalar(2));
2578 cos_lo_m1 = RealScalar(-2) * sin_half * sin_half;
2580 const RealScalar cos_hi = numext::cos(t_hi);
2581 const RealScalar sin_hi = numext::sin(t_hi);
2582 const RealScalar c = cos_hi + (cos_hi * cos_lo_m1 - sin_hi * sin_lo);
2583 const RealScalar s = sin_hi + (sin_hi * cos_lo_m1 + cos_hi * sin_lo);
2586 const int e_max = NumTraits<RealScalar>::max_exponent() - 1;
2587 const int e = a > RealScalar(e_max) ? (a > RealScalar(2 * e_max) ? 2 * e_max : e_max)
2588 : (a < RealScalar(NumTraits<RealScalar>::min_exponent()) ? -e_max : 0);
2589 const RealScalar m = numext::exp2(a - RealScalar(e));
2590 if (numext::abs(t_hi) < (numext::numeric_limits<RealScalar>::min)()) {
2592 const int p = NumTraits<RealScalar>::digits();
2593 const RealScalar b_p = numext::ldexp(abs_b, p);
2594 RealScalar u_hi, u_lo;
2595 twoprod_full_range(b_p, ln2_hi, u_hi, u_lo);
2596 const RealScalar im = numext::ldexp(m * (u_hi + (u_lo + b_p * ln2_lo)), e - p);
2597 return ComplexScalar(numext::ldexp(m, e), negative ? -im : im);
2599 const RealScalar re = e == 0 ? m * c : numext::ldexp(m * c, e);
2600 const RealScalar im = e == 0 ? m * s : numext::ldexp(m * s, e);
2601 return ComplexScalar(re, negative ? -im : im);
2605template <
typename T>
2609#pragma warning(push)
2610#pragma warning(disable : 4804)
2612 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE T run(
const T& x) {
return T(1) / numext::sqrt(x); }
2618#if defined(EIGEN_GPU_COMPILE_PHASE)
2619template <
typename T>
2620struct conj_impl<std::complex<T>, true> {
2621 EIGEN_DEVICE_FUNC
static inline std::complex<T> run(
const std::complex<T>& x) {
2622 return std::complex<T>(numext::real(x), -numext::imag(x));
2630template <
typename T>
2631EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_multiply(
const std::complex<T>& a,
2632 const std::complex<T>& b) {
2633 const T a_real = numext::real(a);
2634 const T a_imag = numext::imag(a);
2635 const T b_real = numext::real(b);
2636 const T b_imag = numext::imag(b);
2637 return std::complex<T>(a_real * b_real - a_imag * b_imag, a_imag * b_real + a_real * b_imag);
2640template <
typename T>
2641EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_fast(
const std::complex<T>& a,
2642 const std::complex<T>& b) {
2643 const T a_real = numext::real(a);
2644 const T a_imag = numext::imag(a);
2645 const T b_real = numext::real(b);
2646 const T b_imag = numext::imag(b);
2647 const T norm = (b_real * b_real + b_imag * b_imag);
2648 return std::complex<T>((a_real * b_real + a_imag * b_imag) / norm, (a_imag * b_real - a_real * b_imag) / norm);
2651template <
typename T>
2652EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_smith(
const std::complex<T>& a,
2653 const std::complex<T>& b) {
2654 const T a_real = numext::real(a);
2655 const T a_imag = numext::imag(a);
2656 T b_real = numext::real(b);
2657 T b_imag = numext::imag(b);
2660 const bool scale_imag = numext::abs(b_imag) <= numext::abs(b_real);
2663 if (numext::abs(b_imag) == numext::abs(b_real)) {
2667 iscale = numext::copysign(T(1), b_real) * numext::copysign(T(1), b_imag);
2668 }
else if (scale_imag) {
2672 volatile_barrier(b_real);
2673 iscale = b_imag / b_real;
2675 volatile_barrier(b_imag);
2676 rscale = b_real / b_imag;
2678 const T denominator = b_real * rscale + b_imag * iscale;
2679 return std::complex<T>((a_real * rscale + a_imag * iscale) / denominator,
2680 (a_imag * rscale - a_real * iscale) / denominator);
2683template <
typename T>
2684EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide(
const std::complex<T>& a,
2685 const std::complex<T>& b) {
2687 return complex_divide_fast(a, b);
2689 return complex_divide_smith(a, b);
2705template <
typename LhsScalar,
typename RhsScalar>
2706EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
auto divide(
const LhsScalar& a,
const RhsScalar& b) ->
decltype(a / b) {
2710template <
typename T>
2711EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const std::complex<T>& b) {
2712 return internal::complex_divide_smith(a, b);
2715template <
typename T>
2716EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const T& a,
const std::complex<T>& b) {
2717 return internal::complex_divide_smith(std::complex<T>(a, T(0)), b);
2720template <
typename T>
2721EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const T& b) {