13#include "DebugServer2/Utils/CompilerSupport.h"
27static const size_t kHalfIntBits =
sizeof(
unsigned int) * 4;
30static typename std::make_unsigned<T>::type MakeUnsigned(T number) {
31 return static_cast<typename std::make_unsigned<T>::type
>(number);
34static inline unsigned int FFS(
unsigned int number) {
35#if defined(COMPILER_GCC) || defined(COMPILER_CLANG)
36 return __builtin_ffs(number);
37#elif defined(COMPILER_MSVC)
39 if (_BitScanForward(&index, number)) {
44 for (
int i = 1; number != 0; i++, number >>= 1) {
53static inline unsigned int PopCount(
unsigned int number) {
54#if defined(COMPILER_GCC) || defined(COMPILER_CLANG)
55 return __builtin_popcount(number);
56#elif defined(COMPILER_MSVC) && defined(ARCH_ARM)
57 return _CountOneBits(number);
59 unsigned int count = 0;
62 number = number & (number - 1);
68template <
typename T>
static inline unsigned int FFS(T number) {
69 auto uNumber = MakeUnsigned(number);
72 ffs += FFS(
static_cast<unsigned int>(uNumber));
76 uNumber >>= kHalfIntBits;
77 uNumber >>= kHalfIntBits;
78 ffs += 2 * kHalfIntBits;
83template <
typename T>
static inline unsigned int PopCount(T number) {
84 auto uNumber = MakeUnsigned(number);
85 unsigned int count = 0;
87 count += PopCount(
static_cast<unsigned int>(number));
88 uNumber >>= kHalfIntBits;
89 uNumber >>= kHalfIntBits;
94template <
typename T>
static inline void EnableBit(T &number,
int idx) {
95 number |=
static_cast<typename std::make_unsigned<T>::type
>(1) << idx;
98template <
typename T>
static inline void DisableBit(T &number,
int idx) {
99 number &= ~(
static_cast<typename std::make_unsigned<T>::type
>(1) << idx);
104static inline void DisableBits(T &number,
int startIdx,
int endIdx) {
105 auto mask = (
static_cast<typename std::make_unsigned<T>::type
>(1)
106 << (endIdx - startIdx)) -
108 number &= ~(mask << startIdx);
112static inline void Align(T &number,
unsigned int alignment) {
113 unsigned int clear = alignment - 1;
114 number = (number + clear) & ~clear;