Skip to content

Commit b361adb

Browse files
committed
APFloat: Make sure that we get a well-formed x87 NaN when converting from a smaller type.
Fixes PR15054. llvm-svn: 173459
1 parent 5548879 commit b361adb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

llvm/lib/Support/APFloat.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,12 @@ APFloat::convert(const fltSemantics &toSemantics,
19131913
*losesInfo = (fs != opOK);
19141914
} else if (category == fcNaN) {
19151915
*losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
1916+
1917+
// For x87 extended precision, we want to make a NaN, not a special NaN if
1918+
// the input wasn't special either.
1919+
if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
1920+
APInt::tcSetBit(significandParts(), semantics->precision - 1);
1921+
19161922
// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
19171923
// does not give you back the same bits. This is dubious, and we
19181924
// don't currently do it. You're really supposed to get

llvm/unittests/ADT/APFloatTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,32 @@ TEST(APFloatTest, convert) {
794794
test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
795795
EXPECT_EQ(4294967295.0, test.convertToDouble());
796796
EXPECT_FALSE(losesInfo);
797+
798+
test = APFloat::getSNaN(APFloat::IEEEsingle);
799+
APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
800+
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
801+
&losesInfo);
802+
EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
803+
EXPECT_FALSE(losesInfo);
804+
805+
test = APFloat::getQNaN(APFloat::IEEEsingle);
806+
APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
807+
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
808+
&losesInfo);
809+
EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
810+
EXPECT_FALSE(losesInfo);
811+
812+
test = APFloat::getSNaN(APFloat::x87DoubleExtended);
813+
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
814+
&losesInfo);
815+
EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
816+
EXPECT_FALSE(losesInfo);
817+
818+
test = APFloat::getQNaN(APFloat::x87DoubleExtended);
819+
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
820+
&losesInfo);
821+
EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
822+
EXPECT_FALSE(losesInfo);
797823
}
798824

799825
TEST(APFloatTest, PPCDoubleDouble) {

0 commit comments

Comments
 (0)