Faster remainders when the divisor is a constant: beating compilers and libdivide
Daniel Lemire
FEBRUARY 8, 2019
Of course, if d is not a power of two, 2 N / d cannot be represented as an integer. I believe that all optimizing C/C++ compilers know how to pull this trick and it is generally beneficial irrespective of the processor’s architecture. uint64_t lowbits = c * n ; return ( ( __uint128_t ) lowbits * d ) > > 64 ; }.
Let's personalize your content