2016-03-07 06:18:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-02 22:08:19 +00:00
|
|
|
#include <cstddef>
|
2020-09-26 00:00:54 +00:00
|
|
|
#include <cstdint>
|
2020-08-06 23:52:18 +00:00
|
|
|
#include <cassert>
|
2019-06-12 17:12:08 +00:00
|
|
|
#include <type_traits>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/defines.h>
|
2016-08-07 06:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** For zero argument, result is zero.
|
2020-08-02 09:48:25 +00:00
|
|
|
* For arguments with most significand bit set, result is n.
|
2016-08-07 06:10:15 +00:00
|
|
|
* For other arguments, returns value, rounded up to power of two.
|
|
|
|
*/
|
|
|
|
inline size_t roundUpToPowerOfTwoOrZero(size_t n)
|
|
|
|
{
|
2020-08-02 09:48:25 +00:00
|
|
|
// if MSB is set, return n, to avoid return zero
|
2020-08-02 16:09:51 +00:00
|
|
|
if (unlikely(n >= 0x8000000000000000ULL))
|
2020-08-02 09:48:25 +00:00
|
|
|
return n;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
--n;
|
|
|
|
n |= n >> 1;
|
|
|
|
n |= n >> 2;
|
|
|
|
n |= n >> 4;
|
|
|
|
n |= n >> 8;
|
|
|
|
n |= n >> 16;
|
|
|
|
n |= n >> 32;
|
|
|
|
++n;
|
2016-08-07 06:10:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return n;
|
2016-08-07 06:10:15 +00:00
|
|
|
}
|
2019-06-12 17:12:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2020-09-26 00:00:54 +00:00
|
|
|
inline size_t getLeadingZeroBitsUnsafe(T x)
|
2019-06-12 17:12:08 +00:00
|
|
|
{
|
2020-09-26 00:00:54 +00:00
|
|
|
assert(x != 0);
|
2019-06-12 17:12:08 +00:00
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
if constexpr (sizeof(T) <= sizeof(unsigned int))
|
|
|
|
{
|
|
|
|
return __builtin_clz(x);
|
|
|
|
}
|
2022-03-11 21:47:28 +00:00
|
|
|
else if constexpr (sizeof(T) <= sizeof(unsigned long int)) /// NOLINT
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
|
|
|
return __builtin_clzl(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return __builtin_clzll(x);
|
|
|
|
}
|
2019-06-12 17:12:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 00:00:54 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline size_t getLeadingZeroBits(T x)
|
|
|
|
{
|
|
|
|
if (!x)
|
|
|
|
return sizeof(x) * 8;
|
|
|
|
|
|
|
|
return getLeadingZeroBitsUnsafe(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns log2 of number, rounded down.
|
|
|
|
* Compiles to single 'bsr' instruction on x86.
|
|
|
|
* For zero argument, result is unspecified.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline uint32_t bitScanReverse(T x)
|
|
|
|
{
|
2020-09-26 03:02:05 +00:00
|
|
|
return (std::max<size_t>(sizeof(T), sizeof(unsigned int))) * 8 - 1 - getLeadingZeroBitsUnsafe(x);
|
2020-09-26 00:00:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 10:53:13 +00:00
|
|
|
// Unsafe since __builtin_ctz()-family explicitly state that result is undefined on x == 0
|
2019-06-12 17:12:08 +00:00
|
|
|
template <typename T>
|
2020-04-02 10:53:13 +00:00
|
|
|
inline size_t getTrailingZeroBitsUnsafe(T x)
|
2019-06-12 17:12:08 +00:00
|
|
|
{
|
2020-09-26 00:00:54 +00:00
|
|
|
assert(x != 0);
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
if constexpr (sizeof(T) <= sizeof(unsigned int))
|
|
|
|
{
|
|
|
|
return __builtin_ctz(x);
|
|
|
|
}
|
2022-03-11 21:47:28 +00:00
|
|
|
else if constexpr (sizeof(T) <= sizeof(unsigned long int)) /// NOLINT
|
2019-06-13 14:04:38 +00:00
|
|
|
{
|
|
|
|
return __builtin_ctzl(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return __builtin_ctzll(x);
|
|
|
|
}
|
2019-06-12 17:12:08 +00:00
|
|
|
}
|
2019-06-17 03:27:42 +00:00
|
|
|
|
2020-04-02 10:53:13 +00:00
|
|
|
template <typename T>
|
|
|
|
inline size_t getTrailingZeroBits(T x)
|
|
|
|
{
|
|
|
|
if (!x)
|
|
|
|
return sizeof(x) * 8;
|
|
|
|
|
|
|
|
return getTrailingZeroBitsUnsafe(x);
|
|
|
|
}
|
|
|
|
|
2019-06-17 03:27:42 +00:00
|
|
|
/** Returns a mask that has '1' for `bits` LSB set:
|
2020-09-26 00:00:54 +00:00
|
|
|
* maskLowBits<UInt8>(3) => 00000111
|
|
|
|
*/
|
2019-06-17 03:27:42 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T maskLowBits(unsigned char bits)
|
|
|
|
{
|
2019-06-19 15:32:13 +00:00
|
|
|
if (bits == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T result = static_cast<T>(~T{0});
|
|
|
|
if (bits < sizeof(T) * 8)
|
|
|
|
{
|
|
|
|
result = static_cast<T>(result >> (sizeof(T) * 8 - bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-06-17 03:27:42 +00:00
|
|
|
}
|