From d0c8fc16b3b79b82099855a67e148a29e605331e Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Tue, 21 Jan 2020 10:45:37 -0500 Subject: [PATCH] use a better popcnt fallback algorithm This one loops once for every set bit, rather than once for each bit position to the right of the highest set bit. https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation --- c/blake3_impl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c/blake3_impl.h b/c/blake3_impl.h index 576ccf4..d345246 100644 --- a/c/blake3_impl.h +++ b/c/blake3_impl.h @@ -50,8 +50,8 @@ INLINE uint8_t popcnt(uint64_t x) { #else uint8_t count = 0; while (x > 0) { - count += ((uint8_t)x) & 1; - x >>= 1; + count += 1; + x &= x - 1; } return count; #endif