Recently on Reddit there was a link to a page which listed a lot of clever “bit twiddling” hacks. I decided to try the hack for computing abs() against the standard library function and the naive implementation:
/* Clever version */
static inline int32_t
abs_1(int32_t v)
{
const int32_t m = v >> sizeof(int32_t) * 8 - 1;
return (v ^ m) - m;
}
/* Naive version */
static inline int32_t
abs_2(int32_t v)
{
return v < 0 ? -v : v;
}
Compiling with gcc -O3 produces exactly the same code for all three versions!
c1 fa 1f sar $0x1f,%edx 31 d0 xor %edx,%eax 29 d0 sub %edx,%eax
If I also specify -march=athlon-xp to optimize for my processor, the sar instruction is replaced with cltd (also known as cdq).
99 cltd 31 d0 xor %edx,%eax 29 d0 sub %edx,%eax
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.
Comment by sandrar — 10 September 2009 @ 14:53