mirror of
https://github.com/g-truc/glm.git
synced 2024-11-10 12:41:54 +00:00
Updated GTC_integer
This commit is contained in:
parent
d3b368b65c
commit
097c1f7b90
@ -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
|
||||
|
@ -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
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include <glm/gtc/integer.hpp>
|
||||
#include <glm/gtc/type_precision.hpp>
|
||||
#include <glm/gtc/vec1.hpp>
|
||||
|
||||
namespace isPowerOfTwo
|
||||
{
|
||||
@ -19,9 +20,55 @@ namespace isPowerOfTwo
|
||||
bool Return;
|
||||
};
|
||||
|
||||
int test_int()
|
||||
int test_int16()
|
||||
{
|
||||
type<int> const DataI32[] =
|
||||
type<glm::int16> const Data[] =
|
||||
{
|
||||
{0x0001, true},
|
||||
{0x0002, true},
|
||||
{0x0004, true},
|
||||
{0x0080, true},
|
||||
{0x0000, true},
|
||||
{0x0003, false}
|
||||
};
|
||||
|
||||
int Error(0);
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int16>); i < n; ++i)
|
||||
{
|
||||
bool Result = glm::isPowerOfTwo(Data[i].Value);
|
||||
Error += Data[i].Return == Result ? 0 : 1;
|
||||
}
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
||||
int test_uint16()
|
||||
{
|
||||
type<glm::uint16> const Data[] =
|
||||
{
|
||||
{0x0001, true},
|
||||
{0x0002, true},
|
||||
{0x0004, true},
|
||||
{0x0000, true},
|
||||
{0x0000, true},
|
||||
{0x0003, false}
|
||||
};
|
||||
|
||||
int Error(0);
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint16>); i < n; ++i)
|
||||
{
|
||||
bool Result = glm::isPowerOfTwo(Data[i].Value);
|
||||
Error += Data[i].Return == Result ? 0 : 1;
|
||||
}
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
||||
int test_int32()
|
||||
{
|
||||
type<int> const Data[] =
|
||||
{
|
||||
{0x00000001, true},
|
||||
{0x00000002, true},
|
||||
@ -33,18 +80,42 @@ namespace isPowerOfTwo
|
||||
|
||||
int Error(0);
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
|
||||
{
|
||||
bool Result = glm::isPowerOfTwo(DataI32[i].Value);
|
||||
Error += DataI32[i].Return == Result ? 0 : 1;
|
||||
bool Result = glm::isPowerOfTwo(Data[i].Value);
|
||||
Error += Data[i].Return == Result ? 0 : 1;
|
||||
}
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
|
||||
{
|
||||
glm::bvec1 Result = glm::isPowerOfTwo(glm::ivec1(Data[i].Value));
|
||||
Error += glm::all(glm::equal(glm::bvec1(Data[i].Return), Result)) ? 0 : 1;
|
||||
}
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
|
||||
{
|
||||
glm::bvec2 Result = glm::isPowerOfTwo(glm::ivec2(Data[i].Value));
|
||||
Error += glm::all(glm::equal(glm::bvec2(Data[i].Return), Result)) ? 0 : 1;
|
||||
}
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
|
||||
{
|
||||
glm::bvec3 Result = glm::isPowerOfTwo(glm::ivec3(Data[i].Value));
|
||||
Error += glm::all(glm::equal(glm::bvec3(Data[i].Return), Result)) ? 0 : 1;
|
||||
}
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
|
||||
{
|
||||
glm::bvec4 Result = glm::isPowerOfTwo(glm::ivec4(Data[i].Value));
|
||||
Error += glm::all(glm::equal(glm::bvec4(Data[i].Return), Result)) ? 0 : 1;
|
||||
}
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
||||
int test_uint()
|
||||
int test_uint32()
|
||||
{
|
||||
type<glm::uint> const DataU32[] =
|
||||
type<glm::uint> const Data[] =
|
||||
{
|
||||
{0x00000001, true},
|
||||
{0x00000002, true},
|
||||
@ -56,10 +127,10 @@ namespace isPowerOfTwo
|
||||
|
||||
int Error(0);
|
||||
|
||||
for(std::size_t i = 0, n = sizeof(DataU32) / sizeof(type<glm::uint>); i < n; ++i)
|
||||
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint>); i < n; ++i)
|
||||
{
|
||||
bool Result = glm::isPowerOfTwo(DataU32[i].Value);
|
||||
Error += DataU32[i].Return == Result ? 0 : 1;
|
||||
bool Result = glm::isPowerOfTwo(Data[i].Value);
|
||||
Error += Data[i].Return == Result ? 0 : 1;
|
||||
}
|
||||
|
||||
return Error;
|
||||
@ -69,8 +140,10 @@ namespace isPowerOfTwo
|
||||
{
|
||||
int Error(0);
|
||||
|
||||
Error += test_int();
|
||||
Error += test_uint();
|
||||
Error += test_int16();
|
||||
Error += test_uint16();
|
||||
Error += test_int32();
|
||||
Error += test_uint32();
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user