mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-20 08:39:37 +08:00
Make bfloat16(float(-nan)) produce -nan, not nan.
This commit is contained in:
parent
3012e755e9
commit
bb56a62582
@ -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) {
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw truncate_to_bfloat16(const float v) {
|
||||||
__bfloat16_raw output;
|
__bfloat16_raw output;
|
||||||
if (Eigen::numext::isnan EIGEN_NOT_A_MACRO(v)) {
|
if (Eigen::numext::isnan EIGEN_NOT_A_MACRO(v)) {
|
||||||
output.value = 0x7FC0;
|
output.value = std::signbit(v) ? 0xFFC0: 0x7FC0;
|
||||||
return output;
|
return output;
|
||||||
} else if (std::fabs(v) < std::numeric_limits<float>::min EIGEN_NOT_A_MACRO()) {
|
} else if (std::fabs(v) < std::numeric_limits<float>::min EIGEN_NOT_A_MACRO()) {
|
||||||
// Flush denormal to +/- 0.
|
// 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
|
// qNaN magic: All exponent bits set + most significant bit of fraction
|
||||||
// set.
|
// 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()) {
|
} else if (std::fabs(ff) < std::numeric_limits<float>::min EIGEN_NOT_A_MACRO()) {
|
||||||
// Flush denormal to +/- 0.0
|
// Flush denormal to +/- 0.0
|
||||||
output.value = std::signbit(ff) ? 0x8000 : 0;
|
output.value = std::signbit(ff) ? 0x8000 : 0;
|
||||||
|
@ -230,6 +230,17 @@ void test_conversion()
|
|||||||
VERIFY((numext::isnan)(bfloat16(__bfloat16_raw(0xffc0))));
|
VERIFY((numext::isnan)(bfloat16(__bfloat16_raw(0xffc0))));
|
||||||
VERIFY((numext::isinf)(bfloat16(__bfloat16_raw(0x7f80))));
|
VERIFY((numext::isinf)(bfloat16(__bfloat16_raw(0x7f80))));
|
||||||
VERIFY((numext::isnan)(bfloat16(__bfloat16_raw(0x7fc0))));
|
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()
|
void test_numtraits()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user