From 0c51a72bfe020f250750474afb38cf55d49de87e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 23 May 2025 23:10:45 +0900 Subject: [PATCH] string.c (popcount): support when sizeof(int) == 4 This function is used when the compiler does not support builtin popcount operation (namely VC++). --- src/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index df7363d3d..7837d2e3a 100644 --- a/src/string.c +++ b/src/string.c @@ -407,12 +407,13 @@ mrb_utf8len(const char* p, const char* e) # define popcount(x) __builtin_popcountl(x) # endif #else +#define POPC_SHIFT (8 * sizeof(bitint) - 8) static inline uint32_t popcount(bitint x) { x = (x & (MASK01*0x55)) + ((x >> 1) & (MASK01*0x55)); x = (x & (MASK01*0x33)) + ((x >> 2) & (MASK01*0x33)); x = (x & (MASK01*0x0F)) + ((x >> 4) & (MASK01*0x0F)); - return (x * MASK01) >> 56; + return (uint32_t)((x * MASK01) >> POPC_SHIFT); } #endif