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) 2007 Julien Pommier
5// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
11/* The sin and cos and functions of this file come from
12 * Julien Pommier's sse math library: http://gruntthepeon.free.fr/ssemath/
13 */
14
15#ifndef EIGEN_MATH_FUNCTIONS_SSE_H
16#define EIGEN_MATH_FUNCTIONS_SSE_H
17
18namespace Eigen {
19
20namespace internal {
21
24 return plog_float(_x);
25}
26
29 return plog_double(_x);
30}
31
34 return plog2_float(_x);
35}
36
39 return plog2_double(_x);
40}
41
44 return generic_plog1p(_x);
45}
46
49 return generic_expm1(_x);
50}
51
54{
55 return pexp_float(_x);
56}
57
60{
61 return pexp_double(x);
62}
63
66{
67 return psin_float(_x);
68}
69
72{
73 return pcos_float(_x);
74}
75
76#if EIGEN_FAST_MATH
77
78// Functions for sqrt.
79// The EIGEN_FAST_MATH version uses the _mm_rsqrt_ps approximation and one step
80// of Newton's method, at a cost of 1-2 bits of precision as opposed to the
81// exact solution. It does not handle +inf, or denormalized numbers correctly.
82// The main advantage of this approach is not just speed, but also the fact that
83// it can be inlined and pipelined with other computations, further reducing its
84// effective latency. This is similar to Quake3's fast inverse square root.
85// For detail see here: http://www.beyond3d.com/content/articles/8/
88{
89 Packet4f minus_half_x = pmul(_x, pset1<Packet4f>(-0.5f));
90 Packet4f denormal_mask = pandnot(
91 pcmp_lt(_x, pset1<Packet4f>((std::numeric_limits<float>::min)())),
92 pcmp_lt(_x, pzero(_x)));
93
94 // Compute approximate reciprocal sqrt.
95 Packet4f x = _mm_rsqrt_ps(_x);
96 // Do a single step of Newton's iteration.
97 x = pmul(x, pmadd(minus_half_x, pmul(x,x), pset1<Packet4f>(1.5f)));
98 // Flush results for denormals to zero.
99 return pandnot(pmul(_x,x), denormal_mask);
100}
101
102#else
103
106
107#endif
108
111
114
115#if EIGEN_FAST_MATH
116
119 _EIGEN_DECLARE_CONST_Packet4f(one_point_five, 1.5f);
120 _EIGEN_DECLARE_CONST_Packet4f(minus_half, -0.5f);
122 _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(flt_min, 0x00800000u);
123
124 Packet4f neg_half = pmul(_x, p4f_minus_half);
125
126 // Identity infinite, zero, negative and denormal arguments.
127 Packet4f lt_min_mask = _mm_cmplt_ps(_x, p4f_flt_min);
128 Packet4f inf_mask = _mm_cmpeq_ps(_x, p4f_inf);
129 Packet4f not_normal_finite_mask = _mm_or_ps(lt_min_mask, inf_mask);
130
131 // Compute an approximate result using the rsqrt intrinsic.
132 Packet4f y_approx = _mm_rsqrt_ps(_x);
133
134 // Do a single step of Newton-Raphson iteration to improve the approximation.
135 // This uses the formula y_{n+1} = y_n * (1.5 - y_n * (0.5 * x) * y_n).
136 // It is essential to evaluate the inner term like this because forming
137 // y_n^2 may over- or underflow.
138 Packet4f y_newton = pmul(
139 y_approx, pmadd(y_approx, pmul(neg_half, y_approx), p4f_one_point_five));
140
141 // Select the result of the Newton-Raphson step for positive normal arguments.
142 // For other arguments, choose the output of the intrinsic. This will
143 // return rsqrt(+inf) = 0, rsqrt(x) = NaN if x < 0, and rsqrt(x) = +inf if
144 // x is zero or a positive denormalized float (equivalent to flushing positive
145 // denormalized inputs to zero).
146 return pselect<Packet4f>(not_normal_finite_mask, y_approx, y_newton);
147}
148
149#else
150
153 // Unfortunately we can't use the much faster mm_rsqrt_ps since it only provides an approximation.
154 return _mm_div_ps(pset1<Packet4f>(1.0f), _mm_sqrt_ps(x));
155}
156
157#endif
158
163
164// Hyperbolic Tangent function.
165template <>
170
171} // end namespace internal
172
173namespace numext {
174
175template<>
177float sqrt(const float &x)
178{
179 return internal::pfirst(internal::Packet4f(_mm_sqrt_ss(_mm_set_ss(x))));
180}
181
182template<>
184double sqrt(const double &x)
185{
186#if EIGEN_COMP_GNUC_STRICT
187 // This works around a GCC bug generating poor code for _mm_sqrt_pd
188 // See https://gitlab.com/libeigen/eigen/commit/8dca9f97e38970
189 return internal::pfirst(internal::Packet2d(__builtin_ia32_sqrtsd(_mm_set_sd(x))));
190#else
191 return internal::pfirst(internal::Packet2d(_mm_sqrt_pd(_mm_set_sd(x))));
192#endif
193}
194
195} // end namespace numex
196
197} // end namespace Eigen
198
199#endif // EIGEN_MATH_FUNCTIONS_SSE_H
#define _EIGEN_DECLARE_CONST_Packet4f(NAME, X)
Definition PacketMath.h:57
#define _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME, X)
Definition PacketMath.h:69
#define EIGEN_ALWAYS_INLINE
Definition Macros.h:932
#define EIGEN_UNUSED
Definition Macros.h:1067
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
Definition Macros.h:985
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_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 Packet4f pexpm1< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:48
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet pexp_double(const Packet _x)
Definition GenericPacketMathFunctions.h:490
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 Packet pcos_float(const Packet &x)
Definition GenericPacketMathFunctions.h:755
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Definition PacketMath.h:827
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f ptanh< Packet4f >(const Packet4f &x)
Definition MathFunctions.h:82
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 Packet2d plog2< Packet2d >(const Packet2d &_x)
Definition MathFunctions.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 Packet plog_float(const Packet _x)
Definition GenericPacketMathFunctions.h:254
Packet generic_expm1(const Packet &x)
Definition GenericPacketMathFunctions.h:408
EIGEN_STRONG_INLINE Packet4f pset1< Packet4f >(const float &from)
Definition PacketMath.h:547
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f prsqrt< Packet4f >(const Packet4f &x)
Definition MathFunctions.h:45
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet plog_double(const Packet _x)
Definition GenericPacketMathFunctions.h:375
EIGEN_STRONG_INLINE bfloat16 pfirst(const Packet8bf &a)
Definition PacketMath.h:1429
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet2d plog< Packet2d >(const Packet2d &_x)
Definition MathFunctions.h:28
T generic_fast_tanh_float(const T &a_x)
Definition MathFunctionsImpl.h:29
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f plog2< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:33
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f pcos< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:38
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f plog< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:20
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet pexp_float(const Packet _x)
Definition GenericPacketMathFunctions.h:439
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f psqrt< Packet4f >(const Packet4f &x)
Definition MathFunctions.h:105
__vector float Packet4f
Definition PacketMath.h:30
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f pexp< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:26
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet2d pexp< Packet2d >(const Packet2d &_x)
Definition MathFunctions.h:326
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet2d psqrt< Packet2d >(const Packet2d &x)
Definition MathFunctions.h:110
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet2d prsqrt< Packet2d >(const Packet2d &x)
Definition MathFunctions.h:160
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f psin< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:32
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet4f plog1p< Packet4f >(const Packet4f &_x)
Definition MathFunctions.h:43
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet16b psqrt< Packet16b >(const Packet16b &x)
Definition MathFunctions.h:113
EIGEN_DEVICE_FUNC const Scalar & x
Definition SpecialFunctionsImpl.h:1990
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition BandTriangularSolver.h:13
Definition ForwardDeclarations.h:17