aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Antonio Sanchez <cantonios@google.com>2020-12-11 14:34:31 -0800
committerGravatar Antonio Sanchez <cantonios@google.com>2020-12-11 14:34:31 -0800
commitc6efc4e0ba4b9acb8f99e6e976d4853bedc21db5 (patch)
tree2324384da59139bc1d91f25e3283d6b5417b046e
parente82722a4a7d69c31155c0de9ccee2c955323b620 (diff)
Replace M_LOG2E and M_LN2 with custom macros.
For these to exist we would need to define `_USE_MATH_DEFINES` before `cmath` or `math.h` is first included. However, we don't control the include order for projects outside Eigen, so even defining the macro in `Eigen/Core` does not fix the issue for projects that end up including `<cmath>` before Eigen does (explicitly or transitively). To fix this, we define `EIGEN_LOG2E` and `EIGEN_LN2` ourselves.
-rw-r--r--Eigen/src/Core/GenericPacketMath.h2
-rw-r--r--Eigen/src/Core/MathFunctions.h6
-rw-r--r--Eigen/src/Core/arch/Default/BFloat16.h2
-rw-r--r--Eigen/src/Core/arch/Default/GenericPacketMathFunctions.h8
-rw-r--r--Eigen/src/Core/arch/Default/Half.h2
-rw-r--r--Eigen/src/Core/functors/UnaryFunctors.h2
-rw-r--r--test/packetmath.cpp2
7 files changed, 13 insertions, 11 deletions
diff --git a/Eigen/src/Core/GenericPacketMath.h b/Eigen/src/Core/GenericPacketMath.h
index e2d9af47f..671ed3c89 100644
--- a/Eigen/src/Core/GenericPacketMath.h
+++ b/Eigen/src/Core/GenericPacketMath.h
@@ -668,7 +668,7 @@ Packet plog10(const Packet& a) { EIGEN_USING_STD(log10); return log10(a); }
template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
Packet plog2(const Packet& a) {
typedef typename internal::unpacket_traits<Packet>::type Scalar;
- return pmul(pset1<Packet>(Scalar(M_LOG2E)), plog(a));
+ return pmul(pset1<Packet>(Scalar(EIGEN_LOG2E)), plog(a));
}
/** \internal \returns the square-root of \a a (coeff-wise) */
diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h
index db27670be..3cf91bdb6 100644
--- a/Eigen/src/Core/MathFunctions.h
+++ b/Eigen/src/Core/MathFunctions.h
@@ -10,9 +10,11 @@
#ifndef EIGEN_MATHFUNCTIONS_H
#define EIGEN_MATHFUNCTIONS_H
-// source: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
// TODO this should better be moved to NumTraits
-#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
+// Source: WolframAlpha
+#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
+#define EIGEN_LOG2E 1.442695040888963407359924681001892137426645954152985934135449406931109219L
+#define EIGEN_LN2 0.693147180559945309417232121458176568075500134360255254120680009493393621L
namespace Eigen {
diff --git a/Eigen/src/Core/arch/Default/BFloat16.h b/Eigen/src/Core/arch/Default/BFloat16.h
index 616dcf667..72a489b0b 100644
--- a/Eigen/src/Core/arch/Default/BFloat16.h
+++ b/Eigen/src/Core/arch/Default/BFloat16.h
@@ -513,7 +513,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log10(const bfloat16& a) {
return bfloat16(::log10f(float(a)));
}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log2(const bfloat16& a) {
- return bfloat16(static_cast<float>(M_LOG2E) * ::logf(float(a)));
+ return bfloat16(static_cast<float>(EIGEN_LOG2E) * ::logf(float(a)));
}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 sqrt(const bfloat16& a) {
return bfloat16(::sqrtf(float(a)));
diff --git a/Eigen/src/Core/arch/Default/GenericPacketMathFunctions.h b/Eigen/src/Core/arch/Default/GenericPacketMathFunctions.h
index 45cc780f1..34e2cb1e7 100644
--- a/Eigen/src/Core/arch/Default/GenericPacketMathFunctions.h
+++ b/Eigen/src/Core/arch/Default/GenericPacketMathFunctions.h
@@ -132,10 +132,10 @@ Packet plog_impl_float(const Packet _x)
// Add the logarithm of the exponent back to the result of the interpolation.
if (base2) {
- const Packet cst_log2e = pset1<Packet>(static_cast<float>(M_LOG2E));
+ const Packet cst_log2e = pset1<Packet>(static_cast<float>(EIGEN_LOG2E));
x = pmadd(x, cst_log2e, e);
} else {
- const Packet cst_ln2 = pset1<Packet>(static_cast<float>(M_LN2));
+ const Packet cst_ln2 = pset1<Packet>(static_cast<float>(EIGEN_LN2));
x = pmadd(e, cst_ln2, x);
}
@@ -253,10 +253,10 @@ Packet plog_impl_double(const Packet _x)
// Add the logarithm of the exponent back to the result of the interpolation.
if (base2) {
- const Packet cst_log2e = pset1<Packet>(M_LOG2E);
+ const Packet cst_log2e = pset1<Packet>(EIGEN_LOG2E);
x = pmadd(x, cst_log2e, e);
} else {
- const Packet cst_ln2 = pset1<Packet>(M_LN2);
+ const Packet cst_ln2 = pset1<Packet>(EIGEN_LN2);
x = pmadd(e, cst_ln2, x);
}
diff --git a/Eigen/src/Core/arch/Default/Half.h b/Eigen/src/Core/arch/Default/Half.h
index 204076e25..54fd86aa7 100644
--- a/Eigen/src/Core/arch/Default/Half.h
+++ b/Eigen/src/Core/arch/Default/Half.h
@@ -623,7 +623,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) {
return half(::log10f(float(a)));
}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(const half& a) {
- return half(static_cast<float>(M_LOG2E) * ::logf(float(a)));
+ return half(static_cast<float>(EIGEN_LOG2E) * ::logf(float(a)));
}
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
diff --git a/Eigen/src/Core/functors/UnaryFunctors.h b/Eigen/src/Core/functors/UnaryFunctors.h
index c037576dc..eee6ae194 100644
--- a/Eigen/src/Core/functors/UnaryFunctors.h
+++ b/Eigen/src/Core/functors/UnaryFunctors.h
@@ -403,7 +403,7 @@ struct functor_traits<scalar_log10_op<Scalar> >
*/
template<typename Scalar> struct scalar_log2_op {
EIGEN_EMPTY_STRUCT_CTOR(scalar_log2_op)
- EIGEN_DEVICE_FUNC inline const Scalar operator() (const Scalar& a) const { return Scalar(M_LOG2E) * std::log(a); }
+ EIGEN_DEVICE_FUNC inline const Scalar operator() (const Scalar& a) const { return Scalar(EIGEN_LOG2E) * std::log(a); }
template <typename Packet>
EIGEN_DEVICE_FUNC inline Packet packetOp(const Packet& a) const { return internal::plog2(a); }
};
diff --git a/test/packetmath.cpp b/test/packetmath.cpp
index 0e49d93a9..f19d72502 100644
--- a/test/packetmath.cpp
+++ b/test/packetmath.cpp
@@ -495,7 +495,7 @@ void packetmath() {
// c++11 has std::log2 for real, but not for complex types.
template <typename Scalar>
Scalar log2(Scalar x) {
- return Scalar(M_LOG2E) * std::log(x);
+ return Scalar(EIGEN_LOG2E) * std::log(x);
}
template <typename Scalar, typename Packet>