diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h index 77dac7a4..5b2e327c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h @@ -37,6 +37,7 @@ namespace PolyVox int32_t roundToInteger(float r); template Type clamp(const Type& value, const Type& low, const Type& high); + uint32_t upperPowerOfTwo(uint32_t v); inline int32_t roundTowardsNegInf(float r) { diff --git a/library/PolyVoxCore/source/Impl/Utility.cpp b/library/PolyVoxCore/source/Impl/Utility.cpp index c82c9630..bce4cd98 100644 --- a/library/PolyVoxCore/source/Impl/Utility.cpp +++ b/library/PolyVoxCore/source/Impl/Utility.cpp @@ -60,4 +60,17 @@ namespace PolyVox else return ((uInput & (uInput-1)) == 0); } + + // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + uint32_t upperPowerOfTwo(uint32_t v) + { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; + } }