Skip to content

Commit 33b7c20

Browse files
committed
Merge #80: Add c++20 version of CountBits
82b6488 Add c++20 version of CountBits (Cory Fields) Pull request description: This corresponds with an upcoming pr to Core to do the same. It's unclear why the optimized versions ignore the max param. I'm guessing it's because the output of the builtins is already capped? I wasn't sure, so I copied the behavior of the other builtins. ACKs for top commit: sipa: ACK 82b6488 Tree-SHA512: 009f84bc67401533f57fd3a2765003d3baebfa87d7b3076ece16bda7e5af3756d082e8af5caf81098b4334a8c1f8c1a3eaa2a9bd21596801377aa58af91d08c9
2 parents 4a48f31 + 82b6488 commit 33b7c20

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/int_utils.h

+8-2
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

@@ -142,7 +144,11 @@ constexpr inline I Mask() { return ((I((I(-1)) << (std::numeric_limits<I>::digit
142144
/** Compute the smallest power of two that is larger than val. */
143145
template<typename I>
144146
static inline int CountBits(I val, int max) {
145-
#ifdef _MSC_VER
147+
#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
148+
// c++20 impl
149+
(void)max;
150+
return std::bit_width(val);
151+
#elif defined(_MSC_VER)
146152
(void)max;
147153
unsigned long index;
148154
unsigned char ret;

0 commit comments

Comments
 (0)