Skip to content

Commit 666457b

Browse files
authored
Merge pull request swiftlang#23122 from ravikandhadai/rdar48605307
2 parents 74d1322 + 04770f6 commit 666457b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,11 +995,14 @@ bool isLossyUnderflow(int srcExponent, uint64_t srcSignificand,
995995
: srcSignificand;
996996

997997
// Compute the significand bits lost due to subnormal form. Note that the
998-
// integer part: 1 will use up a significand bit in denormal form.
998+
// integer part: 1 will use up a significand bit in subnormal form.
999999
unsigned additionalLoss = destSem.minExponent - srcExponent + 1;
1000+
// Lost bits cannot exceed Double.minExponent - Double.significandWidth = 53.
1001+
// This can happen when truncating from Float80 to Double.
1002+
assert(additionalLoss <= 53);
10001003

10011004
// Check whether a set LSB is lost due to subnormal representation.
1002-
unsigned lostLSBBitMask = (1 << additionalLoss) - 1;
1005+
uint64_t lostLSBBitMask = ((uint64_t)1 << additionalLoss) - 1;
10031006
return (truncSignificand & lostLSBBitMask);
10041007
}
10051008

test/SILOptimizer/diagnostic_constant_propagation_floats.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ func testHexFloatImprecision() {
171171
// Smallest non-zero number representable in Double.
172172
let d2: Double = 0x0.0000000000001p-1022
173173
_blackHole(d2)
174+
let d3: Double = 0x1p-1074
175+
_blackHole(d3)
176+
177+
// Test the case where conversion results in subnormality in the destination.
178+
let d4: Float = 0x1p-149
179+
_blackHole(d4)
180+
let d5: Float = 0x1.8p-149 // expected-warning {{'0x1.8p-149' loses precision during conversion to 'Float}}
181+
_blackHole(d5)
174182

175183
// All warnings are disabled during explict conversions.
176184
_blackHole(Float(0x1.000002p-126))

test/SILOptimizer/diagnostic_constant_propagation_floats_x86.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ func testFloatConvertUnderflow() {
6767
let d4: Double = 5E-324 // expected-warning {{'5E-324' underflows and loses precision during conversion to 'Double'}}
6868
_blackHole(d4)
6969

70+
let e4: Float80 = 0x1p-16445
71+
_blackHole(e4)
72+
7073
// FIXME: if a number is so tiny that it underflows even Float80,
7174
// nothing is reported
7275
let e1: Float80 = 0x1p-16446
7376
_blackHole(e1)
7477

78+
// Test the case where conversion results in subnormality in the destination.
79+
let e2: Double = 0x1p-1074
80+
_blackHole(e2)
81+
let e3: Double = 0x11p-1074 // expected-warning {{'0x11p-1074' underflows and loses precision during conversion to 'Double'}}
82+
_blackHole(e3)
83+
7584
// All warnings are disabled during explict conversions
7685
_blackHole(Float(1E-400))
7786
_blackHole(Double(1E-309))

0 commit comments

Comments
 (0)