-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprime_fmod_tests.cpp
More file actions
214 lines (174 loc) · 5.58 KB
/
prime_fmod_tests.cpp
File metadata and controls
214 lines (174 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2022 Christian Mazakas.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/unordered/detail/prime_fmod.hpp>
#include <boost/core/detail/splitmix64.hpp>
#include <boost/core/lightweight_test.hpp>
#include <limits>
#if defined(BOOST_MSVC)
// conditional expression is constant
#pragma warning(disable : 4127)
#endif
void macros_test()
{
if (std::numeric_limits<std::size_t>::digits >= 64) {
#if !defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T)
BOOST_ERROR("std::numeric_limits<size_t>::digits >= 64, but "
"BOOST_UNORDERED_FCA_HAS_64B_SIZE_T is not defined");
#endif
} else {
#if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T)
BOOST_ERROR("std::numeric_limits<size_t>::digits < 64, but "
"BOOST_UNORDERED_FCA_HAS_64B_SIZE_T is defined");
#endif
}
}
// Pretty inefficient, but the test is fast enough.
// Might be too slow if we had larger primes?
bool is_prime(std::size_t x)
{
if (x == 2) {
return true;
}
if (x == 1 || x % 2 == 0) {
return false;
}
// y*y <= x is susceptible to overflow, so instead make sure to use y <= (x/y)
for (std::size_t y = 3; y <= (x / y); y += 2) {
if (x % y == 0) {
return false;
}
}
return true;
}
void prime_sizes_test()
{
// just some basic sanity checks
//
BOOST_TEST(!is_prime(0));
BOOST_TEST(!is_prime(1));
BOOST_TEST(is_prime(2));
BOOST_TEST(is_prime(3));
BOOST_TEST(is_prime(13));
BOOST_TEST(!is_prime(4));
BOOST_TEST(!is_prime(100));
BOOST_TEST(!is_prime(49));
std::size_t const* sizes = boost::unordered::detail::prime_fmod_size<>::sizes;
std::size_t sizes_len =
boost::unordered::detail::prime_fmod_size<>::sizes_len;
// prove every number in our sizes array is prime
//
BOOST_TEST_GT(sizes_len, 0u);
for (std::size_t i = 0; i < sizes_len; ++i) {
BOOST_TEST(is_prime(sizes[i]));
}
// prove that every subsequent number in the sequence is larger than the
// previous
//
for (std::size_t i = 1; i < sizes_len; ++i) {
BOOST_TEST_GT(sizes[i], sizes[i - 1]);
}
#if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T)
// now we wish to prove that if we do have the reciprocals stored, we have the
// correct amount of them, i.e. one for every entry in sizes[] that fits in 32
// bits
//
boost::uint64_t const* inv_sizes32 =
boost::unordered::detail::prime_fmod_size<>::inv_sizes32;
std::size_t inv_sizes32_len =
boost::unordered::detail::prime_fmod_size<>::inv_sizes32_len;
std::size_t count = 0;
for (std::size_t i = 0; i < sizes_len; ++i) {
if (sizes[i] <= UINT32_MAX) {
++count;
}
}
BOOST_TEST_GT(inv_sizes32_len, 0u);
BOOST_TEST_EQ(inv_sizes32_len, count);
// these values should also be monotonically decreasing
//
for (std::size_t i = 1; i < inv_sizes32_len; ++i) {
BOOST_TEST_LT(inv_sizes32[i], inv_sizes32[i - 1]);
}
// now make sure the values in inv_sizes32 are what they should be as derived
// from the paper
//
for (std::size_t i = 0; i < inv_sizes32_len; ++i) {
std::size_t const size = sizes[i];
BOOST_TEST_LE(size, UINT_MAX);
boost::uint32_t d = static_cast<boost::uint32_t>(sizes[i]);
boost::uint64_t M = ((boost::ulong_long_type(0xffffffff) << 32) +
boost::ulong_long_type(0xffffffff)) /
d +
1;
BOOST_TEST_EQ(inv_sizes32[i], M);
}
#endif
}
void get_remainder_test()
{
#if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T)
struct
{
// boost::unordered::detail::prime_fmod_size<>::get_remainder
// uses several internal implementations depending on the availability of
// certain intrinsics or 128 bit integer support, defaulting to a slow,
// portable routine. The following is a transcription of the portable
// routine used here for verification purposes.
//
boost::uint64_t operator()(boost::uint64_t f, boost::uint32_t d)
{
boost::uint64_t r1 = (f & UINT32_MAX) * d;
boost::uint64_t r2 = (f >> 32) * d;
r2 += r1 >> 32;
return r2 >> 32;
}
} get_remainder;
boost::detail::splitmix64 rng;
for (std::size_t i = 0; i < 1000000u; ++i) {
boost::uint64_t f = rng();
boost::uint32_t d = rng() & 0xffffffffu;
boost::uint64_t r1 =
boost::unordered::detail::prime_fmod_size<>::get_remainder(f, d);
boost::uint64_t r2 = get_remainder(f, d);
if (!BOOST_TEST_EQ(r1, r2)) {
std::cerr << "f: " << f << ", d: " << d << std::endl;
return;
}
}
#endif
}
void modulo_test()
{
std::size_t const* sizes = boost::unordered::detail::prime_fmod_size<>::sizes;
std::size_t const sizes_len =
boost::unordered::detail::prime_fmod_size<>::sizes_len;
boost::detail::splitmix64 rng;
for (std::size_t i = 0; i < 1000000u; ++i) {
std::size_t hash = static_cast<std::size_t>(-1) & rng();
for (std::size_t j = 0; j < sizes_len; ++j) {
std::size_t h = hash;
#if defined(BOOST_UNORDERED_FCA_HAS_64B_SIZE_T)
if (sizes[j] <= UINT_MAX) {
h = boost::uint32_t(h & 0xffffffffu) + boost::uint32_t(h >> 32);
}
#endif
std::size_t p1 =
boost::unordered::detail::prime_fmod_size<>::position(hash, j);
std::size_t p2 = h % sizes[j];
if (!BOOST_TEST_EQ(p1, p2)) {
std::cerr << "hash: " << hash << ", j: " << j << ", h: " << h
<< ", sizes[" << j << "]: " << sizes[j] << std::endl;
return;
}
}
}
}
int main()
{
macros_test();
prime_sizes_test();
get_remainder_test();
modulo_test();
return boost::report_errors();
}