From 9401f81416f3d70e66539daf977887447b15ab46 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Thu, 14 Jun 2018 19:20:08 -0700 Subject: [PATCH] ilog2: return unsigned int for applicable routines For the routines where we can't return a negative value, return an unsigned value. Signed-off-by: H. Peter Anvin --- include/ilog2.h | 80 +++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/include/ilog2.h b/include/ilog2.h index 445ba56b..3a828be8 100644 --- a/include/ilog2.h +++ b/include/ilog2.h @@ -44,9 +44,9 @@ #endif #ifdef inline_prototypes -extern int const_func ilog2_32(uint32_t v); -extern int const_func ilog2_64(uint64_t v); -extern int const_func ilog2_64(uint64_t vv); +extern unsigned int const_func ilog2_32(uint32_t v); +extern unsigned int const_func ilog2_64(uint64_t v); +extern unsigned int const_func ilog2_64(uint64_t vv); extern int const_func alignlog2_32(uint32_t v); extern int const_func alignlog2_64(uint64_t v); #endif @@ -62,11 +62,21 @@ extern int const_func alignlog2_64(uint64_t v); } while (0) -#if defined(__GNUC__) && defined(__x86_64__) +#if defined(HAVE___BUILTIN_CLZ) && INT_MAX == 2147483647 -extern_inline int const_func ilog2_32(uint32_t v) +extern_inline unsigned int const_func ilog2_32(uint32_t v) { - int n; + if (!v) + return 0; + + return __builtin_clz(v) ^ 31; +} + +#elif defined(__GNUC__) && defined(__x86_64__) + +extern_inline unsigned int const_func ilog2_32(uint32_t v) +{ + unsigned int n; __asm__("bsrl %1,%0" : "=r" (n) @@ -76,9 +86,9 @@ extern_inline int const_func ilog2_32(uint32_t v) #elif defined(__GNUC__) && defined(__i386__) -extern_inline int const_func ilog2_32(uint32_t v) +extern_inline unsigned int const_func ilog2_32(uint32_t v) { - int n; + unsigned int n; #ifdef __i686__ __asm__("bsrl %1,%0 ; cmovz %2,%0\n" @@ -93,19 +103,9 @@ extern_inline int const_func ilog2_32(uint32_t v) return n; } -#elif defined(HAVE___BUILTIN_CLZ) && INT_MAX == 2147483647 - -extern_inline int const_func ilog2_32(uint32_t v) -{ - if (!v) - return 0; - - return __builtin_clz(v) ^ 31; -} - #elif defined(HAVE__BITSCANREVERSE) -extern_inline int const_func ilog2_32(uint32_t v) +extern_inline unsigned int const_func ilog2_32(uint32_t v) { unsigned long ix; return _BitScanReverse(&ix, v) ? v : 0; @@ -113,9 +113,9 @@ extern_inline int const_func ilog2_32(uint32_t v) #else -extern_inline int const_func ilog2_32(uint32_t v) +extern_inline unsigned int const_func ilog2_32(uint32_t v) { - int p = 0; + unsigned int p = 0; ROUND(v, p, 16); ROUND(v, p, 8); @@ -128,9 +128,19 @@ extern_inline int const_func ilog2_32(uint32_t v) #endif -#if defined(__GNUC__) && defined(__x86_64__) +#if defined(HAVE__BUILTIN_CLZLL) && LLONG_MAX == 9223372036854775807LL -extern_inline int const_func ilog2_64(uint64_t v) +extern_inline unsigned int const_func ilog2_64(uint64_t v) +{ + if (!v) + return 0; + + return __builtin_clzll(v) ^ 63; +} + +#elif defined(__GNUC__) && defined(__x86_64__) + +extern_inline unsigned int const_func ilog2_64(uint64_t v) { uint64_t n; @@ -140,19 +150,9 @@ extern_inline int const_func ilog2_64(uint64_t v) return n; } -#elif defined(HAVE__BUILTIN_CLZLL) && LLONG_MAX == 9223372036854775807LL - -extern_inline int const_func ilog2_64(uint64_t v) -{ - if (!v) - return 0; - - return __builtin_clzll(v) ^ 63; -} - #elif defined(HAVE__BITSCANREVERSE64) -extern_inline int const_func ilog2_64(uint64_t v) +extern_inline unsigned int const_func ilog2_64(uint64_t v) { unsigned long ix; return _BitScanReverse64(&ix, v) ? ix : 0; @@ -160,9 +160,9 @@ extern_inline int const_func ilog2_64(uint64_t v) #else -extern_inline int const_func ilog2_64(uint64_t vv) +extern_inline unsigned int const_func ilog2_64(uint64_t vv) { - int p = 0; + unsigned int p = 0; uint32_t v; v = vv >> 32; @@ -171,13 +171,7 @@ extern_inline int const_func ilog2_64(uint64_t vv) else v = vv; - ROUND(v, p, 16); - ROUND(v, p, 8); - ROUND(v, p, 4); - ROUND(v, p, 2); - ROUND(v, p, 1); - - return p; + return p + ilog2_32(v); } #endif