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
22 changes: 11 additions & 11 deletions stdlib/public/core/FloatingPointFromString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,18 +450,18 @@ fileprivate func hexFloat(
) -> ParseResult {
var i = start + 2 // Skip leading '0x'
let firstDigitOffset = i

let limit = UInt64(1) << 60
var significand: UInt64 = 0

//
// Digits before the binary point
//

// Accumulate the most significant 64 bits...
while i < input.count && hexdigit(input[i]) < 16 && significand < (1 << 60) {
significand &<<= 4
significand += UInt64(hexdigit(input[i]))
i += 1
while i < input.count && hexdigit(input[i]) < 16 && significand < limit {
significand &<<= 4
significand += UInt64(hexdigit(input[i]))
i += 1
}

// Initialize binary exponent to the number of bits we collected above
Expand Down Expand Up @@ -521,7 +521,7 @@ fileprivate func hexFloat(
}
}
// Pack more bits into the accumulator (up to 60)
while i < input.count && hexdigit(input[i]) < 16 && significand < (1 << 60) {
while i < input.count && hexdigit(input[i]) < 16 && significand < limit {
significand &<<= 4
significand += UInt64(hexdigit(input[i]))
i += 1
Expand Down Expand Up @@ -631,12 +631,12 @@ fileprivate func hexFloat(
&& (targetSignificand & 1) == 1)) {
// Round up, test for overflow
targetSignificand += 1
if targetSignificand >= (1 << targetFormat.significandBits) {
if targetSignificand >= (UInt64(1) << targetFormat.significandBits) {
// Normal overflowed, need to renormalize
targetSignificand >>= 1
binaryExponent += 1
} else if (binaryExponent < targetFormat.minBinaryExponent
&& targetSignificand >= (1 << (targetFormat.significandBits - 1))) {
&& targetSignificand >= (UInt64(1) << (targetFormat.significandBits - 1))) {
// Subnormal overflowed to normal
binaryExponent += 1
}
Expand Down Expand Up @@ -1769,7 +1769,7 @@ fileprivate func slowDecimalToBinary(
count: targetFormat.significandBits,
remainderNonZero: false)

if significand >= (1 &<< targetFormat.significandBits) {
if significand >= (UInt64(1) &<< targetFormat.significandBits) {
significand >>= 1
binaryExponent &+= 1
}
Expand Down Expand Up @@ -1865,7 +1865,7 @@ fileprivate func slowDecimalToBinary(
range: quotientRange,
count: targetFormat.significandBits,
remainderNonZero: remainderNonZero)
if significand >= (1 &<< targetFormat.significandBits) {
if significand >= (UInt64(1) &<< targetFormat.significandBits) {
significand >>= 1
binaryExponent &+= 1
}
Expand All @@ -1889,7 +1889,7 @@ fileprivate func slowDecimalToBinary(
// exponent. Then we've transitioned from a subnormal to
// a normal, so the extra overflow bit will naturally get
// dropped, we just have to bump the exponent.
if significand >= (1 &<< (targetFormat.significandBits &- 1)) {
if significand >= (UInt64(1) &<< (targetFormat.significandBits &- 1)) {
binaryExponent &+= 1
}
targetSignificand = significand
Expand Down
5 changes: 2 additions & 3 deletions test/stdlib/ParseFloat16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
// Float16 is only available in watchOS 7.0 or newer
// UNSUPPORTED: OS=watchos

// TODO: Figure out why this test breaks on wasm32
// UNSUPPORTED: CPU=wasm32

// Cannot test with old OS stdlib, because that used libc strtof
// for parsing, which results in incorrect results.
// UNSUPPORTED: use_os_stdlib
Expand Down Expand Up @@ -105,7 +102,9 @@ tests.test("NaNs") {
expectRoundTrip(Float16.nan)
expectRoundTrip(-Float16.nan)
expectRoundTrip(Float16(nan:73, signaling:false))
#if !arch(wasm32)
Copy link
Contributor Author

@tbkka tbkka Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WASM32 doesn't seem to like signaling NaNs for Float16.

expectRoundTrip(Float16(nan:73, signaling:true))
#endif
expectParse("nan", Float16.nan)
expectParse("NAN", Float16.nan)
expectParse("NaN", Float16.nan)
Expand Down
3 changes: 0 additions & 3 deletions test/stdlib/ParseFloat64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

// REQUIRES: executable_test

// TODO: Figure out why this test breaks on wasm32
// UNSUPPORTED: CPU=wasm32

// Needed to declare the ABI entry point
// REQUIRES: swift_feature_Extern

Expand Down