From bb69a8db5da21f3c6b289d010eaa64e757387dca Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Thu, 12 Nov 2020 13:12:00 -0800 Subject: [PATCH] Explicit casts of S -> std::complex When calling `internal::cast>(x)`, clang often generates an implicit conversion warning due to an implicit cast from type `S` to `T`. This currently affects the following tests: - `basicstuff` - `bfloat16_float` - `cxx11_tensor_casts` The implicit cast leads to widening/narrowing float conversions. Widening warnings only seem to be generated by clang (`-Wdouble-promotion`). To eliminate the warning, we explicitly cast the real-component first from `S` to `T`. We also adjust tests to use `internal::cast` instead of `static_cast` when a complex type may be involved. --- Eigen/src/Core/MathFunctions.h | 22 +++++++++++++++++--- Eigen/src/Core/arch/Default/Half.h | 14 ------------- test/basicstuff.cpp | 33 ++---------------------------- test/bfloat16_float.cpp | 16 +++++++-------- 4 files changed, 29 insertions(+), 56 deletions(-) diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h index 07f4b9493..e9da35995 100644 --- a/Eigen/src/Core/MathFunctions.h +++ b/Eigen/src/Core/MathFunctions.h @@ -376,7 +376,7 @@ struct hypot_retval * Implementation of cast * ****************************************************************************/ -template +template struct cast_impl { EIGEN_DEVICE_FUNC @@ -386,6 +386,22 @@ struct cast_impl } }; +// Casting from S -> Complex leads to an implicit conversion from S to T, +// generating warnings on clang. Here we explicitly cast the real component. +template +struct cast_impl::IsComplex && NumTraits::IsComplex + >::type> +{ + EIGEN_DEVICE_FUNC + static inline NewType run(const OldType& x) + { + typedef typename NumTraits::Real NewReal; + return static_cast(static_cast(x)); + } +}; + // here, for once, we're plainly returning NewType: we don't want cast to do weird things. template @@ -486,7 +502,7 @@ struct rint_retval #if defined(EIGEN_HIP_DEVICE_COMPILE) // HIP does not seem to have a native device side implementation for the math routine "arg" using std::arg; - #else + #else EIGEN_USING_STD(arg); #endif return arg(x); @@ -967,7 +983,7 @@ template T generic_fast_tanh_float(const T& a_x); namespace numext { -#if (!defined(EIGEN_GPUCC) || defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)) +#if (!defined(EIGEN_GPUCC) || defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC)) template EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y) diff --git a/Eigen/src/Core/arch/Default/Half.h b/Eigen/src/Core/arch/Default/Half.h index 4fdda8af8..bf408149a 100644 --- a/Eigen/src/Core/arch/Default/Half.h +++ b/Eigen/src/Core/arch/Default/Half.h @@ -60,20 +60,6 @@ namespace Eigen { struct half; -// explicit conversion operators are no available before C++11 so we first cast -// half to RealScalar rather than to std::complex directly -#if !EIGEN_HAS_CXX11 -namespace internal { -template -struct cast_impl > { - EIGEN_DEVICE_FUNC static inline std::complex run(const half &x) - { - return static_cast >(static_cast(x)); - } -}; -} // namespace internal -#endif // EIGEN_HAS_CXX11 - namespace half_impl { #if !defined(EIGEN_HAS_GPU_FP16) diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp index f9044a27a..4ca607c82 100644 --- a/test/basicstuff.cpp +++ b/test/basicstuff.cpp @@ -195,11 +195,8 @@ template void basicStuffComplex(const MatrixType& m) VERIFY(!static_cast(cm).imag().isZero()); } -template::value || internal::is_same::value)> struct casting_test; - - template -struct casting_test { +struct casting_test { static void run() { Matrix m; for (int i=0; i { Matrix n = m.template cast(); for (int i=0; i(m(i, j))); - } - } - } -}; - -template -struct casting_test { - static void run() { - casting_test::run(); - } -}; - -template -struct casting_test, true> { - static void run() { - typedef std::complex TgtScalar; - Matrix m; - for (int i=0; i::value(); - } - } - Matrix n = m.template cast(); - for (int i=0; i(static_cast(m(i, j)))); + VERIFY_IS_APPROX(n(i, j), (internal::cast(m(i, j)))); } } } diff --git a/test/bfloat16_float.cpp b/test/bfloat16_float.cpp index 79c868e84..09df2b2f2 100644 --- a/test/bfloat16_float.cpp +++ b/test/bfloat16_float.cpp @@ -44,14 +44,14 @@ void test_truncate(float input, float expected_truncation, float expected_roundi template void test_roundtrip() { // Representable T round trip via bfloat16 - VERIFY_IS_EQUAL(static_cast(static_cast(-std::numeric_limits::infinity())), -std::numeric_limits::infinity()); - VERIFY_IS_EQUAL(static_cast(static_cast(std::numeric_limits::infinity())), std::numeric_limits::infinity()); - VERIFY_IS_EQUAL(static_cast(static_cast(T(-1.0))), T(-1.0)); - VERIFY_IS_EQUAL(static_cast(static_cast(T(-0.5))), T(-0.5)); - VERIFY_IS_EQUAL(static_cast(static_cast(T(-0.0))), T(-0.0)); - VERIFY_IS_EQUAL(static_cast(static_cast(T(1.0))), T(1.0)); - VERIFY_IS_EQUAL(static_cast(static_cast(T(0.5))), T(0.5)); - VERIFY_IS_EQUAL(static_cast(static_cast(T(0.0))), T(0.0)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(-std::numeric_limits::infinity()))), -std::numeric_limits::infinity()); + VERIFY_IS_EQUAL((internal::cast(internal::cast(std::numeric_limits::infinity()))), std::numeric_limits::infinity()); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(-1.0)))), T(-1.0)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(-0.5)))), T(-0.5)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(-0.0)))), T(-0.0)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(1.0)))), T(1.0)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(0.5)))), T(0.5)); + VERIFY_IS_EQUAL((internal::cast(internal::cast(T(0.0)))), T(0.0)); } void test_conversion()