Skip to content

Commit 52f9bba

Browse files
committed
crypto: replace non-standard CLZ builtins with c++20's bit_width
Also some header cleanups.
1 parent 60b6ff5 commit 52f9bba

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

src/crypto/common.h

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
#ifndef BITCOIN_CRYPTO_COMMON_H
66
#define BITCOIN_CRYPTO_COMMON_H
77

8-
#if defined(HAVE_CONFIG_H)
9-
#include <config/bitcoin-config.h>
10-
#endif
11-
12-
#include <stdint.h>
13-
#include <string.h>
14-
158
#include <compat/endian.h>
169

10+
#include <bit>
11+
#include <cstdint>
12+
#include <cstring>
13+
1714
uint16_t static inline ReadLE16(const unsigned char* ptr)
1815
{
1916
uint16_t x;
@@ -89,22 +86,7 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
8986
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
9087
uint64_t static inline CountBits(uint64_t x)
9188
{
92-
#if HAVE_BUILTIN_CLZL
93-
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
94-
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
95-
}
96-
#endif
97-
#if HAVE_BUILTIN_CLZLL
98-
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
99-
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
100-
}
101-
#endif
102-
int ret = 0;
103-
while (x) {
104-
x >>= 1;
105-
++ret;
106-
}
107-
return ret;
89+
return std::bit_width(x);
10890
}
10991

11092
#endif // BITCOIN_CRYPTO_COMMON_H

0 commit comments

Comments
 (0)