Hi,
A colleague of mine found this very clever algoritm to calculate the
next power of two for a given number. It comes from the Prophecy SDK for
3d game development
http://www.twilight3d.com/modules.php?op=modload&name=Downloads&fil…
Since this is a kind of thing often needed in audio processing, I wanted
to share it with you. I certainly can not think of a faster (or more
elegant) way of doing it.
Maarten
//////
/// Returns the closest power-of-two number greater or equal
/// to n for the given (unsigned) integer n.
/// Will return 0 when n = 0 and 1 when n = 1.
//////
inline uint32_t nextPowerOfTwo(uint32_t n)
{
--n;
n |= n >> 16;
n |= n >> 8;
n |= n >> 4;
n |= n >> 2;
n |= n >> 1;
++n;
return n;
}