Fixed roundPowerOfTwo and floorPowerOfTwo #503

This commit is contained in:
Christophe Riccio 2016-04-29 10:51:21 +02:00
parent 9770c9f73d
commit 76d12fb602
2 changed files with 33 additions and 4 deletions

View File

@ -30,8 +30,9 @@
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
namespace glm
{
#include "../detail/func_integer.hpp"
namespace glm{
namespace detail
{
template <typename T, precision P, template <typename, precision> class vecType, bool compute = false>
@ -275,7 +276,7 @@ namespace detail
template <typename genType>
GLM_FUNC_QUALIFIER genType floorPowerOfTwo(genType value)
{
return isPowerOfTwo(value) ? value : highestBitValue(value);
return isPowerOfTwo(value) ? value : findMSB(value);
}
template <typename T, precision P, template <typename, precision> class vecType>
@ -293,7 +294,7 @@ namespace detail
if(isPowerOfTwo(value))
return value;
genIUType const prev = highestBitValue(value);
genIUType const prev = findMSB(value);
genIUType const next = prev << 1;
return (next - value) < (value - prev) ? next : prev;
}

View File

@ -294,6 +294,32 @@ namespace ceilPowerOfTwo
}
}//namespace ceilPowerOfTwo
namespace roundPowerOfTwo
{
int test()
{
int Error = 0;
glm::uint32 const A = glm::roundPowerOfTwo(7u);
Error += A == 8u ? 0 : 1;
return Error;
}
}//namespace roundPowerOfTwo
namespace floorPowerOfTwo
{
int test()
{
int Error = 0;
glm::uint32 const A = glm::floorPowerOfTwo(7u);
Error += A == 4u ? 0 : 1;
return Error;
}
}//namespace floorPowerOfTwo
namespace floorMultiple
{
template <typename genType>
@ -380,6 +406,8 @@ int main()
Error += isPowerOfTwo::test();
Error += ceilPowerOfTwo::test();
Error += floorPowerOfTwo::test();
Error += roundPowerOfTwo::test();
# ifdef NDEBUG
Error += ceilPowerOfTwo::perf();