Skip to content

Commit df2307c

Browse files
committed
util: move MapIntoRange() for reuse in fuzz tests
1 parent bf66e25 commit df2307c

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

src/blockfilter.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,6 @@ static const std::map<BlockFilterType, std::string> g_filter_types = {
2424
{BlockFilterType::BASIC, "basic"},
2525
};
2626

27-
// Map a value x that is uniformly distributed in the range [0, 2^64) to a
28-
// value uniformly distributed in [0, n) by returning the upper 64 bits of
29-
// x * n.
30-
//
31-
// See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
32-
static uint64_t MapIntoRange(uint64_t x, uint64_t n)
33-
{
34-
#ifdef __SIZEOF_INT128__
35-
return (static_cast<unsigned __int128>(x) * static_cast<unsigned __int128>(n)) >> 64;
36-
#else
37-
// To perform the calculation on 64-bit numbers without losing the
38-
// result to overflow, split the numbers into the most significant and
39-
// least significant 32 bits and perform multiplication piece-wise.
40-
//
41-
// See: https://stackoverflow.com/a/26855440
42-
uint64_t x_hi = x >> 32;
43-
uint64_t x_lo = x & 0xFFFFFFFF;
44-
uint64_t n_hi = n >> 32;
45-
uint64_t n_lo = n & 0xFFFFFFFF;
46-
47-
uint64_t ac = x_hi * n_hi;
48-
uint64_t ad = x_hi * n_lo;
49-
uint64_t bc = x_lo * n_hi;
50-
uint64_t bd = x_lo * n_lo;
51-
52-
uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF);
53-
uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32);
54-
return upper64;
55-
#endif
56-
}
57-
5827
uint64_t GCSFilter::HashToRange(const Element& element) const
5928
{
6029
uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)

src/test/fuzz/golomb_rice.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@
1919
#include <vector>
2020

2121
namespace {
22-
uint64_t MapIntoRange(const uint64_t x, const uint64_t n)
23-
{
24-
const uint64_t x_hi = x >> 32;
25-
const uint64_t x_lo = x & 0xFFFFFFFF;
26-
const uint64_t n_hi = n >> 32;
27-
const uint64_t n_lo = n & 0xFFFFFFFF;
28-
const uint64_t ac = x_hi * n_hi;
29-
const uint64_t ad = x_hi * n_lo;
30-
const uint64_t bc = x_lo * n_hi;
31-
const uint64_t bd = x_lo * n_lo;
32-
const uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF);
33-
const uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32);
34-
return upper64;
35-
}
3622

3723
uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)
3824
{

src/util/golombrice.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,35 @@ uint64_t GolombRiceDecode(BitStreamReader<IStream>& bitreader, uint8_t P)
4040
return (q << P) + r;
4141
}
4242

43+
// Map a value x that is uniformly distributed in the range [0, 2^64) to a
44+
// value uniformly distributed in [0, n) by returning the upper 64 bits of
45+
// x * n.
46+
//
47+
// See: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
48+
static inline uint64_t MapIntoRange(uint64_t x, uint64_t n)
49+
{
50+
#ifdef __SIZEOF_INT128__
51+
return (static_cast<unsigned __int128>(x) * static_cast<unsigned __int128>(n)) >> 64;
52+
#else
53+
// To perform the calculation on 64-bit numbers without losing the
54+
// result to overflow, split the numbers into the most significant and
55+
// least significant 32 bits and perform multiplication piece-wise.
56+
//
57+
// See: https://stackoverflow.com/a/26855440
58+
const uint64_t x_hi = x >> 32;
59+
const uint64_t x_lo = x & 0xFFFFFFFF;
60+
const uint64_t n_hi = n >> 32;
61+
const uint64_t n_lo = n & 0xFFFFFFFF;
62+
63+
const uint64_t ac = x_hi * n_hi;
64+
const uint64_t ad = x_hi * n_lo;
65+
const uint64_t bc = x_lo * n_hi;
66+
const uint64_t bd = x_lo * n_lo;
67+
68+
const uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF);
69+
const uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32);
70+
return upper64;
71+
#endif
72+
}
73+
4374
#endif // BITCOIN_UTIL_GOLOMBRICE_H

0 commit comments

Comments
 (0)