2018-02-23 21:22:52 +00:00
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
2018-02-26 01:27:33 +00:00
|
|
|
inline uint16_t LO_16(uint32_t x) { return static_cast<uint16_t>(x & 0x0000FFFF); }
|
|
|
|
inline uint16_t HI_16(uint32_t x) { return static_cast<uint16_t>(x >> 16); }
|
2018-02-23 21:22:52 +00:00
|
|
|
|
2018-02-26 01:27:33 +00:00
|
|
|
inline uint32_t LO_32(uint64_t x) { return static_cast<uint32_t>(x & 0x00000000FFFFFFFF); }
|
|
|
|
inline uint32_t HI_32(uint64_t x) { return static_cast<uint32_t>(x >> 32); }
|
2018-02-23 21:22:52 +00:00
|
|
|
|
|
|
|
|
2018-02-26 01:27:33 +00:00
|
|
|
/// Clang also defines __GNUC__
|
2018-02-23 21:22:52 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
inline unsigned GetValueBitCountImpl(unsigned int value) noexcept {
|
2018-02-26 01:27:33 +00:00
|
|
|
// NOTE: __builtin_clz* have undefined result for zero.
|
2018-02-23 21:22:52 +00:00
|
|
|
return std::numeric_limits<unsigned int>::digits - __builtin_clz(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned GetValueBitCountImpl(unsigned long value) noexcept {
|
|
|
|
return std::numeric_limits<unsigned long>::digits - __builtin_clzl(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned GetValueBitCountImpl(unsigned long long value) noexcept {
|
|
|
|
return std::numeric_limits<unsigned long long>::digits - __builtin_clzll(value);
|
|
|
|
}
|
|
|
|
#else
|
2019-11-28 10:48:29 +00:00
|
|
|
/// Stupid implementation for non GCC-like compilers. Can use BSR from x86 instructions set.
|
2018-02-23 21:22:52 +00:00
|
|
|
template <typename T>
|
|
|
|
inline unsigned GetValueBitCountImpl(T value) noexcept {
|
2018-02-26 01:27:33 +00:00
|
|
|
unsigned result = 1; // result == 0 - impossible value, since value cannot be zero
|
2018-02-23 21:22:52 +00:00
|
|
|
value >>= 1;
|
|
|
|
while (value) {
|
|
|
|
value >>= 1;
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of leading 0-bits in `value`, starting at the most significant bit position.
|
2018-02-26 01:27:33 +00:00
|
|
|
* NOTE: value cannot be zero
|
2018-02-23 21:22:52 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static inline unsigned GetValueBitCount(T value) noexcept {
|
|
|
|
using TCvt = std::make_unsigned_t<std::decay_t<T>>;
|
|
|
|
return GetValueBitCountImpl(static_cast<TCvt>(value));
|
|
|
|
}
|