TR-mbed 1.0
Loading...
Searching...
No Matches
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
17namespace Eigen {
18
19namespace internal {
20
21template <>
26
27template <>
32
33template <>
38
39template <>
44
45template <>
50
51template <>
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).
70template <>
75
76// Hyperbolic Tangent function.
77template <>
82
83// Exponential function for doubles.
84template <>
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
99template <>
102 Packet8f minus_half_x = pmul(_x, pset1<Packet8f>(-0.5f));
103 Packet8f denormal_mask = pandnot(
104 pcmp_lt(_x, pset1<Packet8f>((std::numeric_limits<float>::min)())),
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
168#endif
169
175
186
187template <>
194
195template <>
199
210
211template <>
218
219template <>
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
ArrayXXi a
Definition Array_initializer_list_23_cxx11.cpp:1
#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
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition gnuplot_common_settings.hh:12
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_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 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
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 bench_norm.cpp:85
Definition BandTriangularSolver.h:13
Definition GenericPacketMath.h:160
Definition ForwardDeclarations.h:17
std::ofstream out("Result.txt")