Skip to content

Commit

Permalink
Replace non-standard CLZ builtins with c++20's bit_width
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni committed Dec 11, 2023
1 parent d5e5810 commit 273e4dc
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/crypto/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <config/bitcoin-config.h>
#endif

#include <bit>
#include <stdint.h>
#include <string.h>

Expand Down Expand Up @@ -89,22 +90,7 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
uint64_t static inline CountBits(uint64_t x)
{
#if HAVE_BUILTIN_CLZL
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
#endif
#if HAVE_BUILTIN_CLZLL
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
}
#endif
int ret = 0;
while (x) {
x >>= 1;
++ret;
}
return ret;
return std::bit_width(x);
}

#endif // BITCOIN_CRYPTO_COMMON_H

0 comments on commit 273e4dc

Please sign in to comment.