|Constant-time exponent of 2 detector|
References : Posted by Brent Lehman (mailbjl[AT]yahoo.com)
In your common FFT program, you want to make sure that the frame you're working with has a size that is a power of 2. This tells you in just a few operations. Granted, you won't be using this algorithm inside a loop, so the savings aren't that great, but every little hack helps ;)
// Quit if size isn't a power of 2
if ((-size ^ size) & size) return;
// If size is an unsigned int, the above might not compile.
// You'd want to use this instead:
if (((~size + 1) ^ size) & size) return;
Added on : 11/02/02 by functorx[ AT ]yahoo[ DOT ]com
I think I prefer:
if (! (size & (size - 1))) return;
I'm not positive this is fewer instructions than the above, but I think it's easier to see why it works (n and n-1 will share bits unless n is a power of two), and it doesn't require two's-complement.
- Tom 7
|Add your own comment|
Comments are displayed in fixed width, no HTML code allowed!