bit_util: Implement BitCount portably
This commit is contained in:
parent
6ec651498d
commit
e8764c129f
1 changed files with 5 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue