finds.dev← search

// the find

lemire/fastmod

★ 350 · C++ · Apache-2.0 · updated Apr 2026

A C/C++ header file for fast 32-bit division remainders (and divisibility tests) on 64-bit hardware.

A single header file implementing Daniel Lemire's fast modulo/division trick for 32-bit divisors on 64-bit hardware. The trick replaces division with a multiply-and-shift using a precomputed magic number, which is faster when you're applying the same divisor repeatedly — hash tables being the canonical use case. Not general-purpose; only useful in tight loops where you control the divisor.

The technique is backed by a published paper with actual math, not just empirical cargo-culting. Header-only with a dead-simple API — precompute M once, call fastmod_u32 in the hot loop, done. Exhaustive test mode covers all 32-bit dividends for correctness, which matters for a low-level numeric primitive. Benchmarks are real Skylake numbers with a specific compiler, not made-up percentages.

The 64-bit variant is experimental and not supported on MSVC, so if you're on Windows or need 64-bit divisors you're on your own. At 350 stars it's niche enough that you should expect to debug issues yourself — the issue tracker is sparse. Modern compilers (GCC, Clang) already apply this optimization automatically for compile-time-constant divisors, so you only gain something when the divisor is runtime-determined but repeated. No CMake install target or package manager support, so integrating it into a larger build requires manual file copying.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →