[linux-audio-dev] next power of two

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: [linux-audio-dev] next power of two
From: Maarten de Boer (mdeboer_AT_iua.upf.es)
Date: Wed Sep 17 2003 - 15:21:32 EEST


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&file=index

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;
         }


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Wed Sep 17 2003 - 15:38:53 EEST