Add (x != 0) assertion to lg_floor(x).

lg_floor(0) is undefined, but depending on compiler options may not
cause a crash.  This assertion makes it harder to accidentally abuse
lg_floor().
This commit is contained in:
Jason Evans 2015-02-04 16:41:55 -08:00
parent f500a10b2e
commit c810fcea1f

View File

@ -170,6 +170,8 @@ lg_floor(size_t x)
{ {
size_t ret; size_t ret;
assert(x != 0);
asm ("bsr %1, %0" asm ("bsr %1, %0"
: "=r"(ret) // Outputs. : "=r"(ret) // Outputs.
: "r"(x) // Inputs. : "r"(x) // Inputs.
@ -182,6 +184,8 @@ lg_floor(size_t x)
{ {
unsigned long ret; unsigned long ret;
assert(x != 0);
#if (LG_SIZEOF_PTR == 3) #if (LG_SIZEOF_PTR == 3)
_BitScanReverse64(&ret, x); _BitScanReverse64(&ret, x);
#elif (LG_SIZEOF_PTR == 2) #elif (LG_SIZEOF_PTR == 2)
@ -196,6 +200,8 @@ JEMALLOC_INLINE size_t
lg_floor(size_t x) lg_floor(size_t x)
{ {
assert(x != 0);
#if (LG_SIZEOF_PTR == LG_SIZEOF_INT) #if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x)); return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x));
#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG) #elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
@ -209,6 +215,8 @@ JEMALLOC_INLINE size_t
lg_floor(size_t x) lg_floor(size_t x)
{ {
assert(x != 0);
x |= (x >> 1); x |= (x >> 1);
x |= (x >> 2); x |= (x >> 2);
x |= (x >> 4); x |= (x >> 4);