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 EIGEN_USING_STD(sqrt);
941 const real_type re = numext::real(a);
942 const real_type im = numext::imag(a);
943 if (numext::is_exactly_zero(re) && numext::is_exactly_zero(im)) return Scalar(0);
949 if ((isfinite_impl<real_type>)(re) && (isfinite_impl<real_type>)(im)) {
950 const real_type abs_re = abs(re);
951 const real_type abs_im = abs(im);
952 const real_type m = abs_re < abs_im ? abs_im : abs_re;
953 const real_type half_highest = NumTraits<real_type>::highest() / real_type(2);
954 if (EIGEN_PREDICT_FALSE(m < (numext::numeric_limits<real_type>::min)() || m > half_highest)) {
957 const real_type scale = m > half_highest ? real_type(0.25) : real_type(1);
958 real_type scaled_m = m * scale;
959 volatile_barrier(scaled_m);
960 const real_type u_re = (re * scale) / scaled_m;
961 const real_type u_im = (im * scale) / scaled_m;
962 const real_type norm = sqrt(u_re * u_re + u_im * u_im);
963 return Scalar(u_re / norm, u_im / norm);
966 const real_type aa = abs(a);
969 return Scalar(re / aa, im / aa);
975struct sign_impl<bool, false, true> {
976 EIGEN_DEVICE_FUNC static inline bool run(const bool& a) { return a; }
980namespace has_copysign_detail {
982template <typename T, typename EnableIf = void>
983struct has_copysign_impl : public std::false_type {};
985EIGEN_USING_STD(copysign)
988struct has_copysign_impl<T, std::enable_if_t<std::is_constructible<
989 T, decltype(copysign(std::declval<const T&>(), std::declval<const T&>()))>::value>>
990 : public std::true_type {};
999struct has_copysign : public has_copysign_detail::has_copysign_impl<T> {};
1001template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
1002 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0),
1003 bool HasCopysign = (!IsComplex && !IsInteger && has_copysign<Scalar>::value)>
1004struct copysign_impl {
1005 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
1006 EIGEN_USING_STD(copysign);
1007 return Scalar(copysign(a, b));
1011template <typename Scalar, bool IsInteger>
1012struct copysign_impl<Scalar, true, IsInteger, false> {
1013 using RealScalar = typename NumTraits<Scalar>::Real;
1014 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
1015 return Scalar(copysign_impl<RealScalar>::run(numext::real(a), numext::real(b)),
1016 copysign_impl<RealScalar>::run(numext::imag(a), numext::imag(b)));
1020template <typename Scalar, bool IsInteger>
1021struct copysign_impl<Scalar, false, IsInteger, false> {
1022 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a, const Scalar& b) {
1023 EIGEN_IF_CONSTEXPR (IsInteger && !NumTraits<Scalar>::IsSigned) return a;
1024 const Scalar abs_a = a < Scalar(0) ? -a : a;
1025 return b < Scalar(0) ? -abs_a : abs_a;
1030struct copysign_impl<bool, false, true, false> {
1031 EIGEN_DEVICE_FUNC static inline bool run(const bool& a, const bool&) { return a; }
1037template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger>
1039 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return -a; }
1042template <typename Scalar>
1043struct negate_impl<Scalar, true> {
1044 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), NEGATE IS NOT DEFINED FOR BOOLEAN TYPES)
1045 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return Scalar(0) - a; }
1048template <typename Scalar, bool IsInteger = NumTraits<typename unpacket_traits<Scalar>::type>::IsInteger>
1049struct nearest_integer_impl {
1050 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) {
1051 EIGEN_USING_STD(floor) return floor(x);
1053 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) {
1054 EIGEN_USING_STD(ceil) return ceil(x);
1056 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) {
1057 EIGEN_USING_STD(rint) return rint(x);
1059 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) {
1060 EIGEN_USING_STD(round) return round(x);
1062 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) {
1063 EIGEN_USING_STD(trunc) return trunc(x);
1066template <typename Scalar>
1067struct nearest_integer_impl<Scalar, true> {
1068 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_floor(const Scalar& x) { return x; }
1069 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) { return x; }
1070 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) { return x; }
1071 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) { return x; }
1072 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) { return x; }
1076namespace has_fma_detail {
1078template <typename T, typename EnableIf = void>
1079struct has_fma_impl : public std::false_type {};
1083template <typename T>
1085 T, std::enable_if_t<std::is_same<T, decltype(fma(std::declval<T>(), std::declval<T>(), std::declval<T>()))>::value>>
1086 : public std::true_type {};
1090template <typename T>
1091struct has_fma : public has_fma_detail::has_fma_impl<T> {};
1094template <typename T, typename Enable = void>
1096 static_assert(has_fma<T>::value, "No function fma(...) for type. Please provide an implementation.");
1100template <typename T>
1101struct fma_impl<T, std::enable_if_t<has_fma<T>::value>> {
1102 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T run(const T& a, const T& b, const T& c) {
1104 return fma(a, b, c);
1108#if defined(EIGEN_GPUCC)
1110struct has_fma<float> : public std::true_type {};
1113struct fma_impl<float, void> {
1114 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float run(const float& a, const float& b, const float& c) {
1115 return ::fmaf(a, b, c);
1120struct has_fma<double> : public std::true_type {};
1123struct fma_impl<double, void> {
1124 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double run(const double& a, const double& b, const double& c) {
1125 return ::fma(a, b, c);
1131template <typename Scalar, typename EnableIf = void>
1133 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1135 const Scalar xy = x * y;
1142template <typename Scalar>
1143struct has_fast_fma : std::false_type {};
1144#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMAF) || defined(__FP_FAST_FMAF)
1146struct has_fast_fma<float> : std::true_type {};
1148#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMA) || defined(__FP_FAST_FMA)
1150struct has_fast_fma<double> : std::true_type {};
1152#if defined(FP_FAST_FMAL) || defined(__FP_FAST_FMAL)
1154struct has_fast_fma<long double> : std::true_type {};
1158struct has_fast_fma<long double>
1159 : bool_constant<std::numeric_limits<long double>::digits == std::numeric_limits<double>::digits &&
1160 has_fast_fma<double>::value> {};
1167template <typename Scalar>
1168struct scalar_madd_uses_fma
1169 : std::conditional_t<bool(EIGEN_SCALAR_MADD_USE_FMA) &&
1170 (!std::is_same<Scalar, long double>::value || has_fast_fma<Scalar>::value),
1171 has_fma<Scalar>, std::false_type> {};
1173template <typename Scalar>
1174struct madd_impl<Scalar, std::enable_if_t<scalar_madd_uses_fma<Scalar>::value>> {
1175 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1176 return fma_impl<Scalar>::run(x, y, z);
1181template <typename Scalar>
1182struct binary_floating_point_traits {
1183 using Bits = typename numext::get_integer_by_size<sizeof(Scalar)>::unsigned_type;
1184 static constexpr int kFractionBits = std::numeric_limits<Scalar>::digits - 1;
1185 static constexpr int kExponentBits = int(sizeof(Scalar) * CHAR_BIT) - std::numeric_limits<Scalar>::digits;
1186 static constexpr int kExponentBias = std::numeric_limits<Scalar>::max_exponent - 1;
1187 static constexpr Bits kSignBit = Bits(1) << (sizeof(Scalar) * CHAR_BIT - 1);
1188 static constexpr Bits kExponentUnit = Bits(1) << kFractionBits;
1189 static constexpr Bits kFractionMask = kExponentUnit - 1;
1190 static constexpr Bits kExponentMask = ((Bits(1) << kExponentBits) - 1) << kFractionBits;
1191 static constexpr Bits kMaxFiniteExponentBits = kExponentMask - kExponentUnit;
1193 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits bits(const Scalar& value) { return numext::bit_cast<Bits>(value); }
1195 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits magnitude(const Scalar& value) { return bits(value) & ~kSignBit; }
1199#if EIGEN_COMP_CXXVER < 17
1200template <typename Scalar>
1201constexpr int binary_floating_point_traits<Scalar>::kFractionBits;
1202template <typename Scalar>
1203constexpr int binary_floating_point_traits<Scalar>::kExponentBits;
1204template <typename Scalar>
1205constexpr int binary_floating_point_traits<Scalar>::kExponentBias;
1206template <typename Scalar>
1207constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kSignBit;
1208template <typename Scalar>
1209constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kExponentUnit;
1210template <typename Scalar>
1211constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kFractionMask;
1212template <typename Scalar>
1213constexpr typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kExponentMask;
1214template <typename Scalar>
1216 typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kMaxFiniteExponentBits;
1219template <typename Scalar>
1220EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, false_type) {
1221 return numext::is_exactly_zero(value);
1224template <typename Scalar>
1225EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE bool is_zero_magnitude_bits(
1226 typename binary_floating_point_traits<Scalar>::Bits magnitude) {
1229 return magnitude == 0;
1233template <typename Scalar>
1234struct sign_impl_binary {
1235 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar& a) {
1236 using Binary = binary_floating_point_traits<Scalar>;
1237 using Bits = typename Binary::Bits;
1238 const Bits bits = Binary::bits(a);
1239 const Bits magnitude = bits & ~Binary::kSignBit;
1241 const Bits nonzero = (magnitude | (Bits(0) - magnitude)) >> (sizeof(Bits) * CHAR_BIT - 1);
1242 const Bits nonzero_mask = Bits(0) - nonzero;
1243 const Bits nan_mask = Bits(0) - Bits(magnitude > Binary::kExponentMask);
1244 const Bits signed_one = (bits & Binary::kSignBit) | Binary::bits(Scalar(1));
1245 return numext::bit_cast<Scalar>((bits & nan_mask) | (signed_one & nonzero_mask & ~nan_mask));
1250struct sign_impl<float, false, false> : sign_impl_binary<float> {};
1253struct sign_impl<double, false, false> : sign_impl_binary<double> {};
1255template <typename Scalar>
1256EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush_impl(const Scalar& value, true_type) {
1257 return is_zero_magnitude_bits<Scalar>(binary_floating_point_traits<Scalar>::magnitude(value));
1261template <typename T, bool IsInteger = NumTraits<T>::IsInteger,
1262 bool IsSignedIntegral = std::is_integral<T>::value && std::is_signed<T>::value>
1263struct absdiff_impl {
1264 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) {
1265 EIGEN_USING_STD(abs);
1271template <typename T>
1272struct absdiff_impl<T, true, false> {
1273 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) { return x > y ? x - y : y - x; }
1277template <typename T>
1278struct absdiff_impl<T, true, true> {
1279 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x, const T& y) {
1280 using U = std::make_unsigned_t<T>;
1281 const U ux = static_cast<U>(x), uy = static_cast<U>(y);
1282 return static_cast<T>(static_cast<U>(x > y ? ux - uy : uy - ux));
1294template <typename X>
1295EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool is_exactly_zero_no_flush(const X& x) {
1296 constexpr bool HasBinaryEncoding = (std::is_same<X, float>::value || std::is_same<X, double>::value) &&
1297 std::numeric_limits<X>::is_iec559 && std::numeric_limits<X>::radix == 2 &&
1298 (sizeof(X) == sizeof(uint32_t) || sizeof(X) == sizeof(uint64_t));
1299 return internal::is_exactly_zero_no_flush_impl(x, internal::bool_constant<HasBinaryEncoding>());
1302#if !defined(EIGEN_GPUCC)
1303template <typename T>
1304EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1305 EIGEN_USING_STD(min)
1306 return min EIGEN_NOT_A_MACRO(x, y);
1309template <typename T>
1310EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1311 EIGEN_USING_STD(max)
1312 return max EIGEN_NOT_A_MACRO(x, y);
1315template <typename T>
1316EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1317 return y < x ? y : x;
1319#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1324EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y) {
1328EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double mini(const double& x, const double& y) {
1332#ifndef EIGEN_GPU_COMPILE_PHASE
1334EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double mini(const long double& x, const long double& y) {
1335#if defined(EIGEN_HIPCC)
1337 return (x < y) ? x : y;
1345template <typename T>
1346EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1347 return x < y ? y : x;
1349#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1351EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y) {
1355EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double maxi(const double& x, const double& y) {
1358#ifndef EIGEN_GPU_COMPILE_PHASE
1360EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double maxi(const long double& x, const long double& y) {
1361#if defined(EIGEN_HIPCC)
1363 return (x > y) ? x : y;
1372#if defined(EIGEN_SYCL_DEVICE_ONLY)
1374#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1375 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1376 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1377 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1378 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1379#define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1380 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
1381 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
1382 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
1383 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
1384#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1385 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1386 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1387 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1388 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1389#define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1390 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
1391 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
1392 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
1393 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
1394#define SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(NAME, FUNC) \
1395 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
1396 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC)
1397#define SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(NAME, FUNC) \
1398 SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
1399 SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC)
1400#define SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(NAME, FUNC) \
1401 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1402 SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1403#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(NAME, FUNC) \
1404 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
1405 SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_double)
1406#define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(NAME, FUNC, RET_TYPE) \
1407 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_float) \
1408 SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_double)
1410#define SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1412 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE& x) { \
1413 return cl::sycl::FUNC(x); \
1416#define SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, TYPE, TYPE)
1418#define SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE1, ARG_TYPE2) \
1420 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE1& x, const ARG_TYPE2& y) { \
1421 return cl::sycl::FUNC(x, y); \
1424#define SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1425 SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE, ARG_TYPE)
1427#define SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, TYPE, TYPE)
1429SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(mini, min)
1430SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(mini, fmin)
1431SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(maxi, max)
1432SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(maxi, fmax)
1436template <typename Scalar>
1437EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real arg(const Scalar& x) {
1438 return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
1441template <typename Scalar>
1442EIGEN_DEVICE_FUNC inline auto imag_ref(const Scalar& x)
1443 -> internal::add_const_on_value_type_t<decltype(internal::imag_ref_impl<Scalar>::run(x))> {
1444 return internal::imag_ref_impl<Scalar>::run(x);
1447template <typename Scalar>
1448EIGEN_DEVICE_FUNC inline auto imag_ref(Scalar& x)
1449 -> decltype(EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x)) {
1450 return EIGEN_MATHFUNC_IMPL(imag_ref, std::remove_const_t<Scalar>)::run(x);
1453template <typename Scalar>
1454EIGEN_DEVICE_FUNC inline Scalar conj(const Scalar& x) {
1455 return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
1458template <typename Scalar>
1459EIGEN_DEVICE_FUNC inline Scalar sign(const Scalar& x) {
1460 return EIGEN_MATHFUNC_IMPL(sign, Scalar)::run(x);
1463template <typename Scalar>
1464EIGEN_DEVICE_FUNC inline Scalar copysign(const Scalar& x, const Scalar& y) {
1465 return EIGEN_MATHFUNC_IMPL(copysign, Scalar)::run(x, y);
1468#if defined(EIGEN_SYCL_DEVICE_ONLY)
1469SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(copysign, copysign)
1472template <typename Scalar>
1473EIGEN_DEVICE_FUNC inline Scalar negate(const Scalar& x) {
1474 return EIGEN_MATHFUNC_IMPL(negate, Scalar)::run(x);
1477template <typename Scalar>
1478EIGEN_DEVICE_FUNC inline decltype(auto) abs2(const Scalar& x) {
1479 return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
1482EIGEN_DEVICE_FUNC inline bool abs2(bool x) { return x; }
1484template <typename T>
1485EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T absdiff(const T& x, const T& y) {
1486 return internal::absdiff_impl<T>::run(x, y);
1489EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float absdiff(const float& x, const float& y) {
1490 return fabsf(x - y);
1493EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double absdiff(const double& x, const double& y) {
1498#ifndef EIGEN_GPU_COMPILE_PHASE
1500EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double absdiff(const long double& x, const long double& y) {
1501 return fabsl(x - y);
1505template <typename Scalar>
1506EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real norm1(const Scalar& x) {
1507 return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
1510template <typename Scalar>
1511EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real hypot(const Scalar& x, const Scalar& y) {
1512 return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
1515#if defined(EIGEN_SYCL_DEVICE_ONLY)
1516SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(hypot, hypot)
1519template <typename Scalar>
1520EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
1521 return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
1524#if defined(EIGEN_SYCL_DEVICE_ONLY)
1525SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log1p, log1p)
1528#if defined(EIGEN_GPUCC)
1530EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log1p(const float& x) {
1535EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log1p(const double& x) {
1540template <typename ScalarX, typename ScalarY>
1541EIGEN_DEVICE_FUNC inline typename internal::pow_impl<ScalarX, ScalarY>::result_type pow(const ScalarX& x,
1543 return internal::pow_impl<ScalarX, ScalarY>::run(x, y);
1546#if defined(EIGEN_SYCL_DEVICE_ONLY)
1547SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(pow, pow)
1550template <typename T>
1551EIGEN_DEVICE_FUNC bool(isnan)(const T& x) {
1552 return internal::isnan_impl(x);
1554template <typename T>
1555EIGEN_DEVICE_FUNC bool(isinf)(const T& x) {
1556 return internal::isinf_impl(x);
1558template <typename T>
1559EIGEN_DEVICE_FUNC bool(isfinite)(const T& x) {
1560 return internal::isfinite_impl(x);
1563#if defined(EIGEN_SYCL_DEVICE_ONLY)
1564SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isnan, isnan, bool)
1565SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isinf, isinf, bool)
1566SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isfinite, isfinite, bool)
1569template <typename Scalar>
1570EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar rint(const Scalar& x) {
1571 return internal::nearest_integer_impl<Scalar>::run_rint(x);
1574template <typename Scalar>
1575EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar round(const Scalar& x) {
1576 return internal::nearest_integer_impl<Scalar>::run_round(x);
1579template <typename Scalar>
1580EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(floor)(const Scalar& x) {
1581 return internal::nearest_integer_impl<Scalar>::run_floor(x);
1584template <typename Scalar>
1585EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(ceil)(const Scalar& x) {
1586 return internal::nearest_integer_impl<Scalar>::run_ceil(x);
1589template <typename Scalar>
1590EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(trunc)(const Scalar& x) {
1591 return internal::nearest_integer_impl<Scalar>::run_trunc(x);
1594#if defined(EIGEN_SYCL_DEVICE_ONLY)
1595SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(rint, rint)
1596SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(round, round)
1597SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(floor, floor)
1598SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(ceil, ceil)
1599SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(trunc, trunc)
1602#if defined(EIGEN_GPUCC)
1604EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float floor(const float& x) {
1608EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double floor(const double& x) {
1612EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float ceil(const float& x) {
1616EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double ceil(const double& x) {
1620EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float trunc(const float& x) {
1624EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double trunc(const double& x) {
1631template <typename T>
1632EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T div_ceil(T a, T b) {
1633 using UnsignedT = std::make_unsigned_t<T>;
1634 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1636 const UnsignedT ua = UnsignedT(a);
1637 const UnsignedT ub = UnsignedT(b);
1639 return ua == 0 ? 0 : (ua - 1) / ub + 1;
1644template <typename T, typename U>
1645EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr T round_down(T a, U b) {
1646 using UnsignedT = std::make_unsigned_t<T>;
1647 using UnsignedU = std::make_unsigned_t<U>;
1648 EIGEN_STATIC_ASSERT((NumTraits<T>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1649 EIGEN_STATIC_ASSERT((NumTraits<U>::IsInteger), THIS FUNCTION IS FOR INTEGER TYPES)
1651 const UnsignedT ua = UnsignedT(a);
1652 const UnsignedU ub = UnsignedU(b);
1653 return ub * (ua / ub);
1656template <typename T>
1657EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log10(T x) {
1658 EIGEN_USING_STD(log10);
1662template <typename T>
1663EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log2(T x) {
1664 EIGEN_USING_STD(log2);
1668#if defined(EIGEN_SYCL_DEVICE_ONLY)
1670EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log10(float x) {
1671 return cl::sycl::log10(x);
1674EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log10(double x) {
1675 return cl::sycl::log10(x);
1678EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log2(float x) {
1679 return cl::sycl::log2(x);
1682EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log2(double x) {
1683 return cl::sycl::log2(x);
1689constexpr int log2(int x) {
1691 constexpr int table[32] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
1692 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
1698 return table[(v * 0x07C4ACDDU) >> 27];
1710template <typename Scalar>
1711EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar sqrt(const Scalar& x) {
1712 return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
1717EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_DEVICE_FUNC bool sqrt<bool>(const bool& x) {
1721#if defined(EIGEN_SYCL_DEVICE_ONLY)
1722SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sqrt, sqrt)
1725#if defined(EIGEN_VECTORIZE_SSE)
1728EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float& x);
1730EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double sqrt(const double& x);
1734template <typename T>
1735EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<!NumTraits<T>::IsComplex, T> cbrt(const T& x) {
1736 EIGEN_USING_STD(cbrt);
1737 return static_cast<T>(cbrt(x));
1740template <typename T>
1741EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, T> cbrt(const T& x) {
1742 EIGEN_USING_STD(pow);
1743 return pow(x, typename NumTraits<T>::Real(1.0 / 3.0));
1746#if defined(EIGEN_SYCL_DEVICE_ONLY)
1747SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cbrt, cbrt)
1751template <typename T>
1752EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T rsqrt(const T& x) {
1753 return internal::rsqrt_impl<T>::run(x);
1756template <typename T>
1757EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log(const T& x) {
1758 return internal::log_impl<T>::run(x);
1761#if defined(EIGEN_SYCL_DEVICE_ONLY)
1762SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log, log)
1765#if defined(EIGEN_GPUCC)
1767EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log(const float& x) {
1772EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log(const double& x) {
1777template <typename T>
1778EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<
1779 (NumTraits<T>::IsSigned || !NumTraits<T>::IsInteger) && !NumTraits<T>::IsComplex, typename NumTraits<T>::Real>
1781 EIGEN_USING_STD(abs);
1785template <typename T>
1786EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, typename NumTraits<T>::Real> abs(
1788 return numext::hypot(numext::real(x), numext::imag(x));
1791template <typename T>
1792EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<
1793 !NumTraits<T>::IsSigned && NumTraits<T>::IsInteger && !NumTraits<T>::IsComplex, typename NumTraits<T>::Real>
1798#if defined(EIGEN_SYCL_DEVICE_ONLY)
1799SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(abs, abs)
1800SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(abs, fabs)
1803#if defined(EIGEN_GPUCC)
1805EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const float& x) {
1810EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const double& x) {
1815EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const std::complex<float>& x) {
1816 return ::hypotf(x.real(), x.imag());
1820EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const std::complex<double>& x) {
1821 return ::hypot(x.real(), x.imag());
1825template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger, bool IsSigned = NumTraits<Scalar>::IsSigned>
1827template <typename Scalar>
1828struct signbit_impl<Scalar, false, true> {
1829 static constexpr size_t Size = sizeof(Scalar);
1830 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1831 using intSize_t = typename get_integer_by_size<Size>::signed_type;
1832 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static Scalar run(const Scalar& x) {
1833 intSize_t a = bit_cast<intSize_t, Scalar>(x);
1835 Scalar result = bit_cast<Scalar, intSize_t>(a);
1839template <typename Scalar>
1840struct signbit_impl<Scalar, true, true> {
1841 static constexpr size_t Size = sizeof(Scalar);
1842 static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
1843 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar run(const Scalar& x) { return x >> Shift; }
1845template <typename Scalar>
1846struct signbit_impl<Scalar, true, false> {
1847 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar run(
const Scalar&) {
return Scalar(0); }
1849template <
typename Scalar>
1850EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static constexpr Scalar signbit(
const Scalar& x) {
1851 return signbit_impl<Scalar>::run(x);
1854template <
typename T>
1855EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp(
const T& x) {
1856 EIGEN_USING_STD(exp);
1862template <
typename RealScalar>
1863EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp(
const std::complex<RealScalar>& x) {
1865 const std::complex<RealScalar> result = std::exp(x);
1868 if ((isfinite)(real_ref(x)) && !(isfinite)(imag_ref(x))) {
1869 return std::complex<RealScalar>(NumTraits<RealScalar>::quiet_NaN(), NumTraits<RealScalar>::quiet_NaN());
1871#if EIGEN_COMP_MSVC < 1931
1875 if ((real_ref(x) == NumTraits<RealScalar>::infinity() && !(isfinite)(imag_ref(x)))) {
1876 return std::complex<RealScalar>(NumTraits<RealScalar>::infinity(), NumTraits<RealScalar>::quiet_NaN());
1883#if defined(EIGEN_SYCL_DEVICE_ONLY)
1884SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp, exp)
1887#if defined(EIGEN_GPUCC)
1889EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp(
const float& x) {
1894EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp(
const double& x) {
1901EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<float> exp(
const std::complex<float>& x) {
1902 float com = ::expf(x.real());
1903 float res_real = com * ::cosf(x.imag());
1904 float res_imag = x.imag() == 0.f ? x.imag() : com * ::sinf(x.imag());
1905 return std::complex<float>(res_real, res_imag);
1909EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<double> exp(
const std::complex<double>& x) {
1910 double com = ::exp(x.real());
1911 double res_real = com * ::cos(x.imag());
1912 double res_imag = x.imag() == 0.0 ? x.imag() : com * ::sin(x.imag());
1913 return std::complex<double>(res_real, res_imag);
1917template <
typename T>
1918EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp2(
const T& x) {
1919 EIGEN_USING_STD(exp2);
1924template <
typename RealScalar>
1925EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp2(
const std::complex<RealScalar>& x) {
1926 return internal::complex_exp2_impl<RealScalar>::run(x);
1929#if defined(EIGEN_SYCL_DEVICE_ONLY)
1930SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp2, exp2)
1933#if defined(EIGEN_GPUCC)
1935EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float exp2(
const float& x) {
1940EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double exp2(
const double& x) {
1947template <
typename T>
1948EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T ldexp(
const T& x,
int exponent) {
1949 EIGEN_USING_STD(ldexp);
1950 return static_cast<T
>(ldexp(x, exponent));
1953#if defined(EIGEN_SYCL_DEVICE_ONLY)
1955EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_float ldexp(
const cl::sycl::cl_float& x,
int exponent) {
1956 return cl::sycl::ldexp(x, exponent);
1959EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE cl::sycl::cl_double ldexp(
const cl::sycl::cl_double& x,
int exponent) {
1960 return cl::sycl::ldexp(x, exponent);
1964#if defined(EIGEN_GPUCC)
1966EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float ldexp(
const float& x,
int exponent) {
1967 return ::ldexpf(x, exponent);
1971EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double ldexp(
const double& x,
int exponent) {
1972 return ::ldexp(x, exponent);
1976template <
typename Scalar>
1977EIGEN_DEVICE_FUNC
inline Scalar expm1(
const Scalar& x) {
1978 return EIGEN_MATHFUNC_IMPL(expm1, Scalar)::run(x);
1981#if defined(EIGEN_SYCL_DEVICE_ONLY)
1982SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(expm1, expm1)
1985#if defined(EIGEN_GPUCC)
1987EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float expm1(
const float& x) {
1992EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double expm1(
const double& x) {
1997template <
typename T>
1998EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cos(
const T& x) {
1999 EIGEN_USING_STD(cos);
2003#if defined(EIGEN_SYCL_DEVICE_ONLY)
2004SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cos, cos)
2007#if defined(EIGEN_GPUCC)
2009EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cos(
const float& x) {
2014EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cos(
const double& x) {
2019template <
typename T>
2020EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sin(
const T& x) {
2021 EIGEN_USING_STD(sin);
2025#if defined(EIGEN_SYCL_DEVICE_ONLY)
2026SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sin, sin)
2029#if defined(EIGEN_GPUCC)
2031EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sin(
const float& x) {
2036EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sin(
const double& x) {
2041template <
typename T>
2042EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tan(
const T& x) {
2043 EIGEN_USING_STD(tan);
2047#if defined(EIGEN_SYCL_DEVICE_ONLY)
2048SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tan, tan)
2051#if defined(EIGEN_GPUCC)
2053EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tan(
const float& x) {
2058EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tan(
const double& x) {
2063template <
typename T>
2064EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acos(
const T& x) {
2065 EIGEN_USING_STD(acos);
2069template <
typename T>
2070EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acosh(
const T& x) {
2071 EIGEN_USING_STD(acosh);
2072 return static_cast<T
>(acosh(x));
2075#if defined(EIGEN_SYCL_DEVICE_ONLY)
2076SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acos, acos)
2077SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acosh, acosh)
2080#if defined(EIGEN_GPUCC)
2082EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float acos(
const float& x) {
2087EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double acos(
const double& x) {
2092template <
typename T>
2093EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asin(
const T& x) {
2094 EIGEN_USING_STD(asin);
2098template <
typename T>
2099EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asinh(
const T& x) {
2100 EIGEN_USING_STD(asinh);
2101 return static_cast<T
>(asinh(x));
2104#if defined(EIGEN_SYCL_DEVICE_ONLY)
2105SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asin, asin)
2106SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asinh, asinh)
2109#if defined(EIGEN_GPUCC)
2111EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float asin(
const float& x) {
2116EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double asin(
const double& x) {
2121template <
typename T>
2122EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan(
const T& x) {
2123 EIGEN_USING_STD(atan);
2124 return static_cast<T
>(atan(x));
2127template <typename T, std::enable_if_t<!NumTraits<T>::IsComplex,
int> = 0>
2128EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atan2(
const T& y,
const T& x) {
2129 EIGEN_USING_STD(atan2);
2130 return static_cast<T
>(atan2(y, x));
2133template <
typename T>
2134EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T atanh(
const T& x) {
2135 EIGEN_USING_STD(atanh);
2136 return static_cast<T
>(atanh(x));
2139#if defined(EIGEN_SYCL_DEVICE_ONLY)
2140SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(atan2, atan2)
2141SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atan, atan)
2142SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atanh, atanh)
2145#if defined(EIGEN_GPUCC)
2147EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float atan(
const float& x) {
2152EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double atan(
const double& x) {
2157template <
typename T>
2158EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cosh(
const T& x) {
2159 EIGEN_USING_STD(cosh);
2160 return static_cast<T
>(cosh(x));
2163#if defined(EIGEN_SYCL_DEVICE_ONLY)
2164SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cosh, cosh)
2167#if defined(EIGEN_GPUCC)
2169EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float cosh(
const float& x) {
2174EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double cosh(
const double& x) {
2179template <
typename T>
2180EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sinh(
const T& x) {
2181 EIGEN_USING_STD(sinh);
2182 return static_cast<T
>(sinh(x));
2185#if defined(EIGEN_SYCL_DEVICE_ONLY)
2186SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sinh, sinh)
2189#if defined(EIGEN_GPUCC)
2191EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float sinh(
const float& x) {
2196EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double sinh(
const double& x) {
2201template <
typename T>
2202EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tanh(
const T& x) {
2203 EIGEN_USING_STD(tanh);
2207#if (!defined(EIGEN_GPUCC)) && EIGEN_FAST_MATH && !defined(EIGEN_SYCL_DEVICE_ONLY)
2208EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
float x) {
return internal::ptanh_float(x); }
2211#if defined(EIGEN_SYCL_DEVICE_ONLY)
2212SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tanh, tanh)
2215#if defined(EIGEN_GPUCC)
2217EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float tanh(
const float& x) {
2222EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double tanh(
const double& x) {
2227template <
typename T>
2228EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(
const T& a,
const T& b) {
2229 EIGEN_USING_STD(fmod);
2233#if defined(EIGEN_SYCL_DEVICE_ONLY)
2234SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(fmod, fmod)
2237#if defined(EIGEN_GPUCC)
2239EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float fmod(
const float& a,
const float& b) {
2240 return ::fmodf(a, b);
2244EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double fmod(
const double& a,
const double& b) {
2245 return ::fmod(a, b);
2249template <
typename T>
2250EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T nextafter(
const T& from,
const T& to) {
2251 EIGEN_USING_STD(nextafter);
2252 return nextafter(from, to);
2260template <
typename Scalar,
2261 std::enable_if_t<!std::is_same<Scalar, long double>::value && std::numeric_limits<Scalar>::is_iec559 &&
2262 std::numeric_limits<Scalar>::radix == 2 &&
2263 (
sizeof(Scalar) ==
sizeof(uint16_t) ||
sizeof(Scalar) ==
sizeof(uint32_t) ||
2264 sizeof(Scalar) ==
sizeof(uint64_t)),
2266EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar ceil_power_of_two(
const Scalar& value) {
2267 using Binary = internal::binary_floating_point_traits<Scalar>;
2268 using Bits =
typename Binary::Bits;
2269 const Bits valueBits = Binary::bits(value);
2270 const Bits resultBits = Bits((valueBits + Binary::kFractionMask) & Binary::kExponentMask);
2271 return bit_cast<Scalar>(resultBits);
2276#if !defined(EIGEN_GPU_COMPILE_PHASE)
2277EIGEN_STRONG_INLINE
long double ceil_power_of_two(
const long double& value) {
2278 EIGEN_USING_STD(frexp);
2279 EIGEN_USING_STD(ldexp);
2281 const long double fraction = frexp(value, &exponent);
2282 if (numext::is_exactly_zero(fraction))
return 0.0L;
2283 if (numext::equal_strict(fraction, 0.5L)) --exponent;
2284 if (exponent < std::numeric_limits<long double>::min_exponent - 1)
2285 exponent = std::numeric_limits<long double>::min_exponent - 1;
2286 return ldexp(1.0L, exponent);
2290#if defined(EIGEN_SYCL_DEVICE_ONLY)
2291SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(nextafter, nextafter)
2294#if defined(EIGEN_GPUCC)
2296EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
float nextafter(
const float& from,
const float& to) {
2297 return ::nextafterf(from, to);
2301EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
double nextafter(
const double& from,
const double& to) {
2302 return ::nextafter(from, to);
2306#if defined(EIGEN_SYCL_DEVICE_ONLY)
2307#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY
2308#undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY
2309#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY
2310#undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY
2311#undef SYCL_SPECIALIZE_INTEGER_TYPES_BINARY
2312#undef SYCL_SPECIALIZE_INTEGER_TYPES_UNARY
2313#undef SYCL_SPECIALIZE_FLOATING_TYPES_BINARY
2314#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY
2315#undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE
2316#undef SYCL_SPECIALIZE_GEN_UNARY_FUNC
2317#undef SYCL_SPECIALIZE_UNARY_FUNC
2318#undef SYCL_SPECIALIZE_GEN1_BINARY_FUNC
2319#undef SYCL_SPECIALIZE_GEN2_BINARY_FUNC
2320#undef SYCL_SPECIALIZE_BINARY_FUNC
2327template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2328EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_left(
const Scalar& a,
int n) {
2329 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2330 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) << n));
2333template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2334EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar logical_shift_right(
const Scalar& a,
int n) {
2335 using UnsignedScalar =
typename numext::get_integer_by_size<
sizeof(Scalar)>::unsigned_type;
2336 return bit_cast<Scalar, UnsignedScalar>(
static_cast<UnsignedScalar
>(bit_cast<UnsignedScalar, Scalar>(a) >> n));
2345template <typename Scalar, typename Enable = std::enable_if_t<std::is_integral<Scalar>::value>>
2346EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar arithmetic_shift_right(
const Scalar& a,
int n) {
2347 return static_cast<Scalar
>(a >> n);
2350template <
typename Scalar>
2351EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar fma(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2352 return internal::fma_impl<Scalar>::run(x, y, z);
2356template <
typename Scalar>
2357EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar madd(
const Scalar& x,
const Scalar& y,
const Scalar& z) {
2358 return internal::madd_impl<Scalar>::run(x, y, z);
2366template <
typename Scalar>
2367EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar twoprod_low_fma(
const Scalar& x,
const Scalar& y,
const Scalar& xy) {
2368 return numext::fma(x, y, Scalar(-xy));
2374template <
typename Scalar>
2375EIGEN_DEVICE_FUNC
void twoprod_full_range(
const Scalar& x,
const Scalar& y, Scalar& p_hi, Scalar& p_lo);
2377template <
typename T>
2378EIGEN_DEVICE_FUNC
bool isfinite_impl(
const std::complex<T>& x) {
2379 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2381 return static_cast<unsigned int>((numext::isfinite)(numext::real(x))) & (numext::isfinite)(numext::imag(x));
2383 return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
2387template <
typename T>
2388EIGEN_DEVICE_FUNC
bool isnan_impl(
const std::complex<T>& x) {
2389 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2390 return static_cast<unsigned int>((numext::isnan)(numext::real(x))) | (numext::isnan)(numext::imag(x));
2392 return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
2396template <
typename T>
2397EIGEN_DEVICE_FUNC
bool isinf_impl(
const std::complex<T>& x) {
2398 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2399 const bool has_inf =
static_cast<unsigned int>((numext::isinf)(numext::real(x))) | (numext::isinf)(numext::imag(x));
2400 const bool has_nan = (numext::isnan)(x);
2401 return has_inf & !has_nan;
2403 return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
2411template <
typename Scalar,
bool IsComplex,
bool IsInteger>
2412struct scalar_fuzzy_default_impl {};
2414template <
typename Scalar>
2415struct scalar_fuzzy_default_impl<Scalar, false, false> {
2416 using RealScalar =
typename NumTraits<Scalar>::Real;
2417 template <
typename OtherScalar>
2418 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2419 const RealScalar& prec) {
2420 return numext::abs(x) <= numext::abs(y) * prec;
2422 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2423 return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
2425 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2426 return x <= y || isApprox(x, y, prec);
2430template <
typename Scalar>
2431struct scalar_fuzzy_default_impl<Scalar, false, true> {
2432 using RealScalar =
typename NumTraits<Scalar>::Real;
2433 template <
typename OtherScalar>
2434 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const Scalar&,
const RealScalar&) {
2435 return x == Scalar(0);
2437 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
return x == y; }
2438 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const Scalar& x,
const Scalar& y,
const RealScalar&) {
2445template <
typename Scalar>
2446struct scalar_fuzzy_default_impl<Scalar, true, false> {
2447 using RealScalar =
typename NumTraits<Scalar>::Real;
2448 template <
typename OtherScalar>
2449 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const Scalar& x,
const OtherScalar& y,
2450 const RealScalar& prec) {
2451 return numext::abs(x) <= numext::abs(y) * prec;
2453 EIGEN_DEVICE_FUNC
static inline bool isApprox(
const Scalar& x,
const Scalar& y,
const RealScalar& prec) {
2454 return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
2458template <
typename Scalar>
2459struct scalar_fuzzy_impl
2460 : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
2462template <
typename Scalar,
typename OtherScalar>
2463EIGEN_DEVICE_FUNC
inline bool isMuchSmallerThan(
2464 const Scalar& x,
const OtherScalar& y,
2465 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2466 return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
2469template <
typename Scalar>
2470EIGEN_DEVICE_FUNC
inline bool isApprox(
2471 const Scalar& x,
const Scalar& y,
2472 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2473 return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
2476template <
typename Scalar>
2477EIGEN_DEVICE_FUNC
inline bool isApproxOrLessThan(
2478 const Scalar& x,
const Scalar& y,
2479 const typename NumTraits<Scalar>::Real& precision = NumTraits<Scalar>::dummy_precision()) {
2480 return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
2488struct scalar_fuzzy_impl<bool> {
2489 using RealScalar = bool;
2491 template <
typename OtherScalar>
2492 EIGEN_DEVICE_FUNC
static inline bool isMuchSmallerThan(
const bool& x,
const bool&,
const bool&) {
2496 EIGEN_DEVICE_FUNC
static inline bool isApprox(
bool x,
bool y,
bool) {
return x == y; }
2498 EIGEN_DEVICE_FUNC
static inline bool isApproxOrLessThan(
const bool& x,
const bool& y,
const bool&) {
2509template <
typename RealScalar>
2510struct expm1_impl<std::complex<RealScalar>> {
2511 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2513 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2514 RealScalar xr = x.real();
2515 RealScalar xi = x.imag();
2525 RealScalar erm1 = numext::expm1<RealScalar>(xr);
2526 RealScalar er = erm1 + RealScalar(1.);
2529 if ((numext::isinf)(xr) && !(numext::isfinite)(xi)) {
2530 return xr > RealScalar(0) ? std::complex<RealScalar>(xr, xi * RealScalar(0))
2531 : std::complex<RealScalar>(RealScalar(-1), RealScalar(0));
2533 const RealScalar s = numext::sin(xi);
2534 const RealScalar c = numext::cos(xi);
2536 const RealScalar one_minus_c = c > RealScalar(0) ? s * s / (RealScalar(1) + c) : RealScalar(1) - c;
2539 if ((numext::isinf)(er) && (numext::isfinite)(xr)) {
2540 const RealScalar h = numext::exp(xr / RealScalar(2));
2541 if (!(numext::isinf)(h)) {
2542 return std::complex<RealScalar>((h * c) * h - RealScalar(1), numext::is_exactly_zero(xi) ? xi : (h * s) * h);
2544 const RealScalar h3 = numext::exp(xr / RealScalar(3));
2545 return std::complex<RealScalar>(((h3 * c) * h3) * h3, numext::is_exactly_zero(xi) ? xi : ((h3 * s) * h3) * h3);
2549 const RealScalar real_part = xr < RealScalar(0) ? erm1 - er * one_minus_c : erm1 * c - one_minus_c;
2551 return std::complex<RealScalar>(real_part, numext::is_exactly_zero(xi) ? xi : er * s);
2555template <
typename RealScalar>
2556struct log1p_impl<std::complex<RealScalar>> {
2557 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2559 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& z) {
2560 const RealScalar x = z.real();
2561 const RealScalar y = z.imag();
2564 const bool is_small = numext::abs(x) < RealScalar(0.5) && numext::abs(y) < RealScalar(0.5);
2565 if (is_small && !numext::is_exactly_zero(y)) {
2566 return std::complex<RealScalar>(numext::log1p(x * (RealScalar(2) + x) + y * y) / RealScalar(2),
2567 numext::atan2(y, RealScalar(1) + x));
2572 if (!is_small)
return numext::log(std::complex<RealScalar>(RealScalar(1) + x, y));
2573 return std_fallback::log1p(z);
2582template <
typename RealScalar>
2583struct complex_exp2_impl {
2584 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2586 EIGEN_DEVICE_FUNC
static inline std::complex<RealScalar> run(
const std::complex<RealScalar>& x) {
2587 using ComplexScalar = std::complex<RealScalar>;
2588 const RealScalar a = x.real();
2589 const RealScalar b = x.imag();
2591 if (numext::is_exactly_zero(b))
return ComplexScalar(numext::exp2(a), b);
2594 if ((numext::isinf)(a) && !(numext::isfinite)(b)) {
2595 return a > RealScalar(0) ? ComplexScalar(a, b * RealScalar(0)) : ComplexScalar(RealScalar(0), RealScalar(0));
2599 constexpr bool kWiderThanDouble = NumTraits<RealScalar>::digits() > NumTraits<double>::digits();
2600 using Wide = std::conditional_t<kWiderThanDouble, RealScalar, double>;
2601 const RealScalar ln2_hi = RealScalar(EIGEN_LN2);
2602 const RealScalar ln2_lo = RealScalar((Wide(
static_cast<double>(EIGEN_LN2)) - Wide(ln2_hi)) +
2603 Wide(2.3190468138462996154948554638754786504e-17L));
2606 const bool negative = b < RealScalar(0);
2607 const RealScalar abs_b = numext::abs(b);
2608 RealScalar t_hi, t_lo;
2609 twoprod_full_range(abs_b, ln2_hi, t_hi, t_lo);
2610 t_lo += abs_b * ln2_lo;
2613 RealScalar sin_lo = t_lo;
2614 RealScalar cos_lo_m1 = -t_lo * t_lo / RealScalar(2);
2615 if (numext::abs(t_lo) > numext::sqrt(NumTraits<RealScalar>::epsilon())) {
2616 sin_lo = numext::sin(t_lo);
2617 const RealScalar sin_half = numext::sin(t_lo / RealScalar(2));
2618 cos_lo_m1 = RealScalar(-2) * sin_half * sin_half;
2620 const RealScalar cos_hi = numext::cos(t_hi);
2621 const RealScalar sin_hi = numext::sin(t_hi);
2622 const RealScalar c = cos_hi + (cos_hi * cos_lo_m1 - sin_hi * sin_lo);
2623 const RealScalar s = sin_hi + (sin_hi * cos_lo_m1 + cos_hi * sin_lo);
2626 const int e_max = NumTraits<RealScalar>::max_exponent() - 1;
2627 const int e = a > RealScalar(e_max) ? (a > RealScalar(2 * e_max) ? 2 * e_max : e_max)
2628 : (a < RealScalar(NumTraits<RealScalar>::min_exponent()) ? -e_max : 0);
2629 const RealScalar m = numext::exp2(a - RealScalar(e));
2630 if (numext::abs(t_hi) < (numext::numeric_limits<RealScalar>::min)()) {
2632 const int p = NumTraits<RealScalar>::digits();
2633 const RealScalar b_p = numext::ldexp(abs_b, p);
2634 RealScalar u_hi, u_lo;
2635 twoprod_full_range(b_p, ln2_hi, u_hi, u_lo);
2636 const RealScalar im = numext::ldexp(m * (u_hi + (u_lo + b_p * ln2_lo)), e - p);
2637 return ComplexScalar(numext::ldexp(m, e), negative ? -im : im);
2639 const RealScalar re = e == 0 ? m * c : numext::ldexp(m * c, e);
2640 const RealScalar im = e == 0 ? m * s : numext::ldexp(m * s, e);
2641 return ComplexScalar(re, negative ? -im : im);
2645template <
typename T>
2649#pragma warning(push)
2650#pragma warning(disable : 4804)
2652 EIGEN_DEVICE_FUNC
static EIGEN_ALWAYS_INLINE T run(
const T& x) {
return T(1) / numext::sqrt(x); }
2658#if defined(EIGEN_GPU_COMPILE_PHASE)
2659template <
typename T>
2660struct conj_impl<std::complex<T>, true> {
2661 EIGEN_DEVICE_FUNC
static inline std::complex<T> run(
const std::complex<T>& x) {
2662 return std::complex<T>(numext::real(x), -numext::imag(x));
2670template <
typename T>
2671EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_multiply(
const std::complex<T>& a,
2672 const std::complex<T>& b) {
2673 const T a_real = numext::real(a);
2674 const T a_imag = numext::imag(a);
2675 const T b_real = numext::real(b);
2676 const T b_imag = numext::imag(b);
2677 return std::complex<T>(a_real * b_real - a_imag * b_imag, a_imag * b_real + a_real * b_imag);
2680template <
typename T>
2681EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_fast(
const std::complex<T>& a,
2682 const std::complex<T>& b) {
2683 const T a_real = numext::real(a);
2684 const T a_imag = numext::imag(a);
2685 const T b_real = numext::real(b);
2686 const T b_imag = numext::imag(b);
2687 const T norm = (b_real * b_real + b_imag * b_imag);
2688 return std::complex<T>((a_real * b_real + a_imag * b_imag) / norm, (a_imag * b_real - a_real * b_imag) / norm);
2694template <
typename T>
2695EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE T complex_divide_scalar_quotient(T x, T y) {
2701template <
typename T>
2702EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE std::complex<T> complex_divide_smith_large(
const std::complex<T>& a,
2703 const std::complex<T>& b, T rscale,
2705 const T quarter_highest = NumTraits<T>::highest() / T(4);
2706 const T a_real = numext::real(a);
2707 const T a_imag = numext::imag(a);
2708 const T b_real = numext::real(b);
2709 const T b_imag = numext::imag(b);
2710 const bool large_a = numext::abs(a_real) > quarter_highest || numext::abs(a_imag) > quarter_highest;
2711 const bool large_b = numext::abs(b_real) > quarter_highest || numext::abs(b_imag) > quarter_highest;
2712 const T a_scale = large_a ? T(0.25) : T(1);
2713 const T b_scale = large_b ? T(0.25) : T(1);
2714 const T scaled_denominator = (b_real * b_scale) * rscale + (b_imag * b_scale) * iscale;
2715 const T unscale = (large_a ? T(4) : T(1)) * b_scale;
2716 const T real_quotient =
2717 complex_divide_scalar_quotient((a_real * a_scale) * rscale + (a_imag * a_scale) * iscale, scaled_denominator);
2718 const T imag_quotient =
2719 complex_divide_scalar_quotient((a_imag * a_scale) * rscale - (a_real * a_scale) * iscale, scaled_denominator);
2720 return std::complex<T>(real_quotient * unscale, imag_quotient * unscale);
2723template <
typename T>
2724EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide_smith(
const std::complex<T>& a,
2725 const std::complex<T>& b) {
2726 const T a_real = numext::real(a);
2727 const T a_imag = numext::imag(a);
2728 T b_real = numext::real(b);
2729 T b_imag = numext::imag(b);
2732 const bool scale_imag = numext::abs(b_imag) <= numext::abs(b_real);
2735 if (numext::abs(b_imag) == numext::abs(b_real)) {
2739 iscale = numext::copysign(T(1), b_real) * numext::copysign(T(1), b_imag);
2740 }
else if (scale_imag) {
2744 volatile_barrier(b_real);
2745 iscale = b_imag / b_real;
2747 volatile_barrier(b_imag);
2748 rscale = b_real / b_imag;
2750 const T denominator = b_real * rscale + b_imag * iscale;
2751 const T numerator_real = a_real * rscale + a_imag * iscale;
2752 const T numerator_imag = a_imag * rscale - a_real * iscale;
2758 const int overflowed =
2759 int((numext::isinf)(numerator_real)) | int((numext::isinf)(numerator_imag)) | int((numext::isinf)(denominator));
2760 if (EIGEN_PREDICT_FALSE(overflowed != 0)) {
2761 return complex_divide_smith_large(a, b, rscale, iscale);
2763 return std::complex<T>(numerator_real / denominator, numerator_imag / denominator);
2766template <
typename T>
2767EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide(
const std::complex<T>& a,
2768 const std::complex<T>& b) {
2770 return complex_divide_fast(a, b);
2772 return complex_divide_smith(a, b);
2788template <
typename LhsScalar,
typename RhsScalar>
2789EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
auto divide(
const LhsScalar& a,
const RhsScalar& b) ->
decltype(a / b) {
2793template <
typename T>
2794EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const std::complex<T>& b) {
2795 return internal::complex_divide_smith(a, b);
2798template <
typename T>
2799EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const T& a,
const std::complex<T>& b) {
2800 return internal::complex_divide_smith(std::complex<T>(a, T(0)), b);
2803template <
typename T>
2804EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(
const std::complex<T>& a,
const T& b) {