Updated GTC_integer

This commit is contained in:
Christophe Riccio
2014-10-26 20:34:10 +01:00
parent d3b368b65c
commit 097c1f7b90
3 changed files with 198 additions and 28 deletions

View File

@@ -41,6 +41,7 @@
// Dependencies
#include "../detail/setup.hpp"
#include "../detail/precision.hpp"
#include "../vector_relational.hpp"
#include <limits>
#if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
@@ -55,8 +56,8 @@ namespace glm
/// Return true if the value is a power of two number.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL bool isPowerOfTwo(genType Value);
template <typename genIUType>
GLM_FUNC_DECL bool isPowerOfTwo(genIUType Value);
/// Return true if the value is a power of two number.
///
@@ -64,29 +65,53 @@ namespace glm
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<bool, P> isPowerOfTwo(vecType<T, P> const & value);
/// Find the highest bit set to 1 in a integer variable and return its value.
/// Find the highest bit set to 1 in a integer variable and return its value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType highestBitValue(genType const & value);
template <typename genIUType>
GLM_FUNC_DECL genIUType highestBitValue(genIUType Value);
/// Find the highest bit set to 1 in a integer variable and return its value.
///
/// @see gtc_integer
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<T, P> highestBitValue(vecType<T, P> const & value);
/// Return the power of two number which value is just higher the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoAbove(genType const & value);
template <typename genIUType>
GLM_FUNC_DECL genIUType powerOfTwoAbove(genIUType Value);
/// Return the power of two number which value is just lower the input value.
/// Return the power of two number which value is just higher the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoBelow(genType const & value);
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<T, P> powerOfTwoAbove(vecType<T, P> const & value);
/// Return the power of two number which value is the closet to the input value.
/// Return the power of two number which value is just lower the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoNearest(genType const & value);
template <typename genIUType>
GLM_FUNC_DECL genIUType powerOfTwoBelow(genIUType Value);
/// Return the power of two number which value is just lower the input value.
///
/// @see gtc_integer
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<T, P> powerOfTwoBelow(vecType<T, P> const & value);
/// Return the power of two number which value is the closet to the input value.
///
/// @see gtc_integer
template <typename genIUType>
GLM_FUNC_DECL genIUType powerOfTwoNearest(genIUType Value);
/// Return the power of two number which value is the closet to the input value.
///
/// @see gtc_integer
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<T, P> powerOfTwoNearest(vecType<T, P> const & value);
/// @}
} //namespace glm

View File

@@ -39,9 +39,81 @@ namespace glm
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<bool, P> isPowerOfTwo(vecType<T, P> const & value)
GLM_FUNC_QUALIFIER vecType<bool, P> isPowerOfTwo(vecType<T, P> const & Value)
{
genType Result = glm::abs(Value);
return !(Result & (Result - 1));
vecType<T, P> Result(abs(Value));
return equal(Result & (Result - 1), vecType<T, P>(0));
}
///////////////////
// highestBitValue
template <typename genIUType>
GLM_FUNC_QUALIFIER genIUType highestBitValue(genIUType Value)
{
genIUType tmp = Value;
genIUType result = genIUType(0);
while(tmp)
{
result = (tmp & (~tmp + 1)); // grab lowest bit
tmp &= ~result; // clear lowest bit
}
return result;
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> highestBitValue(vecType<T, P> const & v)
{
return detail::functor1<T, T, P, vecType>::call(highestBitValue, v);
}
///////////////////
// powerOfTwoAbove
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoAbove(genType value)
{
return isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> powerOfTwoAbove(vecType<T, P> const & v)
{
return detail::functor1<T, T, P, vecType>::call(powerOfTwoAbove, v);
}
///////////////////
// powerOfTwoBelow
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoBelow(genType value)
{
return isPowerOfTwo(value) ? value : highestBitValue(value);
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> powerOfTwoBelow(vecType<T, P> const & v)
{
return detail::functor1<T, T, P, vecType>::call(powerOfTwoBelow, v);
}
/////////////////////
// powerOfTwoNearest
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoNearest(genType value)
{
if(isPowerOfTwo(value))
return value;
genType const prev = highestBitValue(value);
genType const next = prev << 1;
return (next - value) < (value - prev) ? next : prev;
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> powerOfTwoNearest(vecType<T, P> const & v)
{
return detail::functor1<T, T, P, vecType>::call(powerOfTwoNearest, v);
}
}//namespace glm