Switch to truncated casting when converting floating point types to integer. This ensures that vectorized casts are consistent with scalar casts

This commit is contained in:
Benoit Steiner 2015-02-27 09:27:30 -08:00
parent 573b377110
commit 05089aba75
2 changed files with 21 additions and 1 deletions

View File

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

View File

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