diff --git a/glm/detail/func_integer.inl b/glm/detail/func_integer.inl index d6e94e99..1c05a09b 100644 --- a/glm/detail/func_integer.inl +++ b/glm/detail/func_integer.inl @@ -44,7 +44,7 @@ namespace detail { GLM_FUNC_QUALIFIER int mask(int Bits) { - return Bits >= 32 ? 0xffffffff : (static_cast(1) << Bits) - static_cast(1); + return ~((~0) << Bits); } }//namespace detail diff --git a/glm/gtc/bitfield.hpp b/glm/gtc/bitfield.hpp index f3c94cfd..21da04e5 100644 --- a/glm/gtc/bitfield.hpp +++ b/glm/gtc/bitfield.hpp @@ -57,13 +57,14 @@ namespace glm /// Build a mask of 'count' bits /// /// @see gtc_bitfield - GLM_FUNC_DECL int mask(int Bits); + template + GLM_FUNC_DECL genType mask(genType Bits); /// Build a mask of 'count' bits /// /// @see gtc_bitfield - template class vecType> - GLM_FUNC_DECL vecType mask(vecType const & v); + template class vecType> + GLM_FUNC_DECL vecType mask(vecType const & v); /// Rotate all bits to the right. All the bits dropped in the right side are inserted back on the left side. /// diff --git a/glm/gtc/bitfield.inl b/glm/gtc/bitfield.inl index b67ef0ca..051a5067 100644 --- a/glm/gtc/bitfield.inl +++ b/glm/gtc/bitfield.inl @@ -107,23 +107,23 @@ namespace detail glm::uint32 REG1(x); glm::uint32 REG2(y); glm::uint32 REG3(z); - + REG1 = ((REG1 << 16) | REG1) & glm::uint32(0x00FF0000FF0000FF); REG2 = ((REG2 << 16) | REG2) & glm::uint32(0x00FF0000FF0000FF); REG3 = ((REG3 << 16) | REG3) & glm::uint32(0x00FF0000FF0000FF); - + REG1 = ((REG1 << 8) | REG1) & glm::uint32(0xF00F00F00F00F00F); REG2 = ((REG2 << 8) | REG2) & glm::uint32(0xF00F00F00F00F00F); REG3 = ((REG3 << 8) | REG3) & glm::uint32(0xF00F00F00F00F00F); - + REG1 = ((REG1 << 4) | REG1) & glm::uint32(0x30C30C30C30C30C3); REG2 = ((REG2 << 4) | REG2) & glm::uint32(0x30C30C30C30C30C3); REG3 = ((REG3 << 4) | REG3) & glm::uint32(0x30C30C30C30C30C3); - + REG1 = ((REG1 << 2) | REG1) & glm::uint32(0x9249249249249249); REG2 = ((REG2 << 2) | REG2) & glm::uint32(0x9249249249249249); REG3 = ((REG3 << 2) | REG3) & glm::uint32(0x9249249249249249); - + return REG1 | (REG2 << 1) | (REG3 << 2); } @@ -133,30 +133,30 @@ namespace detail glm::uint64 REG1(x); glm::uint64 REG2(y); glm::uint64 REG3(z); - + REG1 = ((REG1 << 32) | REG1) & glm::uint64(0xFFFF00000000FFFF); REG2 = ((REG2 << 32) | REG2) & glm::uint64(0xFFFF00000000FFFF); REG3 = ((REG3 << 32) | REG3) & glm::uint64(0xFFFF00000000FFFF); - + REG1 = ((REG1 << 16) | REG1) & glm::uint64(0x00FF0000FF0000FF); REG2 = ((REG2 << 16) | REG2) & glm::uint64(0x00FF0000FF0000FF); REG3 = ((REG3 << 16) | REG3) & glm::uint64(0x00FF0000FF0000FF); - + REG1 = ((REG1 << 8) | REG1) & glm::uint64(0xF00F00F00F00F00F); REG2 = ((REG2 << 8) | REG2) & glm::uint64(0xF00F00F00F00F00F); REG3 = ((REG3 << 8) | REG3) & glm::uint64(0xF00F00F00F00F00F); - + REG1 = ((REG1 << 4) | REG1) & glm::uint64(0x30C30C30C30C30C3); REG2 = ((REG2 << 4) | REG2) & glm::uint64(0x30C30C30C30C30C3); REG3 = ((REG3 << 4) | REG3) & glm::uint64(0x30C30C30C30C30C3); - + REG1 = ((REG1 << 2) | REG1) & glm::uint64(0x9249249249249249); REG2 = ((REG2 << 2) | REG2) & glm::uint64(0x9249249249249249); REG3 = ((REG3 << 2) | REG3) & glm::uint64(0x9249249249249249); - + return REG1 | (REG2 << 1) | (REG3 << 2); } - + template <> GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint32 x, glm::uint32 y, glm::uint32 z) { @@ -194,25 +194,25 @@ namespace detail glm::uint32 REG2(y); glm::uint32 REG3(z); glm::uint32 REG4(w); - + REG1 = ((REG1 << 12) | REG1) & glm::uint32(0x000F000F000F000F); REG2 = ((REG2 << 12) | REG2) & glm::uint32(0x000F000F000F000F); REG3 = ((REG3 << 12) | REG3) & glm::uint32(0x000F000F000F000F); REG4 = ((REG4 << 12) | REG4) & glm::uint32(0x000F000F000F000F); - + REG1 = ((REG1 << 6) | REG1) & glm::uint32(0x0303030303030303); REG2 = ((REG2 << 6) | REG2) & glm::uint32(0x0303030303030303); REG3 = ((REG3 << 6) | REG3) & glm::uint32(0x0303030303030303); REG4 = ((REG4 << 6) | REG4) & glm::uint32(0x0303030303030303); - + REG1 = ((REG1 << 3) | REG1) & glm::uint32(0x1111111111111111); REG2 = ((REG2 << 3) | REG2) & glm::uint32(0x1111111111111111); REG3 = ((REG3 << 3) | REG3) & glm::uint32(0x1111111111111111); REG4 = ((REG4 << 3) | REG4) & glm::uint32(0x1111111111111111); - + return REG1 | (REG2 << 1) | (REG3 << 2) | (REG4 << 3); } - + template <> GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint16 x, glm::uint16 y, glm::uint16 z, glm::uint16 w) { @@ -245,21 +245,26 @@ namespace detail } }//namespace detail - GLM_FUNC_QUALIFIER int mask(int Bits) + template + GLM_FUNC_QUALIFIER genType mask(genType Bits) { - return Bits >= sizeof(Bits) * 8 ? ~static_cast(0) : (static_cast(1) << Bits) - static_cast(1); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'mask' accepts only integer values"); + + return ~((~static_cast(0)) << Bits); } - template class vecType> - GLM_FUNC_QUALIFIER vecType mask(vecType const & v) + template class vecType> + GLM_FUNC_QUALIFIER vecType mask(vecType const & v) { - return detail::functor1::call(mask, v); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'mask' accepts only integer values"); + + return ~((~static_cast(0)) << v); } template GLM_FUNC_QUALIFIER genIType bitfieldRotateRight(genIType In, int Shift) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateRight' only accept integer values"); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateRight' accepts only integer values"); int const BitSize = static_cast(sizeof(genIType) * 8); return (In << static_cast(Shift)) | (In >> static_cast(BitSize - Shift)); @@ -268,7 +273,7 @@ namespace detail template class vecType> GLM_FUNC_QUALIFIER vecType bitfieldRotateRight(vecType const & In, int Shift) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateRight' only accept integer values"); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateRight' accepts only integer values"); int const BitSize = static_cast(sizeof(T) * 8); return (In << static_cast(Shift)) | (In >> static_cast(BitSize - Shift)); @@ -277,7 +282,7 @@ namespace detail template GLM_FUNC_QUALIFIER genIType bitfieldRotateLeft(genIType In, int Shift) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateLeft' only accept integer values"); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateLeft' accepts only integer values"); int const BitSize = static_cast(sizeof(genIType) * 8); return (In >> static_cast(Shift)) | (In << static_cast(BitSize - Shift)); @@ -286,7 +291,7 @@ namespace detail template class vecType> GLM_FUNC_QUALIFIER vecType bitfieldRotateLeft(vecType const & In, int Shift) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateLeft' only accept integer values"); + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldRotateLeft' accepts only integer values"); int const BitSize = static_cast(sizeof(T) * 8); return (In >> static_cast(Shift)) | (In << static_cast(BitSize - Shift)); diff --git a/test/gtc/gtc_bitfield.cpp b/test/gtc/gtc_bitfield.cpp index aa9c54c1..1801b17d 100644 --- a/test/gtc/gtc_bitfield.cpp +++ b/test/gtc/gtc_bitfield.cpp @@ -9,6 +9,7 @@ #include #include +#include //#include #include #include @@ -16,6 +17,18 @@ namespace mask { + template + struct type + { + genType Value; + genType Return; + }; + + inline int mask_zero(int Bits) + { + return ~((~0) << Bits); + } + inline int mask_mix(int Bits) { return Bits >= 32 ? 0xffffffff : (static_cast(1) << Bits) - static_cast(1); @@ -62,16 +75,97 @@ namespace mask std::clock_t Timestamp4 = std::clock(); + { + std::vector 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 TimeLoop = Timestamp3 - Timestamp2; std::clock_t TimeDefault = Timestamp4 - Timestamp3; + std::clock_t TimeZero = Timestamp5 - Timestamp4; printf("mask[mix]: %d\n", TimeMix); printf("mask[loop]: %d\n", TimeLoop); printf("mask[default]: %d\n", TimeDefault); + printf("mask[zero]: %d\n", TimeZero); return TimeDefault < TimeLoop ? 0 : 1; } + + int test_uint() + { + type const Data[] = + { + {0, 0x00000000}, + {1, 0x00000001}, + {2, 0x00000003}, + {3, 0x00000007} + }; + + int Error(0); + + for(std::size_t i = 0; i < sizeof(Data) / sizeof(type); ++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); ++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); ++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); ++i) + { + int Result = glm::mask(Data[i].Value); + Error += Data[i].Return == Result ? 0 : 1; + } + + return Error; + } + + int test_uvec4() + { + type 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); 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 @@ -502,11 +596,13 @@ int main() { int Error(0); - Error += ::mask::perf(); + Error += ::mask::test(); Error += ::bitfieldInterleave3::test(); Error += ::bitfieldInterleave4::test(); Error += ::bitfieldInterleave::test(); //Error += ::bitRevert::test(); + Error += ::mask::perf(); + return Error; }