back

by raphlinus·9y ago·view on hn ↗
Duh, you're right on both counts. It should work for any odd divisor, I was just computing the inverse in a dumb way when I tried out other divisors. And I should have stated u32 wrapping arithmetic.
1 comments
And for even divisors you can separate the divisor into a power of two, and an odd part. The odd part is checked as before, and the power of two is checked with an and mask.
An even trickier way to do this is to rotate right by the power of two factor between doing the multiply and the compare.
That wouldn't work, because a number that is bigger than the threshold could become smaller than it after the rotation.