From 54f1d398d9a8e91d64aaa739c666292f3ff3f867 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Sep 2023 11:07:20 +0900 Subject: Make popcount bit-masks stricter Each bit run is upto the right shift count, so the each mask does not need more upper bits. --- internal/bits.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'internal') diff --git a/internal/bits.h b/internal/bits.h index 538e6c11ae..1fe98fa430 100644 --- a/internal/bits.h +++ b/internal/bits.h @@ -398,9 +398,9 @@ rb_popcount32(uint32_t x) #else x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); - x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f); - x = (x & 0x001f001f) + (x >> 8 & 0x001f001f); - x = (x & 0x0000003f) + (x >>16 & 0x0000003f); + x = (x & 0x07070707) + (x >> 4 & 0x07070707); + x = (x & 0x000f000f) + (x >> 8 & 0x000f000f); + x = (x & 0x0000001f) + (x >>16 & 0x0000001f); return (unsigned int)x; #endif @@ -428,9 +428,9 @@ rb_popcount64(uint64_t x) x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707); - x = (x & 0x001f001f001f001f) + (x >> 8 & 0x001f001f001f001f); - x = (x & 0x0000003f0000003f) + (x >>16 & 0x0000003f0000003f); - x = (x & 0x000000000000007f) + (x >>32 & 0x000000000000007f); + x = (x & 0x000f000f000f000f) + (x >> 8 & 0x000f000f000f000f); + x = (x & 0x0000001f0000001f) + (x >>16 & 0x0000001f0000001f); + x = (x & 0x000000000000003f) + (x >>32 & 0x000000000000003f); return (unsigned int)x; #endif -- cgit v1.2.3