News for hackers. Stuff that matters.
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.

There are no comments on this comment yet.