Improve mask implementation to support any integer and vector types

This commit is contained in:
Christophe Riccio 2014-10-28 20:28:26 +01:00
parent a88d8935be
commit 44382faf59
4 changed files with 133 additions and 31 deletions

View File

@ -44,7 +44,7 @@ namespace detail
{ {
GLM_FUNC_QUALIFIER int mask(int Bits) GLM_FUNC_QUALIFIER int mask(int Bits)
{ {
return Bits >= 32 ? 0xffffffff : (static_cast<int>(1) << Bits) - static_cast<int>(1); return ~((~0) << Bits);
} }
}//namespace detail }//namespace detail

View File

@ -57,13 +57,14 @@ namespace glm
/// Build a mask of 'count' bits /// Build a mask of 'count' bits
/// ///
/// @see gtc_bitfield /// @see gtc_bitfield
GLM_FUNC_DECL int mask(int Bits); template <typename genType>
GLM_FUNC_DECL genType mask(genType Bits);
/// Build a mask of 'count' bits /// Build a mask of 'count' bits
/// ///
/// @see gtc_bitfield /// @see gtc_bitfield
template <precision P, template <typename, precision> class vecType> template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<int, P> mask(vecType<int, P> const & v); GLM_FUNC_DECL vecType<T, P> mask(vecType<T, P> const & v);
/// Rotate all bits to the right. All the bits dropped in the right side are inserted back on the left side. /// Rotate all bits to the right. All the bits dropped in the right side are inserted back on the left side.
/// ///

View File

@ -245,21 +245,26 @@ namespace detail
} }
}//namespace detail }//namespace detail
GLM_FUNC_QUALIFIER int mask(int Bits) template <typename genType>
GLM_FUNC_QUALIFIER genType mask(genType Bits)
{ {
return Bits >= sizeof(Bits) * 8 ? ~static_cast<int>(0) : (static_cast<int>(1) << Bits) - static_cast<int>(1); GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'mask' accepts only integer values");
return ~((~static_cast<genType>(0)) << Bits);
} }
template <precision P, template <typename, precision> class vecType> template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<int, P> mask(vecType<int, P> const & v) GLM_FUNC_QUALIFIER vecType<T, P> mask(vecType<T, P> const & v)
{ {
return detail::functor1<int, int, P, vecType>::call(mask, v); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'mask' accepts only integer values");
return ~((~static_cast<T>(0)) << v);
} }
template <typename genIType> template <typename genIType>
GLM_FUNC_QUALIFIER genIType bitfieldRotateRight(genIType In, int Shift) GLM_FUNC_QUALIFIER genIType bitfieldRotateRight(genIType In, int Shift)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateRight' only accept integer values"); GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateRight' accepts only integer values");
int const BitSize = static_cast<genIType>(sizeof(genIType) * 8); int const BitSize = static_cast<genIType>(sizeof(genIType) * 8);
return (In << static_cast<genIType>(Shift)) | (In >> static_cast<genIType>(BitSize - Shift)); return (In << static_cast<genIType>(Shift)) | (In >> static_cast<genIType>(BitSize - Shift));
@ -268,7 +273,7 @@ namespace detail
template <typename T, precision P, template <typename, precision> class vecType> template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> bitfieldRotateRight(vecType<T, P> const & In, int Shift) GLM_FUNC_QUALIFIER vecType<T, P> bitfieldRotateRight(vecType<T, P> const & In, int Shift)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateRight' only accept integer values"); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateRight' accepts only integer values");
int const BitSize = static_cast<int>(sizeof(T) * 8); int const BitSize = static_cast<int>(sizeof(T) * 8);
return (In << static_cast<T>(Shift)) | (In >> static_cast<T>(BitSize - Shift)); return (In << static_cast<T>(Shift)) | (In >> static_cast<T>(BitSize - Shift));
@ -277,7 +282,7 @@ namespace detail
template <typename genIType> template <typename genIType>
GLM_FUNC_QUALIFIER genIType bitfieldRotateLeft(genIType In, int Shift) GLM_FUNC_QUALIFIER genIType bitfieldRotateLeft(genIType In, int Shift)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateLeft' only accept integer values"); GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateLeft' accepts only integer values");
int const BitSize = static_cast<genIType>(sizeof(genIType) * 8); int const BitSize = static_cast<genIType>(sizeof(genIType) * 8);
return (In >> static_cast<genIType>(Shift)) | (In << static_cast<genIType>(BitSize - Shift)); return (In >> static_cast<genIType>(Shift)) | (In << static_cast<genIType>(BitSize - Shift));
@ -286,7 +291,7 @@ namespace detail
template <typename T, precision P, template <typename, precision> class vecType> template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> bitfieldRotateLeft(vecType<T, P> const & In, int Shift) GLM_FUNC_QUALIFIER vecType<T, P> bitfieldRotateLeft(vecType<T, P> const & In, int Shift)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateLeft' only accept integer values"); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateLeft' accepts only integer values");
int const BitSize = static_cast<int>(sizeof(T) * 8); int const BitSize = static_cast<int>(sizeof(T) * 8);
return (In >> static_cast<T>(Shift)) | (In << static_cast<T>(BitSize - Shift)); return (In >> static_cast<T>(Shift)) | (In << static_cast<T>(BitSize - Shift));

