From 72a2f498344152d3ab1d02e4e0a53a62fd25bc79 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 24 Oct 2014 02:46:59 +0200 Subject: [PATCH] More integer vectorization --- glm/detail/func_integer.hpp | 8 ++-- glm/detail/func_integer.inl | 72 +++++---------------------------- test/core/core_func_integer.cpp | 48 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 65 deletions(-) diff --git a/glm/detail/func_integer.hpp b/glm/detail/func_integer.hpp index ad808700..a3f45c3f 100644 --- a/glm/detail/func_integer.hpp +++ b/glm/detail/func_integer.hpp @@ -123,8 +123,8 @@ namespace glm template class vecType> GLM_FUNC_DECL vecType bitfieldExtract( vecType const & Value, - int const & Offset, - int const & Bits); + int Offset, + int Bits); /// Returns the insertion the bits least-significant bits of insert into base. /// @@ -144,8 +144,8 @@ namespace glm GLM_FUNC_DECL vecType bitfieldInsert( vecType const & Base, vecType const & Insert, - int const & Offset, - int const & Bits); + int Offset, + int Bits); /// Returns the reversal of the bits of value. /// The bit numbered n of the result will be taken from bit (bits - 1) - n of value, diff --git a/glm/detail/func_integer.inl b/glm/detail/func_integer.inl index 9204a876..c53f70d4 100644 --- a/glm/detail/func_integer.inl +++ b/glm/detail/func_integer.inl @@ -135,7 +135,7 @@ namespace glm } template class vecType> - GLM_FUNC_QUALIFIER vecType bitfieldExtract(vecType const & Value, int const & Offset, int const & Bits) + GLM_FUNC_QUALIFIER vecType bitfieldExtract(vecType const & Value, int Offset, int Bits) { GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldExtract' only accept integer inputs"); @@ -153,78 +153,30 @@ namespace glm // bitfieldInsert template - GLM_FUNC_QUALIFIER genIUType bitfieldInsert - ( - genIUType const & Base, - genIUType const & Insert, - int const & Offset, - int const & Bits - ) + GLM_FUNC_QUALIFIER genIUType bitfieldInsert(genIUType const & Base, genIUType const & Insert, int Offset, int Bits) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldInsert' only accept integer values"); - assert(Offset + Bits <= sizeof(genIUType)); + return bitfieldInsert(tvec1(Base), tvec1(Insert), Offset, Bits).x; + } + + template class vecType> + GLM_FUNC_QUALIFIER vecType bitfieldInsert(vecType const & Base, vecType const & Insert, int Offset, int Bits) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldInsert' only accept integer values"); if(Bits == 0) return Base; - genIUType Mask = 0; + vecType Mask(0); for(int Bit = Offset; Bit < Offset + Bits; ++Bit) - Mask |= (1 << Bit); + Mask |= (static_cast(1) << Bit); return (Base & ~Mask) | (Insert & Mask); } - template - GLM_FUNC_QUALIFIER tvec2 bitfieldInsert - ( - tvec2 const & Base, - tvec2 const & Insert, - int const & Offset, - int const & Bits - ) - { - return tvec2( - bitfieldInsert(Base[0], Insert[0], Offset, Bits), - bitfieldInsert(Base[1], Insert[1], Offset, Bits)); - } - - template - GLM_FUNC_QUALIFIER tvec3 bitfieldInsert - ( - tvec3 const & Base, - tvec3 const & Insert, - int const & Offset, - int const & Bits - ) - { - return tvec3( - bitfieldInsert(Base[0], Insert[0], Offset, Bits), - bitfieldInsert(Base[1], Insert[1], Offset, Bits), - bitfieldInsert(Base[2], Insert[2], Offset, Bits)); - } - - template - GLM_FUNC_QUALIFIER tvec4 bitfieldInsert - ( - tvec4 const & Base, - tvec4 const & Insert, - int const & Offset, - int const & Bits - ) - { - return tvec4( - bitfieldInsert(Base[0], Insert[0], Offset, Bits), - bitfieldInsert(Base[1], Insert[1], Offset, Bits), - bitfieldInsert(Base[2], Insert[2], Offset, Bits), - bitfieldInsert(Base[3], Insert[3], Offset, Bits)); - } - // bitfieldReverse template GLM_FUNC_QUALIFIER T bitfieldReverse(T v) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitfieldReverse' only accept integer values"); - return bitfieldReverse(tvec1(v)).x; } @@ -249,8 +201,6 @@ namespace glm template GLM_FUNC_QUALIFIER int bitCount(genIUType x) { - GLM_STATIC_ASSERT(std::numeric_limits::is_integer, "'bitCount' only accept integer values"); - return bitCount(tvec1(x)).x; } diff --git a/test/core/core_func_integer.cpp b/test/core/core_func_integer.cpp index 764e5341..fedcbd92 100644 --- a/test/core/core_func_integer.cpp +++ b/test/core/core_func_integer.cpp @@ -19,6 +19,53 @@ enum result STATIC_ASSERT }; +namespace bitfieldInsert +{ + template + struct type + { + genType Base; + genType Insert; + sizeType Offset; + sizeType Bits; + genType Return; + result Result; + }; + + typedef type typeU32; + + typeU32 const Data32[] = + { + {0xffffffff, 8,24, 0xffffff00, SUCCESS}, + }; + + int test() + { + glm::uint count = sizeof(Data32) / sizeof(typeU32); + + for(glm::uint i = 0; i < count; ++i) + { + glm::uint Return = glm::bitfieldInsert( + Data32[i].Base, + Data32[i].Insert, + Data32[i].Offset, + Data32[i].Bits); + + bool Compare = Data32[i].Return == Return; + + if(Data32[i].Result == SUCCESS && Compare) + continue; + else if(Data32[i].Result == FAIL && !Compare) + continue; + + std::cout << "glm::bitfieldInsert test fail on test " << i << std::endl; + return 1; + } + + return 0; + } +}//bitfieldInsert + namespace bitfieldExtract { template @@ -480,6 +527,7 @@ int main() Error += ::imulExtended::test(); Error += ::uaddCarry::test(); Error += ::usubBorrow::test(); + Error += ::bitfieldInsert::test(); Error += ::bitfieldExtract::test(); Error += ::bitfieldReverse::test(); Error += ::findMSB::test();