Friday, July 14, 2006

x mod m

Trying to optimize a bit my "delay" program in the lab I've run into a interesting thing: on computers x mod m can be very fast, if m is a power of 2. In fact x mod 8, is the same of doing x AND 7. So C programs can be optimized a bit using this macro instead of % operator.

#define mod1(x,m) ({int res; __asm__ __volatile__ ("and %2,%0;" :"=r"(x) : "0"(x), "r" (m)); res})

No comments: