Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
MathFunctions.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_MATHFUNCTIONS_H
13#define EIGEN_MATHFUNCTIONS_H
14
15// TODO: consider moving these constants to NumTraits.
16// Source: WolframAlpha
17#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
18#define EIGEN_LOG2E 1.442695040888963407359924681001892137426645954152985934135449406931109219L
19#define EIGEN_LN2 0.693147180559945309417232121458176568075500134360255254120680009493393621L
20
21// IWYU pragma: private
22#include "./InternalHeaderCheck.h"
23
24namespace Eigen {
25
26namespace internal {
27
48
49template <typename T, typename dummy = void>
50struct global_math_functions_filtering_base {
51 using type = T;
52};
53
54template <typename T>
55struct global_math_functions_filtering_base<T,
56 void_t<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>> {
57 using type = typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
58};
59
60#define EIGEN_MATHFUNC_IMPL(func, scalar) \
61 Eigen::internal::func##_impl<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>
62
63/****************************************************************************
64 * Implementation of real *
65 ****************************************************************************/
66
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; }
71};
72
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) {
77 using std::real;
78 return real(x);
79 }
80};
81
82template <typename Scalar>
83struct real_impl : real_default_impl<Scalar> {};
84
85#if defined(EIGEN_GPU_COMPILE_PHASE)
86template <typename T>
87struct real_impl<std::complex<T>> {
88 using RealScalar = T;
89 EIGEN_DEVICE_FUNC static inline T run(const std::complex<T>& x) { return x.real(); }
90};
91#endif
92
93/****************************************************************************
94 * Implementation of imag *
95 ****************************************************************************/
96
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); }
101};
102
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) {
107 using std::imag;
108 return imag(x);
109 }
110};
111
112template <typename Scalar>
113struct imag_impl : imag_default_impl<Scalar> {};
114
115#if defined(EIGEN_GPU_COMPILE_PHASE)
116template <typename T>
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(); }
120};
121#endif
122
123/****************************************************************************
124 * Implementation of real_ref *
125 ****************************************************************************/
126
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];
133 }
134};
135
136/****************************************************************************
137 * Implementation of imag_ref *
138 ****************************************************************************/
139
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];
146 }
147};
148
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); }
154};
155
156template <typename Scalar>
157struct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
158
159} // namespace internal
160
161namespace numext {
162
163template <typename Scalar>
164EIGEN_DEVICE_FUNC inline decltype(auto) real(const Scalar& x) {
165 return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
166}
167
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);
172}
173
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);
178}
179
180template <typename Scalar>
181EIGEN_DEVICE_FUNC inline decltype(auto) imag(const Scalar& x) {
182 return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
183}
184
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;
188}
189
190} // namespace numext
191
192namespace internal {
193
194/****************************************************************************
195 * Implementation of conj *
196 ****************************************************************************/
197
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; }
201};
202
203template <typename Scalar>
204struct conj_default_impl<Scalar, true> {
205 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x) {
206 using std::conj;
207 return conj(x);
208 }
209};
210
211template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
212struct conj_impl : conj_default_impl<Scalar, IsComplex> {};
213
214/****************************************************************************
215 * Implementation of mul *
216 ****************************************************************************/
217
218// Unsigned operands narrower than int promote to int, where a product whose wrapped value is representable
219// can still overflow: (unsigned short)0xffff squared is 0xfffe0001 > INT_MAX. Multiplying in unsigned int,
220// wider than every such operand, wraps instead. Mixed operand types, signed types, and bool keep operator*.
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))>
224struct mul_impl {
225 EIGEN_DEVICE_FUNC static constexpr EIGEN_ALWAYS_INLINE auto run(const Lhs& a, const Rhs& b) { return a * b; }
226};
227
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));
232 }
233};
234
235// MSVC's STL evaluates T * complex<T> as complex<T>(a) * b, whose 0 * inf terms turn 2 * (inf, 2) into
236// (inf, NaN). Scale component-wise, as complex<T> * T, libstdc++, libc++, and the GPU overloads do.
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());
242 }
243};
244
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);
251}
252
253/****************************************************************************
254 * Implementation of abs2 *
255 ****************************************************************************/
256
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); }
261};
262
263template <typename Scalar>
264struct abs2_impl_default<Scalar, true> // IsComplex
265{
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);
269 }
270};
271
272template <typename Scalar>
273struct abs2_impl {
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);
277 }
278};
279
280/****************************************************************************
281 * Implementation of sqrt/rsqrt *
282 ****************************************************************************/
283
284template <typename Scalar>
285struct sqrt_impl {
286 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE Scalar run(const Scalar& x) {
287 EIGEN_USING_STD(sqrt);
288 return sqrt(x);
289 }
290};
291
292// Complex sqrt defined in MathFunctionsImpl.h.
293template <typename ComplexT>
294EIGEN_DEVICE_FUNC constexpr ComplexT complex_sqrt(const ComplexT& a_x);
295
296// Custom implementation is faster than `std::sqrt`, works on
297// GPU, and correctly handles special cases (unlike MSVC).
298template <typename T>
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); }
301};
302
303// Default implementation relies on numext::sqrt, at bottom of file.
304template <typename T>
305struct rsqrt_impl;
306
307// Complex rsqrt defined in MathFunctionsImpl.h.
308template <typename ComplexT>
309EIGEN_DEVICE_FUNC constexpr ComplexT complex_rsqrt(const ComplexT& a_x);
310
311template <typename T>
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);
315 }
316};
317
318/****************************************************************************
319 * Implementation of norm1 *
320 ****************************************************************************/
321
322template <typename Scalar, bool IsComplex>
323struct norm1_default_impl;
324
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));
331 }
332};
333
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);
339 return abs(x);
340 }
341};
342
343template <typename Scalar>
344struct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
345
346/****************************************************************************
347 * Implementation of hypot *
348 ****************************************************************************/
349
350template <typename Scalar>
351struct hypot_impl;
352
353/****************************************************************************
354 * Implementation of cast *
355 ****************************************************************************/
356
357template <typename OldType, typename NewType, typename EnableIf = void>
358struct cast_impl {
359 EIGEN_DEVICE_FUNC static inline NewType run(const OldType& x) { return static_cast<NewType>(x); }
360};
361
362template <typename OldType>
363struct cast_impl<OldType, bool> {
364 EIGEN_DEVICE_FUNC static inline bool run(const OldType& x) { return x != OldType(0); }
365};
366
367// Casting from S -> Complex<T> leads to an implicit conversion from S to T,
368// generating warnings on clang. Here we explicitly cast the real component.
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));
374 }
375};
376
377#if EIGEN_ARCH_ARM64 && EIGEN_COMP_CLANG && !EIGEN_CLANG_STRICT_AT_LEAST(21, 0, 0)
378// Clang before 21, and the Clang forks whose version does not say, vectorize a loop of 64-bit integer to float
379// conversions through double on AArch64, which rounds twice. Rounding x to odd on the multiples of 2^11 where
380// |x| >= 2^53 makes the conversion to double exact and leaves a direct conversion unchanged.
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);
386 // In two's complement, clearing the low bits rounds toward -inf for either sign; setting bit 11 then picks the odd
387 // neighbor.
388 const U low = 0x7ff;
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);
392 }
393};
394#endif
395
396// Returns NewType directly to avoid unintended intermediate conversions.
397
398template <typename OldType, typename NewType>
399EIGEN_DEVICE_FUNC inline NewType cast(const OldType& x) {
400 return cast_impl<OldType, NewType>::run(x);
401}
402
403/****************************************************************************
404 * Implementation of arg *
405 ****************************************************************************/
406
407// Visual Studio 2017 has a bug where arg(float) returns 0 for negative inputs.
408// This seems to be fixed in VS 2019.
409#if (!EIGEN_COMP_MSVC || EIGEN_COMP_MSVC >= 1920)
410// std::arg is only defined for types of std::complex, or integer types or float/double/long double
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;
416
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) {
421 // There is no official ::arg on device in CUDA/HIP, so we always need to use std::arg.
422 using std::arg;
423 return static_cast<RealScalar>(arg(x));
424 }
425};
426
427// Must be non-complex floating-point type (e.g. half/bfloat16).
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);
433 }
434};
435#else
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);
441 }
442};
443
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);
449 return arg(x);
450 }
451};
452#endif
453template <typename Scalar>
454struct arg_impl : arg_default_impl<Scalar> {};
455
456/****************************************************************************
457 * Implementation of expm1 *
458 ****************************************************************************/
459
460// This implementation is based on GSL Math's expm1.
461namespace std_fallback {
462// fallback expm1 implementation in case there is no expm1(Scalar) function in namespace of Scalar,
463// or that there is no suitable std::expm1 function available. Implementation
464// attributed to Kahan. See: http://www.plunk.org/~hatch/rightway.php.
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;
469
470 EIGEN_USING_STD(exp);
471 Scalar u = exp(x);
472 if (numext::equal_strict(u, Scalar(1))) {
473 return x;
474 }
475 Scalar um1 = u - RealScalar(1);
476 if (numext::equal_strict(um1, Scalar(-1))) {
477 return RealScalar(-1);
478 }
479
480 EIGEN_USING_STD(log);
481 Scalar logu = log(u);
482 return numext::equal_strict(u, logu) ? u : (u - RealScalar(1)) * x / logu;
483}
484} // namespace std_fallback
485
486template <typename Scalar>
487struct expm1_impl {
488 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x) {
489 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
490 EIGEN_USING_STD(expm1);
491 return expm1(x);
492 }
493};
494
495/****************************************************************************
496 * Implementation of log *
497 ****************************************************************************/
498
499// Complex log defined in MathFunctionsImpl.h.
500template <typename ComplexT>
501EIGEN_DEVICE_FUNC constexpr ComplexT complex_log(const ComplexT& z);
502
503template <typename Scalar>
504struct log_impl {
505 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x) {
506 EIGEN_USING_STD(log);
507 return static_cast<Scalar>(log(x));
508 }
509};
510
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); }
514};
515
516/****************************************************************************
517 * Implementation of log1p *
518 ****************************************************************************/
519
520namespace std_fallback {
521// fallback log1p implementation in case there is no log1p(Scalar) function in namespace of Scalar,
522// or that there is no suitable std::log1p function available
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));
531 // The correction below is 0 * inf or inf / inf once log(1 + x) is not finite; that log is the answer then.
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)));
535}
536} // namespace std_fallback
537
538template <typename Scalar>
539struct log1p_impl {
540 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
541
542 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x) {
543 EIGEN_USING_STD(log1p);
544 return log1p(x);
545 }
546};
547
548// Specialization for complex types that are not supported by std::log1p; defined after the numext functions it is
549// built from.
550template <typename RealScalar>
551struct log1p_impl<std::complex<RealScalar>>;
552
553// numext::exp2 of a complex argument; defined after the numext functions it is built from.
554template <typename RealScalar>
555struct complex_exp2_impl;
556
557/****************************************************************************
558 * Implementation of pow *
559 ****************************************************************************/
560
561template <typename ScalarX, typename ScalarY,
562 bool IsInteger = NumTraits<ScalarX>::IsInteger && NumTraits<ScalarY>::IsInteger>
563struct pow_impl {
564 using result_type =
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);
568 return pow(x, y);
569 }
570};
571
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) {
576 ScalarX res(1);
577 eigen_assert(!NumTraits<ScalarY>::IsSigned || y >= 0);
578 if (y & 1) res = internal::mul(res, x);
579 y >>= 1;
580 while (y) {
581 x = internal::mul(x, x);
582 if (y & 1) res = internal::mul(res, x);
583 y >>= 1;
584 }
585 return res;
586 }
587};
588
589constexpr int floor_log2(unsigned int value) {
590 int result = 0;
591 while (value > 1) {
592 value >>= 1;
593 ++result;
594 }
595 return result;
596}
597
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);
604 int shift = n / 2;
605 while (bits > 0 && shift > 0) {
606 BitsType y = bits >> shift;
607 if (y > 0) {
608 n -= shift;
609 bits = y;
610 }
611 shift /= 2;
612 }
613 if (shift == 0) {
614 --n;
615 }
616 return n;
617 }
618
619 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
620 int n = CHAR_BIT * sizeof(BitsType);
621 int shift = n / 2;
622 while (bits > 0 && shift > 0) {
623 BitsType y = bits << shift;
624 if (y > 0) {
625 n -= shift;
626 bits = y;
627 }
628 shift /= 2;
629 }
630 if (shift == 0) {
631 --n;
632 }
633 return n;
634 }
635
636 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
637 int n = 0;
638 while (bits) {
639 bits &= bits - 1;
640 ++n;
641 }
642 return n;
643 }
644};
645
646// Count leading zeros.
647template <typename BitsType>
648EIGEN_DEVICE_FUNC inline int clz(BitsType bits) {
649 return count_bits_impl<BitsType>::clz(bits);
650}
651
652// Count trailing zeros.
653template <typename BitsType>
654EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
655 return count_bits_impl<BitsType>::ctz(bits);
656}
657
658// Count set bits (population count).
659template <typename BitsType>
660EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
661 return count_bits_impl<BitsType>::popcount(bits);
662}
663
664#if defined(EIGEN_CUDA_ARCH)
665
666// nvcc's device code has only __builtin_clz, __builtin_clzll, __builtin_popcount and __builtin_popcountll. The other
667// builtins are host functions there, and with warning 20014 silenced, a call from here compiles and returns garbage:
668// ctz at every width, and clz and popcount where long has 64 bits. __clz and __clzll return the full width for zero.
669// From CUDA 13.2, their parameter and return types are unsigned on aarch64 with GCC 11.4 or later as the host compiler
670// and signed elsewhere and in earlier toolkits; the conversions below keep the bits under either declaration.
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;
678 }
679
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;
682 }
683
684 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) { return __popc(static_cast<unsigned int>(bits)); }
685};
686
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;
695 }
696
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;
699 }
700
701 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
702 return __popcll(static_cast<unsigned long long>(bits));
703 }
704};
705
706#elif EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
707
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;
715 }
716
717 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
718 return bits == 0 ? kNumBits : __builtin_ctz(static_cast<unsigned int>(bits));
719 }
720
721 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
722 return __builtin_popcount(static_cast<unsigned int>(bits));
723 }
724};
725
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;
734 }
735
736 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
737 return bits == 0 ? kNumBits : __builtin_ctzl(static_cast<unsigned long>(bits));
738 }
739
740 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
741 return __builtin_popcountl(static_cast<unsigned long>(bits));
742 }
743};
744
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;
753 }
754
755 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
756 return bits == 0 ? kNumBits : __builtin_ctzll(static_cast<unsigned long long>(bits));
757 }
758
759 static EIGEN_DEVICE_FUNC inline int popcount(BitsType bits) {
760 return __builtin_popcountll(static_cast<unsigned long long>(bits));
761 }
762};
763
764#elif EIGEN_COMP_MSVC
765
766// `__popcnt`/`__popcnt64` require `POPCNT` hardware support, which MSVC cannot guarantee
767// at its default baseline (unlike `_BitScanReverse`/`_BitScanForward`, which lower to
768// baseline `bsr`/`bsf`). Fall back to a portable count when building without SSE4.2
769// enabled.
770template <typename BitsType>
771EIGEN_DEVICE_FUNC inline int popcount_fallback(BitsType bits) {
772 int n = 0;
773 while (bits) {
774 bits &= bits - 1;
775 ++n;
776 }
777 return n;
778}
779
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) {
785 unsigned long out;
786 _BitScanReverse(&out, static_cast<unsigned long>(bits));
787 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
788 }
789
790 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
791 unsigned long out;
792 _BitScanForward(&out, static_cast<unsigned long>(bits));
793 return bits == 0 ? kNumBits : static_cast<int>(out);
794 }
795
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)));
799#else
800 return popcount_fallback(bits);
801#endif
802 }
803};
804
805#ifdef _WIN64
806
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) {
813 unsigned long out;
814 _BitScanReverse64(&out, static_cast<unsigned __int64>(bits));
815 return bits == 0 ? kNumBits : (kNumBits - 1) - static_cast<int>(out);
816 }
817
818 static EIGEN_DEVICE_FUNC inline int ctz(BitsType bits) {
819 unsigned long out;
820 _BitScanForward64(&out, static_cast<unsigned __int64>(bits));
821 return bits == 0 ? kNumBits : static_cast<int>(out);
822 }
823
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)));
827#else
828 return popcount_fallback(bits);
829#endif
830 }
831};
832
833#endif // _WIN64
834
835#endif // EIGEN_CUDA_ARCH
836
837template <typename BitsType>
838struct log_2_impl {
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;
844 }
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;
848 }
849};
850
851template <typename BitsType>
852int log2_ceil(const BitsType& x) {
853 return log_2_impl<BitsType>::run_ceil(x);
854}
855
856template <typename BitsType>
857int log2_floor(const BitsType& x) {
858 return log_2_impl<BitsType>::run_floor(x);
859}
860
861// Implementation of is* functions
862
863template <typename T>
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),
866 bool>
867isfinite_impl(const T&) {
868 return true;
869}
870
871template <typename 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),
875 bool>
876isfinite_impl(const T& x) {
877 EIGEN_USING_STD(isfinite);
878 return isfinite EIGEN_NOT_A_MACRO(x);
879}
880
881template <typename T>
882EIGEN_DEVICE_FUNC std::enable_if_t<!std::numeric_limits<T>::has_infinity, bool> isinf_impl(const T&) {
883 return false;
884}
885
886template <typename T>
887EIGEN_DEVICE_FUNC std::enable_if_t<(std::numeric_limits<T>::has_infinity && !NumTraits<T>::IsComplex), bool> isinf_impl(
888 const T& x) {
889 EIGEN_USING_STD(isinf);
890 return isinf EIGEN_NOT_A_MACRO(x);
891}
892
893template <typename T>
894EIGEN_DEVICE_FUNC
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&) {
897 return false;
898}
899
900template <typename 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),
903 bool>
904isnan_impl(const T& x) {
905 EIGEN_USING_STD(isnan);
906 return isnan EIGEN_NOT_A_MACRO(x);
907}
908
909// The following overload are defined at the end of this file
910template <typename T>
911EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x);
912template <typename T>
913EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x);
914template <typename T>
915EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x);
916template <typename T>
917EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS T ptanh_float(const T& a_x);
918
919/****************************************************************************
920 * Implementation of sign *
921 ****************************************************************************/
922template <typename Scalar, bool IsComplex = (NumTraits<Scalar>::IsComplex != 0),
923 bool IsInteger = (NumTraits<Scalar>::IsInteger != 0)>
924struct sign_impl {
925 EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& a) { return Scalar((a > Scalar(0)) - (a < Scalar(0))); }
926};
927
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)));
932 }
933};
934
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);
944 // |z| lies in [m, sqrt(2) m] for m = max(|re|, |im|): it overflows only for m > max / sqrt(2) and is rounded to the
945 // subnormal grid for m < min. Then u = z / m, with max(|Re u|, |Im u|) = 1, gives sign(z) = u / sqrt(|u|^2). The
946 // trigger is on the components, as in psign_impl. Non-finite z keeps z / |z|. Under -ffast-math only m > max / 2
947 // is known to give a unit result (tested from 0.6 max). |z| is formed only after the rescaling branch: for large m
948 // it would raise a spurious overflow, and a NaN component must not reach the 0 / 0 of the rescaling.
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)) {
955 // For large m, z / m is formed as (z / 4) / (m / 4): -ffast-math rewrites z / m as z * (1 / m), and 1 / m is
956 // subnormal for m near max and flushed. The barrier keeps m / 4 from being folded back into the quotient.
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);
964 }
965 }
966 const real_type aa = abs(a);
967 // Divide rather than multiply by 1/aa: the reciprocal overflows for subnormal aa and is itself
968 // subnormal, hence inexact, for aa near the top of the range.
969 return Scalar(re / aa, im / aa);
970 }
971};
972
973// The sign function for bool is the identity.
974template <>
975struct sign_impl<bool, false, true> {
976 EIGEN_DEVICE_FUNC static inline bool run(const bool& a) { return a; }
977};
978
979// Extra namespace to prevent leaking std::copysign into Eigen::internal.
980namespace has_copysign_detail {
981
982template <typename T, typename EnableIf = void>
983struct has_copysign_impl : public std::false_type {};
984
985EIGEN_USING_STD(copysign)
986
987template <typename T>
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 {};
991
992} // namespace has_copysign_detail
993
994// Detects whether copysign(T, T) is available via std:: or ADL. When unavailable,
995// copysign_impl falls back to `< Scalar(0)` comparisons, which cannot inspect the
996// sign bit of negative zero or NaN (e.g., copysign(-0, 1) returns -0, copysign(1, -0)
997// returns +1, and complex_sqrt(-1 - 0i) returns +i rather than -i).
998template <typename T>
999struct has_copysign : public has_copysign_detail::has_copysign_impl<T> {};
1000
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));
1008 }
1009};
1010
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)));
1017 }
1018};
1019
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;
1026 }
1027};
1028
1029template <>
1030struct copysign_impl<bool, false, true, false> {
1031 EIGEN_DEVICE_FUNC static inline bool run(const bool& a, const bool&) { return a; }
1032};
1033
1034// suppress "unary minus operator applied to unsigned type, result still unsigned" warnings on MSVC
1035// note: `0 - a` is distinct from `-a` when Scalar is a floating point type and `a` is zero
1036
1037template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger>
1038struct negate_impl {
1039 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar run(const Scalar& a) { return -a; }
1040};
1041
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; }
1046};
1047
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);
1052 }
1053 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_ceil(const Scalar& x) {
1054 EIGEN_USING_STD(ceil) return ceil(x);
1055 }
1056 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_rint(const Scalar& x) {
1057 EIGEN_USING_STD(rint) return rint(x);
1058 }
1059 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_round(const Scalar& x) {
1060 EIGEN_USING_STD(round) return round(x);
1061 }
1062 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run_trunc(const Scalar& x) {
1063 EIGEN_USING_STD(trunc) return trunc(x);
1064 }
1065};
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; }
1073};
1074
1075// Extra namespace to prevent leaking std::fma into Eigen::internal.
1076namespace has_fma_detail {
1077
1078template <typename T, typename EnableIf = void>
1079struct has_fma_impl : public std::false_type {};
1080
1081using std::fma;
1082
1083template <typename T>
1084struct has_fma_impl<
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 {};
1087
1088} // namespace has_fma_detail
1089
1090template <typename T>
1091struct has_fma : public has_fma_detail::has_fma_impl<T> {};
1092
1093// Default implementation.
1094template <typename T, typename Enable = void>
1095struct fma_impl {
1096 static_assert(has_fma<T>::value, "No function fma(...) for type. Please provide an implementation.");
1097};
1098
1099// STD or ADL version if it exists.
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) {
1103 using std::fma;
1104 return fma(a, b, c);
1105 }
1106};
1107
1108#if defined(EIGEN_GPUCC)
1109template <>
1110struct has_fma<float> : public std::true_type {};
1111
1112template <>
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);
1116 }
1117};
1118
1119template <>
1120struct has_fma<double> : public std::true_type {};
1121
1122template <>
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);
1126 }
1127};
1128#endif
1129
1130// Basic multiply-add.
1131template <typename Scalar, typename EnableIf = void>
1132struct madd_impl {
1133 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar run(const Scalar& x, const Scalar& y, const Scalar& z) {
1134 // Separate statements prevent FP_CONTRACT ON from fusing the unfused fallback.
1135 const Scalar xy = x * y;
1136 return xy + z;
1137 }
1138};
1139
1140// Whether fma on Scalar is an instruction rather than a libm routine. x87 extended and IBM double-double long double
1141// have no fma instruction, nor does binary128 on most targets.
1142template <typename Scalar>
1143struct has_fast_fma : std::false_type {};
1144#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMAF) || defined(__FP_FAST_FMAF)
1145template <>
1146struct has_fast_fma<float> : std::true_type {};
1147#endif
1148#if defined(EIGEN_VECTORIZE_FMA) || defined(FP_FAST_FMA) || defined(__FP_FAST_FMA)
1149template <>
1150struct has_fast_fma<double> : std::true_type {};
1151#endif
1152#if defined(FP_FAST_FMAL) || defined(__FP_FAST_FMAL)
1153template <>
1154struct has_fast_fma<long double> : std::true_type {};
1155#else
1156// A long double with double's format is as fast as double.
1157template <>
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> {};
1161#endif
1162
1163// Whether numext::madd fuses Scalar: as EIGEN_SCALAR_MADD_USE_FMA says, except that long double is fused only where
1164// its fma is an instruction, because scalar GEBP through libm's fmal is far slower than the unfused product and sum.
1165// has_fma<Scalar> is passed as a type, not a value, so it is instantiated only when selected, never when
1166// EIGEN_SCALAR_MADD_USE_FMA is 0.
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> {};
1172
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);
1177 }
1178};
1179
1180// Representation properties shared by operations that inspect an IEEE-style binary floating-point value.
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;
1192
1193 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits bits(const Scalar& value) { return numext::bit_cast<Bits>(value); }
1194
1195 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Bits magnitude(const Scalar& value) { return bits(value) & ~kSignBit; }
1196};
1197
1198// Redundant out-of-class definitions are required pre-C++17 but deprecated since.
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>
1215constexpr
1216 typename binary_floating_point_traits<Scalar>::Bits binary_floating_point_traits<Scalar>::kMaxFiniteExponentBits;
1217#endif
1218
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);
1222}
1223
1224template <typename Scalar>
1225EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE bool is_zero_magnitude_bits(
1226 typename binary_floating_point_traits<Scalar>::Bits magnitude) {
1227 // Keep the integer comparison out of line: Clang can otherwise fold it into a
1228 // floating-point comparison that treats subnormals as zero under DAZ/FZ.
1229 return magnitude == 0;
1230}
1231
1232// Floating-point comparisons can treat subnormals as zero under DAZ/FTZ.
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;
1240 // magnitude | -magnitude has its high bit set iff magnitude is nonzero.
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));
1246 }
1247};
1248
1249template <>
1250struct sign_impl<float, false, false> : sign_impl_binary<float> {};
1251
1252template <>
1253struct sign_impl<double, false, false> : sign_impl_binary<double> {};
1254
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));
1258}
1259
1260// |x - y|, as the packet path computes it: abs keeps the +0 of (+0, -0) and the NaN of (inf, inf).
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);
1266 return abs(x - y);
1267 }
1268};
1269
1270// Unsigned x - y wraps, so integers order the operands.
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; }
1274};
1275
1276// A signed integer subtracts in the unsigned type, since |x - y| > max overflows the signed one.
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));
1283 }
1284};
1285
1286} // end namespace internal
1287
1288/****************************************************************************
1289 * Generic math functions *
1290 ****************************************************************************/
1291
1292namespace numext {
1293
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>());
1300}
1301
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);
1307}
1308
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);
1313}
1314#else
1315template <typename T>
1316EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) {
1317 return y < x ? y : x;
1318}
1319#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1320// Without relaxed constexpr, numeric GPU scalars keep fmin/fmax's number-preferring
1321// NaN behavior. With relaxed constexpr, they use the constexpr ternary overloads,
1322// matching std::min/std::max behavior and supporting custom less-comparable scalars.
1323template <>
1324EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y) {
1325 return fminf(x, y);
1326}
1327template <>
1328EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double mini(const double& x, const double& y) {
1329 return fmin(x, y);
1330}
1331
1332#ifndef EIGEN_GPU_COMPILE_PHASE
1333template <>
1334EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double mini(const long double& x, const long double& y) {
1335#if defined(EIGEN_HIPCC)
1336 // no "fminl" on HIP yet
1337 return (x < y) ? x : y;
1338#else
1339 return fminl(x, y);
1340#endif
1341}
1342#endif
1343#endif
1344
1345template <typename T>
1346EIGEN_DEVICE_FUNC constexpr EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y) {
1347 return x < y ? y : x;
1348}
1349#if !defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)
1350template <>
1351EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y) {
1352 return fmaxf(x, y);
1353}
1354template <>
1355EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double maxi(const double& x, const double& y) {
1356 return fmax(x, y);
1357}
1358#ifndef EIGEN_GPU_COMPILE_PHASE
1359template <>
1360EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double maxi(const long double& x, const long double& y) {
1361#if defined(EIGEN_HIPCC)
1362 // no "fmaxl" on HIP yet
1363 return (x > y) ? x : y;
1364#else
1365 return fmaxl(x, y);
1366#endif
1367}
1368#endif
1369#endif
1370#endif
1371
1372#if defined(EIGEN_SYCL_DEVICE_ONLY)
1373
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)
1409
1410#define SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
1411 template <> \
1412 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE& x) { \
1413 return cl::sycl::FUNC(x); \
1414 }
1415
1416#define SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, TYPE, TYPE)
1417
1418#define SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE1, ARG_TYPE2) \
1419 template <> \
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); \
1422 }
1423
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)
1426
1427#define SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, TYPE) SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, TYPE, TYPE)
1428
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)
1433
1434#endif
1435
1436template <typename Scalar>
1437EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real arg(const Scalar& x) {
1438 return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
1439}
1440
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);
1445}
1446
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);
1451}
1452
1453template <typename Scalar>
1454EIGEN_DEVICE_FUNC inline Scalar conj(const Scalar& x) {
1455 return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
1456}
1457
1458template <typename Scalar>
1459EIGEN_DEVICE_FUNC inline Scalar sign(const Scalar& x) {
1460 return EIGEN_MATHFUNC_IMPL(sign, Scalar)::run(x);
1461}
1462
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);
1466}
1467
1468#if defined(EIGEN_SYCL_DEVICE_ONLY)
1469SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(copysign, copysign)
1470#endif
1471
1472template <typename Scalar>
1473EIGEN_DEVICE_FUNC inline Scalar negate(const Scalar& x) {
1474 return EIGEN_MATHFUNC_IMPL(negate, Scalar)::run(x);
1475}
1476
1477template <typename Scalar>
1478EIGEN_DEVICE_FUNC inline decltype(auto) abs2(const Scalar& x) {
1479 return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
1480}
1481
1482EIGEN_DEVICE_FUNC inline bool abs2(bool x) { return x; }
1483
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);
1487}
1488template <>
1489EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float absdiff(const float& x, const float& y) {
1490 return fabsf(x - y);
1491}
1492template <>
1493EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double absdiff(const double& x, const double& y) {
1494 return fabs(x - y);
1495}
1496
1497// HIP and CUDA do not support long double.
1498#ifndef EIGEN_GPU_COMPILE_PHASE
1499template <>
1500EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE long double absdiff(const long double& x, const long double& y) {
1501 return fabsl(x - y);
1502}
1503#endif
1504
1505template <typename Scalar>
1506EIGEN_DEVICE_FUNC inline typename NumTraits<Scalar>::Real norm1(const Scalar& x) {
1507 return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
1508}
1509
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);
1513}
1514
1515#if defined(EIGEN_SYCL_DEVICE_ONLY)
1516SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(hypot, hypot)
1517#endif
1518
1519template <typename Scalar>
1520EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
1521 return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
1522}
1523
1524#if defined(EIGEN_SYCL_DEVICE_ONLY)
1525SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log1p, log1p)
1526#endif
1527
1528#if defined(EIGEN_GPUCC)
1529template <>
1530EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log1p(const float& x) {
1531 return ::log1pf(x);
1532}
1533
1534template <>
1535EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log1p(const double& x) {
1536 return ::log1p(x);
1537}
1538#endif
1539
1540template <typename ScalarX, typename ScalarY>
1541EIGEN_DEVICE_FUNC inline typename internal::pow_impl<ScalarX, ScalarY>::result_type pow(const ScalarX& x,
1542 const ScalarY& y) {
1543 return internal::pow_impl<ScalarX, ScalarY>::run(x, y);
1544}
1545
1546#if defined(EIGEN_SYCL_DEVICE_ONLY)
1547SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(pow, pow)
1548#endif
1549
1550template <typename T>
1551EIGEN_DEVICE_FUNC bool(isnan)(const T& x) {
1552 return internal::isnan_impl(x);
1553}
1554template <typename T>
1555EIGEN_DEVICE_FUNC bool(isinf)(const T& x) {
1556 return internal::isinf_impl(x);
1557}
1558template <typename T>
1559EIGEN_DEVICE_FUNC bool(isfinite)(const T& x) {
1560 return internal::isfinite_impl(x);
1561}
1562
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)
1567#endif
1568
1569template <typename Scalar>
1570EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar rint(const Scalar& x) {
1571 return internal::nearest_integer_impl<Scalar>::run_rint(x);
1572}
1573
1574template <typename Scalar>
1575EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar round(const Scalar& x) {
1576 return internal::nearest_integer_impl<Scalar>::run_round(x);
1577}
1578
1579template <typename Scalar>
1580EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(floor)(const Scalar& x) {
1581 return internal::nearest_integer_impl<Scalar>::run_floor(x);
1582}
1583
1584template <typename Scalar>
1585EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(ceil)(const Scalar& x) {
1586 return internal::nearest_integer_impl<Scalar>::run_ceil(x);
1587}
1588
1589template <typename Scalar>
1590EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar(trunc)(const Scalar& x) {
1591 return internal::nearest_integer_impl<Scalar>::run_trunc(x);
1592}
1593
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)
1600#endif
1601
1602#if defined(EIGEN_GPUCC)
1603template <>
1604EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float floor(const float& x) {
1605 return ::floorf(x);
1606}
1607template <>
1608EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double floor(const double& x) {
1609 return ::floor(x);
1610}
1611template <>
1612EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float ceil(const float& x) {
1613 return ::ceilf(x);
1614}
1615template <>
1616EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double ceil(const double& x) {
1617 return ::ceil(x);
1618}
1619template <>
1620EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float trunc(const float& x) {
1621 return ::truncf(x);
1622}
1623template <>
1624EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double trunc(const double& x) {
1625 return ::trunc(x);
1626}
1627#endif
1628
1629// Integer division with rounding up.
1630// T is assumed to be an integer type with a>=0, and b>0
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)
1635 // Note: explicitly declaring a and b as non-negative values allows the compiler to use better optimizations
1636 const UnsignedT ua = UnsignedT(a);
1637 const UnsignedT ub = UnsignedT(b);
1638 // Note: This form is used because it cannot overflow.
1639 return ua == 0 ? 0 : (ua - 1) / ub + 1;
1640}
1641
1642// Integer round down to nearest power of b
1643// T is assumed to be an integer type with a>=0, and b>0
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)
1650 // Note: explicitly declaring a and b as non-negative values allows the compiler to use better optimizations
1651 const UnsignedT ua = UnsignedT(a);
1652 const UnsignedU ub = UnsignedU(b);
1653 return ub * (ua / ub);
1654}
1655
1656template <typename T>
1657EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log10(T x) {
1658 EIGEN_USING_STD(log10);
1659 return log10(x);
1660}
1661
1662template <typename T>
1663EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log2(T x) {
1664 EIGEN_USING_STD(log2);
1665 return log2(x);
1666}
1667
1668#if defined(EIGEN_SYCL_DEVICE_ONLY)
1669template <>
1670EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log10(float x) {
1671 return cl::sycl::log10(x);
1672}
1673template <>
1674EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log10(double x) {
1675 return cl::sycl::log10(x);
1676}
1677template <>
1678EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log2(float x) {
1679 return cl::sycl::log2(x);
1680}
1681template <>
1682EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log2(double x) {
1683 return cl::sycl::log2(x);
1684}
1685#endif
1686
1689constexpr int log2(int x) {
1690 unsigned int v(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};
1693 v |= v >> 1;
1694 v |= v >> 2;
1695 v |= v >> 4;
1696 v |= v >> 8;
1697 v |= v >> 16;
1698 return table[(v * 0x07C4ACDDU) >> 27];
1699}
1700
1710template <typename Scalar>
1711EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Scalar sqrt(const Scalar& x) {
1712 return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
1713}
1714
1715// Boolean specialization, avoids implicit float to bool conversion (-Wimplicit-conversion-floating-point-to-bool).
1716template <>
1717EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_DEVICE_FUNC bool sqrt<bool>(const bool& x) {
1718 return x;
1719}
1720
1721#if defined(EIGEN_SYCL_DEVICE_ONLY)
1722SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sqrt, sqrt)
1723#endif
1724
1725#if defined(EIGEN_VECTORIZE_SSE)
1726// Half/BFloat16 scalar wrappers precede the SSE definitions and must not instantiate the primary template.
1727template <>
1728EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float& x);
1729template <>
1730EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double sqrt(const double& x);
1731#endif
1732
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));
1738}
1739
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));
1744}
1745
1746#if defined(EIGEN_SYCL_DEVICE_ONLY)
1747SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cbrt, cbrt)
1748#endif
1749
1751template <typename T>
1752EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T rsqrt(const T& x) {
1753 return internal::rsqrt_impl<T>::run(x);
1754}
1755
1756template <typename T>
1757EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T log(const T& x) {
1758 return internal::log_impl<T>::run(x);
1759}
1760
1761#if defined(EIGEN_SYCL_DEVICE_ONLY)
1762SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log, log)
1763#endif
1764
1765#if defined(EIGEN_GPUCC)
1766template <>
1767EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float log(const float& x) {
1768 return ::logf(x);
1769}
1770
1771template <>
1772EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double log(const double& x) {
1773 return ::log(x);
1774}
1775#endif
1776
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>
1780abs(const T& x) {
1781 EIGEN_USING_STD(abs);
1782 return abs(x);
1783}
1784
1785template <typename T>
1786EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::enable_if_t<NumTraits<T>::IsComplex, typename NumTraits<T>::Real> abs(
1787 const T& x) {
1788 return numext::hypot(numext::real(x), numext::imag(x));
1789}
1790
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>
1794abs(const T& x) {
1795 return x;
1796}
1797
1798#if defined(EIGEN_SYCL_DEVICE_ONLY)
1799SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(abs, abs)
1800SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(abs, fabs)
1801#endif
1802
1803#if defined(EIGEN_GPUCC)
1804template <>
1805EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const float& x) {
1806 return ::fabsf(x);
1807}
1808
1809template <>
1810EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const double& x) {
1811 return ::fabs(x);
1812}
1813
1814template <>
1815EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float abs(const std::complex<float>& x) {
1816 return ::hypotf(x.real(), x.imag());
1817}
1818
1819template <>
1820EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double abs(const std::complex<double>& x) {
1821 return ::hypot(x.real(), x.imag());
1822}
1823#endif
1824
1825template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger, bool IsSigned = NumTraits<Scalar>::IsSigned>
1826struct signbit_impl;
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);
1834 a = a >> Shift;
1835 Scalar result = bit_cast<Scalar, intSize_t>(a);
1836 return result;
1837 }
1838};
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; }
1844};
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); }
1848};
1849template <typename Scalar>
1850EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar signbit(const Scalar& x) {
1851 return signbit_impl<Scalar>::run(x);
1852}
1853
1854template <typename T>
1855EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp(const T& x) {
1856 EIGEN_USING_STD(exp);
1857 return exp(x);
1858}
1859
1860// MSVC through at least 19.51 mishandles some finite-real, non-finite-imaginary inputs to std::exp(complex).
1861#if EIGEN_COMP_MSVC
1862template <typename RealScalar>
1863EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::complex<RealScalar> exp(const std::complex<RealScalar>& x) {
1864 // Evaluate eagerly to preserve the floating-point exceptions raised by std::exp before correcting its result.
1865 const std::complex<RealScalar> result = std::exp(x);
1866 // If z is (x,±∞) (for any finite x), the result is (NaN,NaN) and FE_INVALID is raised.
1867 // If z is (x,NaN) (for any finite x), the result is (NaN,NaN) and FE_INVALID may be raised.
1868 if ((isfinite)(real_ref(x)) && !(isfinite)(imag_ref(x))) {
1869 return std::complex<RealScalar>(NumTraits<RealScalar>::quiet_NaN(), NumTraits<RealScalar>::quiet_NaN());
1870 }
1871#if EIGEN_COMP_MSVC < 1931
1872 // Before 19.31, MSVC also mishandles a positive-infinite real part with a non-finite imaginary part.
1873 // If z is (+∞,±∞), the result is (±∞,NaN) and FE_INVALID is raised (the sign of the real part is unspecified)
1874 // If z is (+∞,NaN), the result is (±∞,NaN) (the sign of the real part is unspecified)
1875 if ((real_ref(x) == NumTraits<RealScalar>::infinity() && !(isfinite)(imag_ref(x)))) {
1876 return std::complex<RealScalar>(NumTraits<RealScalar>::infinity(), NumTraits<RealScalar>::quiet_NaN());
1877 }
1878#endif
1879 return result;
1880}
1881#endif
1882
1883#if defined(EIGEN_SYCL_DEVICE_ONLY)
1884SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp, exp)
1885#endif
1886
1887#if defined(EIGEN_GPUCC)
1888template <>
1889EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float exp(const float& x) {
1890 return ::expf(x);
1891}
1892
1893template <>
1894EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double exp(const double& x) {
1895 return ::exp(x);
1896}
1897
1898// exp(x + 0i) = exp(x) + 0i even where exp(x) overflows, so a zero imaginary part is kept rather than computed as
1899// inf * 0.
1900template <>
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);
1906}
1907
1908template <>
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);
1914}
1915#endif
1916
1917template <typename T>
1918EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T exp2(const T& x) {
1919 EIGEN_USING_STD(exp2);
1920 return exp2(x);
1921}
1922
1923// There is no std::exp2 for complex arguments.
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);
1927}
1928
1929#if defined(EIGEN_SYCL_DEVICE_ONLY)
1930SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp2, exp2)
1931#endif
1932
1933#if defined(EIGEN_GPUCC)
1934template <>
1935EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float exp2(const float& x) {
1936 return ::exp2f(x);
1937}
1938
1939template <>
1940EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double exp2(const double& x) {
1941 return ::exp2(x);
1942}
1943#endif
1944
1945// Exact scaling by 2^exponent, including denormals and unrepresentable scale factors.
1946// Results outside the finite range saturate to zero or infinity.
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));
1951}
1952
1953#if defined(EIGEN_SYCL_DEVICE_ONLY)
1954template <>
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);
1957}
1958template <>
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);
1961}
1962#endif
1963
1964#if defined(EIGEN_GPUCC)
1965template <>
1966EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float ldexp(const float& x, int exponent) {
1967 return ::ldexpf(x, exponent);
1968}
1969
1970template <>
1971EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double ldexp(const double& x, int exponent) {
1972 return ::ldexp(x, exponent);
1973}
1974#endif
1975
1976template <typename Scalar>
1977EIGEN_DEVICE_FUNC inline Scalar expm1(const Scalar& x) {
1978 return EIGEN_MATHFUNC_IMPL(expm1, Scalar)::run(x);
1979}
1980
1981#if defined(EIGEN_SYCL_DEVICE_ONLY)
1982SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(expm1, expm1)
1983#endif
1984
1985#if defined(EIGEN_GPUCC)
1986template <>
1987EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float expm1(const float& x) {
1988 return ::expm1f(x);
1989}
1990
1991template <>
1992EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double expm1(const double& x) {
1993 return ::expm1(x);
1994}
1995#endif
1996
1997template <typename T>
1998EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cos(const T& x) {
1999 EIGEN_USING_STD(cos);
2000 return cos(x);
2001}
2002
2003#if defined(EIGEN_SYCL_DEVICE_ONLY)
2004SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cos, cos)
2005#endif
2006
2007#if defined(EIGEN_GPUCC)
2008template <>
2009EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float cos(const float& x) {
2010 return ::cosf(x);
2011}
2012
2013template <>
2014EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double cos(const double& x) {
2015 return ::cos(x);
2016}
2017#endif
2018
2019template <typename T>
2020EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T sin(const T& x) {
2021 EIGEN_USING_STD(sin);
2022 return sin(x);
2023}
2024
2025#if defined(EIGEN_SYCL_DEVICE_ONLY)
2026SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sin, sin)
2027#endif
2028
2029#if defined(EIGEN_GPUCC)
2030template <>
2031EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sin(const float& x) {
2032 return ::sinf(x);
2033}
2034
2035template <>
2036EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double sin(const double& x) {
2037 return ::sin(x);
2038}
2039#endif
2040
2041template <typename T>
2042EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tan(const T& x) {
2043 EIGEN_USING_STD(tan);
2044 return tan(x);
2045}
2046
2047#if defined(EIGEN_SYCL_DEVICE_ONLY)
2048SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tan, tan)
2049#endif
2050
2051#if defined(EIGEN_GPUCC)
2052template <>
2053EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float tan(const float& x) {
2054 return ::tanf(x);
2055}
2056
2057template <>
2058EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double tan(const double& x) {
2059 return ::tan(x);
2060}
2061#endif
2062
2063template <typename T>
2064EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T acos(const T& x) {
2065 EIGEN_USING_STD(acos);
2066 return acos(x);
2067}
2068
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));
2073}
2074
2075#if defined(EIGEN_SYCL_DEVICE_ONLY)
2076SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acos, acos)
2077SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acosh, acosh)
2078#endif
2079
2080#if defined(EIGEN_GPUCC)
2081template <>
2082EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float acos(const float& x) {
2083 return ::acosf(x);
2084}
2085
2086template <>
2087EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double acos(const double& x) {
2088 return ::acos(x);
2089}
2090#endif
2091
2092template <typename T>
2093EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T asin(const T& x) {
2094 EIGEN_USING_STD(asin);
2095 return asin(x);
2096}
2097
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));
2102}
2103
2104#if defined(EIGEN_SYCL_DEVICE_ONLY)
2105SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asin, asin)
2106SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asinh, asinh)
2107#endif
2108
2109#if defined(EIGEN_GPUCC)
2110template <>
2111EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float asin(const float& x) {
2112 return ::asinf(x);
2113}
2114
2115template <>
2116EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double asin(const double& x) {
2117 return ::asin(x);
2118}
2119#endif
2120
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));
2125}
2126
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));
2131}
2132
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));
2137}
2138
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)
2143#endif
2144
2145#if defined(EIGEN_GPUCC)
2146template <>
2147EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float atan(const float& x) {
2148 return ::atanf(x);
2149}
2150
2151template <>
2152EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double atan(const double& x) {
2153 return ::atan(x);
2154}
2155#endif
2156
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));
2161}
2162
2163#if defined(EIGEN_SYCL_DEVICE_ONLY)
2164SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cosh, cosh)
2165#endif
2166
2167#if defined(EIGEN_GPUCC)
2168template <>
2169EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float cosh(const float& x) {
2170 return ::coshf(x);
2171}
2172
2173template <>
2174EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double cosh(const double& x) {
2175 return ::cosh(x);
2176}
2177#endif
2178
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));
2183}
2184
2185#if defined(EIGEN_SYCL_DEVICE_ONLY)
2186SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sinh, sinh)
2187#endif
2188
2189#if defined(EIGEN_GPUCC)
2190template <>
2191EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sinh(const float& x) {
2192 return ::sinhf(x);
2193}
2194
2195template <>
2196EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double sinh(const double& x) {
2197 return ::sinh(x);
2198}
2199#endif
2200
2201template <typename T>
2202EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T tanh(const T& x) {
2203 EIGEN_USING_STD(tanh);
2204 return tanh(x);
2205}
2206
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); }
2209#endif
2210
2211#if defined(EIGEN_SYCL_DEVICE_ONLY)
2212SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tanh, tanh)
2213#endif
2214
2215#if defined(EIGEN_GPUCC)
2216template <>
2217EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float tanh(const float& x) {
2218 return ::tanhf(x);
2219}
2220
2221template <>
2222EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double tanh(const double& x) {
2223 return ::tanh(x);
2224}
2225#endif
2226
2227template <typename T>
2228EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(const T& a, const T& b) {
2229 EIGEN_USING_STD(fmod);
2230 return fmod(a, b);
2231}
2232
2233#if defined(EIGEN_SYCL_DEVICE_ONLY)
2234SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(fmod, fmod)
2235#endif
2236
2237#if defined(EIGEN_GPUCC)
2238template <>
2239EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float fmod(const float& a, const float& b) {
2240 return ::fmodf(a, b);
2241}
2242
2243template <>
2244EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double fmod(const double& a, const double& b) {
2245 return ::fmod(a, b);
2246}
2247#endif
2248
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);
2253}
2254
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)),
2265 int> = 0>
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);
2272}
2273
2274// long double has no portable object representation: depending on the ABI it can be binary64, padded x87 extended
2275// precision, IEEE binary128, or IBM double-double. Construct the result arithmetically instead of inspecting its bits.
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);
2280 int exponent = 0;
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);
2287}
2288#endif
2289
2290#if defined(EIGEN_SYCL_DEVICE_ONLY)
2291SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(nextafter, nextafter)
2292#endif
2293
2294#if defined(EIGEN_GPUCC)
2295template <>
2296EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float nextafter(const float& from, const float& to) {
2297 return ::nextafterf(from, to);
2298}
2299
2300template <>
2301EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE double nextafter(const double& from, const double& to) {
2302 return ::nextafter(from, to);
2303}
2304#endif
2305
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
2321#endif
2322
2323// A logical shift is a shift of the bit pattern, so it goes through the unsigned type of the same
2324// width whatever Scalar's signedness. A Scalar narrower than int promotes to int for the shift
2325// itself, which the explicit truncation undoes: for the left shift the promoted value can carry set
2326// bits above Scalar's width, and dropping them is the operation rather than an accident of the cast.
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));
2331}
2332
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));
2337}
2338
2339// An arithmetic shift propagates the sign bit, so it coincides with the logical shift when Scalar is
2340// unsigned and has none. Shifting through the signed type unconditionally would sign-extend an
2341// ordinary value bit, which is what every backend's parithmetic_shift_right on unsigned packets
2342// avoids, leaving the scalar and vectorized paths of one expression disagreeing. Scalar's own
2343// operator>> already selects on its signedness, so no reinterpretation is needed; the cast only
2344// undoes the integral promotion a Scalar narrower than int is subject to.
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);
2348}
2349
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);
2353}
2354
2355// Multiply-add.
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);
2359}
2360
2361} // end namespace numext
2362
2363namespace internal {
2364
2365// Two-product residual using FMA, including software FMA when hardware FMA is unavailable.
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));
2369}
2370
2371// p_hi = fl(x * y) and p_lo = fl(x * y - p_hi), for 1/2 <= |y| <= 2 and every x for which p_hi is normal. Unlike
2372// twoprod_low_fma, it takes Dekker's product on targets without an FMA instruction where that is exact for Scalar.
2373// Defined with the other error-free transformations in arch/Default/GenericPacketMathDoubleWord.h.
2374template <typename Scalar>
2375EIGEN_DEVICE_FUNC void twoprod_full_range(const Scalar& x, const Scalar& y, Scalar& p_hi, Scalar& p_lo);
2376
2377template <typename T>
2378EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x) {
2379 EIGEN_IF_CONSTEXPR ((std::is_floating_point<T>::value)) {
2380 // Eager classification lets coefficient-wise loops remain branch-free.
2381 return static_cast<unsigned int>((numext::isfinite)(numext::real(x))) & (numext::isfinite)(numext::imag(x));
2382 } else {
2383 return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
2384 }
2385}
2386
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));
2391 } else {
2392 return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
2393 }
2394}
2395
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;
2402 } else {
2403 return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
2404 }
2405}
2406
2407/****************************************************************************
2408 * Implementation of fuzzy comparisons *
2409 ****************************************************************************/
2410
2411template <typename Scalar, bool IsComplex, bool IsInteger>
2412struct scalar_fuzzy_default_impl {};
2413
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;
2421 }
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;
2424 }
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);
2427 }
2428};
2429
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);
2436 }
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&) {
2439 return x <= y;
2440 }
2441};
2442
2443// Compares moduli, not their squares: abs2 overflows above sqrt(highest) and underflows below sqrt(min), where unequal
2444// values then compared as approximately equal.
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;
2452 }
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;
2455 }
2456};
2457
2458template <typename Scalar>
2459struct scalar_fuzzy_impl
2460 : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
2461
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);
2467}
2468
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);
2474}
2475
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);
2481}
2482
2483/******************************************
2484*** The special case of the bool type ***
2485******************************************/
2486
2487template <>
2488struct scalar_fuzzy_impl<bool> {
2489 using RealScalar = bool;
2490
2491 template <typename OtherScalar>
2492 EIGEN_DEVICE_FUNC static inline bool isMuchSmallerThan(const bool& x, const bool&, const bool&) {
2493 return !x;
2494 }
2495
2496 EIGEN_DEVICE_FUNC static inline bool isApprox(bool x, bool y, bool) { return x == y; }
2497
2498 EIGEN_DEVICE_FUNC static inline bool isApproxOrLessThan(const bool& x, const bool& y, const bool&) {
2499 return (!x) || y;
2500 }
2501};
2502
2503} // end namespace internal
2504
2505// Default implementations that rely on other numext implementations
2506namespace internal {
2507
2508// Specialization for complex types that are not supported by std::expm1.
2509template <typename RealScalar>
2510struct expm1_impl<std::complex<RealScalar>> {
2511 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2512
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();
2516 // expm1(z) = exp(z) - 1
2517 // = exp(x + i * y) - 1
2518 // = exp(x) * (cos(y) + i * sin(y)) - 1
2519 // = exp(x) * cos(y) - 1 + i * exp(x) * sin(y)
2520 // Imag(expm1(z)) = exp(x) * sin(y)
2521 // Real(expm1(z)) = exp(x) * cos(y) - 1
2522 // = exp(x) * cos(y) - 1.
2523 // = expm1(x) + exp(x) * (cos(y) - 1)
2524 // = expm1(x) + exp(x) * (2 * sin(y / 2) ** 2)
2525 RealScalar erm1 = numext::expm1<RealScalar>(xr);
2526 RealScalar er = erm1 + RealScalar(1.);
2527 // C99 Annex G for exp, less one: (+inf, inf or NaN) -> (inf, NaN) and (-inf, inf or NaN) -> (-1, 0).
2528 // xi * 0 is that NaN, raising FE_INVALID for xi = inf as Annex G asks.
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));
2532 }
2533 const RealScalar s = numext::sin(xi);
2534 const RealScalar c = numext::cos(xi);
2535 // 1 - cos(y) = 2 sin(y / 2)^2, taken as sin(y)^2 / (1 + cos(y)) where the difference would cancel.
2536 const RealScalar one_minus_c = c > RealScalar(0) ? s * s / (RealScalar(1) + c) : RealScalar(1) - c;
2537 // exp(x) overflows where exp(x) cos(y) and exp(x) sin(y) need not; multiply exp(x / 2) in twice then, or
2538 // exp(x / 3) three times where that overflows too, and exp(x) sin(y) is finite only for denormal y.
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);
2543 }
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);
2546 }
2547 // For x < 0 both terms of expm1(x) - exp(x) (1 - cos(y)) are <= 0, and the result is -1 exactly once exp(x) is
2548 // negligible. For x >= 0, expm1(x) cos(y) - (1 - cos(y)) has the smaller terms, and no inf - inf at x = inf.
2549 const RealScalar real_part = xr < RealScalar(0) ? erm1 - er * one_minus_c : erm1 * c - one_minus_c;
2550 // exp(x) * sin(0) is NaN for infinite exp(x); keep the exact zero instead.
2551 return std::complex<RealScalar>(real_part, numext::is_exactly_zero(xi) ? xi : er * s);
2552 }
2553};
2554
2555template <typename RealScalar>
2556struct log1p_impl<std::complex<RealScalar>> {
2557 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2558
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();
2562 // log|1 + z| = log1p(x (2 + x) + y^2) / 2. Rounding |1 + z| instead loses the real part to cancellation near
2563 // |1 + z| = 1, and with it the relative accuracy of log1p(z) for small z off the real axis.
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));
2568 }
2569 // The correction in std_fallback::log1p, z log(u) / (u - 1) for u = 1 + z, presumes log(u) ~ u - 1, so small z. For
2570 // |z| >= 1/2 it only adds error, up to ~1e2 ulps in the argument for |x| >= 1e4 in float, where the quotient's
2571 // imaginary part cancels; log(u) is accurate there.
2572 if (!is_small) return numext::log(std::complex<RealScalar>(RealScalar(1) + x, y));
2573 return std_fallback::log1p(z);
2574 }
2575};
2576
2577// 2^(a + ib) = 2^a (cos(t) + i sin(t)) with t = b ln(2). The modulus is the real exp2, so 2^n is exact. Rounding t
2578// would cost |t| eps in the phase, so b ln(2) = t_hi + t_lo is kept in double-word precision, ln(2) = ln2_hi + ln2_lo:
2579// cos(t) = cos(t_hi) + (cos(t_hi) (cos(t_lo) - 1) - sin(t_hi) sin(t_lo)),
2580// sin(t) = sin(t_hi) + (sin(t_hi) (cos(t_lo) - 1) + cos(t_hi) sin(t_lo)).
2581// Special values are those of exp(z) in C99 Annex G, since ln(2) > 0.
2582template <typename RealScalar>
2583struct complex_exp2_impl {
2584 EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
2585
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();
2590 // (a, +-0) -> (2^a, +-0), also for a = +-inf and NaN.
2591 if (numext::is_exactly_zero(b)) return ComplexScalar(numext::exp2(a), b);
2592 // (+inf, inf or NaN) -> (inf, NaN) and (-inf, inf or NaN) -> (0, 0), where 2^a cos(t) would be NaN.
2593 // b * 0 is that NaN, raising FE_INVALID for b = inf as Annex G asks.
2594 if ((numext::isinf)(a) && !(numext::isfinite)(b)) {
2595 return a > RealScalar(0) ? ComplexScalar(a, b * RealScalar(0)) : ComplexScalar(RealScalar(0), RealScalar(0));
2596 }
2597 // ln2_lo = (double(ln(2)) - ln2_hi) + (ln(2) - double(ln(2))), the first difference exact in Wide.
2598 // nvcc 11.8 drops parentheses around a `>` in a template argument when it regenerates host code.
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));
2604 // The phase is computed for |b|: a libm sin need not be exactly odd (IBM double-double's is not), and
2605 // exp2(conj(z)) = conj(exp2(z)) should hold exactly.
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;
2611 // |t_lo| <= ulp(t_hi) / 2, so the Taylor terms below are exact to rounding unless |t| exceeds about 2 / sqrt(eps).
2612 // The phase is within an ulp of b ln(2) while |t| < 1 / eps.
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;
2619 }
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);
2624 // 2^a overflows or is denormal where 2^a cos(t) and 2^a sin(t) need not be; apply 2^e after the products then.
2625 // Past 2 e_max, a part is finite only for denormal t.
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)()) {
2631 // A denormal t has lost bits that 2^a can bring back. There cos(t) = 1 and sin(t) = t, taken from b 2^digits.
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);
2638 }
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);
2642 }
2643};
2644
2645template <typename T>
2646struct rsqrt_impl {
2647// C4804: unsafe use of type 'bool' in operation. Unavoidable when instantiated with T=bool.
2648#if EIGEN_COMP_MSVC
2649#pragma warning(push)
2650#pragma warning(disable : 4804)
2651#endif
2652 EIGEN_DEVICE_FUNC static EIGEN_ALWAYS_INLINE T run(const T& x) { return T(1) / numext::sqrt(x); }
2653#if EIGEN_COMP_MSVC
2654#pragma warning(pop)
2655#endif
2656};
2657
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));
2663 }
2664};
2665#endif
2666
2667// Complex multiply and division operators.
2668// Note that these do not handle the case if inf+NaNi, which is considered an infinity.
2669// This is for consistency with our standard pmul, pdiv implementations.
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);
2678}
2679
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);
2689}
2690
2691// x / y as a scalar division, which the optimizer cannot pack with another one. Clang 14 packs the two quotients of
2692// complex_divide_smith_large into one divps, whose spare lanes divide the denominator by itself (or another value that
2693// is not an operand), so an infinite denominator raises FE_INVALID where both quotients are zeros.
2694template <typename T>
2695EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE T complex_divide_scalar_quotient(T x, T y) {
2696 return x / y;
2697}
2698
2699// Smith's quotient where a numerator or the denominator has overflowed: a (b) is scaled by 1/4 where a component
2700// exceeds highest / 4, and the quotient by the inverse scale. rscale and iscale are Smith's ratios for b.
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,
2704 T iscale) {
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);
2721}
2722
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);
2730 // Smith's complex division (https://arxiv.org/pdf/1210.4539.pdf),
2731 // guards against over/under-flow.
2732 const bool scale_imag = numext::abs(b_imag) <= numext::abs(b_real);
2733 T rscale = T(1);
2734 T iscale = T(1);
2735 if (numext::abs(b_imag) == numext::abs(b_real)) {
2736 // d / c = +-1 for |c| = |d|, which the quotient misses as 0 / 0 or inf / inf when b = 0 or both parts of b are
2737 // infinite; a zero divisor then gives an infinity and an infinite one zero, as in pdiv_complex. The sign is not
2738 // taken from b_real * b_imag, which can overflow or underflow.
2739 iscale = numext::copysign(T(1), b_real) * numext::copysign(T(1), b_imag);
2740 } else if (scale_imag) {
2741 // Each quotient divides by the larger part of b, which is not zero here. The barrier keeps the quotient on its
2742 // branch: an if-converted form evaluates both quotients, and the untaken one divides by zero for a divisor on an
2743 // axis, raising FE_DIVBYZERO. The barrier is applied in place, so that the branch costs no register copy.
2744 volatile_barrier(b_real);
2745 iscale = b_imag / b_real;
2746 } else {
2747 volatile_barrier(b_imag);
2748 rscale = b_real / b_imag;
2749 }
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;
2753 // The numerators are at most 2 max(|a_r|, |a_i|) and the denominator lies in [m, 2 m] for m = max(|b_r|, |b_i|), so
2754 // any of them can overflow where the quotient does not, as in pdiv_complex, which this follows. Each is tested on its
2755 // own, by a test that raises no exception for a NaN: a test of their sum raises FE_OVERFLOW, and FE_INVALID
2756 // for inf - inf, where no term overflows. The tests are folded under -ffinite-math-only, where division then behaves
2757 // as before the guard (no rescaling).
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);
2762 }
2763 return std::complex<T>(numerator_real / denominator, numerator_imag / denominator);
2764}
2765
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) {
2769#if EIGEN_FAST_MATH
2770 return complex_divide_fast(a, b);
2771#else
2772 return complex_divide_smith(a, b);
2773#endif
2774}
2775
2776} // end namespace internal
2777
2778namespace numext {
2779
2788template <typename LhsScalar, typename RhsScalar>
2789EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto divide(const LhsScalar& a, const RhsScalar& b) -> decltype(a / b) {
2790 return a / b;
2791}
2792
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);
2796}
2797
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);
2801}
2802
2803template <typename T>
2804EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> divide(const std::complex<T>& a, const T& b) {
2805 return a / b;
2806}
2807
2808} // end namespace numext
2809
2810} // end namespace Eigen
2811
2812#endif // EIGEN_MATHFUNCTIONS_H