4 ms·
If modulus is expensive, and he's checking a power-of-2, why not just use a bitwise AND. Eg, for positive integers, x % 16 == x & 15. That should be trivially
by drewg123 2y ago
If modulus is expensive, and he's checking a power-of-2, why not just use a bitwise AND.
Eg, for positive integers, x % 16 == x & 15. That should be trivially cheap.
- ladberg 2y agoIt wasn't `x % 16` it was `x % y` where x and y are 16-bit integers. A compiler would also have taken care of it if it were just a literal.
- drewg123 2y agoWhoops.. I misread what he was doing.
- xgkickt 2y agoReading the comments, that's kinda what they did, though they had to learn that first and only now realize they only needed one.
- kevin_thibedeau 2y agoAny top-tier C compiler will optimize modular division by a constant into a more efficient operation(s). It is better to keep the intent of the code clear rather than devolve it into increasingly obtuse bit-twiddling tricks the compiler can figure out on its own.