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] 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