Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,14 @@ bool isLossyUnderflow(int srcExponent, uint64_t srcSignificand,
: srcSignificand;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func testHexFloatImprecision() {
// Smallest non-zero number representable in Double.
let d2: Double = 0x0.0000000000001p-1022
_blackHole(d2)
let d3: Double = 0x1p-1074
_blackHole(d3)

// Test the case where conversion results in subnormality in the destination.
let d4: Float = 0x1p-149
_blackHole(d4)
let d5: Float = 0x1.8p-149 // expected-warning {{'0x1.8p-149' loses precision during conversion to 'Float}}
_blackHole(d5)

// All warnings are disabled during explict conversions.
_blackHole(Float(0x1.000002p-126))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,20 @@ func testFloatConvertUnderflow() {
let d4: Double = 5E-324 // expected-warning {{'5E-324' underflows and loses precision during conversion to 'Double'}}
_blackHole(d4)

let e4: Float80 = 0x1p-16445
_blackHole(e4)

// FIXME: if a number is so tiny that it underflows even Float80,
// nothing is reported
let e1: Float80 = 0x1p-16446
_blackHole(e1)

// Test the case where conversion results in subnormality in the destination.
let e2: Double = 0x1p-1074
_blackHole(e2)
let e3: Double = 0x11p-1074 // expected-warning {{'0x11p-1074' underflows and loses precision during conversion to 'Double'}}
_blackHole(e3)

// All warnings are disabled during explict conversions
_blackHole(Float(1E-400))
_blackHole(Double(1E-309))
Expand Down