112112/// * The Swift version is slightly faster than the C version;
113113/// mostly thanks to various minor algorithmic tweaks that were
114114/// found during the translation process.
115+ /// * Most of this file is annotated for SwiftStdlib 6.2
116+ /// because it relies on UInt128, MutableSpan, and InlineArray.
115117///
116118// ----------------------------------------------------------------------------
117119
128130#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
129131
130132// Support Legacy ABI on top of new implementation
131- @_silgen_name ( " swift_float16ToString2 " )
132- internal func _float16ToStringImpl2(
133+ @available ( SwiftStdlib 6 . 2 , * )
134+ @_silgen_name ( " swift_float16ToString " )
135+ public func _float16ToStringImpl(
133136 _ textBuffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
134137 _ bufferLength: UInt ,
135138 _ value: Float16 ,
@@ -153,14 +156,14 @@ internal func Float16ToASCII(
153156 value f: Float16 ,
154157 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
155158{
156- if #available( macOS 9999 , * ) {
159+ if #available( SwiftStdlib 6 . 2 , * ) {
157160 return _Float16ToASCII ( value: f, buffer: & utf8Buffer)
158161 } else {
159162 return 0 ..< 0
160163 }
161164}
162165
163- @available ( macOS 9999 , * )
166+ @available ( SwiftStdlib 6 . 2 , * )
164167fileprivate func _Float16ToASCII(
165168 value f: Float16 ,
166169 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
@@ -396,8 +399,8 @@ fileprivate func _Float16ToASCII(
396399// ================================================================
397400
398401// Support Legacy ABI on top of new implementation
399- @_silgen_name ( " swift_float32ToString2 " )
400- internal func _float32ToStringImpl2 (
402+ @_silgen_name ( " swift_float32ToString " )
403+ public func _float32ToStringImpl (
401404 _ textBuffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
402405 _ bufferLength: UInt ,
403406 _ value: Float32 ,
@@ -421,14 +424,14 @@ internal func Float32ToASCII(
421424 value f: Float32 ,
422425 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
423426{
424- if #available( macOS 9999 , * ) {
427+ if #available( SwiftStdlib 6 . 2 , * ) {
425428 return _Float32ToASCII ( value: f, buffer: & utf8Buffer)
426429 } else {
427430 return 0 ..< 0
428431 }
429432}
430433
431- @available ( macOS 9999 , * )
434+ @available ( SwiftStdlib 6 . 2 , * )
432435fileprivate func _Float32ToASCII(
433436 value f: Float32 ,
434437 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
@@ -616,8 +619,8 @@ fileprivate func _Float32ToASCII(
616619// ================================================================
617620
618621// Support Legacy ABI on top of new implementation
619- @_silgen_name ( " swift_float64ToString2 " )
620- internal func _float64ToStringImpl2 (
622+ @_silgen_name ( " swift_float64ToString " )
623+ public func _float64ToStringImpl (
621624 _ textBuffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
622625 _ bufferLength: UInt ,
623626 _ value: Float64 ,
@@ -641,14 +644,14 @@ internal func Float64ToASCII(
641644 value d: Float64 ,
642645 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
643646{
644- if #available( macOS 9999 , * ) {
647+ if #available( SwiftStdlib 6 . 2 , * ) {
645648 return _Float64ToASCII ( value: d, buffer: & utf8Buffer)
646649 } else {
647650 return 0 ..< 0
648651 }
649652}
650653
651- @available ( macOS 9999 , * )
654+ @available ( SwiftStdlib 6 . 2 , * )
652655fileprivate func _Float64ToASCII(
653656 value d: Float64 ,
654657 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
@@ -1080,11 +1083,15 @@ fileprivate func _Float64ToASCII(
10801083// Float80
10811084//
10821085// ================================================================
1083- #if ((os(macOS) || targetEnvironment(macCatalyst) || os(Linux)) && arch(x86_64))
1086+
1087+ // Float80 is only available on Intel x86/x86_64 processors on certain operating systems
1088+ // This matches the condition for the Float80 type
1089+
1090+ #if !(os(Windows) || os(Android) || ($Embedded && !os(Linux) && !(os(macOS) || os(iOS) || os(watchOS) || os(tvOS)))) && (arch(i386) || arch(x86_64))
10841091
10851092// Support Legacy ABI on top of new implementation
1086- @_silgen_name ( " swift_float80ToString2 " )
1087- internal func _float80ToStringImpl2 (
1093+ @_silgen_name ( " swift_float80ToString " )
1094+ internal func _float80ToStringImpl (
10881095 _ textBuffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
10891096 _ bufferLength: UInt ,
10901097 _ value: Float80 ,
@@ -1108,14 +1115,14 @@ internal func Float80ToASCII(
11081115 value d: Float80 ,
11091116 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
11101117{
1111- if #available( macOS 9999 , * ) {
1118+ if #available( SwiftStdlib 6 . 2 , * ) {
11121119 return _Float80ToASCII ( value: d, buffer: & utf8Buffer)
11131120 } else {
11141121 return 0 ..< 0
11151122 }
11161123}
11171124
1118- @available ( macOS 9999 , * )
1125+ @available ( SwiftStdlib 6 . 2 , * )
11191126fileprivate func _Float80ToASCII(
11201127 value f: Float80 ,
11211128 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
@@ -1232,14 +1239,14 @@ internal func Float128ToASCII(
12321239 value d: Float128 ,
12331240 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
12341241{
1235- if #available( macOS 9999 , * ) {
1242+ if #available( SwiftStdlib 6 . 2 , * ) {
12361243 return _Float128ToASCII ( value: d, buffer: & utf8Buffer)
12371244 } else {
12381245 return 0 ..< 0
12391246 }
12401247}
12411248
1242- @available ( macOS 9999 , * )
1249+ @available ( SwiftStdlib 6 . 2 , * )
12431250fileprivate func _Float128ToASCII(
12441251 value d: Float128 ,
12451252 buffer utf8Buffer: inout MutableSpan < UTF8 . CodeUnit > ) -> Range < Int >
@@ -1282,7 +1289,7 @@ fileprivate func _Float128ToASCII(
12821289// ================================================================
12831290#if ((os(macOS) || targetEnvironment(macCatalyst) || os(Linux)) && arch(x86_64))
12841291
1285- @available ( macOS 9999 , * )
1292+ @available ( SwiftStdlib 6 . 2 , * )
12861293fileprivate func _backend_256bit(
12871294 buffer: inout MutableRawSpan ,
12881295 upperMidpointExact: UInt128 ,
@@ -1390,9 +1397,9 @@ fileprivate func _backend_256bit(
13901397 delta. multiply ( by: UInt32 ( 10 ) )
13911398 t. multiply ( by: UInt32 ( 10 ) )
13921399 let digit = UInt8 ( truncatingIfNeeded: t. extractIntegerPart ( integerBits) )
1393- buffer. storeBytes ( of: 0x30 &+ digit,
1394- toUncheckedByteOffset: nextDigit,
1395- as: UInt8 . self)
1400+ unsafe buffer. storeBytes ( of: 0x30 &+ digit,
1401+ toUncheckedByteOffset: nextDigit,
1402+ as: UInt8 . self)
13961403 nextDigit &+= 1
13971404 }
13981405 }
@@ -1448,7 +1455,7 @@ fileprivate func _backend_256bit(
14481455// `firstDigit` and that those bytes are filled with `"0"` (0x30)
14491456// characters.
14501457
1451- @available ( macOS 9999 , * )
1458+ @available ( SwiftStdlib 6 . 2 , * )
14521459fileprivate func finishFormatting( _ buffer: inout MutableRawSpan ,
14531460 _ sign: FloatingPointSign ,
14541461 _ firstDigit: Int ,
@@ -1585,7 +1592,7 @@ fileprivate func finishFormatting(_ buffer: inout MutableRawSpan,
15851592
15861593// Table with ASCII strings for all 2-digit decimal numbers.
15871594// Stored as little-endian UInt16s for efficiency
1588- @available( macOS 9999 , * )
1595+ @available( SwiftStdlib 6.2 , * )
15891596fileprivate let asciiDigitTable: InlineArray< 100 , UInt16> = [
15901597 0x3030 , 0x3130 , 0x3230 , 0x3330 , 0x3430 ,
15911598 0x3530 , 0x3630 , 0x3730 , 0x3830 , 0x3930 ,
@@ -1610,6 +1617,7 @@ fileprivate let asciiDigitTable: InlineArray<100, UInt16> = [
16101617]
16111618
16121619// The constants below assume we're on a little-endian processor
1620+ @available ( SwiftStdlib 6 . 2 , * )
16131621fileprivate func infinity( buffer: inout MutableRawSpan, sign: FloatingPointSign) - > Range< Int> {
16141622 if sign == . minus {
16151623 buffer. storeBytes ( of: 0x666e692d , toByteOffset: 0 , as: UInt32 . self) // "-inf"
@@ -1620,6 +1628,7 @@ fileprivate func infinity(buffer: inout MutableRawSpan, sign: FloatingPointSign)
16201628 }
16211629}
16221630
1631+ @available ( SwiftStdlib 6 . 2 , * )
16231632fileprivate func zero( buffer: inout MutableRawSpan, sign: FloatingPointSign) - > Range< Int> {
16241633 if sign == . minus {
16251634 buffer. storeBytes ( of: 0x302e302d , toByteOffset: 0 , as: UInt32 . self) // "-0.0"
@@ -1630,10 +1639,10 @@ fileprivate func zero(buffer: inout MutableRawSpan, sign: FloatingPointSign) ->
16301639 }
16311640}
16321641
1633- @available ( macOS 9999 , * )
1642+ @available ( SwiftStdlib 6 . 2 , * )
16341643fileprivate let hexdigits: InlineArray< 16 , UInt8> = [ 0x30 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39 , 0x61 , 0x62 , 0x63 , 0x64 , 0x65 , 0x66 ]
16351644
1636- @available ( macOS 9999 , * )
1645+ @available ( SwiftStdlib 6 . 2 , * )
16371646fileprivate func hexWithoutLeadingZeros( buffer: inout MutableRawSpan, offset: inout Int, value: UInt64) {
16381647 var shift = 60
16391648 while ( shift > 0 ) && ( ( value >> shift) & 0xf == 0 ) {
@@ -1647,7 +1656,7 @@ fileprivate func hexWithoutLeadingZeros(buffer: inout MutableRawSpan, offset: in
16471656 }
16481657}
16491658
1650- @available ( macOS 9999 , * )
1659+ @available ( SwiftStdlib 6 . 2 , * )
16511660fileprivate func hexWithLeadingZeros( buffer: inout MutableRawSpan , offset: inout Int , value: UInt64 ) {
16521661 var shift = 60
16531662 while shift >= 0 {
@@ -1658,7 +1667,7 @@ fileprivate func hexWithLeadingZeros(buffer: inout MutableRawSpan, offset: inout
16581667 }
16591668}
16601669
1661- @available ( macOS 9999 , * )
1670+ @available ( SwiftStdlib 6 . 2 , * )
16621671fileprivate func nan_details( buffer: inout MutableRawSpan ,
16631672 sign: FloatingPointSign ,
16641673 quiet: Bool ,
@@ -1704,6 +1713,7 @@ fileprivate func nan_details(buffer: inout MutableRawSpan,
17041713//
17051714// This implementation is based on work by Paul Khuong:
17061715// https://pvk.ca/Blog/2017/12/22/appnexus-common-framework-its-out-also-how-to-print-integers-faster/
1716+ @available ( SwiftStdlib 6 . 2 , * )
17071717@inline ( __always)
17081718fileprivate func intToEightDigits( _ n: UInt32 ) -> UInt64 {
17091719 // Break into two numbers of 4 decimal digits each
@@ -1755,7 +1765,7 @@ fileprivate func multiply64x32RoundingUp(_ lhs: UInt64, _ rhs: UInt32) -> UInt64
17551765 return t + ( lhs >> 32 ) * UInt64( rhs)
17561766}
17571767
1758- @available ( SwiftStdlib 6 . 0 , * )
1768+ @available ( SwiftStdlib 6 . 2 , * )
17591769@inline ( __always)
17601770fileprivate func multiply128x64RoundingDown( _ lhs: UInt128 , _ rhs: UInt64 ) -> UInt128 {
17611771 let lhsHigh = UInt128 ( truncatingIfNeeded: lhs. _high)
@@ -1764,7 +1774,7 @@ fileprivate func multiply128x64RoundingDown(_ lhs: UInt128, _ rhs: UInt64) -> UI
17641774 return ( lhsHigh &* rhs128) &+ ( ( lhsLow &* rhs128) >> 64 )
17651775}
17661776
1767- @available ( SwiftStdlib 6 . 0 , * )
1777+ @available ( SwiftStdlib 6 . 2 , * )
17681778@inline ( __always)
17691779fileprivate func multiply128x64RoundingUp( _ lhs: UInt128 , _ rhs: UInt64 ) -> UInt128 {
17701780 let lhsHigh = UInt128 ( truncatingIfNeeded: lhs. _high)
@@ -1776,11 +1786,9 @@ fileprivate func multiply128x64RoundingUp(_ lhs: UInt128, _ rhs: UInt64) -> UInt
17761786 return h + ( ( l &+ bias) &>> 64 )
17771787}
17781788
1779- #if ((os(macOS) || targetEnvironment(macCatalyst) || os(Linux)) && arch(x86_64))
17801789// Custom 256-bit unsigned integer type, with various arithmetic helpers as methods.
1781-
17821790// Used by 80- and 128-bit floating point formatting logic above...
1783- @available ( macOS 15 , * )
1791+ @available ( SwiftStdlib 6 . 2 , * )
17841792fileprivate struct UInt256 {
17851793 var high : UInt128
17861794 var low : UInt128
@@ -1910,7 +1918,7 @@ fileprivate struct UInt256 {
19101918 }
19111919
19121920 mutating func extractIntegerPart( _ bits: Int ) -> UInt {
1913- assert ( bits < 64 )
1921+ assert ( bits < 16 )
19141922 let integral = high. _high >> ( 64 &- bits)
19151923 high = UInt128 ( _low: high. _low,
19161924 _high: high. _high &- ( integral &<< ( 64 &- bits) ) )
@@ -1933,15 +1941,14 @@ fileprivate struct UInt256 {
19331941 && lhs. low < rhs. low)
19341942 }
19351943}
1936- #endif
19371944
19381945// ================================================================
19391946//
19401947// Powers of 10
19411948//
19421949// ================================================================
19431950
1944- @available ( macOS 9999 , * )
1951+ @available ( SwiftStdlib 6 . 2 , * )
19451952@inline ( __always)
19461953fileprivate func intervalContainingPowerOf10_Binary32( _ p: Int , _ lower: inout UInt64 , _ upper: inout UInt64 ) -> Int {
19471954 if p >= 0 {
@@ -1960,7 +1967,7 @@ fileprivate func intervalContainingPowerOf10_Binary32(_ p: Int, _ lower: inout U
19601967 return binaryExponentFor10ToThe ( p)
19611968}
19621969
1963- @available ( macOS 9999 , * )
1970+ @available ( SwiftStdlib 6 . 2 , * )
19641971@inline ( __always)
19651972fileprivate func intervalContainingPowerOf10_Binary64( _ p: Int , _ lower: inout UInt128 , _ upper: inout UInt128 ) -> Int {
19661973 if p >= 0 && p <= 55 {
@@ -2018,7 +2025,7 @@ fileprivate func decimalExponentFor2ToThe(_ p: Int) -> Int {
20182025// This covers the negative powers of 10 for Float32.
20192026// Positive powers of 10 come from the next table below.
20202027// Table size: 320 bytes
2021- @available ( macOS 9999 , * )
2028+ @available ( SwiftStdlib 6 . 2 , * )
20222029fileprivate let powersOf10_negativeBinary32 : InlineArray < _ , UInt64 > = [
20232030 0x8b61313bbabce2c6 , // x 2^-132 ~= 10^-40
20242031 0xae397d8aa96c1b77 , // x 2^-129 ~= 10^-39
@@ -2077,7 +2084,7 @@ fileprivate let powersOf10_negativeBinary32: InlineArray<_, UInt64> = [
20772084// support.
20782085
20792086// Table size: 896 bytes
2080- @available ( macOS 9999 , * )
2087+ @available ( SwiftStdlib 6 . 2 , * )
20812088fileprivate let powersOf10_Exact128 : InlineArray < _ , UInt64 > = [
20822089 // Low order ... high order
20832090 0x0000000000000000 , 0x8000000000000000 , // x 2^1 == 10^0 exactly
@@ -2150,7 +2157,7 @@ fileprivate let powersOf10_Exact128: InlineArray<_, UInt64> = [
21502157// penalty.
21512158
21522159// Table size: 464 bytes
2153- @available ( macOS 9999 , * )
2160+ @available ( SwiftStdlib 6 . 2 , * )
21542161fileprivate let powersOf10_Binary64 : InlineArray < _ , UInt64 > = [
21552162 // low-order half, high-order half
21562163 0x3931b850df08e738 , 0x95fe7e07c91efafa , // x 2^-1328 ~= 10^-400
@@ -2184,15 +2191,13 @@ fileprivate let powersOf10_Binary64: InlineArray<_, UInt64> = [
21842191 0x1027fff56784f444 , 0xc4c5e310aef8aa17 , // x 2^1276 ~= 10^384
21852192]
21862193
2187- #if ((os(macOS) || targetEnvironment(macCatalyst) || os(Linux)) && arch(x86_64))
2188-
21892194// Needed by 80- and 128-bit formatters above
21902195
21912196// We could cut this in half by keeping only the positive powers and doing
21922197// a single additional 256-bit multiplication by 10^-4984 to recover the negative powers.
21932198
21942199// Table size: 5728 bytes
2195- @available ( macOS 9999 , * )
2200+ @available ( SwiftStdlib 6 . 2 , * )
21962201fileprivate let powersOf10_Binary128 : InlineArray < _ , UInt64 > = [
21972202 // Low-order ... high-order
21982203 0xaec2e6aff96b46ae , 0xf91044c2eff84750 , 0x2b55c9e70e00c557 , 0xb6536903bf8f2bda , // x 2^-16556 ~= 10^-4984
@@ -2376,7 +2381,7 @@ fileprivate let powersOf10_Binary128: InlineArray<_, UInt64> = [
23762381 0x7128a8aad239ce8f , 0x8737bd250290cd5b , 0xd950102978dbd0ff , 0xb3b8e2eda91a232d , // x 2^16557 ~= 10^4984
23772382]
23782383
2379- @available ( macOS 9999 , * )
2384+ @available ( SwiftStdlib 6 . 2 , * )
23802385fileprivate func intervalContainingPowerOf10_Binary128( p: Int , lower: inout UInt256 , upper: inout UInt256 ) -> Int {
23812386 if p >= 0 && p <= 55 {
23822387 let exactLow = powersOf10_Exact128 [ p * 2 ]
@@ -2405,4 +2410,3 @@ fileprivate func intervalContainingPowerOf10_Binary128(p: Int, lower: inout UInt
24052410 upper. low += 2
24062411 return e
24072412}
2408- #endif
0 commit comments