From c7411004be8453ed2221579b79114a03c7ca8cfc Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 31 Mar 2020 23:21:42 +0300 Subject: [PATCH] Fix count_bits implementation. GitOrigin-RevId: a6b56a2f9dbef462107787c852bacee6571df2c9 --- tdutils/td/utils/bits.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tdutils/td/utils/bits.h b/tdutils/td/utils/bits.h index 25c26864..70ebde63 100644 --- a/tdutils/td/utils/bits.h +++ b/tdutils/td/utils/bits.h @@ -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(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(x >> 32)) + count_bits32(static_cast(x)); } #else