std/softfloat: add bit-exact IEEE 754 binary64/binary32 arithmetic

Wuffs the Language has no floating point types, but some reference
implementations (notably the brotli encoder) make decisions with double and
float arithmetic and libm's log2. Porting them with byte-identical output
needs bit-exact floating point results.

This package implements f64 and f32 add, sub, mul, div, fma, comparisons,
min/max, integer conversions and f64<->f32 conversions on bit patterns,
with round-to-nearest-even, subnormals, infinities and NaNs, following the
structure of Berkeley SoftFloat 3. It also ports glibc 2.39's x86-64
__log2_fma log2 implementation (the variant selected on CPUs with FMA and
AVX2), following the shipped machine code's fused operations.

Verification: test/c/std/softfloat.c checks every operation against the C
compiler's native arithmetic on edge cases and random operands. The
script/manual-test-softfloat-*.c programs run 10^8 random operations per
operation group and compare log2_u64 against libm for every integer in
[1, 2^32) plus 10^7 larger values; all with zero mismatches.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NoosQNe7DKWnqDCBeqw1p
This commit is contained in:
Claude committed 2026-10-26 22:46:32 +00:00
1 parent 4be865da25
commit f33e9f3377
9 files changed
+26111

No files matched your search

+4
View File
@@ -8,6 +8,9 @@ categories:
- [Hashers](/doc/std/hashers.md).
- [Image Decoders](/doc/std/image-decoders.md).
Other packages include [std/softfloat](/std/softfloat/README.md), which
implements bit-exact IEEE 754 floating point arithmetic using only integers.
The general pattern is that a package `foo` (e.g. `jpeg`, `png`) contains a
struct `bar` (e.g. `hasher`, `decoder`, etc) that implements a
package-independent interface. For example, every compression decoder struct
@@ -66,6 +69,7 @@ to enable.
- `PNG: BASE, ADLER32, CRC32, DEFLATE, ZLIB`
- `QOI: BASE`
- `SHA256: BASE`
- `SOFTFLOAT: BASE`
- `TGA: BASE`
- `THUMBHASH: BASE`
- `VP8: BASE`