@@ -23,13 +23,25 @@ cargo run -p pow10gen
2323
2424## Features
2525
26- ### ` small ` — compact tables for WASM
26+ ### ` small ` — compact tables for Wasm
2727
28- Enable the ` small ` feature to replace the 11 KB power-of-10 lookup table with
29- two smaller tables (632 bytes total) that are multiplied at runtime.
30- This reduces WASM binary size from 14 KB to ** 4 KB** with a modest
31- formatting slowdown (~ 1.6x), while parsing is unaffected.
32- Still ** 2.4x faster** than ryu for formatting.
28+ The core algorithm needs ` 10^p ` as a 128-bit normalized mantissa for each
29+ exponent ` p ` in −348..=347. By default, all 696 entries are stored in a flat
30+ lookup table (696 × 16 = 11 KB).
31+
32+ The ` small ` feature decomposes the lookup using ` 10^p = 10^(27q) × 10^r `
33+ where ` p = 27q + r ` and ` 0 ≤ r < 27 ` . This replaces the single table with:
34+
35+ - ` POW10_COARSE ` : 26 entries of ` (u64, u64) ` for ` 10^(27q) ` — 416 bytes
36+ - ` POW10_FINE ` : 27 entries of ` u64 ` for ` 10^r ` — 216 bytes
37+
38+ Fine entries need only one ` u64 ` (not two) because ` 10^r ` for ` r ≤ 26 ` is
39+ exact at 128 bits and the low 64 bits are always zero.
40+
41+ At runtime, ` prescale ` multiplies the two factors back together with u128
42+ arithmetic instead of doing a direct table lookup. This reduces Wasm binary
43+ size from 14 KB to ** 4 KB** with a modest formatting slowdown (~ 1.6x),
44+ while parsing is unaffected. Still ** 2.4x faster** than ryu for formatting.
3345
3446``` toml
3547fpfmt = { version = " 0.2" , features = [" small" ] }
@@ -41,22 +53,22 @@ Formatting and parsing 8 representative f64 values (`1.0`, `0.1`, `3.14`, `PI`,
4153
4254Measured on Apple M3 Pro, macOS 15.7.3 (aarch64):
4355
44- | Task | fpfmt | fpfmt ` small ` | ryu | stdlib |
45- | ------| ------: | --------------: | ----: | -------: |
46- | ** format** (f64 → string) | 65 ns | 102 ns | 240 ns | 528 ns |
47- | ** parse** (string → f64) | 697 ns | 701 ns | — | 658 ns |
56+ | Task | fpfmt | fpfmt ` small ` | ryu | stdlib |
57+ | ------------------------- | -----: | ------------: | -----: | -----: |
58+ | ** format** (f64 → string) | 65 ns | 102 ns | 240 ns | 528 ns |
59+ | ** parse** (string → f64) | 697 ns | 701 ns | — | 658 ns |
4860
4961``` sh
5062cargo bench -p bench
5163cargo bench -p bench --features small
5264```
5365
54- ## WASM size
66+ ## Wasm size
5567
56- | Configuration | Size |
57- | --------------- | -----: |
58- | default | 14,428 bytes |
59- | ` small ` | ** 4,224 bytes** |
68+ | Configuration | Size |
69+ | ------------- | -- ------------: |
70+ | default | 14,428 bytes |
71+ | ` small ` | ** 4,224 bytes** |
6072
6173``` sh
6274cargo build --target wasm32-unknown-unknown --release -p wasm-size
0 commit comments