Merge branch 'develop' into feature/cubiquity-version

Conflicts:
	library/PolyVoxCore/source/Impl/Utility.cpp
This commit is contained in:
Daviw Williams 2014-02-25 16:55:52 +01:00
commit 60396a2699

View File

@ -1,72 +1,72 @@
/******************************************************************************* /*******************************************************************************
Copyright (c) 2005-2009 David Williams Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
arising from the use of this software. arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions: freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not 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 claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software. misrepresented as being the original software.
3. This notice may not be removed or altered from any source 3. This notice may not be removed or altered from any source
distribution. distribution.
*******************************************************************************/ *******************************************************************************/
#include "PolyVoxCore/Impl/ErrorHandling.h" #include "PolyVoxCore/Impl/ErrorHandling.h"
#include "PolyVoxCore/Impl/Utility.h" #include "PolyVoxCore/Impl/Utility.h"
namespace PolyVox namespace PolyVox
{ {
//Note: this function only works for inputs which are a power of two and not zero //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. //If this is not the case then the output is undefined.
uint8_t logBase2(uint32_t uInput) uint8_t logBase2(uint32_t uInput)
{ {
//Release mode validation //Release mode validation
if(uInput == 0) if(uInput == 0)
{ {
POLYVOX_THROW(std::invalid_argument, "Cannot compute the log of zero."); POLYVOX_THROW(std::invalid_argument, "Cannot compute the log of zero.");
} }
if(!isPowerOf2(uInput)) if(!isPowerOf2(uInput))
{ {
POLYVOX_THROW(std::invalid_argument, "Input must be a power of two in order to compute the log."); POLYVOX_THROW(std::invalid_argument, "Input must be a power of two in order to compute the log.");
} }
uint32_t uResult = 0; uint32_t uResult = 0;
while( (uInput >> uResult) != 0) while( (uInput >> uResult) != 0)
{ {
++uResult; ++uResult;
} }
return static_cast<uint8_t>(uResult-1); return static_cast<uint8_t>(uResult-1);
} }
bool isPowerOf2(uint32_t uInput) bool isPowerOf2(uint32_t uInput)
{ {
if(uInput == 0) if(uInput == 0)
return false; return false;
else else
return ((uInput & (uInput-1)) == 0); return ((uInput & (uInput-1)) == 0);
} }
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
uint32_t upperPowerOfTwo(uint32_t v) uint32_t upperPowerOfTwo(uint32_t v)
{ {
v--; v--;
v |= v >> 1; v |= v >> 1;
v |= v >> 2; v |= v >> 2;
v |= v >> 4; v |= v >> 4;
v |= v >> 8; v |= v >> 8;
v |= v >> 16; v |= v >> 16;
v++; v++;
return v; return v;
} }
} }