Add defense for builtin_clz/builtin_ctz in case that input is zero

This commit is contained in:
obdev
2021-07-18 15:24:13 +08:00
committed by wangzelin.wzl
parent 3a6db0a542
commit 4770dbcdcb
9 changed files with 14 additions and 8 deletions

View File

@ -79,11 +79,13 @@ private:
inline int calc_clz(const uint32_t s)
{
OB_ASSERT(0 != s);
return __builtin_clz(s);
}
inline int calc_clz(const uint64_t s)
{
OB_ASSERT(0ULL != s);
return __builtin_clzl(s);
}

View File

@ -138,6 +138,7 @@ public:
private:
static uint64_t calc_shift(uint64_t capacity)
{
OB_ASSERT(0ULL != capacity);
return __builtin_clzll(capacity) + 1;
}
HashNode* locate(uint64_t hash)

View File

@ -163,6 +163,7 @@ public:
private:
static uint64_t calc_n_cond(uint64_t capacity)
{
OB_ASSERT(0ULL != capacity);
return std::min(1024ULL, 1ULL << (63 - __builtin_clzll(capacity)));
}
uint64_t push_bounded(void* p, uint64_t limit)

View File

@ -71,7 +71,7 @@ int32_t parse_string_to_int_array(const char* line, const char del, int32_t* arr
bool is2n(int64_t input);
constexpr int64_t next_pow2(const int64_t x)
{
return x ? (1ULL << (8 * sizeof(int64_t) - __builtin_clzll(x - 1))) : 1;
return x > 1LL ? (1ULL << (8 * sizeof(int64_t) - __builtin_clzll(x - 1))) : 1LL;
}
bool all_zero(const char* buffer, const int64_t size);