From da530ac46ed55f897475fe15d4bf76615afb8db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Saraj=C3=A4rvi?= Date: Wed, 12 Sep 2012 01:30:12 +0300 Subject: [PATCH 1/3] Fix integer version of glm::log2 for GCC This pretty much reverts the fix done in commit 1ed0e3865b5de0ee993b064b37749bc85e928d7f This temporarily breaks log2 for GCC in cases where GLM_FORCE_PURE is not defined. The workaround introduced in commit 1ed0e3865b5de0ee993b064b37749bc85e928d7f seems to rely on getting invalid results from the nlz function. Broken nlz is caused by a broken findMSB function for GCC. A fix for the findMSB function should be available in a nearby separate commit. --- glm/gtx/integer.inl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/glm/gtx/integer.inl b/glm/gtx/integer.inl index 2f2535b4..14e6afa9 100644 --- a/glm/gtx/integer.inl +++ b/glm/gtx/integer.inl @@ -60,10 +60,8 @@ namespace _detail template GLM_FUNC_QUALIFIER T operator() (T const & Value) const { -#if(GLM_COMPILER & GLM_COMPILER_VC) +#if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC)) return Value <= T(1) ? T(0) : T(32) - nlz(Value - T(1)); -#elif(GLM_COMPILER & GLM_COMPILER_GCC) - return Value <= T(1) ? T(0) : nlz(Value - T(1)) + 1; #else return T(32) - nlz(Value - T(1)); #endif From daa51e42bba127e54383f5f72c086e005de8aa47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Saraj=C3=A4rvi?= Date: Wed, 12 Sep 2012 01:44:34 +0300 Subject: [PATCH 2/3] Fix glm::findMSB for GCC >= 4.0 Before this fix, the GCC specific MSB function returned the number of leading zero bits in the parameter value. With this change, the number of trailing zero bits is returned instead. I am not entirely sure if this fix is correct, because I could not find a clear reference about what findMSB in GLSL is really supposed to return with some concrete input value. At least the result is now consistent with the GLM_ARCH_PURE implementation of glm::findMSB. --- glm/core/func_integer.inl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/glm/core/func_integer.inl b/glm/core/func_integer.inl index 5de9fcbf..07a748ba 100644 --- a/glm/core/func_integer.inl +++ b/glm/core/func_integer.inl @@ -543,7 +543,11 @@ namespace glm genIUType const & Value ) { - return __builtin_clz(Value); + /** + * ctz returns the number or trailing 0-bits; see + * http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Other-Builtins.html + */ + return __builtin_ctz(Value); } #else From ad3422f6aa0622ee3e4d99892f8e02ec859de3d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Saraj=C3=A4rvi?= Date: Wed, 12 Sep 2012 15:17:21 +0300 Subject: [PATCH 3/3] Better fix glm::findMSB for GCC >= 4.0 The previous fix only worked correctly for values where the most significant enabled bit was the only enabled bit. This change changes the implementation back to using clz, but so that the result is changed with additional arithmetics. There is still at least one known limitation with regards to acceptable input types, but this is documented in the code. --- glm/core/func_integer.inl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/glm/core/func_integer.inl b/glm/core/func_integer.inl index 07a748ba..62d5b920 100644 --- a/glm/core/func_integer.inl +++ b/glm/core/func_integer.inl @@ -544,10 +544,13 @@ namespace glm ) { /** - * ctz returns the number or trailing 0-bits; see + * clz returns the number or trailing 0-bits; see * http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Other-Builtins.html + * + * NoteBecause __builtin_clz only works for unsigned ints, this + * implementation will not work for 64-bit integers. */ - return __builtin_ctz(Value); + return 31 - __builtin_clz(Value); } #else