1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Host-invariant transcendental functions.
//!
//! # Why this module exists
//!
//! `f64::powf`, `f64::exp`, `f64::ln` and friends call into the platform's
//! libm. On glibc that is not one implementation — glibc resolves libm symbols
//! through **IFUNC** at load time and selects an AVX2/FMA variant when the CPU
//! offers one. Same binary, same glibc, different instruction path, chosen at
//! runtime by the hardware it happens to land on.
//!
//! That is enough to break this crate's reason for existing. Measured on the
//! 723-row reference corpus through `holomap-clusterer`, with identical source,
//! toolchain and `Cargo.lock`:
//!
//! | regime | Ivy Bridge (no AVX2/FMA) | Skylake (AVX2+FMA) |
//! |---|---|---|
//! | raw | 36 / 30.0% | 36 / 31.4% |
//! | normalised f32 | 37 / 29.2% | 36 / 28.9% |
//! | normalised f64 | 34 / 27.2% | 33 / 27.7% |
//!
//! Masking glibc's dispatch on the Skylake host with
//! `GLIBC_TUNABLES=glibc.cpu.hwcaps=-AVX2,-FMA` reproduces the Ivy Bridge
//! column **exactly**, in all three regimes. Rust-side feature detection
//! (`is_x86_feature_detected!`, which `matrixmultiply` uses) is unaffected by
//! that tunable and was verified to report `avx2=true fma=true` in both runs —
//! so the delta cannot come from there. glibc's libm is what moved.
//!
//! Routing every transcendental through the pure-Rust [`libm`] crate removes
//! the dispatch entirely: one implementation, chosen at compile time, identical
//! on every x86-64 host. There is nothing left to select between.
//!
//! # What is deliberately NOT routed here
//!
//! - **`sqrt`** compiles to the `sqrtsd`/`sqrtss` instruction, which IEEE-754
//! defines as correctly rounded. There is no libm call and no dispatch, so
//! wrapping it would cost speed and buy nothing.
//! - **`powi`** is expanded by the compiler into multiplications. Same reason.
//!
//! # This changes the numbers — and it changes them *towards* wasm
//!
//! `libm`'s results are not bit-identical to glibc's, so adopting it moves
//! native output on every host, including hosts the dispatch never affected.
//! That cost is real and is paid once.
//!
//! What makes it the right trade is *where* it lands. `wasm32-unknown-unknown`
//! has no system libm, so the wasm build was already using these same pure-Rust
//! implementations — which is precisely **why** wasm was host-invariant when
//! native was not. Pointing native at the same crate does not invent a third
//! answer; it converges native onto the one that was already reproducible.
//! Measured on the 723-row corpus through `holomap-clusterer`:
//!
//! | regime | wasm (before *and* after) | native before | native after |
//! |---|---|---|---|
//! | raw | 37 / 29.6% | 36 / 30.0% | **37 / 29.6%** |
//! | normalised | 36 / 27.0% | 34 / 27.2% | **36 / 27.0%** |
//!
//! wasm output is unchanged, so wasm consumers see nothing. The cross-backend
//! delta that `npm/test/backend-equivalence-corpus.test.ts` previously reported
//! without bounding is now **zero clusters, zero noise rows** in both regimes.
//! The spread across mathematically-equivalent input regimes also narrowed,
//! from 3 clusters to 1.
//!
//! # The cost, measured rather than assumed
//!
//! `libm`'s pure-Rust `pow` is not free: glibc's dispatch exists because the
//! AVX2/FMA kernels are genuinely faster. On the 1000×50-d bench this crate
//! ships, **3.37 s → 4.55 s, about 1.35×**.
//!
//! Read that as a *lower* bound. It was measured on an Ivy Bridge host with no
//! AVX2 and no FMA — the machine where glibc was already taking its baseline
//! path and had least to offer. On a CPU where glibc would have selected an
//! AVX2 kernel, the gap should be wider. Not yet measured there; do not quote
//! 1.35× as the cross-machine figure.
//!
//! Determinism is this crate's product, so trading throughput for it is the
//! trade the crate exists to make. Stating the number is not a caveat on the
//! decision — it is so nobody rediscovers the slowdown and files it as a bug.
/// Transcendentals that resolve identically on every host.
///
/// Implemented for `f32` and `f64`. Method names are prefixed `hi_`
/// ("host-invariant") so a call site never silently reverts to the inherent
/// `std` method after an edit — `x.exp()` and `x.hi_exp()` do not look alike.
pub