Commit Graph

44 Commits

Author SHA1 Message Date
Tim Kientzle 0c510958f7 Float80: Fix an overly strict assertion 2026-05-19 11:22:07 -07:00
Tim Kientzle 95103e7dc7 Do not mark safe operations as unsafe 2026-05-11 16:44:43 -07:00
Tim Kientzle 94f3318702 Schubfach-based Float16/32/64/80.debugDescription
Compared to the previous implementation:
* Faster
* Smaller code size
* Produces exactly the same output
* New! Float80 support in Swift!

(Float128 support is also here, tested and ready to go.
Just commented out until we actually have Float128. ;-)
2026-05-11 15:30:28 -07:00
Guillaume Lessard d03531ff16 [stdlib] more MutableSpan updates 2026-05-02 05:46:29 -07:00
Tim Kientzle 7e2a4a12c1 Revert "Schubfach-based Floating Point debugDescription" 2026-03-31 07:23:58 -07:00
Tim Kientzle 46248149ef Touch up a few comments to match the code 2026-03-16 17:48:50 -07:00
Tim Kientzle 2dd35df202 Fix a couple of compile errors 2026-03-13 22:31:22 -07:00
Tim Kientzle bff72d60c9 New Float80: half the size, 3x the speed 2026-03-13 21:55:08 -07:00
Tim Kientzle 944fcef8f7 Simplify some expressions 2026-03-12 15:12:48 -07:00
Tim Kientzle 10579ccaa9 Significant renaming and code simplification 2026-03-12 14:20:01 -07:00
Tim Kientzle afa837ede8 Complete Float16: 4.5ns! 2026-03-10 07:28:42 -07:00
Tim Kientzle 9e5165aa01 Unbreak Float80 printing 2026-03-03 13:44:44 -08:00
Tim Kientzle 115a04f2af Back out an unrelated Float16 change that seems to have broken something 2026-03-03 13:09:16 -08:00
Tim Kientzle 9ca285ada8 Speed up some right shifts 2026-03-03 13:08:25 -08:00
Tim Kientzle 9661843876 Schubfach-based Float/Double debugDescription
This reimplements debugDescription for Float and Double using a new algorithm
based on Raffaello Guilietti's Schubfach paper, carrying over some performance
improvements from the previous Grisu-based implementation.

In practice, this is about twice as fast as the previous implementation,
and the code is both shorter and simpler.
Extensive testing shows that the new implementations produce identical
output to the previous implementations.

The basic idea is:
* Start by expressing the floating-point value as `s * 2^e` where `s`
  and `e` are both integers.
* Estimate the power of 10 as `p = ceiling(e * log10(2))`
* Scale the upper bound of the rounding interval by `10^(-p)` to obtain
  between 0 and 16 digits for Double (0 to 8 for Float)
* About 40% of the time, those initial digits (after pruning
  any trailing zeros) are precisely the desired optimal form
* The remaining 60% of the time, we need exactly one additional digit.
* A slight adjustment allows us to directly compute one of only
  two possibilities for that final digit.  A quick test discriminates
  between them.

On an M4 MacBook Air, the core `_Float32ToASCII` for single-precision
takes about 6ns to produce the final text in a local buffer.
`Float.debugDescription` based on this takes a total of 9ns.

The core double-precision implementation takes about 10ns,
with another 14ns required to allocate a `String` on the heap.

I have not yet converted Float16 and Float80 over to
the new algorithm.  When I do so, I'd like to factor the Float80
tables a bit more carefully to further reduce overall code size.

The fact that `Double.debugDescription` is now dominated by heap
allocation means that we should explore APIs that
append to an existing buffer rather than allocating from scratch.

Observation: It would be possible to restructure the current code
into two functions:

* A base conversion that takes a pair of integers
  (binary significand and power of 2)
  and returns another pair of integers
  (decimal significand and power of 10)

* A formatting routine that takes a decimal significand
  and power of 10 and formats it appropriately.

