File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
test/Transforms/ConstProp Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -2242,6 +2242,21 @@ IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
22422242 if (!X86SpecialNan && semantics == &semX87DoubleExtended)
22432243 APInt::tcSetBit (significandParts (), semantics->precision - 1 );
22442244
2245+ // If we are truncating NaN, it is possible that we shifted out all of the
2246+ // set bits in a signalling NaN payload. But NaN must remain NaN, so some
2247+ // bit in the significand must be set (otherwise it is Inf).
2248+ // This can only happen with sNaN. Set the 1st bit after the quiet bit,
2249+ // so that we still have an sNaN.
2250+ // FIXME: Set quiet and return opInvalidOp (on convert of any sNaN).
2251+ // But this requires fixing LLVM to parse 32-bit hex FP or ignoring
2252+ // conversions while parsing IR.
2253+ if (APInt::tcIsZero (significandParts (), newPartCount)) {
2254+ assert (shift < 0 && " Should not lose NaN payload on extend" );
2255+ assert (semantics->precision >= 3 && " Unexpectedly narrow significand" );
2256+ assert (*losesInfo && " Missing payload should have set lost info" );
2257+ APInt::tcSetBit (significandParts (), semantics->precision - 3 );
2258+ }
2259+
22452260 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
22462261 // does not give you back the same bits. This is dubious, and we
22472262 // don't currently do it. You're really supposed to get
Original file line number Diff line number Diff line change @@ -38,3 +38,26 @@ define float @overflow_sitofp() {
3838 ret float %i
3939}
4040
41+ ; https://llvm.org/PR43907 - make sure that NaN doesn't morph into Inf.
42+ ; SNaN remains SNaN.
43+
44+ define float @nan_f64_trunc () {
45+ ; CHECK-LABEL: @nan_f64_trunc(
46+ ; CHECK-NEXT: ret float 0x7FF4000000000000
47+ ;
48+ %f = fptrunc double 0x7FF0000000000001 to float
49+ ret float %f
50+ }
51+
52+ ; Verify again with a vector and different destination type.
53+ ; SNaN remains SNaN (first two elements).
54+ ; QNaN remains QNaN (third element).
55+ ; Lower 42 bits of NaN source payload are lost.
56+
57+ define <3 x half > @nan_v3f64_trunc () {
58+ ; CHECK-LABEL: @nan_v3f64_trunc(
59+ ; CHECK-NEXT: ret <3 x half> <half 0xH7D00, half 0xH7D00, half 0xH7E00>
60+ ;
61+ %f = fptrunc <3 x double > <double 0x7FF0020000000000 , double 0x7FF003FFFFFFFFFF , double 0x7FF8000000000001 > to <3 x half >
62+ ret <3 x half > %f
63+ }
Original file line number Diff line number Diff line change @@ -1840,6 +1840,21 @@ TEST(APFloatTest, convert) {
18401840 &losesInfo);
18411841 EXPECT_TRUE (test.bitwiseIsEqual (X87QNaN));
18421842 EXPECT_FALSE (losesInfo);
1843+
1844+ // The payload is lost in truncation, but we must retain NaN, so we set the bit after the quiet bit.
1845+ APInt payload (52 , 1 );
1846+ test = APFloat::getSNaN (APFloat::IEEEdouble (), false , &payload);
1847+ APFloat::opStatus status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
1848+ EXPECT_EQ (0x7fa00000 , test.bitcastToAPInt ());
1849+ EXPECT_TRUE (losesInfo);
1850+ EXPECT_EQ (status, APFloat::opOK);
1851+
1852+ // The payload is lost in truncation. QNaN remains QNaN.
1853+ test = APFloat::getQNaN (APFloat::IEEEdouble (), false , &payload);
1854+ status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
1855+ EXPECT_EQ (0x7fc00000 , test.bitcastToAPInt ());
1856+ EXPECT_TRUE (losesInfo);
1857+ EXPECT_EQ (status, APFloat::opOK);
18431858}
18441859
18451860TEST (APFloatTest, PPCDoubleDouble) {
You can’t perform that action at this time.
0 commit comments