From 66dfd956daf9f346b429868dc3a14e84eab9b9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Wed, 20 May 2026 20:33:10 +0200 Subject: [PATCH] Fix 64 bit variants. Note undefined behavior --- include/ucore/math.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/ucore/math.h b/include/ucore/math.h index 858af09..73e6172 100644 --- a/include/ucore/math.h +++ b/include/ucore/math.h @@ -25,6 +25,10 @@ uc_phi_64(uint64_t N); # 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 static inline uint32_t @@ -39,7 +43,7 @@ static inline uint64_t uc_nextpow2_64(uint64_t x) { if (x <= 1) return 1; - return 1u << (64 - __builtin_clzll(x - 1)); + return (uint64_t)1 << (64 - __builtin_clzll(x - 1)); return x; } #else @@ -61,7 +65,7 @@ uc_nextpow2(uint32_t x) } static inline uint64_t -uc_nextpow2(uint64_t x) +uc_nextpow2_64(uint64_t x) { if (x == 0) { return 1;