Skip to content

Commit 175e1d7

Browse files
committed
Float32/64 safety improvements
1 parent b50a2e6 commit 175e1d7

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

stdlib/public/core/FloatingPointToString.swift

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,9 @@ fileprivate func _Float64ToASCII(
925925
}
926926
// t0 has t0digits digits. Write them out
927927
let text = intToEightDigits(t0) >> ((8 - t0digits) * 8)
928-
unsafe buffer.storeBytes(of: text,
929-
toUncheckedByteOffset: nextDigit,
930-
as: UInt64.self)
928+
buffer.storeBytes(of: text,
929+
toByteOffset: nextDigit,
930+
as: UInt64.self)
931931
nextDigit &+= t0digits
932932
firstDigit &+= 1
933933
} else {
@@ -1004,28 +1004,24 @@ fileprivate func _Float64ToASCII(
10041004
skew = deltaHigh64 / 2 &- tHigh64
10051005
}
10061006

1007+
var lastDigit = unsafe buffer.unsafeLoad(fromUncheckedByteOffset: nextDigit - 1,
1008+
as: UInt8.self)
1009+
10071010
// We use the `skew` to figure out whether there's
10081011
// a better base-10 value than our current one.
10091012
if (skew & adjustFractionMask) == oneHalf {
10101013
// Difference is an integer + exactly 1/2, so ...
10111014
let adjust = skew >> (64 - adjustIntegerBits)
1012-
var t = unsafe buffer.unsafeLoad(fromUncheckedByteOffset: nextDigit - 1,
1013-
as: UInt8.self)
1014-
t &-= UInt8(truncatingIfNeeded: adjust)
1015+
lastDigit &-= UInt8(truncatingIfNeeded: adjust)
10151016
// ... we round the last digit even.
1016-
t &= ~1
1017-
unsafe buffer.storeBytes(of: t,
1018-
toUncheckedByteOffset: nextDigit - 1,
1019-
as: UInt8.self)
1017+
lastDigit &= ~1
10201018
} else {
10211019
let adjust = (skew + oneHalf) >> (64 - adjustIntegerBits)
1022-
var t = unsafe buffer.unsafeLoad(fromUncheckedByteOffset: nextDigit - 1,
1023-
as: UInt8.self)
1024-
t &-= UInt8(truncatingIfNeeded: adjust)
1025-
unsafe buffer.storeBytes(of: t,
1026-
toUncheckedByteOffset: nextDigit - 1,
1027-
as: UInt8.self)
1020+
lastDigit &-= UInt8(truncatingIfNeeded: adjust)
10281021
}
1022+
buffer.storeBytes(of: lastDigit,
1023+
toByteOffset: nextDigit - 1,
1024+
as: UInt8.self)
10291025
}
10301026
}
10311027

@@ -1074,7 +1070,7 @@ fileprivate func finishFormatting(_ buffer: inout MutableRawSpan,
10741070
as: UInt8.self)
10751071
}
10761072
// Append the exponent:
1077-
unsafe buffer.storeBytes(of: 0x65,
1073+
unsafe buffer.storeBytes(of: 0x65, // "e"
10781074
toUncheckedByteOffset: nextDigit,
10791075
as: UInt8.self)
10801076
nextDigit &+= 1
@@ -1107,9 +1103,9 @@ fileprivate func finishFormatting(_ buffer: inout MutableRawSpan,
11071103
e = e % 100
11081104
}
11091105
let d = unsafe asciiDigitTable[unchecked: e]
1110-
unsafe buffer.storeBytes(of: d,
1111-
toUncheckedByteOffset: nextDigit,
1112-
as: UInt16.self)
1106+
buffer.storeBytes(of: d,
1107+
toByteOffset: nextDigit,
1108+
as: UInt16.self)
11131109
nextDigit &+= 2
11141110
} else if base10Exponent < 0 {
11151111
// "-0.000123456789"
@@ -1137,9 +1133,9 @@ fileprivate func finishFormatting(_ buffer: inout MutableRawSpan,
11371133
toUncheckedByteOffset: firstDigit &+ i,
11381134
as: UInt8.self)
11391135
}
1140-
unsafe buffer.storeBytes(of: 0x2e,
1141-
toUncheckedByteOffset: firstDigit &+ base10Exponent &+ 1,
1142-
as: UInt8.self)
1136+
buffer.storeBytes(of: 0x2e,
1137+
toByteOffset: firstDigit &+ base10Exponent &+ 1,
1138+
as: UInt8.self)
11431139
} else {
11441140
// "12345678900.0"
11451141
// Fill trailing zeros, put ".0" at the end
@@ -1161,14 +1157,14 @@ fileprivate func finishFormatting(_ buffer: inout MutableRawSpan,
11611157
i &+= 1
11621158
}
11631159
nextDigit = zeroEnd
1164-
unsafe buffer.storeBytes(of: 0x2e,
1165-
toUncheckedByteOffset: nextDigit &- 2,
1166-
as: UInt8.self)
1160+
buffer.storeBytes(of: 0x2e,
1161+
toByteOffset: nextDigit &- 2,
1162+
as: UInt8.self)
11671163
}
11681164
if sign == .minus {
1169-
unsafe buffer.storeBytes(of: 0x2d,
1170-
toUncheckedByteOffset: firstDigit &- 1,
1171-
as: UInt8.self) // "-"
1165+
buffer.storeBytes(of: 0x2d, // "-"
1166+
toByteOffset: firstDigit &- 1,
1167+
as: UInt8.self)
11721168
firstDigit &-= 1
11731169
}
11741170

0 commit comments

Comments
 (0)