Make bfloat16(float(-nan)) produce -nan, not nan.

This commit is contained in:
Tim Shen 2020-09-14 17:32:56 -07:00
parent 3012e755e9
commit bb56a62582
2 changed files with 13 additions and 2 deletions

View File

@ -256,7 +256,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 operator / (const bfloat16& a, In
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw truncate_to_bfloat16(const float v) {
__bfloat16_raw output;
if (Eigen::numext::isnan EIGEN_NOT_A_MACRO(v)) {
output.value = 0x7FC0;
output.value = std::signbit(v) ? 0xFFC0: 0x7FC0;
return output;
} else if (std::fabs(v) < std::numeric_limits<float>::min EIGEN_NOT_A_MACRO()) {
// Flush denormal to +/- 0.
@ -293,7 +293,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw float_to_bfloat16_rtne<fals
//
// qNaN magic: All exponent bits set + most significant bit of fraction
// set.
output.value = 0x7fc0;
output.value = std::signbit(ff) ? 0xFFC0: 0x7FC0;
} else if (std::fabs(ff) < std::numeric_limits<float>::min EIGEN_NOT_A_MACRO()) {
// Flush denormal to +/- 0.0
output.value = std::signbit(ff) ? 0x8000 : 0;

View File

@ -230,6 +230,17 @@ void test_conversion()
VERIFY((numext::isnan)(bfloat16(__bfloat16_raw(0xffc0))));
VERIFY((numext::isinf)(bfloat16(__bfloat16_raw(0x7f80))));
VERIFY((numext::isnan)(bfloat16(__bfloat16_raw(0x7fc0))));
VERIFY_IS_EQUAL(bfloat16(BinaryToFloat(0x0, 0xff, 0x40, 0x0)).value, 0x7fc0);
VERIFY_IS_EQUAL(bfloat16(BinaryToFloat(0x1, 0xff, 0x40, 0x0)).value, 0xffc0);
VERIFY_IS_EQUAL(Eigen::bfloat16_impl::truncate_to_bfloat16(
BinaryToFloat(0x0, 0xff, 0x40, 0x0))
.value,
0x7fc0);
VERIFY_IS_EQUAL(Eigen::bfloat16_impl::truncate_to_bfloat16(
BinaryToFloat(0x1, 0xff, 0x40, 0x0))
.value,
0xffc0);
}
void test_numtraits()