From 9d9e3996a38a49970a42cb9fafa8f9b4d79cb3c0 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 6 Feb 2015 21:28:47 +0100 Subject: [PATCH] Moved utility functions to header. --- library/PolyVoxCore/CMakeLists.txt | 2 +- .../include/PolyVoxCore/Impl/Utility.h | 48 +++++++++++-- library/PolyVoxCore/source/Impl/Utility.cpp | 72 ------------------- 3 files changed, 42 insertions(+), 80 deletions(-) delete mode 100644 library/PolyVoxCore/source/Impl/Utility.cpp diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index e024eb4f..d81b093a 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -88,7 +88,7 @@ SET(IMPL_SRC_FILES #source/Impl/RandomUnitVectors.cpp #source/Impl/RandomVectors.cpp #source/Impl/Timer.cpp - source/Impl/Utility.cpp + #source/Impl/Utility.cpp ) SET(IMPL_INC_FILES diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h index ff9a6e7b..e5ba354c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h @@ -30,14 +30,48 @@ freely, subject to the following restrictions: namespace PolyVox { - POLYVOX_API uint8_t logBase2(uint32_t uInput); - POLYVOX_API bool isPowerOf2(uint32_t uInput); + inline bool isPowerOf2(uint32_t uInput) + { + if (uInput == 0) + return false; + else + return ((uInput & (uInput - 1)) == 0); + } - int32_t roundTowardsNegInf(float r); - int32_t roundToInteger(float r); - template - Type clamp(const Type& value, const Type& low, const Type& high); - uint32_t upperPowerOfTwo(uint32_t v); + //Note: this function only works for inputs which are a power of two and not zero + //If this is not the case then the output is undefined. + inline uint8_t logBase2(uint32_t uInput) + { + //Release mode validation + if (uInput == 0) + { + POLYVOX_THROW(std::invalid_argument, "Cannot compute the log of zero."); + } + if (!isPowerOf2(uInput)) + { + POLYVOX_THROW(std::invalid_argument, "Input must be a power of two in order to compute the log."); + } + + uint32_t uResult = 0; + while ((uInput >> uResult) != 0) + { + ++uResult; + } + return static_cast(uResult - 1); + } + + // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + inline 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; + } inline int32_t roundTowardsNegInf(float r) { diff --git a/library/PolyVoxCore/source/Impl/Utility.cpp b/library/PolyVoxCore/source/Impl/Utility.cpp deleted file mode 100644 index 83be3b0a..00000000 --- a/library/PolyVoxCore/source/Impl/Utility.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "PolyVoxCore/Impl/ErrorHandling.h" -#include "PolyVoxCore/Impl/Utility.h" - -namespace PolyVox -{ - //Note: this function only works for inputs which are a power of two and not zero - //If this is not the case then the output is undefined. - uint8_t logBase2(uint32_t uInput) - { - //Release mode validation - if(uInput == 0) - { - POLYVOX_THROW(std::invalid_argument, "Cannot compute the log of zero."); - } - if(!isPowerOf2(uInput)) - { - POLYVOX_THROW(std::invalid_argument, "Input must be a power of two in order to compute the log."); - } - - uint32_t uResult = 0; - while( (uInput >> uResult) != 0) - { - ++uResult; - } - return static_cast(uResult-1); - } - - - bool isPowerOf2(uint32_t uInput) - { - if(uInput == 0) - return false; - 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; - } -}