This would be compelling if there were other uses for either
of these two functions.
2026-03-03 07:06:52 -08:00
Guillaume Lessard 8170936050 [stdlib] add required unsafe markers 2026-01-08 21:29:33 -08:00
Tim Kientzle 655c5ff76e Fill in missing unsafe markers 2025-12-02 09:56:19 -08:00
Tim Kientzle f65358521f Properly initialize buffer when called via legacy ABI
Resolves #85539
2025-11-17 10:56:10 -08:00
Tim Kientzle 1d23852f2c Float16 support only on Swift 5.3+ 2025-10-10 17:22:04 -07:00
Tim Kientzle 7b55ac3094 Remove availability restrictions from these internal functions
This is possible because this replaces
* `InlineArray` with `internal _InlineArray`
* `UInt128` with `internal _UInt128`

And the replacements are not availability-restricted.

We don't need to do the same thing with `MutableSpan`
because that back-deploys to Swift 5.0, which is as
far back as the stdlib builds anyway.
2025-10-10 14:07:44 -07:00
Tim Kientzle f0679b363e Re-apply PR #82750: Reimplement floating-point description implementation in Swift.
This reverts PR #84576, which was a revert of PR #82750
It reverts commit 4ac18aa32e, reversing
changes made to b46eddbabd.
2025-10-07 07:11:57 -07:00
Tim Kientzle 7d85d75f78 Revert "Merge pull request #82750 from tbkka/tbkka-swift-floatingpointtostring"
This reverts commit 54627fb49b, reversing
changes made to dda4608a84.
2025-09-29 15:13:19 -07:00
Tim Kientzle b761d8f743 Another try at fixing the ABI change 2025-09-24 09:37:50 -07:00
Tim Kientzle 9635ef51c5 Try to fix up availability of legacy shim functions 2025-09-24 07:29:56 -07:00
Tim Kientzle f8d9b52e7d another attempt at fixing the float16 ABI break 2025-09-23 15:11:38 -07:00
Tim Kientzle 7a4bd46d4c Adjust availability checks for float16 2025-09-23 08:07:04 -07:00
Tim Kientzle a337e727ab Try to fix up the availability of the Swift and C projections of the legacy ABI 2025-09-22 12:40:12 -07:00
Tim Kientzle e943878f9a Prefill the work buffer with '0' characters
For example, this avoids the need to do any explicit byte-by-byte
writes when expanding "123" out to "123000000.0".

This also required reworking the "back out extra digits" process
for Float64 to ensure the unused digits get written as '0' characters
instead of null bytes.
2025-09-22 12:40:12 -07:00
Tim Kientzle 1a3a40003c Minor safety and performance tweaks 2025-09-22 12:40:11 -07:00
Tim Kientzle ae75afbb88 Try to fix a crash only seen so far on WASI32 2025-09-22 12:40:11 -07:00
Tim Kientzle 31930123e5 Remove extra semicolons 2025-09-22 12:40:11 -07:00
Tim Kientzle acd43ad355 Fix Float32 formatting on 32-bit architectures 2025-09-22 12:40:11 -07:00
Tim Kientzle a6fd1aa955 Fix build when Int is 16 bits 2025-09-22 12:40:11 -07:00
Tim Kientzle 2bf38bf578 Remove a layer of indirection 2025-09-22 12:40:11 -07:00
Tim Kientzle de7c280e9c Try to match stdlib coding conventions 2025-09-22 12:40:10 -07:00
Tim Kientzle cf552ea9ce Match up conditional for Float80 support 2025-09-22 12:40:10 -07:00
Tim Kientzle 8e40ac8568 Fix availability 2025-09-22 12:40:10 -07:00
Tim Kientzle d9907324a1 Comment updates 2025-09-22 12:40:09 -07:00
Tim Kientzle e3990c6085 Float80 performance 2025-09-22 12:40:09 -07:00
Tim Kientzle 7be098f839 Initial Float80 support + some general reorganization and commenting 2025-09-22 12:40:09 -07:00
Tim Kientzle 175e1d78b3 Float32/64 safety improvements 2025-09-22 12:40:09 -07:00
Tim Kientzle b50a2e6ead Float16 tweaks: balancing safety and performance 2025-09-22 12:40:09 -07:00
Tim Kientzle 070fd6006e Add Float16 support to the Swift implementation 2025-09-22 12:40:08 -07:00
Tim Kientzle 4cf03a47bc Reimplement floating-point description implementation in Swift.
Note:  This is still incomplete.  I need to finish porting the Float16
and Float80 support before it can fully replace the existing
C implementation.
2025-09-22 12:40:08 -07:00