22#if !__has_builtin(__builtin_bit_cast)
26#if defined(_MSC_VER) && !defined(_DEBUG)
35unsigned char _BitScanForward(
unsigned long *_Index,
unsigned long _Mask);
36unsigned char _BitScanForward64(
unsigned long *_Index,
unsigned __int64 _Mask);
37unsigned char _BitScanReverse(
unsigned long *_Index,
unsigned long _Mask);
38unsigned char _BitScanReverse64(
unsigned long *_Index,
unsigned __int64 _Mask);
48 typename To,
typename From,
49 typename = std::enable_if_t<
sizeof(To) ==
sizeof(
From)>,
50 typename = std::enable_if_t<std::is_trivially_constructible<To>::value>,
51 typename = std::enable_if_t<std::is_trivially_copyable<To>::value>,
52 typename = std::enable_if_t<std::is_trivially_copyable<From>::value>>
54#if __has_builtin(__builtin_bit_cast)
55 return __builtin_bit_cast(To, from);
58 std::memcpy(&to, &from,
sizeof(To));
64template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
66 if constexpr (
sizeof(
T) == 1) {
68 }
else if constexpr (
sizeof(
T) == 2) {
70#if defined(_MSC_VER) && !defined(_DEBUG)
73 return _byteswap_ushort(UV);
79 }
else if constexpr (
sizeof(
T) == 4) {
81#if __has_builtin(__builtin_bswap32)
82 return __builtin_bswap32(UV);
83#elif defined(_MSC_VER) && !defined(_DEBUG)
84 return _byteswap_ulong(UV);
90 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
92 }
else if constexpr (
sizeof(
T) == 8) {
94#if __has_builtin(__builtin_bswap64)
95 return __builtin_bswap64(UV);
96#elif defined(_MSC_VER) && !defined(_DEBUG)
97 return _byteswap_uint64(UV);
100 uint32_t Lo = llvm::byteswap<uint32_t>(UV >> 32);
101 return (
Hi << 32) |
Lo;
104 static_assert(!
sizeof(
T *),
"Don't know how to handle the given type.");
109template <
typename T,
typename = std::enable_if_t<std::is_
unsigned_v<T>>>
118 return std::numeric_limits<T>::digits;
123 unsigned ZeroBits = 0;
124 T Shift = std::numeric_limits<T>::digits >> 1;
125 T Mask = std::numeric_limits<T>::max() >> Shift;
127 if ((Val & Mask) == 0) {
138#if defined(__GNUC__) || defined(_MSC_VER)
139template <
typename T>
struct TrailingZerosCounter<
T, 4> {
140 static unsigned count(
T Val) {
144#if __has_builtin(__builtin_ctz) || defined(__GNUC__)
145 return __builtin_ctz(Val);
146#elif defined(_MSC_VER)
148 _BitScanForward(&
Index, Val);
154#if !defined(_MSC_VER) || defined(_M_X64)
155template <
typename T>
struct TrailingZerosCounter<
T, 8> {
156 static unsigned count(
T Val) {
160#if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
161 return __builtin_ctzll(Val);
162#elif defined(_MSC_VER)
164 _BitScanForward64(&
Index, Val);
180 static_assert(std::is_unsigned_v<T>,
181 "Only unsigned integral types are allowed.");
189 return std::numeric_limits<T>::digits;
192 unsigned ZeroBits = 0;
193 for (
T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
194 T Tmp = Val >> Shift;
204#if defined(__GNUC__) || defined(_MSC_VER)
205template <
typename T>
struct LeadingZerosCounter<
T, 4> {
206 static unsigned count(
T Val) {
210#if __has_builtin(__builtin_clz) || defined(__GNUC__)
211 return __builtin_clz(Val);
212#elif defined(_MSC_VER)
214 _BitScanReverse(&
Index, Val);
220#if !defined(_MSC_VER) || defined(_M_X64)
221template <
typename T>
struct LeadingZerosCounter<
T, 8> {
222 static unsigned count(
T Val) {
226#if __has_builtin(__builtin_clzll) || defined(__GNUC__)
227 return __builtin_clzll(Val);
228#elif defined(_MSC_VER)
230 _BitScanReverse64(&
Index, Val);
246 static_assert(std::is_unsigned_v<T>,
247 "Only unsigned integral types are allowed.");
259 static_assert(std::is_unsigned_v<T>,
260 "Only unsigned integral types are allowed.");
261 return llvm::countl_zero<T>(~
Value);
272 static_assert(std::is_unsigned_v<T>,
273 "Only unsigned integral types are allowed.");
274 return llvm::countr_zero<T>(~
Value);
282 static_assert(std::is_unsigned_v<T>,
283 "Only unsigned integral types are allowed.");
292 static_assert(std::is_unsigned_v<T>,
293 "Only unsigned integral types are allowed.");
307 static_assert(std::is_unsigned_v<T>,
308 "Only unsigned integral types are allowed.");
311 return T(1) << llvm::bit_width<T>(
Value - 1u);
318 static_assert(SizeOfT <= 4,
"Not implemented!");
320 return (
int)__builtin_popcount(
Value);
323 v = v - ((v >> 1) & 0x55555555);
324 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
325 return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
333 return (
int)__builtin_popcountll(
Value);
336 v = v - ((v >> 1) & 0x5555555555555555ULL);
337 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
338 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
339 return int((
uint64_t)(v * 0x0101010101010101ULL) >> 56);
348template <
typename T,
typename = std::enable_if_t<std::is_
unsigned_v<T>>>
354template <
typename T,
typename = std::enable_if_t<std::is_
unsigned_v<T>>>
355[[nodiscard]]
constexpr T rotr(
T V,
int R);
357template <
typename T,
typename = std::enable_if_t<std::is_
unsigned_v<T>>>
358[[nodiscard]]
constexpr T rotl(
T V,
int R) {
359 unsigned N = std::numeric_limits<T>::digits;
368 return (V << R) | (V >> (
N - R));
371template <
typename T,
typename> [[nodiscard]]
constexpr T rotr(
T V,
int R) {
372 unsigned N = std::numeric_limits<T>::digits;
381 return (V >> R) | (V << (
N - R));
BlockVerifier::State From
LLVM Value Representation.
This is an optimization pass for GlobalISel generic memory operations.
constexpr T rotr(T V, int R)
int popcount(T Value) noexcept
Count the number of set bits in a value.
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
constexpr bool has_single_bit(T Value) noexcept
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
To bit_cast(const From &from) noexcept
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
constexpr T rotl(T V, int R)
static unsigned count(T Val)
static int count(T Value)
static int count(T Value)
static unsigned count(T Val)