Skip to content

Instantly share code, notes, and snippets.

@huwan
Last active August 29, 2015 14:01
Show Gist options
  • Save huwan/b4cecc6c44803a4eaa66 to your computer and use it in GitHub Desktop.
Save huwan/b4cecc6c44803a4eaa66 to your computer and use it in GitHub Desktop.
cmath
/*!
* @brief Checks if n is a power of 2.
* @returns true if n is power of 2
*/
static inline bool IsPower2(UINT32 n)
{
return ((n & (n - 1)) == 0);
}
/*!
* @brief Computes floor(log2(n))
* Works by finding position of MSB set.
* @returns -1 if n == 0.
*/
static inline INT32 FloorLog2(UINT32 n)
{
INT32 p = 0;
if (n == 0) return -1;
if (n & 0xffff0000) { p += 16; n >>= 16; }
if (n & 0x0000ff00) { p += 8; n >>= 8; }
if (n & 0x000000f0) { p += 4; n >>= 4; }
if (n & 0x0000000c) { p += 2; n >>= 2; }
if (n & 0x00000002) { p += 1; }
return p;
}
/*!
* @brief Computes floor(log2(n))
* Works by finding position of MSB set.
* @returns -1 if n == 0.
*/
static inline INT32 CeilLog2(UINT32 n)
{
return FloorLog2(n - 1) + 1;
}