aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-02-25 12:16:58 -0800
committerPieter Wuille <pieter.wuille@gmail.com>2017-03-29 11:26:08 -0700
commit4fd2d2fc97e21efceab849576e544160fd5e3e3d (patch)
tree4d36745b0b5a72bf51b2e54bfd13151920a2c57b /src/crypto
parent16329224e70d0525208f6b0ba00c5e1531a4f5ea (diff)
Add a FastRandomContext::randrange and use it
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/common.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/crypto/common.h b/src/crypto/common.h
index 4a9d1150b6..bcca3d30ea 100644
--- a/src/crypto/common.h
+++ b/src/crypto/common.h
@@ -79,4 +79,25 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
memcpy(ptr, (char*)&v, 8);
}
+/** 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)
+{
+#ifdef HAVE_DECL___BUILTIN_CLZL
+ if (sizeof(unsigned long) >= sizeof(uint64_t)) {
+ return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
+ }
+#endif
+#ifdef HAVE_DECL___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