diff --git a/glm/gtx/bit.hpp b/glm/gtx/bit.hpp index d6e0a633..850b032b 100644 --- a/glm/gtx/bit.hpp +++ b/glm/gtx/bit.hpp @@ -38,8 +38,11 @@ namespace glm //! Component wise extraction of bit fields. //! genType and genIType could be a scalar or a vector. //! From GLM_GTX_bit extension. - template - genIType extractField(genType const & v, genIType const & first, genIType const & count); + template + genIUType extractField( + genIUType const & v, + sizeType const & first, + sizeType const & count); //! Find the lowest bit set to 1 in a integer variable. //! From GLM_GTX_bit extension. diff --git a/glm/gtx/bit.inl b/glm/gtx/bit.inl index f9eb8fb1..5d8a2faa 100644 --- a/glm/gtx/bit.inl +++ b/glm/gtx/bit.inl @@ -92,141 +92,147 @@ inline genIType extractField ) { assert(first + count < sizeof(double)); - return (detail::uif64(value).i << first) >> ((sizeof(double) << 3) - count); + return (detail::uif64(value).i << first) >> ((sizeof(double) << genIType(3)) - count); } -template -inline genIType extractField +template +inline genIUType extractField ( - genType const & value, - genIType const & first, - genIType const & count + genIUType const & Value, + sizeType const & First, + sizeType const & Count ) { - assert(first + count < sizeof(genType)); - return (value << first) >> ((sizeof(genType) << 3) - count); + sizeType GenSize = sizeof(genIUType) << 3; + + assert(First + Count <= GenSize); + + genIUType ShiftLeft = Count ? Value << (GenSize - (Count + First)) : 0; + genIUType ShiftBack = ShiftLeft >> genIUType(GenSize - Count); + + return ShiftBack; } -template -inline detail::tvec2 extractField +template +inline detail::tvec2 extractField ( - detail::tvec2 const & value, - valIType const & first, - valIType const & count + detail::tvec2 const & value, + sizeType const & first, + sizeType const & count ) { - return detail::tvec2( + return detail::tvec2( extractField(value[0], first, count), extractField(value[1], first, count)); } -template -inline detail::tvec3 extractField +template +inline detail::tvec3 extractField ( - detail::tvec3 const & value, - valIType const & first, - valIType const & count + detail::tvec3 const & value, + sizeType const & first, + sizeType const & count ) { - return detail::tvec3( + return detail::tvec3( extractField(value[0], first, count), extractField(value[1], first, count), extractField(value[2], first, count)); } -template -inline detail::tvec4 extractField +template +inline detail::tvec4 extractField ( - detail::tvec4 const & value, - valIType const & first, - valIType const & count + detail::tvec4 const & value, + sizeType const & first, + sizeType const & count ) { - return detail::tvec4( + return detail::tvec4( extractField(value[0], first, count), extractField(value[1], first, count), extractField(value[2], first, count), extractField(value[3], first, count)); } -template -inline detail::tvec2 extractField +template +inline detail::tvec2 extractField ( - detail::tvec2 const & value, - detail::tvec2 const & first, - detail::tvec2 const & count + detail::tvec2 const & value, + detail::tvec2 const & first, + detail::tvec2 const & count ) { - return detail::tvec2( + return detail::tvec2( extractField(value[0], first[0], count[0]), extractField(value[1], first[1], count[1])); } -template -inline detail::tvec3 extractField +template +inline detail::tvec3 extractField ( - detail::tvec3 const & value, - detail::tvec3 const & first, - detail::tvec3 const & count + detail::tvec3 const & value, + detail::tvec3 const & first, + detail::tvec3 const & count ) { - return detail::tvec3( + return detail::tvec3( extractField(value[0], first[0], count[0]), extractField(value[1], first[1], count[1]), extractField(value[2], first[2], count[2])); } -template -inline detail::tvec4 extractField +template +inline detail::tvec4 extractField ( - detail::tvec4 const & value, - detail::tvec4 const & first, - detail::tvec4 const & count + detail::tvec4 const & value, + detail::tvec4 const & first, + detail::tvec4 const & count ) { - return detail::tvec4( + return detail::tvec4( extractField(value[0], first[0], count[0]), extractField(value[1], first[1], count[1]), extractField(value[2], first[2], count[2]), extractField(value[3], first[3], count[3])); } -template -inline detail::tvec2 extractField +template +inline detail::tvec2 extractField ( - valType const & value, - detail::tvec2 const & first, - detail::tvec2 const & count + genIUType const & value, + detail::tvec2 const & first, + detail::tvec2 const & count ) { - return detail::tvec2( + return detail::tvec2( extractField(value, first[0], count[0]), extractField(value, first[1], count[1])); } -template -inline detail::tvec3 extractField +template +inline detail::tvec3 extractField ( - valType const & value, - detail::tvec3 const & first, - detail::tvec3 const & count + genIUType const & value, + detail::tvec3 const & first, + detail::tvec3 const & count ) { - return detail::tvec3( + return detail::tvec3( extractField(value, first[0], count[0]), extractField(value, first[1], count[1]), extractField(value, first[2], count[2])); } -template -inline detail::tvec4 extractField +template +inline detail::tvec4 extractField ( - valType const & value, - detail::tvec4 const & first, - detail::tvec4 const & count + genIUType const & value, + detail::tvec4 const & first, + detail::tvec4 const & count ) { - return detail::tvec4( + return detail::tvec4( extractField(value, first[0], count[0]), extractField(value, first[1], count[1]), extractField(value, first[2], count[2]),