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 <hpa@linux.intel.com>
This commit is contained in:
H. Peter Anvin 2018-06-14 19:20:08 -07:00
parent 48fd154d1c
commit 9401f81416

View File

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