File indexing completed on 2025-01-18 09:56:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef EIGEN_MATHFUNCTIONSIMPL_H
0012 #define EIGEN_MATHFUNCTIONSIMPL_H
0013
0014 namespace Eigen {
0015
0016 namespace internal {
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 template<typename T>
0029 T generic_fast_tanh_float(const T& a_x)
0030 {
0031
0032 #ifdef EIGEN_VECTORIZE_FMA
0033 const T plus_clamp = pset1<T>(7.99881172180175781f);
0034 const T minus_clamp = pset1<T>(-7.99881172180175781f);
0035 #else
0036 const T plus_clamp = pset1<T>(7.90531110763549805f);
0037 const T minus_clamp = pset1<T>(-7.90531110763549805f);
0038 #endif
0039 const T tiny = pset1<T>(0.0004f);
0040 const T x = pmax(pmin(a_x, plus_clamp), minus_clamp);
0041 const T tiny_mask = pcmp_lt(pabs(a_x), tiny);
0042
0043 const T alpha_1 = pset1<T>(4.89352455891786e-03f);
0044 const T alpha_3 = pset1<T>(6.37261928875436e-04f);
0045 const T alpha_5 = pset1<T>(1.48572235717979e-05f);
0046 const T alpha_7 = pset1<T>(5.12229709037114e-08f);
0047 const T alpha_9 = pset1<T>(-8.60467152213735e-11f);
0048 const T alpha_11 = pset1<T>(2.00018790482477e-13f);
0049 const T alpha_13 = pset1<T>(-2.76076847742355e-16f);
0050
0051
0052 const T beta_0 = pset1<T>(4.89352518554385e-03f);
0053 const T beta_2 = pset1<T>(2.26843463243900e-03f);
0054 const T beta_4 = pset1<T>(1.18534705686654e-04f);
0055 const T beta_6 = pset1<T>(1.19825839466702e-06f);
0056
0057
0058 const T x2 = pmul(x, x);
0059
0060
0061 T p = pmadd(x2, alpha_13, alpha_11);
0062 p = pmadd(x2, p, alpha_9);
0063 p = pmadd(x2, p, alpha_7);
0064 p = pmadd(x2, p, alpha_5);
0065 p = pmadd(x2, p, alpha_3);
0066 p = pmadd(x2, p, alpha_1);
0067 p = pmul(x, p);
0068
0069
0070 T q = pmadd(x2, beta_6, beta_4);
0071 q = pmadd(x2, q, beta_2);
0072 q = pmadd(x2, q, beta_0);
0073
0074
0075 return pselect(tiny_mask, x, pdiv(p, q));
0076 }
0077
0078 template<typename RealScalar>
0079 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0080 RealScalar positive_real_hypot(const RealScalar& x, const RealScalar& y)
0081 {
0082
0083 if ((numext::isinf)(x) || (numext::isinf)(y))
0084 return NumTraits<RealScalar>::infinity();
0085 if ((numext::isnan)(x) || (numext::isnan)(y))
0086 return NumTraits<RealScalar>::quiet_NaN();
0087
0088 EIGEN_USING_STD(sqrt);
0089 RealScalar p, qp;
0090 p = numext::maxi(x,y);
0091 if(p==RealScalar(0)) return RealScalar(0);
0092 qp = numext::mini(y,x) / p;
0093 return p * sqrt(RealScalar(1) + qp*qp);
0094 }
0095
0096 template<typename Scalar>
0097 struct hypot_impl
0098 {
0099 typedef typename NumTraits<Scalar>::Real RealScalar;
0100 static EIGEN_DEVICE_FUNC
0101 inline RealScalar run(const Scalar& x, const Scalar& y)
0102 {
0103 EIGEN_USING_STD(abs);
0104 return positive_real_hypot<RealScalar>(abs(x), abs(y));
0105 }
0106 };
0107
0108
0109
0110 template<typename T>
0111 EIGEN_DEVICE_FUNC std::complex<T> complex_sqrt(const std::complex<T>& z) {
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 const T x = numext::real(z);
0135 const T y = numext::imag(z);
0136 const T zero = T(0);
0137 const T w = numext::sqrt(T(0.5) * (numext::abs(x) + numext::hypot(x, y)));
0138
0139 return
0140 (numext::isinf)(y) ? std::complex<T>(NumTraits<T>::infinity(), y)
0141 : x == zero ? std::complex<T>(w, y < zero ? -w : w)
0142 : x > zero ? std::complex<T>(w, y / (2 * w))
0143 : std::complex<T>(numext::abs(y) / (2 * w), y < zero ? -w : w );
0144 }
0145
0146
0147 template<typename T>
0148 EIGEN_DEVICE_FUNC std::complex<T> complex_rsqrt(const std::complex<T>& z) {
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 const T x = numext::real(z);
0172 const T y = numext::imag(z);
0173 const T zero = T(0);
0174
0175 const T abs_z = numext::hypot(x, y);
0176 const T w = numext::sqrt(T(0.5) * (numext::abs(x) + abs_z));
0177 const T woz = w / abs_z;
0178
0179 return
0180 abs_z == zero ? std::complex<T>(NumTraits<T>::infinity(), NumTraits<T>::quiet_NaN())
0181 : ((numext::isinf)(x) || (numext::isinf)(y)) ? std::complex<T>(zero, zero)
0182 : x == zero ? std::complex<T>(woz, y < zero ? woz : -woz)
0183 : x > zero ? std::complex<T>(woz, -y / (2 * w * abs_z))
0184 : std::complex<T>(numext::abs(y) / (2 * w * abs_z), y < zero ? woz : -woz );
0185 }
0186
0187 template<typename T>
0188 EIGEN_DEVICE_FUNC std::complex<T> complex_log(const std::complex<T>& z) {
0189
0190 T a = numext::abs(z);
0191 EIGEN_USING_STD(atan2);
0192 T b = atan2(z.imag(), z.real());
0193 return std::complex<T>(numext::log(a), b);
0194 }
0195
0196 }
0197
0198 }
0199
0200 #endif