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
>(
static_cast<NewReal
>(x));
379template <
typename OldType,
typename NewType>
380EIGEN_DEVICE_FUNC
inline NewType cast(
const OldType& x) {
381 return cast_impl<OldType, NewType>::run(x);
390#if (!EIGEN_COMP_MSVC || EIGEN_COMP_MSVC >= 1920)
392template <typename Scalar, bool HasStdImpl = NumTraits<Scalar>::IsComplex || std::is_integral<Scalar>::value ||
393 std::is_same<Scalar, float>::value ||
394 std::is_same<Scalar, double>::value ||
395 std::is_same<Scalar, long double>::value>
396struct arg_default_impl;
398template <
typename Scalar>
399struct arg_default_impl<Scalar, true> {
400 using RealScalar =
typename NumTraits<Scalar>::Real;
401 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
404 return static_cast<RealScalar
>(arg(x));
409template <
typename Scalar>
410struct arg_default_impl<Scalar, false> {
411 using RealScalar =
typename NumTraits<Scalar>::Real;
412 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
413 return (x < Scalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
417template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
418struct arg_default_impl {
419 using RealScalar =
typename NumTraits<Scalar>::Real;
420 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
421 return (x < RealScalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
425template <
typename Scalar>
426struct arg_default_impl<Scalar, true> {
427 using RealScalar =
typename NumTraits<Scalar>::Real;
428 EIGEN_DEVICE_FUNC
static inline RealScalar run(
const Scalar& x) {
429 EIGEN_USING_STD(arg);
434template <
typename Scalar>
435struct arg_impl : arg_default_impl<Scalar> {};
442namespace std_fallback {
446template <
typename Scalar>
447EIGEN_DEVICE_FUNC
inline Scalar expm1(
const Scalar& x) {
448 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
449 using RealScalar =
typename NumTraits<Scalar>::Real;
451 EIGEN_USING_STD(exp);
453 if (numext::equal_strict(u, Scalar(1))) {
456 Scalar um1 = u - RealScalar(1);
457 if (numext::equal_strict(um1, Scalar(-1))) {
458 return RealScalar(-1);
461 EIGEN_USING_STD(log);
462 Scalar logu = log(u);
463 return numext::equal_strict(u, logu) ? u : (u - RealScalar(1)) * x / logu;
467template <
typename Scalar>
469 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
470 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
471 EIGEN_USING_STD(expm1);
481template <
typename ComplexT>
482EIGEN_DEVICE_FUNC
constexpr ComplexT complex_log(
const ComplexT& z);
484template <
typename Scalar>
486 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
487 EIGEN_USING_STD(log);
488 return static_cast<Scalar
>(log(x));
492template <
typename Scalar>
493struct log_impl<std::complex<Scalar>> {
494 EIGEN_DEVICE_FUNC
static inline std::complex<Scalar> run(
const std::complex<Scalar>& z) {
return complex_log(z); }
501namespace std_fallback {
504template <
typename Scalar>
505EIGEN_DEVICE_FUNC
inline Scalar log1p(
const Scalar& x) {
506 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
507 using RealScalar =
typename NumTraits<Scalar>::Real;
508 EIGEN_USING_STD(log);
509 Scalar x1p = RealScalar(1) + x;
510 Scalar log_1p = log_impl<Scalar>::run(x1p);
511 const bool is_small = numext::equal_strict(x1p, Scalar(1));
513 EIGEN_USING_STD(isfinite);
514 if (!((isfinite)(numext::real(log_1p)) && (isfinite)(numext::imag(log_1p))))
return log_1p;
515 return is_small ? x : x * (log_1p / (x1p - RealScalar(1)));
519template <
typename Scalar>
521 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
523 EIGEN_DEVICE_FUNC
static inline Scalar run(
const Scalar& x) {
524 EIGEN_USING_STD(log1p);
530template <
typename RealScalar>
531struct log1p_impl<std::complex<RealScalar>> {
532 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
534 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
535 return std_fallback::log1p(x);
540template <
typename RealScalar>
541struct complex_exp2_impl;
547template <
typename ScalarX,
typename ScalarY,
548 bool IsInteger = NumTraits<ScalarX>::IsInteger && NumTraits<ScalarY>::IsInteger>
551 typename ScalarBinaryOpTraits<ScalarX, ScalarY, internal::scalar_pow_op<ScalarX, ScalarY>>::ReturnType;
552 static EIGEN_DEVICE_FUNC
inline result_type run(
const ScalarX& x,
const ScalarY& y) {
553 EIGEN_USING_STD(pow);
558template <
typename ScalarX,
typename ScalarY>
559struct pow_impl<ScalarX, ScalarY, true> {
560 using result_type = ScalarX;
561 static EIGEN_DEVICE_FUNC
inline ScalarX run(ScalarX x, ScalarY y) {
563 eigen_assert(!NumTraits<ScalarY>::IsSigned || y >= 0);
564 if (y & 1) res = internal::mul(res, x);
567 x = internal::mul(x, x);
568 if (y & 1) res = internal::mul(res, x);
575constexpr int floor_log2(
unsigned int value) {
584template <
typename BitsType,
typename EnableIf =
void>
585struct count_bits_impl {
586 static_assert(std::is_integral<BitsType>::value && std::is_unsigned<BitsType>::value,
587 "BitsType must be an unsigned integer");
588 static EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
589 int n = CHAR_BIT *
sizeof(BitsType);
591 while (bits > 0 && shift > 0) {
592 BitsType y = bits >> shift;
605 static EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
606 int n = CHAR_BIT *
sizeof(BitsType);
608 while (bits > 0 && shift > 0) {
609 BitsType y = bits << shift;
622 static EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
633template <
typename BitsType>
634EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
635 return count_bits_impl<BitsType>::clz(bits);
639template <
typename BitsType>
640EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
641 return count_bits_impl<BitsType>::ctz(bits);
645template <
typename BitsType>
646EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
647 return count_bits_impl<BitsType>::popcount(bits);
650#if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
652template <
typename BitsType>
653struct count_bits_impl<
654 BitsType, std::enable_if_t<std::is_integral<BitsType>::value && sizeof(BitsType) <= sizeof(unsigned int)>> {
655 static constexpr int kNumBits =
static_cast<int>(
sizeof(BitsType) * CHAR_BIT);
656 static EIGEN_DEVICE_FUNC
inline int clz(BitsType bits) {
657 static constexpr int kLeadingBitsOffset = (
sizeof(
unsigned int) -
sizeof(BitsType)) * CHAR_BIT;
658 return bits == 0 ? kNumBits : __builtin_clz(
static_cast<unsigned int>(bits)) - kLeadingBitsOffset;
661 static EIGEN_DEVICE_FUNC
inline int ctz(BitsType bits) {
662 return bits == 0 ? kNumBits : __builtin_ctz(
static_cast<unsigned int>(bits));
665 static EIGEN_DEVICE_FUNC
inline int popcount(BitsType bits) {
666 return __builtin_popcount(
static_cast<unsigned int>(bits));
670template <
typename BitsType>
671struct count_bits_impl<BitsType,
672 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned int) < sizeof(BitsType) &&
673 sizeof(BitsType) <= sizeof(unsigned long)>> {
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 long) - sizeof(BitsType)) * CHAR_BIT;
677 return bits == 0 ? kNumBits : __builtin_clzl(static_cast<unsigned long>(bits)) - kLeadingBitsOffset;
680 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
681 return bits == 0 ? kNumBits : __builtin_ctzl(static_cast<unsigned long>(bits));
684 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
685 return __builtin_popcountl(static_cast<unsigned long>(bits));
689template <typename BitsType>
690struct count_bits_impl<BitsType,
691 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned long) < sizeof(BitsType) &&
692 sizeof(BitsType) <= sizeof(unsigned long long)>> {
693 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
694 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
695 static constexpr int kLeadingBitsOffset = (sizeof(unsigned long long) - sizeof(BitsType)) * CHAR_BIT;
696 return bits == 0 ? kNumBits : __builtin_clzll(static_cast<unsigned long long>(bits)) - kLeadingBitsOffset;
699 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
700 return bits == 0 ? kNumBits : __builtin_ctzll(static_cast<unsigned long long>(bits));
703 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
704 return __builtin_popcountll(static_cast<unsigned long long>(bits));
714template <typename BitsType>
715EIGEN_DEVICE_FUNC inline int popcount_fallback(BitsType bits) {
724template <typename BitsType>
725struct count_bits_impl<
726 BitsType, std::enable_if_t<std::is_integral<BitsType>::value && sizeof(BitsType) <= sizeof(unsigned long)>> {
727 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
728 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
730 _BitScanReverse(&out, static_cast<unsigned long>(bits));
731 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
734 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
736 _BitScanForward(&out, static_cast<unsigned long>(bits));
737 return bits == 0 ? kNumBits : static_cast<int>(out);
740 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
741#if defined(EIGEN_VECTORIZE_SSE4_2)
742 return static_cast<int>(__popcnt(static_cast<unsigned int>(bits)));
744 return popcount_fallback(bits);
751template <typename BitsType>
752struct count_bits_impl<BitsType,
753 std::enable_if_t<std::is_integral<BitsType>::value && sizeof(unsigned long) < sizeof(BitsType) &&
754 sizeof(BitsType) <= sizeof(__int64)>> {
755 static constexpr int kNumBits = static_cast<int>(sizeof(BitsType) * CHAR_BIT);
756 static EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
758 _BitScanReverse64(&out, static_cast<unsigned __int64>(bits));
759 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
762 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
764 _BitScanForward64(&out, static_cast<unsigned __int64>(bits));
765 return bits == 0 ? kNumBits : static_cast<int>(out);
768 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
769#if defined(EIGEN_VECTORIZE_SSE4_2)
770 return static_cast<int>(__popcnt64(static_cast<unsigned __int64>(bits)));
772 return popcount_fallback(bits);
781template <typename BitsType>
783 static constexpr int kTotalBits = sizeof(BitsType) * CHAR_BIT;
784 static EIGEN_DEVICE_FUNC inline int run_ceil(const BitsType& x) {
785 const int n = kTotalBits - clz(x);
786 bool power_of_two = (x & (x - 1)) == 0;
787 return x == 0 ? 0 : power_of_two ? (n - 1) : n;
789 static EIGEN_DEVICE_FUNC inline int run_floor(const BitsType& x) {
790 const int n = kTotalBits - clz(x);
791 return x == 0 ? 0 : n - 1;
795template <typename BitsType>
796int log2_ceil(const BitsType& x) {
797 return log_2_impl<BitsType>::run_ceil(x);
800template <typename BitsType>
801int log2_floor(const BitsType& x) {
802 return log_2_impl<BitsType>::run_floor(x);
808EIGEN_DEVICE_FUNC std::enable_if_t<!(std::numeric_limits<T>::has_infinity || std::numeric_limits<T>::has_quiet_NaN ||
809 std::numeric_limits<T>::has_signaling_NaN),
811isfinite_impl(const T&) {
816EIGEN_DEVICE_FUNC std::enable_if_t<(std::numeric_limits<T>::has_infinity || std::numeric_limits<T>::has_quiet_NaN ||
817 std::numeric_limits<T>::has_signaling_NaN) &&
818 (!NumTraits<T>::IsComplex),
820isfinite_impl(const T& x) {
821 EIGEN_USING_STD(isfinite);
822 return isfinite EIGEN_NOT_A_MACRO(x);
826EIGEN_DEVICE_FUNC std::enable_if_t<!std::numeric_limits<T>::has_infinity, bool> isinf_impl(const T&) {
831EIGEN_DEVICE_FUNC std::enable_if_t<(std::numeric_limits<T>::has_infinity && !NumTraits<T>::IsComplex), bool> isinf_impl(
833 EIGEN_USING_STD(isinf);
834 return isinf EIGEN_NOT_A_MACRO(x);
839 std::enable_if_t<!(std::numeric_limits<T>::has_quiet_NaN || std::numeric_limits<T>::has_signaling_NaN), bool>
840 isnan_impl(const T&) {
845EIGEN_DEVICE_FUNC std::enable_if_t<
846 (std::numeric_limits<T>::has_quiet_NaN || std::numeric_limits<T>::has_signaling_NaN) && (!NumTraits<T>::IsComplex),
848isnan_impl(const T& x) {
849 EIGEN_USING_STD(isnan);
850 return isnan EIGEN_NOT_A_MACRO(x);
855EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x);
857EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x);
859EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x);
861EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS T ptanh_float(const T& a_x);
866template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
867 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0)>
869 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) { return Scalar((a > Scalar(0)) - (a < Scalar(0))); }
872template <typename Scalar>
873struct sign_impl<Scalar, false, false> {
874 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) {
875 return (isnan_impl<Scalar>)(a) ? a : Scalar((a > Scalar(0)) - (a < Scalar(0)));
879template <typename Scalar, bool IsInteger>
880struct sign_impl<Scalar, true, IsInteger> {
881 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) {
882 using real_type = typename NumTraits<Scalar>::Real;
883 EIGEN_USING_STD(abs);
884 real_type aa = abs(a);
885 if (aa == real_type(0)) return Scalar(0);
889 return Scalar(numext::real(a) / aa, numext::imag(a) / aa);
895struct sign_impl<bool, false, true> {
896 EIGEN_DEVICE_FUNC static inline bool run(const bool& a) { return a; }
899template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
900 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0)>
901struct copysign_impl {
902 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
903 EIGEN_USING_STD(copysign);
904 return Scalar(copysign(a, b));
908template <typename Scalar, bool IsInteger>
909struct copysign_impl<Scalar, true, IsInteger> {
910 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
911 EIGEN_USING_STD(copysign);
912 return Scalar(copysign(numext::real(a), numext::real(b)), copysign(numext::imag(a), numext::imag(b)));
916template <typename Scalar>
917struct copysign_impl<Scalar, false, true> {
918 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
919 EIGEN_IF_CONSTEXPR (!NumTraits<Scalar>::IsSigned) return a;
920 const Scalar abs_a = a < Scalar(0) ? -a : a;
921 return b < Scalar(0) ? -abs_a : abs_a;
926struct copysign_impl<bool, false, true> {
927 EIGEN_DEVICE_FUNC static inline bool run(const bool& a, const bool&) { return a; }
933template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger>
935 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return -a; }
938template <typename Scalar>
939struct negate_impl<Scalar, true> {
940 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), NEGATE IS NOT DEFINED FOR BOOLEAN TYPES)
941 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return Scalar(0) - a; }
944template <typename Scalar, bool IsInteger = NumTraits<typename unpacket_traits<Scalar>::type>::IsInteger>
945struct nearest_integer_impl {
946 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) {
947 EIGEN_USING_STD(floor) return floor(x);
949 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) {
950 EIGEN_USING_STD(ceil) return ceil(x);
952 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) {
953 EIGEN_USING_STD(rint) return rint(x);
955 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) {
956 EIGEN_USING_STD(round) return round(x);
958 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) {
959 EIGEN_USING_STD(trunc) return trunc(x);
962template <typename Scalar>
963struct nearest_integer_impl<Scalar, true> {
964 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) { return x; }
965 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) { return x; }
966 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) { return x; }
967 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) { return x; }
968 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) { return x; }
972namespace has_fma_detail {
974template <typename T, typename EnableIf = void>
975struct has_fma_impl : public std::false_type {};
981 T, std::enable_if_t<std::is_same<T, decltype(fma(std::declval<T>(), std::declval<T>(), std::declval<T>()))>::value>>
982 : public std::true_type {};
987struct has_fma : public has_fma_detail::has_fma_impl<T> {};
990template <typename T, typename Enable = void>
992 static_assert(has_fma<T>::value, "No function fma(...) for type. Please provide an implementation.");
997struct fma_impl<T, std::enable_if_t<has_fma<T>::value>> {
998 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T run(const T& a, const T& b, const T& c) {
1000 return fma(a, b, c);
1004#if defined(EIGEN_GPUCC)
1006struct has_fma<float> : public std::true_type {};
1009struct fma_impl<float, void> {
1010 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float run(const float& a, const float& b, const float& c) {
1011 return ::fmaf(a, b, c);
1016struct has_fma<double> : public std::true_type {};
1019struct fma_impl<double, void> {
1020 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double run(const double& a, const double& b, const double& c) {
1021 return ::fma(a, b, c);
1027template <typename Scalar, typename EnableIf = void>
1029 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1034#if EIGEN_SCALAR_MADD_USE_FMA
1035template <typename Scalar>
1036struct madd_impl<Scalar, std::enable_if_t<has_fma<Scalar>::value>> {
1037 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1038 return fma_impl<Scalar>::run(x, y, z);
1044template <typename Scalar>
1045struct binary_floating_point_traits {
1046 using Bits = typename numext::get_integer_by_size<sizeof(Scalar)>::unsigned_type;
1047 static constexpr int kFractionBits = std::numeric_limits<Scalar>::digits - 1;
1048 static constexpr int kExponentBits = int(sizeof(Scalar) * CHAR_BIT) - std::numeric_limits<Scalar>::digits;
1049 static constexpr int kExponentBias = std::numeric_limits<Scalar>::max_exponent - 1;
1050 static constexpr Bits kSignBit = Bits(1) << (sizeof(Scalar) * CHAR_BIT - 1);
1051 static constexpr Bits kExponentUnit = Bits(1) << kFractionBits;
1052 static constexpr Bits kFractionMask = kExponentUnit - 1;
1053 static constexpr Bits kExponentMask = ((Bits(1) << kExponentBits) - 1) << kFractionBits;
1054 static constexpr Bits kMaxFiniteExponentBits = kExponentMask - kExponentUnit;
1056 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits bits(const Scalar& value) { return numext::bit_cast<Bits>(value); }
1058 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits magnitude(const Scalar& value) { return bits(value) & ~kSignBit; }
1061template <typename Scalar>
1062EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, false_type) {
1063 return numext::is_exactly_zero(value);
1066template <typename Scalar>
1067EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE bool is_zero_magnitude_bits(
1068 typename binary_floating_point_traits<Scalar>::Bits magnitude) {
1071 return magnitude == 0;
1075template <typename Scalar>
1076struct sign_impl_binary {
1077 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar& a) {
1078 using Binary = binary_floating_point_traits<Scalar>;
1079 using Bits = typename Binary::Bits;
1080 const Bits bits = Binary::bits(a);
1081 const Bits magnitude = bits & ~Binary::kSignBit;
1083 const Bits nonzero = (magnitude | (Bits(0) - magnitude)) >> (sizeof(Bits) * CHAR_BIT - 1);
1084 const Bits nonzero_mask = Bits(0) - nonzero;
1085 const Bits nan_mask = Bits(0) - Bits(magnitude > Binary::kExponentMask);
1086 const Bits signed_one = (bits & Binary::kSignBit) | Binary::bits(Scalar(1));
1087 return numext::bit_cast<Scalar>((bits & nan_mask) | (signed_one & nonzero_mask & ~nan_mask));
1092struct sign_impl<float, false, false> : sign_impl_binary<float> {};
1095struct sign_impl<double, false, false> : sign_impl_binary<double> {};
1097template <typename Scalar>
1098EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, true_type) {
1099 return is_zero_magnitude_bits<Scalar>(binary_floating_point_traits<Scalar>::magnitude(value));
1103template <typename T, bool IsInteger = NumTraits<T>::IsInteger>
1104struct absdiff_impl {
1105 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) {
1106 EIGEN_USING_STD(abs);
1112template <typename T>
1113struct absdiff_impl<T, true> {
1114 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) { return x > y ? x - y : y - x; }
1125template <typename X>
1126EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush(const X& x) {
1127 constexpr bool HasBinaryEncoding = (std::is_same<X, float>::value || std::is_same<X, double>::value) &&
1128 std::numeric_limits<X>::is_iec559 && std::numeric_limits<X>::radix == 2 &&
1129 (sizeof(X) == sizeof(uint32_t) || sizeof(X) == sizeof(uint64_t));
1130 return internal::is_exactly_zero_no_flush_impl(x, internal::bool_constant<HasBinaryEncoding>());
1133#if !defined(EIGEN_GPUCC)
1134template <typename T>
1135EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1136 EIGEN_USING_STD(min)
1137 return min EIGEN_NOT_A_MACRO(x, y);
1140template <typename T>
1141EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1142 EIGEN_USING_STD(max)
1143 return max EIGEN_NOT_A_MACRO(x, y);
1146template <typename T>
1147EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1148 return y < x ? y : x;
1150#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1155EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y) {
1159EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double mini(const double& x, const double& y) {
1163#ifndef EIGEN_GPU_COMPILE_PHASE
1165EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double mini(const long double& x, const long double& y) {
1166#if defined(EIGEN_HIPCC)
1168 return (x < y) ? x : y;
1176template <typename T>
1177EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1178 return x < y ? y : x;
1180#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1182EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y) {
1186EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double maxi(const double& x, const double& y) {
1189#ifndef EIGEN_GPU_COMPILE_PHASE
1191EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double maxi(const long double& x, const long double& y) {
1192#if defined(EIGEN_HIPCC)
1194 return (x > y) ? x : y;
1203#if defined(SYCL_DEVICE_ONLY)
1205#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1206 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1207 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1208 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1209 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1210#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1211 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1212 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1213 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1214 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1215#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1216 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1217 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1218 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1219 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1220#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1221 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1222 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1223 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1224 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1225#define SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(NAME, FUNC) \
1226 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1227 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC)
1228#define SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(NAME, FUNC) \
1229 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1230 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC)
1231#define SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(NAME, FUNC) \
1232 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1233 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1234#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(NAME, FUNC) \
1235 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1236 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1237#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(NAME, FUNC, RET_TYPE) \
1238 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_float) \
1239 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_double)
1241#define SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1243 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE& x) { \
1244 return cl::sycl::FUNC(x); \
1247#define SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, TYPE, TYPE)
1249#define SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE1, ARG_TYPE2) \
1251 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE1& x, const ARG_TYPE2& y) { \
1252 return cl::sycl::FUNC(x, y); \
1255#define SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1256 SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE, ARG_TYPE)
1258#define SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, TYPE, TYPE)
1260SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(mini, min)
1261SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(mini, fmin)
1262SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(maxi, max)
1263SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(maxi, fmax)
1267template <typename Scalar>
1268EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real arg(const Scalar& x) {
1269 return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
1272template <typename Scalar>
1273EIGEN_DEVICE_FUNC inline auto imag_ref(const Scalar& x)
1274 -> internal::add_const_on_value_type_t<decltype(internal::imag_ref_impl<Scalar>::run(x))> {
1275 return internal::imag_ref_impl<Scalar>::run(x);
1278template <typename Scalar>
1279EIGEN_DEVICE_FUNC inline auto imag_ref(Scalar& x)
1280 -> decltype(EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x)) {
1281 return EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x);
1284template <typename Scalar>
1285EIGEN_DEVICE_FUNC inline Scalar conj(const Scalar& x) {
1286 return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
1289template <typename Scalar>
1290EIGEN_DEVICE_FUNC inline Scalar sign(const Scalar& x) {
1291 return EIGEN_MATHFUNC_IMPL(sign, Scalar)::run(x);
1294template <typename Scalar>
1295EIGEN_DEVICE_FUNC inline Scalar copysign(const Scalar& x, const Scalar& y) {
1296 return EIGEN_MATHFUNC_IMPL(copysign, Scalar)::run(x, y);
1299template <typename Scalar>
1300EIGEN_DEVICE_FUNC inline Scalar negate(const Scalar& x) {
1301 return EIGEN_MATHFUNC_IMPL(negate, Scalar)::run(x);
1304template <typename Scalar>
1305EIGEN_DEVICE_FUNC inline decltype(auto) abs2(const Scalar& x) {
1306 return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
1309EIGEN_DEVICE_FUNC inline bool abs2(bool x) { return x; }
1311template <typename T>
1312EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T absdiff(const T& x, const T& y) {
1313 return internal::absdiff_impl<T>::run(x, y);
1316EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float absdiff(const float& x, const float& y) {
1317 return fabsf(x - y);
1320EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double absdiff(const double& x, const double& y) {
1325#ifndef EIGEN_GPU_COMPILE_PHASE
1327EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double absdiff(const long double& x, const long double& y) {
1328 return fabsl(x - y);
1332template <typename Scalar>
1333EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real norm1(const Scalar& x) {
1334 return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
1337template <typename Scalar>
1338EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real hypot(const Scalar& x, const Scalar& y) {
1339 return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
1342#if defined(SYCL_DEVICE_ONLY)
1343SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(hypot, hypot)
1346template <typename Scalar>
1347EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
1348 return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
1351#if defined(SYCL_DEVICE_ONLY)
1352SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log1p, log1p)
1355#if defined(EIGEN_GPUCC)
1357EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log1p(const float& x) {
1362EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log1p(const double& x) {
1367template <typename ScalarX, typename ScalarY>
1368EIGEN_DEVICE_FUNC inline typename internal::pow_impl<ScalarX, ScalarY>::result_type pow(const ScalarX& x,
1370 return internal::pow_impl<ScalarX, ScalarY>::run(x, y);
1373#if defined(SYCL_DEVICE_ONLY)
1374SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(pow, pow)
1377template <typename T>
1378EIGEN_DEVICE_FUNC bool(isnan)(const T& x) {
1379 return internal::isnan_impl(x);
1381template <typename T>
1382EIGEN_DEVICE_FUNC bool(isinf)(const T& x) {
1383 return internal::isinf_impl(x);
1385template <typename T>
1386EIGEN_DEVICE_FUNC bool(isfinite)(const T& x) {
1387 return internal::isfinite_impl(x);
1390#if defined(SYCL_DEVICE_ONLY)
1391SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isnan, isnan, bool)
1392SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isinf, isinf, bool)
1393SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isfinite, isfinite, bool)
1396template <typename Scalar>
1397EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar rint(const Scalar& x) {
1398 return internal::nearest_integer_impl<Scalar>::run_rint(x);
1401template <typename Scalar>
1402EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar round(const Scalar& x) {
1403 return internal::nearest_integer_impl<Scalar>::run_round(x);
1406template <typename Scalar>
1407EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(floor)(const Scalar& x) {
1408 return internal::nearest_integer_impl<Scalar>::run_floor(x);
1411template <typename Scalar>
1412EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(ceil)(const Scalar& x) {
1413 return internal::nearest_integer_impl<Scalar>::run_ceil(x);
1416template <typename Scalar>
1417EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(trunc)(const Scalar& x) {
1418 return internal::nearest_integer_impl<Scalar>::run_trunc(x);
1421#if defined(SYCL_DEVICE_ONLY)
1422SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(round, round)
1423SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(floor, floor)
1424SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(ceil, ceil)
1425SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(trunc, trunc)
1428#if defined(EIGEN_GPUCC)
1430EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float floor(const float& x) {
1434EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double floor(const double& x) {
1438EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float ceil(const float& x) {
1442EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double ceil(const double& x) {
1446EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float trunc(const float& x) {
1450EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double trunc(const double& x) {
1457template <typename T>
1458EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T div_ceil(T a, T b) {
1459 using UnsignedT = std::make_unsigned_t<T>;
1460 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1462 const UnsignedT ua = UnsignedT(a);
1463 const UnsignedT ub = UnsignedT(b);
1465 return ua == 0 ? 0 : (ua - 1) / ub + 1;
1470template <typename T, typename U>
1471EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T round_down(T a, U b) {
1472 using UnsignedT = std::make_unsigned_t<T>;
1473 using UnsignedU = std::make_unsigned_t<U>;
1474 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1475 EIGEN_STATIC_ASSERT((NumTraits<U>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1477 const UnsignedT ua = UnsignedT(a);
1478 const UnsignedU ub = UnsignedU(b);
1479 return ub * (ua / ub);
1482template <typename T>
1483EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log2(T x) {
1484 EIGEN_USING_STD(log2);
1490constexpr int log2(int x) {
1492 constexpr int table[32] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
1493 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
1499 return table[(v * 0x07C4ACDDU) >> 27];
1511template <typename Scalar>
1512EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar sqrt(const Scalar& x) {
1513 return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
1518EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_DEVICE_FUNC
bool sqrt<bool>(
const bool& x) {
1522#if defined(SYCL_DEVICE_ONLY)
1523SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sqrt, sqrt)
1527template <
typename T>
1528EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<!NumTraits<T>::IsComplex, T> cbrt(
const T& x) {
1529 EIGEN_USING_STD(cbrt);
1530 return static_cast<T
>(cbrt(x));
1533template <
typename T>
1534EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, T> cbrt(
const T& x) {
1535 EIGEN_USING_STD(pow);
1536 return pow(x,
typename NumTraits<T>::Real(1.0 / 3.0));
1540template <
typename T>
1541EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T rsqrt(
const T& x) {
1542 return internal::rsqrt_impl<T>::run(x);
1545template <
typename T>
1546EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log(
const T& x) {
1547 return internal::log_impl<T>::run(x);
1550#if defined(SYCL_DEVICE_ONLY)
1551SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log, log)
1554#if defined(EIGEN_GPUCC)
1556EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float log(
const float& x) {
1561EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double log(
const double& x) {
1566template <
typename T>
1567EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1568 std::enable_if_t<NumTraits<T>::IsSigned && !NumTraits<T>::IsComplex,
typename NumTraits<T>::Real>
1570 EIGEN_USING_STD(abs);
1574template <
typename T>
1575EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex,
typename NumTraits<T>::Real> abs(
1577 return numext::hypot(numext::real(x), numext::imag(x));
1580template <
typename T>
1581EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
1582 std::enable_if_t<!NumTraits<T>::IsSigned && !NumTraits<T>::IsComplex,
typename NumTraits<T>::Real>
1587#if defined(SYCL_DEVICE_ONLY)
1588SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(abs, abs)
1589SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(abs, fabs)
1592#if defined(EIGEN_GPUCC)
1594EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float abs(
const float& x) {
1599EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double abs(
const double& x) {
1604EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float abs(
const std::complex<float>& x) {
1605 return ::hypotf(x.real(), x.imag());
1609EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double abs(
const std::complex<double>& x) {
1610 return ::hypot(x.real(), x.imag());
1614template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger,
bool IsSigned = NumTraits<Scalar>::IsSigned>
1616template <
typename Scalar>
1617struct signbit_impl<Scalar, false, true> {
1618 static constexpr size_t Size =
sizeof(Scalar);
1619 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1620 using intSize_t =
typename get_integer_by_size<Size>::signed_type;
1621 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static Scalar run(
const Scalar& x) {
1622 intSize_t a = bit_cast<intSize_t, Scalar>(x);
1624 Scalar result = bit_cast<Scalar, intSize_t>(a);
1628template <
typename Scalar>
1629struct signbit_impl<Scalar, true, true> {
1630 static constexpr size_t Size =
sizeof(Scalar);
1631 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1632 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar run(
const Scalar& x) {
return x >> Shift; }
1634template <
typename Scalar>
1635struct signbit_impl<Scalar, true, false> {
1636 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar run(
const Scalar&) {
return Scalar(0); }
1638template <
typename Scalar>
1639EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar signbit(
const Scalar& x) {
1640 return signbit_impl<Scalar>::run(x);
1643template <
typename T>
1644EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp(
const T& x) {
1645 EIGEN_USING_STD(exp);
1651template <
typename RealScalar>
1652EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp(
const std::complex<RealScalar>& x) {
1654 const std::complex<RealScalar> result = std::exp(x);
1657 if ((isfinite)(real_ref(x)) && !(isfinite)(imag_ref(x))) {
1658 return std::complex<RealScalar>(NumTraits<RealScalar>::quiet_NaN(), NumTraits<RealScalar>::quiet_NaN());
1660#if EIGEN_COMP_MSVC < 1931
1664 if ((real_ref(x) == NumTraits<RealScalar>::infinity() && !(isfinite)(imag_ref(x)))) {
1665 return std::complex<RealScalar>(NumTraits<RealScalar>::infinity(), NumTraits<RealScalar>::quiet_NaN());
1672#if defined(SYCL_DEVICE_ONLY)
1673SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp, exp)
1676#if defined(EIGEN_GPUCC)
1678EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp(
const float& x) {
1683EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp(
const double& x) {
1688EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<float> exp(
const std::complex<float>& x) {
1689 float com = ::expf(x.real());
1690 float res_real = com * ::cosf(x.imag());
1691 float res_imag = com * ::sinf(x.imag());
1692 return std::complex<float>(res_real, res_imag);
1696EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<double> exp(
const std::complex<double>& x) {
1697 double com = ::exp(x.real());
1698 double res_real = com * ::cos(x.imag());
1699 double res_imag = com * ::sin(x.imag());
1700 return std::complex<double>(res_real, res_imag);
1704template <
typename T>
1705EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp2(
const T& x) {
1706 EIGEN_USING_STD(exp2);
1711template <
typename RealScalar>
1712EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp2(
const std::complex<RealScalar>& x) {
1713 return internal::complex_exp2_impl<RealScalar>::run(x);
1716#if defined(SYCL_DEVICE_ONLY)
1717SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp2, exp2)
1720#if defined(EIGEN_GPUCC)
1722EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp2(
const float& x) {
1727EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp2(
const double& x) {
1734template <
typename T>
1735EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T ldexp(
const T& x,
int exponent) {
1736 EIGEN_USING_STD(ldexp);
1737 return static_cast<T
>(ldexp(x, exponent));
1740#if defined(SYCL_DEVICE_ONLY)
1742EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_float ldexp(
const cl::sycl::cl_float& x,
int exponent) {
1743 return cl::sycl::ldexp(x, exponent);
1746EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_double ldexp(
const cl::sycl::cl_double& x,
int exponent) {
1747 return cl::sycl::ldexp(x, exponent);
1751#if defined(EIGEN_GPUCC)
1753EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float ldexp(
const float& x,
int exponent) {
1754 return ::ldexpf(x, exponent);
1758EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double ldexp(
const double& x,
int exponent) {
1759 return ::ldexp(x, exponent);
1763template <
typename Scalar>
1764EIGEN_DEVICE_FUNC
inline Scalar expm1(
const Scalar& x) {
1765 return EIGEN_MATHFUNC_IMPL(expm1, Scalar)::run(x);
1768#if defined(SYCL_DEVICE_ONLY)
1769SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(expm1, expm1)
1772#if defined(EIGEN_GPUCC)
1774EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float expm1(
const float& x) {
1779EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double expm1(
const double& x) {
1784template <
typename T>
1785EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cos(
const T& x) {
1786 EIGEN_USING_STD(cos);
1790#if defined(SYCL_DEVICE_ONLY)
1791SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cos, cos)
1794#if defined(EIGEN_GPUCC)
1796EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cos(
const float& x) {
1801EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cos(
const double& x) {
1806template <
typename T>
1807EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sin(
const T& x) {
1808 EIGEN_USING_STD(sin);
1812#if defined(SYCL_DEVICE_ONLY)
1813SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sin, sin)
1816#if defined(EIGEN_GPUCC)
1818EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sin(
const float& x) {
1823EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sin(
const double& x) {
1828template <
typename T>
1829EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tan(
const T& x) {
1830 EIGEN_USING_STD(tan);
1834#if defined(SYCL_DEVICE_ONLY)
1835SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tan, tan)
1838#if defined(EIGEN_GPUCC)
1840EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tan(
const float& x) {
1845EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tan(
const double& x) {
1850template <
typename T>
1851EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acos(
const T& x) {
1852 EIGEN_USING_STD(acos);
1856template <
typename T>
1857EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acosh(
const T& x) {
1858 EIGEN_USING_STD(acosh);
1859 return static_cast<T
>(acosh(x));
1862#if defined(SYCL_DEVICE_ONLY)
1863SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acos, acos)
1864SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acosh, acosh)
1867#if defined(EIGEN_GPUCC)
1869EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float acos(
const float& x) {
1874EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double acos(
const double& x) {
1879template <
typename T>
1880EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asin(
const T& x) {
1881 EIGEN_USING_STD(asin);
1885template <
typename T>
1886EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asinh(
const T& x) {
1887 EIGEN_USING_STD(asinh);
1888 return static_cast<T
>(asinh(x));
1891#if defined(SYCL_DEVICE_ONLY)
1892SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asin, asin)
1893SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asinh, asinh)
1896#if defined(EIGEN_GPUCC)
1898EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float asin(
const float& x) {
1903EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double asin(
const double& x) {
1908template <
typename T>
1909EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan(
const T& x) {
1910 EIGEN_USING_STD(atan);
1911 return static_cast<T
>(atan(x));
1914template <typename T, std::enable_if_t<!NumTraits<T>::IsComplex,
int> = 0>
1915EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan2(
const T& y,
const T& x) {
1916 EIGEN_USING_STD(atan2);
1917 return static_cast<T
>(atan2(y, x));
1920template <
typename T>
1921EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atanh(
const T& x) {
1922 EIGEN_USING_STD(atanh);
1923 return static_cast<T
>(atanh(x));
1926#if defined(SYCL_DEVICE_ONLY)
1927SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atan, atan)
1928SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atanh, atanh)
1931#if defined(EIGEN_GPUCC)
1933EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float atan(
const float& x) {
1938EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double atan(
const double& x) {
1943template <
typename T>
1944EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cosh(
const T& x) {
1945 EIGEN_USING_STD(cosh);
1946 return static_cast<T
>(cosh(x));
1949#if defined(SYCL_DEVICE_ONLY)
1950SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cosh, cosh)
1953#if defined(EIGEN_GPUCC)
1955EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cosh(
const float& x) {
1960EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cosh(
const double& x) {
1965template <
typename T>
1966EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sinh(
const T& x) {
1967 EIGEN_USING_STD(sinh);
1968 return static_cast<T
>(sinh(x));
1971#if defined(SYCL_DEVICE_ONLY)
1972SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sinh, sinh)
1975#if defined(EIGEN_GPUCC)
1977EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sinh(
const float& x) {
1982EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sinh(
const double& x) {
1987template <
typename T>
1988EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tanh(
const T& x) {
1989 EIGEN_USING_STD(tanh);
1993#if (!defined(EIGEN_GPUCC)) && EIGEN_FAST_MATH && !defined(SYCL_DEVICE_ONLY)
1994EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
float x) {
return internal::ptanh_float(x); }
1997#if defined(SYCL_DEVICE_ONLY)
1998SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tanh, tanh)
2001#if defined(EIGEN_GPUCC)
2003EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
const float& x) {
2008EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tanh(
const double& x) {
2013template <
typename T>
2014EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(
const T& a,
const T& b) {
2015 EIGEN_USING_STD(fmod);
2019#if defined(SYCL_DEVICE_ONLY)
2020SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(fmod, fmod)
2023#if defined(EIGEN_GPUCC)
2025EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float fmod(
const float& a,
const float& b) {
2026 return ::fmodf(a, b);
2030EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double fmod(
const double& a,
const double& b) {
2031 return ::fmod(a, b);
2035template <
typename T>
2036EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T nextafter(
const T& from,
const T& to) {
2037 EIGEN_USING_STD(nextafter);
2038 return nextafter(from, to);
2046template <
typename Scalar,
2047 std::enable_if_t<!std::is_same<Scalar, long double>::value && std::numeric_limits<Scalar>::is_iec559 &&
2048 std::numeric_limits<Scalar>::radix == 2 &&
2049 (
sizeof(Scalar) ==
sizeof(uint16_t) ||
sizeof(Scalar) ==
sizeof(uint32_t) ||
2050 sizeof(Scalar) ==
sizeof(uint64_t)),
2052EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar ceil_power_of_two(
const Scalar& value) {
2053 using Binary = internal::binary_floating_point_traits<Scalar>;
2054 using Bits =
typename Binary::Bits;
2055 const Bits valueBits = Binary::bits(value);
2056 const Bits resultBits = Bits((valueBits + Binary::kFractionMask) & Binary::kExponentMask);
2057 return bit_cast<Scalar>(resultBits);
2062#if !defined(EIGEN_GPU_COMPILE_PHASE)
2063EIGEN_STRONG_INLINE
long double ceil_power_of_two(
const long double& value) {
2064 EIGEN_USING_STD(frexp);
2065 EIGEN_USING_STD(ldexp);
2067 const long double fraction = frexp(value, &exponent);
2068 if (numext::is_exactly_zero(fraction))
return 0.0L;
2069 if (numext::equal_strict(fraction, 0.5L)) --exponent;
2070 if (exponent < std::numeric_limits<long double>::min_exponent - 1)
2071 exponent = std::numeric_limits<long double>::min_exponent - 1;
2072 return ldexp(1.0L, exponent);
2076#if defined(SYCL_DEVICE_ONLY)
2077SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(nextafter, nextafter)
2080#if defined(EIGEN_GPUCC)
2082EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float nextafter(
const float& from,
const float& to) {
2083 return ::nextafterf(from, to);
2087EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double nextafter(
const double& from,
const double& to) {
2088 return ::nextafter(from, to);
2092#if defined(SYCL_DEVICE_ONLY)
2093#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY
2094#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY
2095#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY
2096#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY
2097#undef SYCL_SPECIALIZE_INTEGER_TYPES_BINARY
2098#undef SYCL_SPECIALIZE_INTEGER_TYPES_UNARY
2099#undef SYCL_SPECIALIZE_FLOATING_TYPES_BINARY
2100#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY
2101#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE
2102#undef SYCL_SPECIALIZE_GEN_UNARY_FUNC
2103#undef SYCL_SPECIALIZE_UNARY_FUNC
2104#undef SYCL_SPECIALIZE_GEN1_BINARY_FUNC
2105#undef SYCL_SPECIALIZE_GEN2_BINARY_FUNC
2106#undef SYCL_SPECIALIZE_BINARY_FUNC
2113template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2114EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_left(
const Scalar& a,
int n) {
2115 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2116 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) << n));
2119template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2120EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_right(
const Scalar& a,
int n) {
2121 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2122 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) >> n));
2131template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2132EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar arithmetic_shift_right(
const Scalar& a,
int n) {
2133 return static_cast<Scalar
>(a >> n);
2136template <
typename Scalar>
2137EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar fma(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2138 return internal::fma_impl<Scalar>::run(x, y, z);
2142template <
typename Scalar>
2143EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar madd(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2144 return internal::madd_impl<Scalar>::run(x, y, z);
2151template <
typename T>
2152EIGEN_DEVICE_FUNC
bool isfinite_impl(
const std::complex<T>& x) {
2153 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2155 return static_cast<unsigned int>((numext::isfinite)(numext::real(x))) & (numext::isfinite)(numext::imag(x));
2157 return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
2161template <
typename T>
2162EIGEN_DEVICE_FUNC
bool isnan_impl(
const std::complex<T>& x) {
2163 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2164 return static_cast<unsigned int>((numext::isnan)(numext::real(x))) | (numext::isnan)(numext::imag(x));
2166 return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
2170template <
typename T>
2171EIGEN_DEVICE_FUNC
bool isinf_impl(
const std::complex<T>& x) {
2172 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2173 const bool has_inf =
static_cast<unsigned int>((numext::isinf)(numext::real(x))) | (numext::isinf)(numext::imag(x));
2174 const bool has_nan = (numext::isnan)(x);
2175 return has_inf & !has_nan;
2177 return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
2185template <
typename Scalar,
bool IsComplex,
bool IsInteger>
2186struct scalar_fuzzy_default_impl {};
2188template <
typename Scalar>
2189struct scalar_fuzzy_default_impl<Scalar, false, false> {
2190 using RealScalar =
typename NumTraits<Scalar>::Real;
2191 template <
typename OtherScalar>
2192 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2193 const RealScalar& prec) {
2194 return numext::abs(x) <= numext::abs(y) * prec;
2196 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2197 return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
2199 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2200 return x <= y || isApprox(x, y, prec);
2204template <
typename Scalar>
2205struct scalar_fuzzy_default_impl<Scalar, false, true> {
2206 using RealScalar =
typename NumTraits<Scalar>::Real;
2207 template <
typename OtherScalar>
2208 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const Scalar&,
const RealScalar&) {
2209 return x == Scalar(0);
2211 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
return x == y; }
2212 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
2217template <
typename Scalar>
2218struct scalar_fuzzy_default_impl<Scalar, true, false> {
2219 using RealScalar =
typename NumTraits<Scalar>::Real;
2220 template <
typename OtherScalar>
2221 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2222 const RealScalar& prec) {
2223 return numext::abs2(x) <= numext::abs2(y) * prec * prec;
2225 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2226 return numext::abs2(x - y) <= numext::mini(numext::abs2(x), numext::abs2(y)) * prec * prec;
2230template <
typename Scalar>
2231struct scalar_fuzzy_impl
2232 : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
2234template <
typename Scalar,
typename OtherScalar>
2235EIGEN_DEVICE_FUNC
inline bool isMuchSmallerThan(
2236 const Scalar& x,
const OtherScalar& y,
2237 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2238 return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
2241template <
typename Scalar>
2242EIGEN_DEVICE_FUNC
inline bool isApprox(
2243 const Scalar& x,
const Scalar& y,
2244 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2245 return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
2248template <
typename Scalar>
2249EIGEN_DEVICE_FUNC
inline bool isApproxOrLessThan(
2250 const Scalar& x,
const Scalar& y,
2251 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2252 return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
2260struct scalar_fuzzy_impl<bool> {
2261 using RealScalar = bool;
2263 template <
typename OtherScalar>
2264 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const bool& x,
const bool&,
const bool&) {
2268 EIGEN_DEVICE_FUNC
static inline bool isApprox(
bool x,
bool y,
bool) {
return x == y; }
2270 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const bool& x,
const bool& y,
const bool&) {
2281template <
typename RealScalar>
2282struct expm1_impl<std::complex<RealScalar>> {
2283 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2285 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2286 RealScalar xr = x.real();
2287 RealScalar xi = x.imag();
2297 RealScalar erm1 = numext::expm1<RealScalar>(xr);
2298 RealScalar er = erm1 + RealScalar(1.);
2301 if ((numext::isinf)(xr) && !(numext::isfinite)(xi)) {
2302 return xr > RealScalar(0) ? std::complex<RealScalar>(xr, xi * RealScalar(0))
2303 : std::complex<RealScalar>(RealScalar(-1), RealScalar(0));
2305 const RealScalar s = numext::sin(xi);
2306 const RealScalar c = numext::cos(xi);
2308 const RealScalar one_minus_c = c > RealScalar(0) ? s * s / (RealScalar(1) + c) : RealScalar(1) - c;
2311 if ((numext::isinf)(er) && (numext::isfinite)(xr)) {
2312 const RealScalar h = numext::exp(xr / RealScalar(2));
2313 if (!(numext::isinf)(h)) {
2314 return std::complex<RealScalar>((h * c) * h - RealScalar(1), numext::is_exactly_zero(xi) ? xi : (h * s) * h);
2316 const RealScalar h3 = numext::exp(xr / RealScalar(3));
2317 return std::complex<RealScalar>(((h3 * c) * h3) * h3, numext::is_exactly_zero(xi) ? xi : ((h3 * s) * h3) * h3);
2321 const RealScalar real_part = xr < RealScalar(0) ? erm1 - er * one_minus_c : erm1 * c - one_minus_c;
2323 return std::complex<RealScalar>(real_part, numext::is_exactly_zero(xi) ? xi : er * s);
2332template <
typename RealScalar>
2333struct complex_exp2_impl {
2334 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2336 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2337 using Complex = std::complex<RealScalar>;
2338 const RealScalar a = x.real();
2339 const RealScalar b = x.imag();
2341 if (numext::is_exactly_zero(b))
return Complex(numext::exp2(a), b);
2344 if ((numext::isinf)(a) && !(numext::isfinite)(b)) {
2345 return a > RealScalar(0) ? Complex(a, b * RealScalar(0)) : Complex(RealScalar(0), RealScalar(0));
2349 std::conditional_t<(NumTraits<RealScalar>::digits() > NumTraits<double>::digits()), RealScalar,
double>;
2350 const RealScalar ln2_hi = RealScalar(EIGEN_LN2);
2351 const RealScalar ln2_lo = RealScalar((Wide(
static_cast<double>(EIGEN_LN2)) - Wide(ln2_hi)) +
2352 Wide(2.3190468138462996154948554638754786504e-17L));
2353 const RealScalar t_hi = b * ln2_hi;
2354 const RealScalar t_lo = numext::fma(b, ln2_hi, -t_hi) + b * ln2_lo;
2357 RealScalar sin_lo = t_lo;
2358 RealScalar cos_lo_m1 = -t_lo * t_lo / RealScalar(2);
2359 if (numext::abs(t_lo) > numext::sqrt(NumTraits<RealScalar>::epsilon())) {
2360 sin_lo = numext::sin(t_lo);
2361 const RealScalar sin_half = numext::sin(t_lo / RealScalar(2));
2362 cos_lo_m1 = RealScalar(-2) * sin_half * sin_half;
2364 const RealScalar cos_hi = numext::cos(t_hi);
2365 const RealScalar sin_hi = numext::sin(t_hi);
2366 const RealScalar c = cos_hi + (cos_hi * cos_lo_m1 - sin_hi * sin_lo);
2367 const RealScalar s = sin_hi + (sin_hi * cos_lo_m1 + cos_hi * sin_lo);
2370 const int e_max = NumTraits<RealScalar>::max_exponent() - 1;
2371 const int e = a > RealScalar(e_max) ? (a > RealScalar(2 * e_max) ? 2 * e_max : e_max)
2372 : (a < RealScalar(NumTraits<RealScalar>::min_exponent()) ? -e_max : 0);
2373 const RealScalar m = numext::exp2(a - RealScalar(e));
2374 if (numext::abs(t_hi) < (numext::numeric_limits<RealScalar>::min)()) {
2376 const int p = NumTraits<RealScalar>::digits();
2377 const RealScalar b_p = numext::ldexp(b, p);
2378 return Complex(numext::ldexp(m, e), numext::ldexp(m * numext::fma(b_p, ln2_hi, b_p * ln2_lo), e - p));
2380 if (e == 0)
return Complex(m * c, m * s);
2381 return Complex(numext::ldexp(m * c, e), numext::ldexp(m * s, e));
2385template <
typename T>
2389#pragma warning(push)
2390#pragma warning(disable : 4804)
2392 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE T run(
const T& x) {
return T(1) / numext::sqrt(x); }
2398#if defined(EIGEN_GPU_COMPILE_PHASE)
2399template <
typename T>
2400struct conj_impl<std::complex<T>, true> {
2401 EIGEN_DEVICE_FUNC
static inline std::complex<T> run(
const std::complex<T>& x) {
2402 return std::complex<T>(numext::real(x), -numext::imag(x));
2410template <
typename T>
2411EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_multiply(
const std::complex<T>& a,
2412 const std::complex<T>& b) {
2413 const T a_real = numext::real(a);
2414 const T a_imag = numext::imag(a);
2415 const T b_real = numext::real(b);
2416 const T b_imag = numext::imag(b);
2417 return std::complex<T>(a_real * b_real - a_imag * b_imag, a_imag * b_real + a_real * b_imag);
2420template <
typename T>
2421EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_fast(
const std::complex<T>& a,
2422 const std::complex<T>& b) {
2423 const T a_real = numext::real(a);
2424 const T a_imag = numext::imag(a);
2425 const T b_real = numext::real(b);
2426 const T b_imag = numext::imag(b);
2427 const T norm = (b_real * b_real + b_imag * b_imag);
2428 return std::complex<T>((a_real * b_real + a_imag * b_imag) / norm, (a_imag * b_real - a_real * b_imag) / norm);
2431template <
typename T>
2432EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_smith(
const std::complex<T>& a,
2433 const std::complex<T>& b) {
2434 const T a_real = numext::real(a);
2435 const T a_imag = numext::imag(a);
2436 const T b_real = numext::real(b);
2437 const T b_imag = numext::imag(b);
2440 const bool scale_imag = numext::abs(b_imag) <= numext::abs(b_real);
2441 const T rscale = scale_imag ? T(1) : b_real / b_imag;
2442 const T iscale = scale_imag ? b_imag / b_real : T(1);
2443 const T denominator = b_real * rscale + b_imag * iscale;
2444 return std::complex<T>((a_real * rscale + a_imag * iscale) / denominator,
2445 (a_imag * rscale - a_real * iscale) / denominator);
2448template <
typename T>
2449EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide(
const std::complex<T>& a,
2450 const std::complex<T>& b) {
2452 return complex_divide_fast(a, b);
2454 return complex_divide_smith(a, b);
2470template <
typename LhsScalar,
typename RhsScalar>
2471EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
auto divide(
const LhsScalar& a,
const RhsScalar& b) ->
decltype(a / b) {
2475template <
typename T>
2476EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const std::complex<T>& b) {
2477 return internal::complex_divide_smith(a, b);
2480template <
typename T>
2481EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const T& a,
const std::complex<T>& b) {
2482 return internal::complex_divide_smith(std::complex<T>(a, T(0)), b);
2485template <
typename T>
2486EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const T& b) {