crypto: replace non-standard CLZ builtins with c++20's bit_width
Also some header cleanups.
This commit is contained in:
1 parent
60b6ff5ac0
commit
52f9bba889
1 file changed
+5
-23
+5
-23
@@ -5,15 +5,12 @@
|
|||||||
#ifndef BITCOIN_CRYPTO_COMMON_H
|
#ifndef BITCOIN_CRYPTO_COMMON_H
|
||||||
#define BITCOIN_CRYPTO_COMMON_H
|
#define BITCOIN_CRYPTO_COMMON_H
|
||||||
|
|
||||||
#if defined(HAVE_CONFIG_H)
|
|
||||||
#include <config/bitcoin-config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <compat/endian.h>
|
#include <compat/endian.h>
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
uint16_t static inline ReadLE16(const unsigned char* ptr)
|
uint16_t static inline ReadLE16(const unsigned char* ptr)
|
||||||
{
|
{
|
||||||
uint16_t x;
|
uint16_t x;
|
||||||
@@ -89,22 +86,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. */
|
/** 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)
|
uint64_t static inline CountBits(uint64_t x)
|
||||||
{
|
{
|
||||||
#if HAVE_BUILTIN_CLZL
|
return std::bit_width(x);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_CRYPTO_COMMON_H
|
#endif // BITCOIN_CRYPTO_COMMON_H
|
||||||
Reference in new issue
Block a user