diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-02-27 09:27:30 -0800 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-02-27 09:27:30 -0800 |
commit | 05089aba75f8bf1ab3d1bfd566decfc08ce729f9 (patch) | |
tree | ef866b5e9b4707a5e7c8a185c5868f9c795300f1 | |
parent | 573b377110d488886bfc6b319c140a3375d5d91a (diff) |
Switch to truncated casting when converting floating point types to integer. This ensures that vectorized casts are consistent with scalar casts
-rw-r--r-- | Eigen/src/Core/arch/SSE/TypeCasting.h | 2 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_casts.cpp | 20 |
2 files changed, 21 insertions, 1 deletions
diff --git a/Eigen/src/Core/arch/SSE/TypeCasting.h b/Eigen/src/Core/arch/SSE/TypeCasting.h index 454f4d38d..c84893230 100644 --- a/Eigen/src/Core/arch/SSE/TypeCasting.h +++ b/Eigen/src/Core/arch/SSE/TypeCasting.h @@ -24,7 +24,7 @@ struct type_casting_traits<float, int> { }; template<> EIGEN_STRONG_INLINE Packet4i pcast<Packet4f, Packet4i>(const Packet4f& a) { - return _mm_cvtps_epi32(a); + return _mm_cvttps_epi32(a); } diff --git a/unsupported/test/cxx11_tensor_casts.cpp b/unsupported/test/cxx11_tensor_casts.cpp index f53679d7b..729e43327 100644 --- a/unsupported/test/cxx11_tensor_casts.cpp +++ b/unsupported/test/cxx11_tensor_casts.cpp @@ -56,6 +56,25 @@ static void test_vectorized_cast() } +static void test_float_to_int_cast() +{ + Tensor<float, 2> ftensor(20,30); + ftensor = ftensor.random() * 1000.0f; + Tensor<double, 2> dtensor(20,30); + dtensor = dtensor.random() * 1000.0; + + Tensor<int, 2> i1tensor = ftensor.cast<int>(); + Tensor<int, 2> i2tensor = dtensor.cast<int>(); + + for (int i = 0; i < 20; ++i) { + for (int j = 0; j < 30; ++j) { + VERIFY_IS_EQUAL(i1tensor(i,j), static_cast<int>(ftensor(i,j))); + VERIFY_IS_EQUAL(i2tensor(i,j), static_cast<int>(dtensor(i,j))); + } + } +} + + static void test_big_to_small_type_cast() { Tensor<double, 2> dtensor(20, 30); @@ -90,6 +109,7 @@ void test_cxx11_tensor_casts() { CALL_SUBTEST(test_simple_cast()); CALL_SUBTEST(test_vectorized_cast()); + CALL_SUBTEST(test_float_to_int_cast()); CALL_SUBTEST(test_big_to_small_type_cast()); CALL_SUBTEST(test_small_to_big_type_cast()); } |