From c6d15c45d971fb25551b7a66a2615e3f0bee999b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 6 Jan 2022 11:27:06 -0500 Subject: [PATCH 1/3] [moveonly] Move MapIntoRange() to separate util/fastrange.h --- src/Makefile.am | 1 + src/util/fastrange.h | 41 +++++++++++++++++++++++++++++++++++++++++ src/util/golombrice.h | 33 ++------------------------------- 3 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 src/util/fastrange.h diff --git a/src/Makefile.am b/src/Makefile.am index b946a325c9c..0b177480c8f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -240,6 +240,7 @@ BITCOIN_CORE_H = \ util/check.h \ util/epochguard.h \ util/error.h \ + util/fastrange.h \ util/fees.h \ util/getuniquepath.h \ util/golombrice.h \ diff --git a/src/util/fastrange.h b/src/util/fastrange.h new file mode 100644 index 00000000000..57bb8e07dc4 --- /dev/null +++ b/src/util/fastrange.h @@ -0,0 +1,41 @@ +// Copyright (c) 2018-2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_FASTRANGE_H +#define BITCOIN_UTIL_FASTRANGE_H + +#include + +// Map a value x that is uniformly distributed in the range [0, 2^64) to a +// value uniformly distributed in [0, n) by returning the upper 64 bits of +// x * n. +// +// See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ +static inline uint64_t MapIntoRange(uint64_t x, uint64_t n) +{ +#ifdef __SIZEOF_INT128__ + return (static_cast(x) * static_cast(n)) >> 64; +#else + // To perform the calculation on 64-bit numbers without losing the + // result to overflow, split the numbers into the most significant and + // least significant 32 bits and perform multiplication piece-wise. + // + // See: https://stackoverflow.com/a/26855440 + const uint64_t x_hi = x >> 32; + const uint64_t x_lo = x & 0xFFFFFFFF; + const uint64_t n_hi = n >> 32; + const uint64_t n_lo = n & 0xFFFFFFFF; + + const uint64_t ac = x_hi * n_hi; + const uint64_t ad = x_hi * n_lo; + const uint64_t bc = x_lo * n_hi; + const uint64_t bd = x_lo * n_lo; + + const uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF); + const uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32); + return upper64; +#endif +} + +#endif // BITCOIN_UTIL_FASTRANGE_H diff --git a/src/util/golombrice.h b/src/util/golombrice.h index f14cb594db7..4ff4f6d7e59 100644 --- a/src/util/golombrice.h +++ b/src/util/golombrice.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_UTIL_GOLOMBRICE_H #define BITCOIN_UTIL_GOLOMBRICE_H +#include + #include #include @@ -40,35 +42,4 @@ uint64_t GolombRiceDecode(BitStreamReader& bitreader, uint8_t P) return (q << P) + r; } -// Map a value x that is uniformly distributed in the range [0, 2^64) to a -// value uniformly distributed in [0, n) by returning the upper 64 bits of -// x * n. -// -// See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ -static inline uint64_t MapIntoRange(uint64_t x, uint64_t n) -{ -#ifdef __SIZEOF_INT128__ - return (static_cast(x) * static_cast(n)) >> 64; -#else - // To perform the calculation on 64-bit numbers without losing the - // result to overflow, split the numbers into the most significant and - // least significant 32 bits and perform multiplication piece-wise. - // - // See: https://stackoverflow.com/a/26855440 - const uint64_t x_hi = x >> 32; - const uint64_t x_lo = x & 0xFFFFFFFF; - const uint64_t n_hi = n >> 32; - const uint64_t n_lo = n & 0xFFFFFFFF; - - const uint64_t ac = x_hi * n_hi; - const uint64_t ad = x_hi * n_lo; - const uint64_t bc = x_lo * n_hi; - const uint64_t bd = x_lo * n_lo; - - const uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF); - const uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32); - return upper64; -#endif -} - #endif // BITCOIN_UTIL_GOLOMBRICE_H From 96ecd6fa3e0f53c3a25eb7c328220b819f8dde03 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 6 Jan 2022 11:29:08 -0500 Subject: [PATCH 2/3] scripted-diff: rename MapIntoRange to FastRange64 -BEGIN VERIFY SCRIPT- sed -i -e 's/MapIntoRange/FastRange64/' src/blockfilter.cpp src/test/fuzz/golomb_rice.cpp src/util/fastrange.h -END VERIFY SCRIPT- --- src/blockfilter.cpp | 2 +- src/test/fuzz/golomb_rice.cpp | 2 +- src/util/fastrange.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp index 566254d8399..63a9ba498f5 100644 --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -29,7 +29,7 @@ uint64_t GCSFilter::HashToRange(const Element& element) const uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1) .Write(element.data(), element.size()) .Finalize(); - return MapIntoRange(hash, m_F); + return FastRange64(hash, m_F); } std::vector GCSFilter::BuildHashedSet(const ElementSet& elements) const diff --git a/src/test/fuzz/golomb_rice.cpp b/src/test/fuzz/golomb_rice.cpp index 2d0b29953c6..b4bb4c6dc64 100644 --- a/src/test/fuzz/golomb_rice.cpp +++ b/src/test/fuzz/golomb_rice.cpp @@ -25,7 +25,7 @@ uint64_t HashToRange(const std::vector& element, const uint64_t f) const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL) .Write(element.data(), element.size()) .Finalize(); - return MapIntoRange(hash, f); + return FastRange64(hash, f); } std::vector BuildHashedSet(const std::unordered_set, ByteVectorHash>& elements, const uint64_t f) diff --git a/src/util/fastrange.h b/src/util/fastrange.h index 57bb8e07dc4..963d21c03af 100644 --- a/src/util/fastrange.h +++ b/src/util/fastrange.h @@ -12,7 +12,7 @@ // x * n. // // See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ -static inline uint64_t MapIntoRange(uint64_t x, uint64_t n) +static inline uint64_t FastRange64(uint64_t x, uint64_t n) { #ifdef __SIZEOF_INT128__ return (static_cast(x) * static_cast(n)) >> 64; From efab28b06bfaa50c41337e84136cb58437e7ba00 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 6 Jan 2022 11:46:50 -0500 Subject: [PATCH 3/3] Add FastRange32 function and use it throughout the codebase --- src/common/bloom.cpp | 13 +++---------- src/cuckoocache.h | 27 ++++++++++++--------------- src/util/fastrange.h | 20 +++++++++++++++----- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp index c3603b5d2aa..0bb72dbcbb6 100644 --- a/src/common/bloom.cpp +++ b/src/common/bloom.cpp @@ -11,6 +11,7 @@ #include