View File

@ -9,6 +9,7 @@
#include <glm/gtc/bitfield.hpp> #include <glm/gtc/bitfield.hpp>
#include <glm/gtc/type_precision.hpp> #include <glm/gtc/type_precision.hpp>
#include <glm/vector_relational.hpp>
//#include <glm/vec2.hpp> //#include <glm/vec2.hpp>
#include <ctime> #include <ctime>
#include <cstdio> #include <cstdio>
@ -16,6 +17,18 @@
namespace mask namespace mask
{ {
template <typename genType>
struct type
{
genType Value;
genType Return;
};
inline int mask_zero(int Bits)
{
return ~((~0) << Bits);
}
inline int mask_mix(int Bits) inline int mask_mix(int Bits)
{ {
return Bits >= 32 ? 0xffffffff : (static_cast<int>(1) << Bits) - static_cast<int>(1); return Bits >= 32 ? 0xffffffff : (static_cast<int>(1) << Bits) - static_cast<int>(1);
@ -62,16 +75,97 @@ namespace mask
std::clock_t Timestamp4 = std::clock(); std::clock_t Timestamp4 = std::clock();
{
std::vector<int> Mask;
Mask.resize(Count);
for(int i = 0; i < Count; ++i)
Mask[i] = mask_zero(i % 32);
}
std::clock_t Timestamp5 = std::clock();
std::clock_t TimeMix = Timestamp2 - Timestamp1; std::clock_t TimeMix = Timestamp2 - Timestamp1;
std::clock_t TimeLoop = Timestamp3 - Timestamp2; std::clock_t TimeLoop = Timestamp3 - Timestamp2;
std::clock_t TimeDefault = Timestamp4 - Timestamp3; std::clock_t TimeDefault = Timestamp4 - Timestamp3;
std::clock_t TimeZero = Timestamp5 - Timestamp4;
printf("mask[mix]: %d\n", TimeMix); printf("mask[mix]: %d\n", TimeMix);
printf("mask[loop]: %d\n", TimeLoop); printf("mask[loop]: %d\n", TimeLoop);
printf("mask[default]: %d\n", TimeDefault); printf("mask[default]: %d\n", TimeDefault);
printf("mask[zero]: %d\n", TimeZero);
return TimeDefault < TimeLoop ? 0 : 1; return TimeDefault < TimeLoop ? 0 : 1;
} }
int test_uint()
{
type<glm::uint> const Data[] =
{
{0, 0x00000000},
{1, 0x00000001},
{2, 0x00000003},
{3, 0x00000007}
};
int Error(0);
for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
{
int Result = mask_zero(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
{
int Result = mask_mix(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
{
int Result = mask_loop(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
{
int Result = glm::mask(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
return Error;
}
int test_uvec4()
{
type<glm::ivec4> const Data[] =
{
{glm::ivec4(0), glm::ivec4(0x00000000)},
{glm::ivec4(1), glm::ivec4(0x00000001)},
{glm::ivec4(2), glm::ivec4(0x00000003)},
{glm::ivec4(3), glm::ivec4(0x00000007)}
};
int Error(0);
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::ivec4>); i < n; ++i)
{
glm::ivec4 Result = glm::mask(Data[i].Value);
Error += glm::all(glm::equal(Data[i].Return, Result)) ? 0 : 1;
}
return Error;
}
int test()
{
int Error(0);
Error += test_uint();
Error += test_uvec4();
return Error;
}
}//namespace mask }//namespace mask
@ -502,11 +596,13 @@ int main()
{ {
int Error(0); int Error(0);
Error += ::mask::perf(); Error += ::mask::test();
Error += ::bitfieldInterleave3::test(); Error += ::bitfieldInterleave3::test();
Error += ::bitfieldInterleave4::test(); Error += ::bitfieldInterleave4::test();
Error += ::bitfieldInterleave::test(); Error += ::bitfieldInterleave::test();
//Error += ::bitRevert::test(); //Error += ::bitRevert::test();
Error += ::mask::perf();
return Error; return Error;
} }