From dfa50c37d951128b1e77167dd9291081aa88eea4 Mon Sep 17 00:00:00 2001 From: Daan Date: Fri, 13 Jun 2025 21:45:06 -0700 Subject: [PATCH] nicefy mi_popcount --- include/mimalloc/bits.h | 2 +- src/libc.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/mimalloc/bits.h b/include/mimalloc/bits.h index e3038d11..1d4063bb 100644 --- a/include/mimalloc/bits.h +++ b/include/mimalloc/bits.h @@ -209,7 +209,7 @@ static inline size_t mi_popcount(size_t x) { return mi_builtinz(popcount)(x); #else #define MI_HAS_FAST_POPCOUNT 0 - return (x<=1 ? x : _mi_popcount_generic(x)); + return _mi_popcount_generic(x); #endif } diff --git a/src/libc.c b/src/libc.c index a54eec5b..b40d1204 100644 --- a/src/libc.c +++ b/src/libc.c @@ -383,6 +383,8 @@ static size_t mi_popcount_generic32(uint32_t x) { } mi_decl_noinline size_t _mi_popcount_generic(size_t x) { + if (x<=1) return x; + if (~x==0) return MI_SIZE_BITS; return mi_popcount_generic32(x); } @@ -407,6 +409,8 @@ static size_t mi_popcount_generic64(uint64_t x) { } mi_decl_noinline size_t _mi_popcount_generic(size_t x) { + if (x<=1) return x; + if (~x==0) return MI_SIZE_BITS; return mi_popcount_generic64(x); } #endif