#arithmetic #strength #reduction #division #modulus


Faster integer division and modulus operations

5 releases

Uses old Rust 2015

0.2.3 Dec 27, 2019
0.2.2 Jun 12, 2019
0.2.1 Jan 5, 2019
0.1.1 Jan 3, 2019

#200 in Algorithms

Download history 9069/week @ 2022-03-12 7690/week @ 2022-03-19 10374/week @ 2022-03-26 17586/week @ 2022-04-02 18811/week @ 2022-04-09 16612/week @ 2022-04-16 13189/week @ 2022-04-23 11146/week @ 2022-04-30 11489/week @ 2022-05-07 10674/week @ 2022-05-14 10355/week @ 2022-05-21 12220/week @ 2022-05-28 12702/week @ 2022-06-04 15156/week @ 2022-06-11 15811/week @ 2022-06-18 12685/week @ 2022-06-25

58,266 downloads per month
Used in 147 crates (5 directly)


727 lines


crate license documentation minimum rustc 1.26

strength_reduce implements integer division and modulo via "arithmetic strength reduction".

Modern processors can do multiplication and shifts much faster than division, and "arithmetic strength reduction" is an algorithm to transform divisions into multiplications and shifts. ompilers already perform this optimization for divisors that are known at compile time; this library enables this optimization for divisors that are only known at runtime.

Benchmarking shows a 5-10x speedup or integer division and modulo operations.

This library is intended for hot loops like the example below, where a division is repeated many times in a loop with the divisor remaining unchanged. There is a setup cost associated with creating stength-reduced division instances, so using strength-reduced division for 1-2 divisions is not worth the setup cost. The break-even point differs by use-case, but is typically low: Benchmarking has shown that takes 3 to 4 repeated divisions with the same StengthReduced## instance to be worth it.

strength_reduce is #![no_std]

See the API Documentation for more details.


use strength_reduce::StrengthReducedU64;

let mut my_array: Vec<u64> = (0..500).collect();
let divisor = 3;
let modulo = 14;

// slow naive division and modulo
for element in &mut my_array {
    *element = (*element / divisor) % modulo;

// fast strength-reduced division and modulo
let reduced_divisor = StrengthReducedU64::new(divisor);
let reduced_modulo = StrengthReducedU64::new(modulo);
for element in &mut my_array {
    *element = (*element / reduced_divisor) % reduced_modulo;


strength_reduce uses proptest to generate test cases. In addition, the u8 and u16 problem spaces are small enough that we can exhaustively test every possible combination of numerator and divisor. However, the u16 exhaustive test takes several minutes to run, so it is marked #[ignore]. Before submitting pull requests, please test with cargo test -- --ignored at least once.


The strength_reduce crate requires rustc 1.26 or greater.


Licensed under either of

at your option.

No runtime deps