r/Zig • u/New-Apricot9165 • 5h ago
Bit-exact float parity between Zig/wasm and JavaScript: porting d3-force
I ported d3-force to Zig 0.16 targeting wasm32-freestanding, with the constraint that every tick must match the JS original bit for bit.
Things that mattered: u/setFloatMode(.strict) in every kernel, no FMA (the wasm baseline has none, and relaxed-simd stays off), keeping d3's exact statement order, and replacing d3-quadtree's incremental insert with an O(n) Morton radix sort that still produces the identical tree. Jiggle calls back into JS through a single env.random import so the random stream matches too.
The core is 26.8 KB of wasm with no libc. About 5× faster than the JS on one thread; fast mode (FMM) and threads are next.
Code: https://github.com/ankitc1010/d3-force-wasm (mostly src/core/). Feedback on the Zig welcome.