NDDEM
MathFunctions.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com)
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_MATH_FUNCTIONS_AVX_H
11 #define EIGEN_MATH_FUNCTIONS_AVX_H
12 
13 /* The sin and cos functions of this file are loosely derived from
14  * Julien Pommier's sse math library: http://gruntthepeon.free.fr/ssemath/
15  */
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template <>
23 psin<Packet8f>(const Packet8f& _x) {
24  return psin_float(_x);
25 }
26 
27 template <>
29 pcos<Packet8f>(const Packet8f& _x) {
30  return pcos_float(_x);
31 }
32 
33 template <>
35 plog<Packet8f>(const Packet8f& _x) {
36  return plog_float(_x);
37 }
38 
39 template <>
41 plog<Packet4d>(const Packet4d& _x) {
42  return plog_double(_x);
43 }
44 
45 template <>
47 plog2<Packet8f>(const Packet8f& _x) {
48  return plog2_float(_x);
49 }
50 
51 template <>
53 plog2<Packet4d>(const Packet4d& _x) {
54  return plog2_double(_x);
55 }
56 
59  return generic_plog1p(_x);
60 }
61 
64  return generic_expm1(_x);
65 }
66 
67 // Exponential function. Works by writing "x = m*log(2) + r" where
68 // "m = floor(x/log(2)+1/2)" and "r" is the remainder. The result is then
69 // "exp(x) = 2^m*exp(r)" where exp(r) is in the range [-1,1).
70 template <>
72 pexp<Packet8f>(const Packet8f& _x) {
73  return pexp_float(_x);
74 }
75 
76 // Hyperbolic Tangent function.
77 template <>
79 ptanh<Packet8f>(const Packet8f& _x) {
81 }
82 
83 // Exponential function for doubles.
84 template <>
86 pexp<Packet4d>(const Packet4d& _x) {
87  return pexp_double(_x);
88 }
89 
90 // Functions for sqrt.
91 // The EIGEN_FAST_MATH version uses the _mm_rsqrt_ps approximation and one step
92 // of Newton's method, at a cost of 1-2 bits of precision as opposed to the
93 // exact solution. It does not handle +inf, or denormalized numbers correctly.
94 // The main advantage of this approach is not just speed, but also the fact that
95 // it can be inlined and pipelined with other computations, further reducing its
96 // effective latency. This is similar to Quake3's fast inverse square root.
97 // For detail see here: http://www.beyond3d.com/content/articles/8/
98 #if EIGEN_FAST_MATH
99 template <>
101 Packet8f psqrt<Packet8f>(const Packet8f& _x) {
102  Packet8f minus_half_x = pmul(_x, pset1<Packet8f>(-0.5f));
103  Packet8f denormal_mask = pandnot(
105  pcmp_lt(_x, pzero(_x)));
106 
107  // Compute approximate reciprocal sqrt.
108  Packet8f x = _mm256_rsqrt_ps(_x);
109  // Do a single step of Newton's iteration.
110  x = pmul(x, pmadd(minus_half_x, pmul(x,x), pset1<Packet8f>(1.5f)));
111  // Flush results for denormals to zero.
112  return pandnot(pmul(_x,x), denormal_mask);
113 }
114 
115 #else
116 
119  return _mm256_sqrt_ps(_x);
120 }
121 
122 #endif
123 
126  return _mm256_sqrt_pd(_x);
127 }
128 
129 #if EIGEN_FAST_MATH
133  _EIGEN_DECLARE_CONST_Packet8f(one_point_five, 1.5f);
134  _EIGEN_DECLARE_CONST_Packet8f(minus_half, -0.5f);
135  _EIGEN_DECLARE_CONST_Packet8f_FROM_INT(flt_min, 0x00800000);
136 
137  Packet8f neg_half = pmul(_x, p8f_minus_half);
138 
139  // select only the inverse sqrt of positive normal inputs (denormals are
140  // flushed to zero and cause infs as well).
141  Packet8f lt_min_mask = _mm256_cmp_ps(_x, p8f_flt_min, _CMP_LT_OQ);
142  Packet8f inf_mask = _mm256_cmp_ps(_x, p8f_inf, _CMP_EQ_OQ);
143  Packet8f not_normal_finite_mask = _mm256_or_ps(lt_min_mask, inf_mask);
144 
145  // Compute an approximate result using the rsqrt intrinsic.
146  Packet8f y_approx = _mm256_rsqrt_ps(_x);
147 
148  // Do a single step of Newton-Raphson iteration to improve the approximation.
149  // This uses the formula y_{n+1} = y_n * (1.5 - y_n * (0.5 * x) * y_n).
150  // It is essential to evaluate the inner term like this because forming
151  // y_n^2 may over- or underflow.
152  Packet8f y_newton = pmul(y_approx, pmadd(y_approx, pmul(neg_half, y_approx), p8f_one_point_five));
153 
154  // Select the result of the Newton-Raphson step for positive normal arguments.
155  // For other arguments, choose the output of the intrinsic. This will
156  // return rsqrt(+inf) = 0, rsqrt(x) = NaN if x < 0, and rsqrt(x) = +inf if
157  // x is zero or a positive denormalized float (equivalent to flushing positive
158  // denormalized inputs to zero).
159  return pselect<Packet8f>(not_normal_finite_mask, y_approx, y_newton);
160 }
161 
162 #else
166  return _mm256_div_ps(p8f_one, _mm256_sqrt_ps(_x));
167 }
168 #endif
169 
173  return _mm256_div_pd(p4d_one, _mm256_sqrt_pd(_x));
174 }
175 
186 
187 template <>
189  Packet8f fexponent;
190  const Packet8h out = float2half(pfrexp<Packet8f>(half2float(a), fexponent));
191  exponent = float2half(fexponent);
192  return out;
193 }
194 
195 template <>
197  return float2half(pldexp<Packet8f>(half2float(a), half2float(exponent)));
198 }
199 
210 
211 template <>
213  Packet8f fexponent;
214  const Packet8bf out = F32ToBf16(pfrexp<Packet8f>(Bf16ToF32(a), fexponent));
215  exponent = F32ToBf16(fexponent);
216  return out;
217 }
218 
219 template <>
221  return F32ToBf16(pldexp<Packet8f>(Bf16ToF32(a), Bf16ToF32(exponent)));
222 }
223 
224 } // end namespace internal
225 
226 } // end namespace Eigen
227 
228 #endif // EIGEN_MATH_FUNCTIONS_AVX_H
#define _EIGEN_DECLARE_CONST_Packet8f(NAME, X)
Definition: PacketMath.h:43
#define _EIGEN_DECLARE_CONST_Packet8f_FROM_INT(NAME, X)
Definition: PacketMath.h:49
#define _EIGEN_DECLARE_CONST_Packet4d(NAME, X)
Definition: PacketMath.h:46
#define BF16_PACKET_FUNCTION(PACKET_F, PACKET_BF16, METHOD)
Definition: BFloat16.h:19
#define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD)
Definition: Half.h:53
#define EIGEN_UNUSED
Definition: Macros.h:1067
#define EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
Definition: Macros.h:985
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:571
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet pexpm1(const Packet &a)
Definition: GenericPacketMath.h:792
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f plog1p< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:58
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4d plog< Packet4d >(const Packet4d &_x)
Definition: MathFunctions.h:41
EIGEN_STRONG_INLINE Packet8h float2half(const Packet8f &a)
Definition: PacketMath.h:1007
EIGEN_STRONG_INLINE Packet8f Bf16ToF32(const Packet8bf &a)
Definition: PacketMath.h:1260
EIGEN_STRONG_INLINE Packet8f pzero(const Packet8f &)
Definition: PacketMath.h:247
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet plog2_float(const Packet _x)
Definition: GenericPacketMathFunctions.h:262
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f plog< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:35
EIGEN_STRONG_INLINE Packet8f pselect< Packet8f >(const Packet8f &mask, const Packet8f &a, const Packet8f &b)
Definition: PacketMath.h:542
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4d prsqrt< Packet4d >(const Packet4d &_x)
Definition: MathFunctions.h:171
EIGEN_STRONG_INLINE Packet8f pfrexp< Packet8f >(const Packet8f &a, Packet8f &exponent)
Definition: PacketMath.h:736
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet plog2(const Packet &a)
Definition: GenericPacketMath.h:808
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet plog(const Packet &a)
Definition: GenericPacketMath.h:796
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet pexp_double(const Packet _x)
Definition: GenericPacketMathFunctions.h:490
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet pexp(const Packet &a)
Definition: GenericPacketMath.h:788
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f pcos< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:29
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet pcos(const Packet &a)
Definition: GenericPacketMath.h:756
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet psin_float(const Packet &x)
Definition: GenericPacketMathFunctions.h:747
Packet generic_plog1p(const Packet &x)
Definition: GenericPacketMathFunctions.h:392
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f pexpm1< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:63
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet psin(const Packet &a)
Definition: GenericPacketMath.h:752
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet pcos_float(const Packet &x)
Definition: GenericPacketMathFunctions.h:755
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f prsqrt< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:164
EIGEN_STRONG_INLINE Packet8f half2float(const Packet8h &a)
Definition: PacketMath.h:988
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Definition: PacketMath.h:827
EIGEN_DEVICE_FUNC Packet pmul(const Packet &a, const Packet &b)
Definition: GenericPacketMath.h:237
EIGEN_STRONG_INLINE Packet8h pandnot(const Packet8h &a, const Packet8h &b)
Definition: PacketMath.h:1053
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f psqrt< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:118
eigen_packet_wrapper< __vector unsigned short int, 0 > Packet8bf
Definition: PacketMath.h:38
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet plog2_double(const Packet _x)
Definition: GenericPacketMathFunctions.h:383
EIGEN_STRONG_INLINE Packet4f pcmp_lt(const Packet4f &a, const Packet4f &b)
Definition: PacketMath.h:868
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f ptanh< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:79
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet ptanh(const Packet &a)
Definition: GenericPacketMath.h:784
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet plog_float(const Packet _x)
Definition: GenericPacketMathFunctions.h:254
Packet generic_expm1(const Packet &x)
Definition: GenericPacketMathFunctions.h:408
eigen_packet_wrapper< __m128i, 2 > Packet8h
Definition: PacketMath.h:34
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet plog1p(const Packet &a)
Definition: GenericPacketMath.h:800
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4d pexp< Packet4d >(const Packet4d &_x)
Definition: MathFunctions.h:86
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet plog_double(const Packet _x)
Definition: GenericPacketMathFunctions.h:375
EIGEN_STRONG_INLINE Packet4f psqrt(const Packet4f &a)
Definition: PacketMath.h:723
T generic_fast_tanh_float(const T &a_x)
Definition: MathFunctionsImpl.h:29
EIGEN_STRONG_INLINE Packet8h pldexp(const Packet8h &a, const Packet8h &exponent)
Definition: MathFunctions.h:196
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4d psqrt< Packet4d >(const Packet4d &_x)
Definition: MathFunctions.h:125
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f plog2< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:47
EIGEN_STRONG_INLINE Packet8f pldexp< Packet8f >(const Packet8f &a, const Packet8f &exponent)
Definition: PacketMath.h:768
EIGEN_STRONG_INLINE Packet8h pfrexp(const Packet8h &a, Packet8h &exponent)
Definition: MathFunctions.h:188
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet pexp_float(const Packet _x)
Definition: GenericPacketMathFunctions.h:439
__m256 Packet8f
Definition: PacketMath.h:31
EIGEN_STRONG_INLINE Packet4f prsqrt(const Packet4f &a)
Definition: PacketMath.h:730
EIGEN_STRONG_INLINE Packet8bf F32ToBf16(Packet4f p4f)
Definition: PacketMath.h:1252
EIGEN_STRONG_INLINE Packet8f pset1< Packet8f >(const float &from)
Definition: PacketMath.h:240
__m256d Packet4d
Definition: PacketMath.h:33
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f pexp< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:72
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8f psin< Packet8f >(const Packet8f &_x)
Definition: MathFunctions.h:23
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4d plog2< Packet4d >(const Packet4d &_x)
Definition: MathFunctions.h:53
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Definition: document.h:416
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
Definition: GenericPacketMath.h:160