Re:How do compilers optimize divisions?
by curun1r on Monday February 20, 2017 @08:12PM
(#13692441)
Those are very convenient numbers for fizz buzz. Have you ever pulled out that trick in an interview?
[ Parent
| on HN ]
Re:How do compilers optimize divisions?
by contravariant on Tuesday February 21, 2017 @09:41AM
(#13695798)
Hmm, I wonder if this is more efficient than calculating mod 15 directly. It's basically something like
gcd15(int x) {
return ((x * 0xaaaaaaab) < (0xfffffff/3 + 1) ? 1 : 3)
* ((x * 0xaaaaaaab) < (0xfffffff/5 + 1) ? 1 : 5)
}
versus something like gcd15(int x) {
LOOKUP_TABLE = { /* gcd(n,15) for n = 0,...,15 */ }
int q = (x * ((1 << 28) / 15)) >> 28;
return LOOKUP_TABLE[x - 15 * q];
}
Well, you're probably better of using the method in the article for the division, but this should work for small numbers.