Fix nextpow2 , provide optimized and 64 bit variants
This commit is contained in:
@@ -19,9 +19,36 @@ uc_phi_32(uint32_t N);
|
|||||||
uint64_t
|
uint64_t
|
||||||
uc_phi_64(uint64_t N);
|
uc_phi_64(uint64_t N);
|
||||||
|
|
||||||
|
#if defined(__has_builtin)
|
||||||
|
# if __has_builtin(__builtin_clzll)
|
||||||
|
# define HAVE_BUILTIN_CLZ 1
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAVE_BUILTIN_CLZ
|
||||||
|
|
||||||
static inline uint32_t
|
static inline uint32_t
|
||||||
uc_nextpow2(uint32_t x)
|
uc_nextpow2(uint32_t x)
|
||||||
{
|
{
|
||||||
|
if (x <= 1) return 1;
|
||||||
|
return 1u << (32 - __builtin_clz(x - 1));
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
uc_nextpow2_64(uint64_t x)
|
||||||
|
{
|
||||||
|
if (x <= 1) return 1;
|
||||||
|
return 1u << (64 - __builtin_clzll(x - 1));
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static inline uint32_t
|
||||||
|
uc_nextpow2(uint32_t x)
|
||||||
|
{
|
||||||
|
if (x == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
x--;
|
x--;
|
||||||
x |= x >> 1;
|
x |= x >> 1;
|
||||||
x |= x >> 2;
|
x |= x >> 2;
|
||||||
@@ -33,6 +60,26 @@ uc_nextpow2(uint32_t x)
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
uc_nextpow2(uint64_t x)
|
||||||
|
{
|
||||||
|
if (x == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
x--;
|
||||||
|
x |= x >> 1;
|
||||||
|
x |= x >> 2;
|
||||||
|
x |= x >> 4;
|
||||||
|
x |= x >> 8;
|
||||||
|
x |= x >> 16;
|
||||||
|
x |= x >> 32;
|
||||||
|
x++;
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef HAVE_BUILTIN_CLZ
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user