Skip to content

Commit cfb3c3a

Browse files
committed
Add prefix and mention pitfall value
1 parent 84ea32f commit cfb3c3a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/common.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@
3131

3232
#if defined(_MSC_VER)
3333
#include <intrin.h>
34-
static inline int clz(uint32_t v)
34+
static inline int rv_clz(uint32_t v)
3535
{
3636
uint32_t leading_zero = 0;
3737
if (_BitScanReverse(&leading_zero, v))
3838
return 31 - leading_zero;
3939
return 32; /* undefined behavior */
4040
}
4141
#elif defined(__GNUC__) || defined(__clang__)
42-
static inline int clz(uint32_t v)
42+
static inline int rv_clz(uint32_t v)
4343
{
44+
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
45+
/* If x is 0, the result is undefined. */
4446
return __builtin_clz(v);
4547
}
4648
#else /* generic implementation */
47-
static inline int clz(uint32_t v)
49+
static inline int rv_clz(uint32_t v)
4850
{
4951
/* http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn */
5052
static const uint8_t mul_debruijn[] = {
@@ -64,10 +66,14 @@ static inline int clz(uint32_t v)
6466

6567
/*
6668
* Integer log base 2
69+
*
70+
* The input x must not be zero.
71+
* Otherwise, the result is undefined on some platform.
72+
*
6773
*/
6874
static inline uint8_t ilog2(uint32_t x)
6975
{
70-
return 31 - clz(x);
76+
return 31 - rv_clz(x);
7177
}
7278

7379
/* Alignment macro */

0 commit comments

Comments
 (0)