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.
This commit is contained in:
Antonio Sanchez 2020-12-11 14:34:31 -08:00
parent e82722a4a7
commit c6efc4e0ba
7 changed files with 13 additions and 11 deletions

View File

@ -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) */

View File

@ -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
// Source: WolframAlpha
#define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
#define EIGEN_LOG2E 1.442695040888963407359924681001892137426645954152985934135449406931109219L
#define EIGEN_LN2 0.693147180559945309417232121458176568075500134360255254120680009493393621L
namespace Eigen {

View File

@ -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)));

View File

@ -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);
}

View File

@ -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) {

View File

@ -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); }
};

View File

@ -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>