25static inline uint32_t ctz64(uint64_t n) {
26#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX && ULLONG_MAX >= UINT64_MAX
27 return static_cast<uint32_t
>(__builtin_ctzll(n));
28#elif defined(_WIN64) && defined(_MSC_VER) && _MSC_VER >= 1400 && \
29 ULONG_MAX >= UINT64_MAX
31 _BitScanForward64((
unsigned long *)&i, n);
35 if ((n &
static_cast<uint64_t
>(4294967295)) == 0) {
39 if ((n &
static_cast<uint64_t
>(0x0000FFFFUL)) == 0) {
44 if ((n &
static_cast<uint64_t
>(0x000000FFUL)) == 0) {
49 if ((n &
static_cast<uint64_t
>(0x0000000FUL)) == 0) {
54 if ((n &
static_cast<uint64_t
>(0x00000003UL)) == 0) {
63static inline uint32_t ctz32(uint32_t n) {
64#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX
65 return static_cast<uint32_t
>(__builtin_ctz(n));
67#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
69 __asm__(
"bsfl %1, %0" :
"=r"(i) :
"rm"(n));
72#elif defined(_MSC_VER) && _MSC_VER >= 1400
74 _BitScanForward((
unsigned long *)&i, n);
80 if ((n &
static_cast<uint32_t
>(0x0000FFFF)) == 0) {
85 if ((n &
static_cast<uint32_t
>(0x000000FF)) == 0) {
90 if ((n &
static_cast<uint32_t
>(0x0000000F)) == 0) {
95 if ((n &
static_cast<uint32_t
>(0x00000003)) == 0) {
106static inline uint32_t ctz16(uint16_t n) {
107#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX
108 return static_cast<uint32_t
>(__builtin_ctz(n));
110#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
112 __asm__(
"bsfl %1, %0" :
"=r"(i) :
"rm"(n));
115#elif defined(_MSC_VER) && _MSC_VER >= 1400
117 _BitScanForward((
unsigned long *)&i, n);
123 if ((n &
static_cast<uint16_t
>(0x000000FF)) == 0) {
128 if ((n &
static_cast<uint16_t
>(0x0000000F)) == 0) {
133 if ((n &
static_cast<uint16_t
>(0x00000003)) == 0) {
147inline uint32_t countOnes(uint32_t x) {
148 return static_cast<uint32_t
>(__builtin_popcount(x));
150#elif defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_M_ARM) && \
152inline uint32_t countOnes(uint32_t x) {
return __popcnt(x); }
154inline uint32_t countOnes(uint32_t v) {
155 v = v - ((v >> 1) & 0x55555555);
156 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
157 return static_cast<uint32_t
>((((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >>
166inline uint32_t countOnes(uint64_t x) {
167 return static_cast<uint32_t
>(__builtin_popcountll(x));
169#elif defined(_WIN64) && defined(_MSC_VER) && _MSC_VER >= 1400 && \
171inline uint32_t countOnes(uint64_t x) {
172 return static_cast<uint32_t
>(__popcnt64(
static_cast<__int64
>(x)));
175inline uint32_t countOnes(uint64_t v) {
176 v = v - ((v >> 1) & 0x5555555555555555);
177 v = (v & 0x3333333333333333) + ((v >> 2) & 0x3333333333333333);
178 v = ((v + (v >> 4)) & 0x0F0F0F0F0F0F0F0F);
179 return static_cast<uint32_t
>((v * (0x0101010101010101)) >> 56);
183inline uint32_t countOnes(uint16_t v) {
184 return countOnes(
static_cast<uint32_t
>(v));
187inline uint32_t numberOfTrailingZeros(uint32_t x) {
193inline uint32_t numberOfTrailingZeros(uint64_t x) {
199inline uint32_t numberOfTrailingZeros(uint16_t x) {
208template <
class uword> std::string toBinaryString(
const uword w) {
209 std::ostringstream convert;
210 for (uint32_t k = 0; k <
sizeof(uword) * 8; ++k) {
211 if (w & (
static_cast<uword
>(1) << k))
216 return convert.str();