From 9cb8771e9c4a1f44ba59741c9fac495d1872bb25 Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Thu, 25 Jun 2020 14:31:16 -0700 Subject: Fix tensor casts for large packets and casts to/from std::complex The original tensor casts were only defined for `SrcCoeffRatio`:`TgtCoeffRatio` 1:1, 1:2, 2:1, 4:1. Here we add the missing 1:N and 8:1. We also add casting `Eigen::half` to/from `std::complex`, which was missing to make it consistent with `Eigen:bfloat16`, and generalize the overload to work for any complex type. Tests were added to `basicstuff`, `packetmath`, and `cxx11_tensor_casts` to test all cast configurations. --- test/packetmath.cpp | 185 +++++++++++----------------------------------------- 1 file changed, 38 insertions(+), 147 deletions(-) (limited to 'test/packetmath.cpp') diff --git a/test/packetmath.cpp b/test/packetmath.cpp index dbc1d3f5a..7821877db 100644 --- a/test/packetmath.cpp +++ b/test/packetmath.cpp @@ -8,8 +8,8 @@ // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -#include #include "packetmath_test_shared.h" +#include "random_without_cast_overflow.h" template inline T REF_ADD(const T& a, const T& b) { @@ -126,129 +126,6 @@ struct test_cast_helper -struct random_without_cast_overflow { - static SrcScalar value() { return internal::random(); } -}; - -// Widening integer cast signed to unsigned. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::IsInteger && NumTraits::IsInteger && - !NumTraits::IsSigned && - (std::numeric_limits::digits < std::numeric_limits::digits || - (std::numeric_limits::digits == std::numeric_limits::digits && - NumTraits::IsSigned))>::type> { - static SrcScalar value() { - SrcScalar a = internal::random(); - return a < SrcScalar(0) ? -(a + 1) : a; - } -}; - -// Narrowing integer cast to unsigned. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if< - NumTraits::IsInteger && NumTraits::IsInteger && !NumTraits::IsSigned && - (std::numeric_limits::digits > std::numeric_limits::digits)>::type> { - static SrcScalar value() { - TgtScalar b = internal::random(); - return static_cast(b < TgtScalar(0) ? -(b + 1) : b); - } -}; - -// Narrowing integer cast to signed. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if< - NumTraits::IsInteger && NumTraits::IsInteger && NumTraits::IsSigned && - (std::numeric_limits::digits > std::numeric_limits::digits)>::type> { - static SrcScalar value() { - TgtScalar b = internal::random(); - return static_cast(b); - } -}; - -// Unsigned to signed narrowing cast. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::IsInteger && NumTraits::IsInteger && - !NumTraits::IsSigned && NumTraits::IsSigned && - (std::numeric_limits::digits == - std::numeric_limits::digits)>::type> { - static SrcScalar value() { return internal::random() / 2; } -}; - -template -struct is_floating_point { - enum { value = 0 }; -}; -template <> -struct is_floating_point { - enum { value = 1 }; -}; -template <> -struct is_floating_point { - enum { value = 1 }; -}; -template <> -struct is_floating_point { - enum { value = 1 }; -}; -template <> -struct is_floating_point { - enum { value = 1 }; -}; - -// Floating-point to integer, full precision. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::value && NumTraits::IsInteger && - (std::numeric_limits::digits <= - std::numeric_limits::digits)>::type> { - static SrcScalar value() { return static_cast(internal::random()); } -}; - -// Floating-point to integer, narrowing precision. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::value && NumTraits::IsInteger && - (std::numeric_limits::digits > - std::numeric_limits::digits)>::type> { - static SrcScalar value() { - static const int BitShift = std::numeric_limits::digits - std::numeric_limits::digits; - return static_cast(internal::random() >> BitShift); - } -}; - -// Floating-point target from integer, re-use above logic. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::IsInteger && is_floating_point::value>::type> { - static SrcScalar value() { - return static_cast(random_without_cast_overflow::value()); - } -}; - -// Floating-point narrowing conversion. -template -struct random_without_cast_overflow< - SrcScalar, TgtScalar, - typename internal::enable_if::value && is_floating_point::value && - (std::numeric_limits::digits > - std::numeric_limits::digits)>::type> { - static SrcScalar value() { return static_cast(internal::random()); } -}; - template struct test_cast_helper { static void run() { @@ -266,10 +143,12 @@ struct test_cast_helper::value(); + data1[i] = internal::random_without_cast_overflow::value(); } - for (int i = 0; i < DataSize; ++i) ref[i] = static_cast(data1[i]); + for (int i = 0; i < DataSize; ++i) { + ref[i] = static_cast(data1[i]); + } pcast_array::cast(data1, DataSize, data2); @@ -318,21 +197,37 @@ struct test_cast_runner { static void run() {} }; +template +struct packetmath_pcast_ops_runner { + static void run() { + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner::run(); + test_cast_runner>::run(); + test_cast_runner>::run(); + test_cast_runner::run(); + test_cast_runner::run(); + } +}; + +// Only some types support cast from std::complex<>. template -void packetmath_pcast_ops() { - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); - test_cast_runner::run(); -} +struct packetmath_pcast_ops_runner::IsComplex>::type> { + static void run() { + test_cast_runner>::run(); + test_cast_runner>::run(); + test_cast_runner::run(); + test_cast_runner::run(); + } +}; template void packetmath_boolean_mask_ops() { @@ -356,10 +251,8 @@ void packetmath_boolean_mask_ops() { // Packet16b representing bool does not support ptrue, pandnot or pcmp_eq, since the scalar path // (for some compilers) compute the bitwise and with 0x1 of the results to keep the value in [0,1]. -#ifdef EIGEN_PACKET_MATH_SSE_H -template <> -void packetmath_boolean_mask_ops() {} -#endif +template<> +void packetmath_boolean_mask_ops::type>() {} template void packetmath() { @@ -560,7 +453,7 @@ void packetmath() { CHECK_CWISE2_IF(true, internal::pand, internal::pand); packetmath_boolean_mask_ops(); - packetmath_pcast_ops(); + packetmath_pcast_ops_runner::run(); } template @@ -975,9 +868,7 @@ EIGEN_DECLARE_TEST(packetmath) { CALL_SUBTEST_11(test::runner >::run()); CALL_SUBTEST_12(test::runner >::run()); CALL_SUBTEST_13((packetmath::type>())); -#ifdef EIGEN_PACKET_MATH_SSE_H CALL_SUBTEST_14((packetmath::type>())); -#endif CALL_SUBTEST_15((packetmath::type>())); g_first_pass = false; } -- cgit v1.2.3