mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
4fa465fc8f
Replace the O(n) shift-subtract loop with a constant-time trial quotient approach (Knuth Algorithm D, TAOCP Vol 2 Section 4.3.1). The old code iterates clz(b_hi)-clz(a_hi)+1 times (up to 64 iterations of 128-bit arithmetic). The new code uses a single divwide call to get a trial quotient, then verifies with two native-width widening multiplies. Benchmark (Apple M1, ReleaseFast): - Large divisor, large shift: 87ns -> 7.5ns (11.5x faster) - Small divisor / uniform: unchanged