"When modulus is static, gcc / clang know that it is private to the current compilation unit, and therefore they can inline the value itself. Then, they turn the expensive div into a much cheaper and – since
mod’ing by a power of two -- is equal to bitwise and of that number minus one!
All you need to do is keep the bits lower than that power of two, which is what the and will do."
Comments
mod’ing by a power of two -- is equal to bitwise and of that number minus one!
All you need to do is keep the bits lower than that power of two, which is what the and will do."