From bd62a91d03034f9f1600b6994b7f3cff2f76bc7d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 28 Apr 2023 12:01:21 -0600 Subject: [PATCH] [algs] 64bit popcount --- src/hb-algs.hh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 13587eac0..faf467c76 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -618,16 +618,18 @@ hb_popcount (T v) if (sizeof (T) <= 4) { /* "HACKMEM 169" */ - uint32_t y; - y = (v >> 1) &033333333333; + uint32_t y = (uint32_t) v; + y = (y >> 1) &033333333333; y = v - y - ((y >>1) & 033333333333); return (((y + (y >> 3)) & 030707070707) % 077); } if (sizeof (T) == 8) { - unsigned int shift = 32; - return hb_popcount ((uint32_t) v) + hb_popcount ((uint32_t) (v >> shift)); + uint64_t y = (uint64_t) v; + y -= ((y >> 1) & 0x5555555555555555ull); + y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull); + return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56; } if (sizeof (T) == 16)