Fix count_bits implementation.

GitOrigin-RevId: a6b56a2f9dbef462107787c852bacee6571df2c9
This commit is contained in:
levlam 2020-03-31 23:21:42 +03:00
parent d2b441f812
commit c7411004be

View File

@ -127,7 +127,11 @@ inline uint64 bswap64(uint64 x) {
}
inline int32 count_bits32(uint32 x) {
return __popcnt(x);
x -= (x >> 1) & 0x55555555;
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
x += x >> 8;
return (x + (x >> 16)) & 0x3F;
}
inline int32 count_bits64(uint64 x) {
@ -197,11 +201,15 @@ inline uint64 bswap64(uint64 x) {
}
inline int32 count_bits32(uint32 x) {
return _popcnt32(static_cast<int>(x));
x -= (x >> 1) & 0x55555555;
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
x += x >> 8;
return (x + (x >> 16)) & 0x3F;
}
inline int32 count_bits64(uint64 x) {
return _popcnt64(static_cast<__int64>(x));
return count_bits32(static_cast<uint32>(x >> 32)) + count_bits32(static_cast<uint32>(x));
}
#else