`Divide` precomputes an integer reciprocal for a signed 32-bit denominator, then uses it for repeated divisions that round toward zero. ```cpp #include "divide.hpp" auto d = Divide(3); auto a = d(7); // 2 auto b = d(-7); // -2 auto negative = Divide(-3); auto c = negative(7); // -2 ``` The denominator can come from runtime input. Construct the object outside the loop that uses it. Both construction and evaluation are also `constexpr`, so `constexpr Divide d(3);` computes the parameters at compile time. The constructor rejects zero with `std::invalid_argument`. All nonzero signed 32-bit denominators are supported, including `INT32_MIN`. As with built-in signed division, the caller must exclude `INT32_MIN / -1`: its mathematical result does not fit the return type. A debug assertion checks that precondition; `-DNDEBUG` removes the assertion. The result of an invalid call is not part of the API. Signed division's domain and truncation rule are specified in the [C++ working draft](https://eel.is/c++draft/expr.mul). For a denominator whose magnitude is `a`, the constructor computes: ```text k = ceil(log2(a)) = bit_width(a - 1) s = 31 + k S = 2^s m = floor(S / a) + 1 ``` Evaluation first divides by the positive magnitude, then applies the denominator's sign: ```cpp auto product = std::int64_t(numerator) * multiplier_; auto quotient = product >> shift_; quotient += (numerator < 0); return std::int32_t((quotient ^ sign_) - sign_); ``` `sign_` is zero for positive denominators and minus one for negative ones. For `Divide(3)`, the multiplier is `2,863,311,531` (`0xAAAAAAAB`) and the shift is `33`, giving `(7 * 2,863,311,531) >> 33 == 2`. The `+ 1` when constructing the multiplier is deliberate, including for powers of two. The reciprocal is strictly greater than the exact reciprocal. Simply using `ceil(S / a)` would break the negative-input correction when `a` is a power of two. To see why it is exact, write `m = S/a + e`, where `0 < e <= 1`, and note that `S >= 2^31 * a`. For a positive numerator `n < 2^31`, the extra amount `n*e/S` is strictly less than `1/a`. The fractional part of `n/a` is at most `(a-1)/a`, so the approximation cannot cross the next integer. For a negative numerator, let `x = |n| <= 2^31` and `q = floor(x/a)`. Then `q < x*m/S <= q+1`, which means `floor(-x*m/S) + 1 == -q`. The arithmetic right shift supplies that floor, and the correction supplies the `+ 1`. Negative signed right shifts round down in [C++20 and later](https://eel.is/c++draft/expr.shift). For the supported denominator range, `31 <= s <= 62` and `2^31 < m < 2^32`. Therefore the signed 64-bit product always fits. Arithmetic remains 64-bit until the final cast, including the `Divide(1)(INT32_MIN)` case. Build and run with a compiler accepting C++26 mode: ```sh mkdir -p build clang++ -std=c++26 -O3 -DNDEBUG example.cpp -o build/example ./build/example clang++ -std=c++26 -O3 -DNDEBUG -Wall -Wextra -Wpedantic test_divide.cpp -o build/test_divide ./build/test_divide ``` The header uses a C++20 subset and does not require native C++26 contracts support. Its zero check is ordinary constructor code, so it remains enabled with `NDEBUG`; only the per-call overflow assertion is disabled. Run the same correctness tests with assertions and sanitizers enabled: ```sh clang++ -std=c++26 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all test_divide.cpp -o build/test_divide_sanitized ./build/test_divide_sanitized ``` Tests cover compile-time evaluation, zero rejection, every signed 16-bit numerator with divisors from -128 through 128 except zero, full-width boundary numerators, quotient transitions, powers of two and adjacent divisors, and deterministically randomized full-width inputs. Apple Clang 21 passed 43,143,231 comparisons in both optimized and sanitized builds. Inspect the callable independently of construction: ```sh clang++ -std=c++26 -O3 -DNDEBUG -S codegen.cpp -o build/codegen.s ``` `apply_divide` receives an existing object and an unknown numerator, so the compiler cannot substitute a constant denominator. With Apple Clang 21, its ARM64 and x86-64 assembly contains one multiply, a variable arithmetic shift, and sign corrections, with no divide instruction or conditional branch. The ordinary division function in the same file uses `sdiv` or `idiv`. This is a compact scalar implementation of runtime invariant division, a technique also used by [libdivide](https://libdivide.com/). It deliberately uses one uniform formula, including for powers of two. Compiler-generated constant division or a more specialized implementation may use fewer instructions. A speed comparison must account for the target CPU, whether construction is amortized over many calls, and whether the compiler can vectorize the surrounding loop. The [blog post](https://adamdeprince.com/blog/posts/one-divide-instruction-can-still-be-too-slow/) reports measured Apple M4 Max, Intel Xeon E5-4657L v2, and Loongson 3A6000 results; [benchmark notes](benchmark-results/README.txt) include the method, raw samples, assembly, and reproduction commands.