Hardcore Chaos Testing for Coni's Native Float32 Arrays
When you’re building high-performance applications in the browser, JavaScript’s generic Number type often isn’t enough. You need raw, contiguous memory arrays. In Coni WASM, we rely heavily on native WebAssembly float32 arrays for our most mathematically intensive applications.
Where is Float32 actually used in Coni?
Native float32 arrays (make-float32-array, f32-set!, f32-get) are the absolute backbone of Coni’s performance layer. You’ll find them powering:
- WebGL Geometry: In our
deep-focus-webglapp, we usefloat32arrays to mathematically sculpt an 80,000-vertex brain matrix (240,000 floats!) natively, before shipping it to the GPU in a single shot viajs/float32-buffer. - Game Engine Particle Systems: For games like
flappy-birdorneon-boids, parallelfloat32arrays keep track of thousands of X/Y coordinates, velocities, and lifetimes without garbage collection pauses. - Raw DSP Audio: Our
sound-nodessynthesizer calculates complex impulse responses and noise waveforms natively usingfloat32arrays before mapping them into WebAudio channels.
The Bug: Type Coercion and f64.reinterpret_i64
Because of how critical these arrays are, any bug in them is catastrophic. Recently, we discovered an issue where passing an exact integer (like 150) into f32-set! would mysteriously yield 0.0.
What was happening? In a prior version of the WASM compiler, passing an integer into a float setter caused the compiler to incorrectly use f64.reinterpret_i64 to interpret the bits, completely garbling the underlying value.
The Solution: Float32 Chaos Testing
We patched the compiler to correctly coerce integers into floats before memory insertion, but we wanted a guarantee that it would never regress. Enter Chaos Testing.
In our new float32_coercion_test.coni suite, we built a hardcore chaos test. Instead of just testing a few happy-path numbers, we allocate a massive array and hammer it with a deterministic, pseudo-random loop.
(deftest test-f32-chaos
"Hardcore chaos test for float32 array bounds and type coercions."
(let [size 1000
arr (make-float32-array size)]
(loop [i 0]
(if (< i size)
(let [
;; Generate a mix of extreme edge cases:
val (if (= (mod i 4) 0) (- 0 i) ;; negative int
(if (= (mod i 4) 1) (* i 10000) ;; large positive int
(if (= (mod i 4) 2) (+ i 0.5) ;; exact float32 fraction
0))) ;; zero integer
]
(f32-set! arr i val)
(recur (+ i 1)))
nil))
;; ... verification loop ...
By intentionally throwing a brutal mix of extreme positive integers, negative boundaries, pure zeroes, and fractional floats at the f32-set! function 1,000 times, we guarantee that WASM traps are avoided and type coercion is bulletproof.
If you want to build GPU-accelerated applications or complex state machines with zero overhead, you need absolute trust in your memory primitives. With chaos testing, Coni’s float32 arrays are now battle-hardened!