Skip to content

Commit

Permalink
APFloat: Make sure that we get a well-formed x87 NaN when converting …
Browse files Browse the repository at this point in the history
…from a smaller type.

Fixes PR15054.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173459 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
d0k committed Jan 25, 2013
1 parent 5b7c057 commit bd7561e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Support/APFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,12 @@ APFloat::convert(const fltSemantics &toSemantics,
*losesInfo = (fs != opOK);
} else if (category == fcNaN) {
*losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;

// For x87 extended precision, we want to make a NaN, not a special NaN if
// the input wasn't special either.
if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
APInt::tcSetBit(significandParts(), semantics->precision - 1);

// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
// does not give you back the same bits. This is dubious, and we
// don't currently do it. You're really supposed to get
Expand Down
26 changes: 26 additions & 0 deletions unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,32 @@ TEST(APFloatTest, convert) {
test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
EXPECT_EQ(4294967295.0, test.convertToDouble());
EXPECT_FALSE(losesInfo);

test = APFloat::getSNaN(APFloat::IEEEsingle);
APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
&losesInfo);
EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
EXPECT_FALSE(losesInfo);

test = APFloat::getQNaN(APFloat::IEEEsingle);
APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
&losesInfo);
EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
EXPECT_FALSE(losesInfo);

test = APFloat::getSNaN(APFloat::x87DoubleExtended);
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
&losesInfo);
EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
EXPECT_FALSE(losesInfo);

test = APFloat::getQNaN(APFloat::x87DoubleExtended);
test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
&losesInfo);
EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
EXPECT_FALSE(losesInfo);
}

TEST(APFloatTest, PPCDoubleDouble) {
Expand Down

0 comments on commit bd7561e

Please sign in to comment.