Fix 64 bit variants. Note undefined behavior

This commit is contained in:
Nils O. Selåsdal
2026-05-20 20:33:10 +02:00
parent c7d175d90f
commit 66dfd956da
+6 -2
View File
@@ -25,6 +25,10 @@ uc_phi_64(uint64_t N);
# endif # endif
#endif #endif
// Note: the nextpow2 functions have undefined behavior if
// x > 2^(n-1) for n of the 32 or 64 bit variants
#if HAVE_BUILTIN_CLZ #if HAVE_BUILTIN_CLZ
static inline uint32_t static inline uint32_t
@@ -39,7 +43,7 @@ static inline uint64_t
uc_nextpow2_64(uint64_t x) uc_nextpow2_64(uint64_t x)
{ {
if (x <= 1) return 1; if (x <= 1) return 1;
return 1u << (64 - __builtin_clzll(x - 1)); return (uint64_t)1 << (64 - __builtin_clzll(x - 1));
return x; return x;
} }
#else #else
@@ -61,7 +65,7 @@ uc_nextpow2(uint32_t x)
} }
static inline uint64_t static inline uint64_t
uc_nextpow2(uint64_t x) uc_nextpow2_64(uint64_t x)
{ {
if (x == 0) { if (x == 0) {
return 1; return 1;