Skip to content

Commit e0e3dc5

Browse files
committed
Add c++20 version of CountBits
1 parent 022b959 commit e0e3dc5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/int_utils.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include <algorithm>
1515
#include <type_traits>
1616

17-
#ifdef _MSC_VER
17+
#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
18+
# include <bit>
19+
#elif defined(_MSC_VER)
1820
# include <intrin.h>
1921
#endif
2022

@@ -130,7 +132,11 @@ constexpr inline I Mask() { return ((I((I(-1)) << (std::numeric_limits<I>::digit
130132
/** Compute the smallest power of two that is larger than val. */
131133
template<typename I>
132134
static inline int CountBits(I val, int max) {
133-
#ifdef _MSC_VER
135+
#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
136+
// c++20 impl
137+
(void)max;
138+
return std::bit_width(val);
139+
#elif defined(_MSC_VER)
134140
(void)max;
135141
unsigned long index;
136142
unsigned char ret;

0 commit comments

Comments
 (0)