From 8d4fb26ad4eed40831261f0a37065f944c9dc04d Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 15 Nov 2016 14:23:54 -0800 Subject: [PATCH] Various fixes to the ilog2 functions Fix several bugs in the ilog2 functions. Signed-off-by: H. Peter Anvin --- configure.ac | 6 +++--- nasmlib/ilog2.c | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 21624950..fc03822f 100644 --- a/configure.ac +++ b/configure.ac @@ -109,9 +109,9 @@ AC_CHECK_FUNCS(sysconf) AC_CHECK_FUNCS([access _access faccessat]) -PA_HAVE_FUNC(__builtin_ctz, (0U)) -PA_HAVE_FUNC(__builtin_ctzl, (0UL)) -PA_HAVE_FUNC(__builtin_ctzll, (0ULL)) +PA_HAVE_FUNC(__builtin_clz, (0U)) +PA_HAVE_FUNC(__builtin_clzl, (0UL)) +PA_HAVE_FUNC(__builtin_clzll, (0ULL)) PA_HAVE_FUNC(__builtin_expect, (1,1)) dnl Functions for which we have replacements available in lib/ diff --git a/nasmlib/ilog2.c b/nasmlib/ilog2.c index f250262d..51c3e07a 100644 --- a/nasmlib/ilog2.c +++ b/nasmlib/ilog2.c @@ -33,6 +33,7 @@ #include "compiler.h" #include "nasmlib.h" +#include #define ROUND(v, a, w) \ do { \ @@ -61,14 +62,20 @@ int ilog2_32(uint32_t v) { int n; +#ifdef __i686__ + __asm__("bsrl %1,%0 ; cmovz %2,%0\n" + : "=&r" (n) + : "rm" (v), "r" (0)); +#else __asm__("bsrl %1,%0 ; jnz 1f ; xorl %0,%0\n" "1:" : "=&r" (n) : "rm" (v)); - return n; +#endif + return n; } -#elif defined(HAVE___BUILTIN_CTZ) && INT_MAX == 2147483647 +#elif defined(HAVE___BUILTIN_CLZ) && INT_MAX == 2147483647 int ilog2_32(uint32_t v) { @@ -107,7 +114,7 @@ int ilog2_64(uint64_t v) return n; } -#elif defined(HAVE__BUILTIN_CTZLL) && LLONG_MAX == 9223372036854775807LL +#elif defined(HAVE__BUILTIN_CLZLL) && LLONG_MAX == 9223372036854775807LL int ilog2_64(uint64_t v) {