Repository navigation
What's Changed
fastdiv_s32is now branchless and uses a single signed multiplication. Previously, compilers produced a branch on the sign of the dividend (x64) or two multiplications (ARM). When |d| > 2, the precomputedMis smaller than 2^63, so a signed 64-bit multiplication gives the result directly; when |d| == 2, the dividend is added back. The API andcomputeM_s32are unchanged.- New helper:
mul128_s64_hi(most significant 64 bits of a signed 64-bit product).
On x64, with random dividends and a divisor known only at runtime, fastdiv_s32 goes from about 3 to 4 ns to 0.77 ns per division with GCC 14, and from about 3.4 ns to 0.78 ns with clang 21 (Intel Xeon Gold 6548N). The old version suffered from branch mispredictions. On an Apple M4 Max, scalar performance is about the same (0.39 ns versus 0.38 ns), but clang can now partially vectorize loops calling fastdiv_s32 (0.22 ns).
Full Changelog: v0.1.0...v0.2.0