Small improvements to logBase2() and isPowerOfTwo() functions.

This commit is contained in:
David Williams 2008-06-12 18:54:09 +00:00
parent e1e8e2c8cc
commit 3566fc1863

View File

@ -32,28 +32,20 @@ namespace PolyVox
assert(uInput != 0); assert(uInput != 0);
assert(isPowerOf2(uInput)); assert(isPowerOf2(uInput));
boost::uint8_t result = 0; boost::uint32_t uResult = 0;
uInput = uInput >> 1; while( (uInput >> uResult) != 0)
while(uInput)
{ {
uInput = uInput >> 1; ++uResult;
++result;
} }
return result; return static_cast<boost::uint8_t>(uResult-1);
} }
bool isPowerOf2(boost::uint32_t uInput) bool isPowerOf2(boost::uint32_t uInput)
{ {
boost::uint8_t uNonZeroBits = 0; if(uInput == 0)
boost::uint32_t uMask = 0x00000001; return false;
for(boost::uint8_t ct = 0; ct < 32; ++ct) else
{ return ((uInput & (uInput-1)) == 0);
if(uInput & uMask)
{
++uNonZeroBits;
}
uMask = uMask << 1;
}
return uNonZeroBits == 1 ? true : false;
} }
} }