Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.

Commit 8cfce6f

Browse files
author
Lior Amram
committed
ilog2 64bit
1 parent 97e3b5e commit 8cfce6f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/ilog2.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,22 @@ static inline uint32_t next_p2(uint32_t x) {
3838
return 2U << ilog2(x-1);
3939
}
4040

41+
static inline uint64_t ilog2_64(uint64_t x) {
42+
#if defined(__i386__) || defined(__x86_64__)
43+
uint64_t res;
44+
asm ("bsr %[x], %[res]"
45+
: [res] "=r" (res)
46+
: [x] "mr" (x));
47+
return res;
48+
#else
49+
if (!x)
50+
return 0;
51+
return __builtin_clz(x) ^ 63;
52+
#endif
53+
}
54+
55+
static inline uint64_t next_p2_64(uint64_t x) {
56+
return 2ULL << ilog2_64(x-1);
57+
}
58+
4159
#endif // _ILOG2__H_

0 commit comments

Comments
 (0)