bit_util: Implement BitCount portably

This commit is contained in:
MerryMage 2016-09-02 19:05:49 +01:00
parent 6ec651498d
commit e8764c129f

View file

@ -72,11 +72,11 @@ inline T SignExtend(const T value) {
}
inline size_t BitCount(u32 value) {
#ifdef _MSC_VER
return __popcnt(value);
#else
return __builtin_popcount(value);
#endif
// 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
}
} // namespace Common