From cc3e7e71aa020cb243cb24d06f7577a9fd8170fe Mon Sep 17 00:00:00 2001 From: MerryMage Date: Fri, 2 Sep 2016 21:57:50 +0100 Subject: [PATCH] bit_util: std::bitset-based BitCount implementation Suggestion by @lioncash. --- src/common/bit_util.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 28430063..3e9c1750 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include "common/assert.h" @@ -71,12 +72,9 @@ inline T SignExtend(const T value) { return value; } -inline size_t BitCount(u32 value) { - // Portable SWAR algorithm for population count - value = value - ((value >> 1) & 0x55555555); // Two-bit count - value = (value & 0x33333333) + ((value >> 2) & 0x33333333); // Nybble count - value = (value + (value >> 4)) & 0x0F0F0F0F; // Byte count - return ((value * 0x01010101) >> 24) & 0xFF; // Summate the byte counts +template +inline size_t BitCount(Integral value) { + return std::bitset()>(value).count(); } } // namespace Common