2018-12-19 15:48:39 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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
|
2018-12-19 21:35:13 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-12-19 15:48:39 +01:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#if TD_MSVC
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
namespace td {
|
|
|
|
|
2018-12-19 21:35:13 +01:00
|
|
|
inline int32 count_leading_zeroes32(uint32 x);
|
|
|
|
inline int32 count_leading_zeroes64(uint64 x);
|
|
|
|
inline int32 count_trailing_zeroes32(uint32 x);
|
|
|
|
inline int32 count_trailing_zeroes64(uint64 x);
|
|
|
|
inline uint32 bswap32(uint32 x);
|
|
|
|
inline uint64 bswap64(uint64 x);
|
|
|
|
inline int32 count_bits32(uint32 x);
|
|
|
|
inline int32 count_bits64(uint64 x);
|
2018-12-19 15:48:39 +01:00
|
|
|
|
|
|
|
#if TD_MSVC
|
|
|
|
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);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline uint64 bswap64(uint64 x) {
|
|
|
|
return _byteswap_uint64(x);
|
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
inline int32 count_bits32(uint32 x) {
|
|
|
|
return __popcnt(x);
|
|
|
|
}
|
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)
|
|
|
|
return __popcnt64(x);
|
|
|
|
#else
|
|
|
|
return count_bits32(static_cast<uint32>(x >> 32)) + count_bits32(static_cast<uint32>(x));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-19 21:35:13 +01: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
|
|
|
} // namespace td
|