2016-03-07 06:18:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-02 22:08:19 +00:00
|
|
|
#include <cstddef>
|
2019-06-12 17:12:08 +00:00
|
|
|
#include <type_traits>
|
2020-08-02 09:48:25 +00:00
|
|
|
#include <common/defines.h>
|
2016-08-07 06:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Returns log2 of number, rounded down.
|
|
|
|
* Compiles to single 'bsr' instruction on x86.
|
|
|
|
* For zero argument, result is unspecified.
|
|
|
|
*/
|
|
|
|
inline unsigned int bitScanReverse(unsigned int x)
|
2016-03-07 06:18:06 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return sizeof(unsigned int) * 8 - 1 - __builtin_clz(x);
|
2016-03-07 06:18:06 +00:00
|
|
|
}
|
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>
|
2019-06-13 14:04:38 +00:00
|
|
|
inline size_t getLeadingZeroBits(T x)
|
2019-06-12 17:12:08 +00:00
|
|
|
{
|
2019-06-13 14:04:38 +00:00
|
|
|
if (!x)
|
|
|
|
return sizeof(x) * 8;
|
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);
|
|
|
|
}
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(unsigned long int))
|
|
|
|
{
|
|
|
|
return __builtin_clzl(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return __builtin_clzll(x);
|
|
|
|
}
|
2019-06-12 17:12:08 +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
|
|
|
{
|
2019-06-13 14:04:38 +00:00
|
|
|
if constexpr (sizeof(T) <= sizeof(unsigned int))
|
|
|
|
{
|
|
|
|
return __builtin_ctz(x);
|
|
|
|
}
|
|
|
|
else if constexpr (sizeof(T) <= sizeof(unsigned long int))
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
* maskLowBits<UInt8>(3) => 00000111
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|