2018-12-19 15:48:39 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-19 15:48:39 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2018-12-19 21:35:13 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#if TD_MSVC
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2019-04-30 20:25:17 +02:00
|
|
|
#ifdef bswap32
|
|
|
|
#undef bswap32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef bswap64
|
|
|
|
#undef bswap64
|
|
|
|
#endif
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
namespace td {
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
int32 count_leading_zeroes32(uint32 x);
|
|
|
|
int32 count_leading_zeroes64(uint64 x);
|
|
|
|
int32 count_trailing_zeroes32(uint32 x);
|
|
|
|
int32 count_trailing_zeroes64(uint64 x);
|
|
|
|
uint32 bswap32(uint32 x);
|
|
|
|
uint64 bswap64(uint64 x);
|
|
|
|
int32 count_bits32(uint32 x);
|
|
|
|
int32 count_bits64(uint64 x);
|
|
|
|
|
|
|
|
inline uint32 bits_negate32(uint32 x) {
|
|
|
|
return ~x + 1;
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
inline uint64 bits_negate64(uint64 x) {
|
|
|
|
return ~x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32 lower_bit32(uint32 x) {
|
|
|
|
return x & bits_negate32(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64 lower_bit64(uint64 x) {
|
|
|
|
return x & bits_negate64(x);
|
|
|
|
}
|
|
|
|
|
2020-06-17 03:11:57 +02:00
|
|
|
inline uint64 host_to_big_endian64(uint64 x) {
|
|
|
|
// NB: works only for little-endian systems
|
|
|
|
return bswap64(x);
|
|
|
|
}
|
|
|
|
inline uint64 big_endian_to_host64(uint64 x) {
|
|
|
|
// NB: works only for little-endian systems
|
2020-06-16 17:34:55 +02:00
|
|
|
return bswap64(x);
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
//TODO: optimize
|
|
|
|
inline int32 count_leading_zeroes_non_zero32(uint32 x) {
|
|
|
|
DCHECK(x != 0);
|
|
|
|
return count_leading_zeroes32(x);
|
|
|
|
}
|
|
|
|
inline int32 count_leading_zeroes_non_zero64(uint64 x) {
|
|
|
|
DCHECK(x != 0);
|
|
|
|
return count_leading_zeroes64(x);
|
|
|
|
}
|
|
|
|
inline int32 count_trailing_zeroes_non_zero32(uint32 x) {
|
|
|
|
DCHECK(x != 0);
|
|
|
|
return count_trailing_zeroes32(x);
|
|
|
|
}
|
|
|
|
inline int32 count_trailing_zeroes_non_zero64(uint64 x) {
|
|
|
|
DCHECK(x != 0);
|
|
|
|
return count_trailing_zeroes64(x);
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
//
|
|
|
|
// Platform specific implementation
|
|
|
|
//
|
2018-12-19 15:48:39 +01:00
|
|
|
#if TD_MSVC
|
2018-12-20 01:30:44 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_leading_zeroes32(uint32 x) {
|
|
|
|
unsigned long res = 0;
|
|
|
|
if (_BitScanReverse(&res, x)) {
|
|
|
|
return 31 - res;
|
|
|
|
}
|
|
|
|
return 32;
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_leading_zeroes64(uint64 x) {
|
|
|
|
#if defined(_M_X64)
|
|
|
|
unsigned long res = 0;
|
|
|
|
if (_BitScanReverse64(&res, x)) {
|
|
|
|
return 63 - res;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
#else
|
|
|
|
if ((x >> 32) == 0) {
|
|
|
|
return count_leading_zeroes32(static_cast<uint32>(x)) + 32;
|
|
|
|
} else {
|
|
|
|
return count_leading_zeroes32(static_cast<uint32>(x >> 32));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_trailing_zeroes32(uint32 x) {
|
|
|
|
unsigned long res = 0;
|
|
|
|
if (_BitScanForward(&res, x)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return 32;
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_trailing_zeroes64(uint64 x) {
|
|
|
|
#if defined(_M_X64)
|
|
|
|
unsigned long res = 0;
|
|
|
|
if (_BitScanForward64(&res, x)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
#else
|
|
|
|
if (static_cast<uint32>(x) == 0) {
|
|
|
|
return count_trailing_zeroes32(static_cast<uint32>(x >> 32)) + 32;
|
|
|
|
} else {
|
|
|
|
return count_trailing_zeroes32(static_cast<uint32>(x));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline uint32 bswap32(uint32 x) {
|
|
|
|
return _byteswap_ulong(x);
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline uint64 bswap64(uint64 x) {
|
|
|
|
return _byteswap_uint64(x);
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_bits32(uint32 x) {
|
2020-06-24 13:47:36 +02:00
|
|
|
// Do not use __popcnt because it will fail on some platforms.
|
2020-03-31 22:21:42 +02:00
|
|
|
x -= (x >> 1) & 0x55555555;
|
|
|
|
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
|
|
x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
|
|
x += x >> 8;
|
|
|
|
return (x + (x >> 16)) & 0x3F;
|
2018-12-19 15:48:39 +01:00
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_bits64(uint64 x) {
|
|
|
|
#if defined(_M_X64)
|
2019-04-30 21:15:43 +02:00
|
|
|
return static_cast<int32>(__popcnt64(x));
|
2018-12-19 15:48:39 +01:00
|
|
|
#else
|
|
|
|
return count_bits32(static_cast<uint32>(x >> 32)) + count_bits32(static_cast<uint32>(x));
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-20 01:30:44 +01:00
|
|
|
|
|
|
|
#elif TD_INTEL
|
|
|
|
|
|
|
|
inline int32 count_leading_zeroes32(uint32 x) {
|
|
|
|
unsigned __int32 res = 0;
|
2018-12-20 01:44:11 +01:00
|
|
|
if (_BitScanReverse(&res, x)) {
|
2018-12-20 01:30:44 +01:00
|
|
|
return 31 - res;
|
|
|
|
}
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 count_leading_zeroes64(uint64 x) {
|
|
|
|
#if defined(_M_X64) || defined(__x86_64__)
|
|
|
|
unsigned __int32 res = 0;
|
|
|
|
if (_BitScanReverse64(&res, x)) {
|
|
|
|
return 63 - res;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
#else
|
|
|
|
if ((x >> 32) == 0) {
|
|
|
|
return count_leading_zeroes32(static_cast<uint32>(x)) + 32;
|
|
|
|
} else {
|
|
|
|
return count_leading_zeroes32(static_cast<uint32>(x >> 32));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 count_trailing_zeroes32(uint32 x) {
|
|
|
|
unsigned __int32 res = 0;
|
|
|
|
if (_BitScanForward(&res, x)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 count_trailing_zeroes64(uint64 x) {
|
|
|
|
#if defined(_M_X64) || defined(__x86_64__)
|
|
|
|
unsigned __int32 res = 0;
|
|
|
|
if (_BitScanForward64(&res, x)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
#else
|
|
|
|
if (static_cast<uint32>(x) == 0) {
|
|
|
|
return count_trailing_zeroes32(static_cast<uint32>(x >> 32)) + 32;
|
|
|
|
} else {
|
|
|
|
return count_trailing_zeroes32(static_cast<uint32>(x));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32 bswap32(uint32 x) {
|
|
|
|
return _bswap(static_cast<int>(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64 bswap64(uint64 x) {
|
|
|
|
return _bswap64(static_cast<__int64>(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 count_bits32(uint32 x) {
|
2020-03-31 22:21:42 +02:00
|
|
|
x -= (x >> 1) & 0x55555555;
|
|
|
|
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
|
|
x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
|
|
x += x >> 8;
|
|
|
|
return (x + (x >> 16)) & 0x3F;
|
2018-12-20 01:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 count_bits64(uint64 x) {
|
2020-03-31 22:21:42 +02:00
|
|
|
return count_bits32(static_cast<uint32>(x >> 32)) + count_bits32(static_cast<uint32>(x));
|
2018-12-20 01:30:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#else
|
2018-12-20 01:30:44 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_leading_zeroes32(uint32 x) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
return __builtin_clz(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_leading_zeroes64(uint64 x) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
return __builtin_clzll(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_trailing_zeroes32(uint32 x) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
return __builtin_ctz(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_trailing_zeroes64(uint64 x) {
|
|
|
|
if (x == 0) {
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
return __builtin_ctzll(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline uint32 bswap32(uint32 x) {
|
|
|
|
return __builtin_bswap32(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline uint64 bswap64(uint64 x) {
|
|
|
|
return __builtin_bswap64(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_bits32(uint32 x) {
|
|
|
|
return __builtin_popcount(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_bits64(uint64 x) {
|
|
|
|
return __builtin_popcountll(x);
|
|
|
|
}
|
2018-12-20 01:30:44 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#endif
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
struct BitsRange {
|
2020-06-26 01:24:13 +02:00
|
|
|
explicit BitsRange(uint64 bits = 0) : bits{bits}, pos{-1} {
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BitsRange begin() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitsRange end() const {
|
|
|
|
return BitsRange{};
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
int32 operator*() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
if (pos == -1) {
|
2020-06-26 01:24:13 +02:00
|
|
|
pos = count_trailing_zeroes64(bits);
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const BitsRange &other) const {
|
|
|
|
return bits != other.bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitsRange &operator++() {
|
|
|
|
auto i = **this;
|
|
|
|
if (i != 64) {
|
|
|
|
bits ^= 1ull << i;
|
|
|
|
}
|
|
|
|
pos = -1;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint64 bits{0};
|
|
|
|
mutable int32 pos{-1};
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
} // namespace td
|