File tree Expand file tree Collapse file tree 3 files changed +27
-5
lines changed
test/Transforms/InstSimplify/ConstProp Expand file tree Collapse file tree 3 files changed +27
-5
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 @@ -39,19 +39,25 @@ define float @overflow_sitofp() {
3939 ret float %i
4040}
4141
42- ; https://llvm.org/PR43907
42+ ; https://llvm.org/PR43907 - make sure that NaN doesn't morph into Inf.
43+ ; SNaN remains SNaN.
4344
4445define float @nan_f64_trunc () {
4546; CHECK-LABEL: @nan_f64_trunc(
46- ; CHECK-NEXT: ret float 0x7FF0000000000000
47+ ; CHECK-NEXT: ret float 0x7FF4000000000000
4748;
4849 %f = fptrunc double 0x7FF0000000000001 to float
4950 ret float %f
5051}
5152
53+ ; Verify again with a vector and different destination type.
54+ ; SNaN remains SNaN (first two elements).
55+ ; QNaN remains QNaN (third element).
56+ ; Lower 42 bits of NaN source payload are lost.
57+
5258define <3 x half > @nan_v3f64_trunc () {
5359; CHECK-LABEL: @nan_v3f64_trunc(
54- ; CHECK-NEXT: ret <3 x half> <half 0xH7C00 , half 0xH7C00 , half 0xH7E00>
60+ ; CHECK-NEXT: ret <3 x half> <half 0xH7D00 , half 0xH7D00 , half 0xH7E00>
5561;
5662 %f = fptrunc <3 x double > <double 0x7FF0020000000000 , double 0x7FF003FFFFFFFFFF , double 0x7FF8000000000001 > to <3 x half >
5763 ret <3 x half > %f
Original file line number Diff line number Diff line change @@ -1841,14 +1841,15 @@ TEST(APFloatTest, convert) {
18411841 EXPECT_TRUE (test.bitwiseIsEqual (X87QNaN));
18421842 EXPECT_FALSE (losesInfo);
18431843
1844- // FIXME: This is wrong - NaN becomes Inf .
1844+ // The payload is lost in truncation, but we must retain NaN, so we set the bit after the quiet bit .
18451845 APInt payload (52 , 1 );
18461846 test = APFloat::getSNaN (APFloat::IEEEdouble (), false , &payload);
18471847 status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
1848- EXPECT_EQ (0x7f800000 , test.bitcastToAPInt ());
1848+ EXPECT_EQ (0x7fa00000 , test.bitcastToAPInt ());
18491849 EXPECT_TRUE (losesInfo);
18501850 EXPECT_EQ (status, APFloat::opOK);
18511851
1852+ // The payload is lost in truncation. QNaN remains QNaN.
18521853 test = APFloat::getQNaN (APFloat::IEEEdouble (), false , &payload);
18531854 status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
18541855 EXPECT_EQ (0x7fc00000 , test.bitcastToAPInt ());
You can’t perform that action at this time.
0 commit comments