Skip to content

Commit 281f811

Browse files
committed
Add MSVC CountBits implementation using _BitScanReverse
1 parent b21917d commit 281f811

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/int_utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include <algorithm>
1414
#include <type_traits>
1515

16+
#ifdef _MSC_VER
17+
# include <intrin.h>
18+
#endif
19+
1620
template<int bits>
1721
static constexpr inline uint64_t Rot(uint64_t x) { return (x << bits) | (x >> (64 - bits)); }
1822

@@ -135,6 +139,17 @@ static inline int CountBits(I val, int max) {
135139
} else {
136140
return std::numeric_limits<unsigned long long>::digits - __builtin_clzll(val);
137141
}
142+
#elif _MSC_VER
143+
(void)max;
144+
unsigned long index;
145+
unsigned char ret;
146+
if (std::numeric_limits<I>::digits <= 32) {
147+
ret = _BitScanReverse(&index, val);
148+
} else {
149+
ret = _BitScanReverse64(&index, val);
150+
}
151+
if (!ret) return 0;
152+
return index;
138153
#else
139154
while (max && (val >> (max - 1) == 0)) --max;
140155
return max;

0 commit comments

Comments
 (0)