File tree 3 files changed +27
-5
lines changed
test/Transforms/InstSimplify/ConstProp
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,
2242
2242
if (!X86SpecialNan && semantics == &semX87DoubleExtended)
2243
2243
APInt::tcSetBit (significandParts (), semantics->precision - 1 );
2244
2244
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
+
2245
2260
// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2246
2261
// does not give you back the same bits. This is dubious, and we
2247
2262
// 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() {
39
39
ret float %i
40
40
}
41
41
42
- ; https://llvm.org/PR43907
42
+ ; https://llvm.org/PR43907 - make sure that NaN doesn't morph into Inf.
43
+ ; SNaN remains SNaN.
43
44
44
45
define float @nan_f64_trunc () {
45
46
; CHECK-LABEL: @nan_f64_trunc(
46
- ; CHECK-NEXT: ret float 0x7FF0000000000000
47
+ ; CHECK-NEXT: ret float 0x7FF4000000000000
47
48
;
48
49
%f = fptrunc double 0x7FF0000000000001 to float
49
50
ret float %f
50
51
}
51
52
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
+
52
58
define <3 x half > @nan_v3f64_trunc () {
53
59
; 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>
55
61
;
56
62
%f = fptrunc <3 x double > <double 0x7FF0020000000000 , double 0x7FF003FFFFFFFFFF , double 0x7FF8000000000001 > to <3 x half >
57
63
ret <3 x half > %f
Original file line number Diff line number Diff line change @@ -1841,14 +1841,15 @@ TEST(APFloatTest, convert) {
1841
1841
EXPECT_TRUE (test.bitwiseIsEqual (X87QNaN));
1842
1842
EXPECT_FALSE (losesInfo);
1843
1843
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 .
1845
1845
APInt payload (52 , 1 );
1846
1846
test = APFloat::getSNaN (APFloat::IEEEdouble (), false , &payload);
1847
1847
status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
1848
- EXPECT_EQ (0x7f800000 , test.bitcastToAPInt ());
1848
+ EXPECT_EQ (0x7fa00000 , test.bitcastToAPInt ());
1849
1849
EXPECT_TRUE (losesInfo);
1850
1850
EXPECT_EQ (status, APFloat::opOK);
1851
1851
1852
+ // The payload is lost in truncation. QNaN remains QNaN.
1852
1853
test = APFloat::getQNaN (APFloat::IEEEdouble (), false , &payload);
1853
1854
status = test.convert (APFloat::IEEEsingle (), APFloat::rmNearestTiesToEven, &losesInfo);
1854
1855
EXPECT_EQ (0x7fc00000 , test.bitcastToAPInt ());
You can’t perform that action at this time.
0 commit comments