-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.cpp
More file actions
414 lines (363 loc) · 13.4 KB
/
Copy pathtest.cpp
File metadata and controls
414 lines (363 loc) · 13.4 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <array>
#include <iostream>
#include <limits>
#include <random>
#include "intdiv.hpp"
using big_int = long long;
template<class T>
constexpr bool is_div_defined(T x, T y) {
if (y == 0) return false;
if constexpr (std::is_signed_v<T>) {
if (x == std::numeric_limits<T>::min() && y == -1) return false;
}
return true;
}
// This function tests for two things that have to hold true irrespective of rounding mode:
// 1. The quotient has to be within 1 of the quotient of the builtin division.
// 2. The equation (x == y * q + r) has to hold true.
template<class T>
constexpr bool is_valid_division(T x, T y, T q, T r) {
const auto q_to_zero = big_int(x) / big_int(y);
const int q_tolerance = big_int(x) % big_int(y) == 0 ? 0 : 1;
if (std::abs(big_int(q) - q_to_zero) > q_tolerance) {
return false;
}
if constexpr (std::is_signed_v<T>) {
return big_int(x) == big_int(y) * big_int(q) + big_int(r);
} else {
return x == T(y * q + r);
}
}
// Subsequent functions further constrain the remainder.
// For example, when rounding towards -inf,
// the sign of the remainder is the sign of the divisor or the remainder is zero.
template<class T>
constexpr bool is_valid_division_to_zero(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
if constexpr (std::is_signed_v<T>) {
return r == 0 || __sgn2(r) == __sgn2(x);
} else {
return r < y;
}
}
template<class T>
constexpr bool is_valid_division_away_zero(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
if constexpr (std::is_signed_v<T>) {
return r == 0 || __sgn2(r) != __sgn2(x);
} else {
return big_int(q) == div_away_zero(big_int(x), big_int(y));
}
}
template<class T>
constexpr bool is_valid_division_to_pos_inf(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
if constexpr (std::is_signed_v<T>) {
return r == 0 || __sgn2(r) != __sgn2(y);
} else {
return big_int(q) == div_to_pos_inf(big_int(x), big_int(y));
}
}
template<class T>
constexpr bool is_valid_division_to_neg_inf(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
if constexpr (std::is_signed_v<T>) {
return r == 0 || __sgn2(r) == __sgn2(y);
} else {
return r < y;
}
}
template<class T>
constexpr bool is_valid_euclid_remainder(T y, T r) {
if constexpr (std::is_signed_v<T>) {
return r >= 0 && std::abs(big_int(r)) < std::abs(big_int(y));
} else {
return r < y;
}
}
template<class T>
constexpr bool is_valid_division_euclid(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
return is_valid_euclid_remainder(y, r);
}
template<class T>
constexpr bool is_valid_division_to_odd(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
return r == 0 || q % 2 != 0;
}
template<class T>
constexpr bool is_valid_division_to_even(T x, T y, T q, T r) {
if (!is_valid_division(x, y, q, r)) return false;
return r == 0 || q % 2 == 0;
}
// The tie-rounding functions should always:
// - round away from zero when abs(quotient) > 0.5,
// - round towards zero when abs(quotient) < 0.5, otherwise
// - check if the exact tie was rounded correctly
template<class T>
constexpr bool is_valid_division_to_nearest(T x, T y, T q, T r, bool is_tie_valid) {
if (!is_valid_division(x, y, q, r)) return false;
if (r == 0) return true;
const big_int comp = std::abs(big_int(x) % big_int(y) * 2) - std::abs(big_int(y));
if constexpr (std::is_signed_v<T>) {
if (comp > 0) return __sgn2(r) != __sgn2(x);
if (comp < 0) return __sgn2(r) == __sgn2(x);
} else {
if (comp != 0) return big_int(q) == div_ties_to_zero(big_int(x), big_int(y));
}
return is_tie_valid;
}
static_assert(div_rem_away_zero(72'777'531u, 3'405'476'348u).quotient == 1);
static_assert(div_rem_away_zero(72'777'531u, 3'405'476'348u).quotient == 1);
// dividend == quotient * divisor + remainder
static_assert(72'777'531u == 1u * 3'405'476'348u + 962'268'479u);
static_assert(div_rem_away_zero(72'777'531u, 3'405'476'348u).remainder == 962'268'479u);
static_assert(div_rem_to_pos_inf(72'777'531u, 3'405'476'348u).remainder == 962'268'479u);
static_assert(div_rem_euclid(-8, 3) == div_result<int>{-3, 1});
static_assert(div_rem_euclid(-8, -3) == div_result<int>{3, 1});
static_assert(div_rem_euclid(8, -3) == div_result<int>{-2, 2});
static_assert(div_euclid(-8, 3) == -3);
static_assert(rem_euclid(8, -3) == 2);
template<class T>
constexpr bool is_valid_division_ties_to_zero(T x, T y, T q, T r) {
if constexpr (std::is_signed_v<T>) {
return is_valid_division_to_nearest(x, y, q, r, __sgn2(r) == __sgn2(x));
} else {
return is_valid_division_to_nearest(x, y, q, r, r < y);
}
}
template<class T>
constexpr bool is_valid_division_ties_away_zero(T x, T y, T q, T r) {
if constexpr (std::is_signed_v<T>) {
return is_valid_division_to_nearest(x, y, q, r, __sgn2(r) != __sgn2(x));
} else {
bool quotient_like_signed = big_int(q) == div_away_zero(big_int(x), big_int(y));
return is_valid_division_to_nearest(x, y, q, r, quotient_like_signed);
}
}
template<class T>
constexpr bool is_valid_division_ties_to_pos_inf(T x, T y, T q, T r) {
if constexpr (std::is_signed_v<T>) {
return is_valid_division_to_nearest(x, y, q, r, __sgn2(r) != __sgn2(y));
} else {
bool quotient_like_signed = big_int(q) == div_away_zero(big_int(x), big_int(y));
return is_valid_division_to_nearest(x, y, q, r, quotient_like_signed);
}
}
template<class T>
constexpr bool is_valid_division_ties_to_neg_inf(T x, T y, T q, T r) {
if constexpr (std::is_signed_v<T>) {
return is_valid_division_to_nearest(x, y, q, r, __sgn2(r) == __sgn2(y));
} else {
return is_valid_division_to_nearest(x, y, q, r, r < y);
}
}
template<class T>
constexpr bool is_valid_division_ties_to_odd(T x, T y, T q, T r) {
return is_valid_division_to_nearest(x, y, q, r, q % 2 != 0);
}
template<class T>
constexpr bool is_valid_division_ties_to_even(T x, T y, T q, T r) {
return is_valid_division_to_nearest(x, y, q, r, q % 2 == 0);
}
using rng_type = std::default_random_engine;
template<class T, int SignedValue>
constexpr T signed_or_zero() {
if constexpr (std::is_signed_v<T>)
return T(SignedValue);
else
return T(0);
}
template<class T>
inline auto interesting_values = [] {
if constexpr (std::is_signed_v<T>) {
return std::array{
T{std::numeric_limits<T>::min() + 2},
T{std::numeric_limits<T>::min() + 1},
T{std::numeric_limits<T>::min()},
T{-2},
T{-1},
T{0},
T{1},
T{2},
T{std::numeric_limits<T>::max() - 2},
T{std::numeric_limits<T>::max() - 1},
T{std::numeric_limits<T>::max()},
};
} else {
return std::array{
T{0},
T{1},
T{2},
T{3},
T{4},
T{5},
T{6},
T{7},
T{std::numeric_limits<T>::max() - 2},
T{std::numeric_limits<T>::max() - 1},
T{std::numeric_limits<T>::max()},
};
}
}();
template<class T, div_result<T> (&div_rem)(T, T), bool (&verify)(T, T, T, T)>
void check_interesting() {
for (const T& x : interesting_values<T>) {
for (const T& y : interesting_values<T>) {
if (!is_div_defined(x, y)) continue;
const auto [q, r] = div_rem(x, y);
if (!verify(x, y, q, r)) {
std::cout << "failed for (" << x << " / " << y << ") = " << q << " R " << r << "\n";
std::exit(1);
}
}
}
}
template<class T, div_result<T> (&div_rem)(T, T), bool (&verify)(T, T, T, T)>
void sample(rng_type& rng, std::uniform_int_distribution<T>& distr, int samples) {
for (int i = 0; i < samples; ++i) {
const T x = distr(rng);
const T y = distr(rng);
if (!is_div_defined(x, y)) continue;
const auto [q, r] = div_rem(x, y);
if (!verify(x, y, q, r)) {
std::cout << "failed for (" << x << " / " << y << ") = " << q << " R " << r << "\n";
std::exit(1);
}
}
}
constexpr int full_samples =
#ifndef NDEBUG
1'000'000;
#else
10'000'000;
#endif
template<class T, div_result<T> (&div_rem)(T, T), bool (&verify)(T, T, T, T)>
void fuzz_test(std::string_view name) {
std::cout << name << " ... " << std::flush;
check_interesting<T, div_rem, verify>();
std::default_random_engine rng{12345};
constexpr T tiny_min = signed_or_zero<T, -4>();
std::uniform_int_distribution<T> distr_tiny{tiny_min, T(4)};
sample<T, div_rem, verify>(rng, distr_tiny, 100);
constexpr T small_min = signed_or_zero<T, -100>();
std::uniform_int_distribution<T> distr_small{small_min, T(100)};
sample<T, div_rem, verify>(rng, distr_small, 100'000);
std::uniform_int_distribution<T> distr_full;
sample<T, div_rem, verify>(rng, distr_full, full_samples);
std::cout << "OK" << std::endl;
}
template<class T>
void fuzz_test_mod(std::string_view name) {
std::cout << name << " ... ";
std::default_random_engine rng{12345};
std::uniform_int_distribution<T> distr_full;
for (int i = 0; i < full_samples; ++i) {
T x = distr_full(rng);
T y = distr_full(rng);
if (!is_div_defined(x, y)) continue;
T r = mod(x, y);
if (r != div_rem_to_neg_inf(x, y).remainder) {
std::cout << "failure for (" << x << " mod " << y << ") = " << r << '\n';
std::exit(1);
}
}
std::cout << "OK" << std::endl;
}
template<class T>
void fuzz_test_euclid_projection(std::string_view name) {
std::cout << name << " ... " << std::flush;
auto verify = [](T x, T y) {
const auto result = div_rem_euclid(x, y);
return div_euclid(x, y) == result.quotient && rem_euclid(x, y) == result.remainder;
};
for (const T& x : interesting_values<T>) {
for (const T& y : interesting_values<T>) {
if (!is_div_defined(x, y)) continue;
if (!verify(x, y)) {
std::cout << "failed for (" << x << " / " << y << ")\n";
std::exit(1);
}
}
}
std::default_random_engine rng{12345};
constexpr T tiny_min = signed_or_zero<T, -4>();
std::uniform_int_distribution<T> distr_tiny{tiny_min, T(4)};
for (int i = 0; i < 100; ++i) {
const T x = distr_tiny(rng);
const T y = distr_tiny(rng);
if (!is_div_defined(x, y)) continue;
if (!verify(x, y)) {
std::cout << "failed for (" << x << " / " << y << ")\n";
std::exit(1);
}
}
std::uniform_int_distribution<T> distr_full;
for (int i = 0; i < full_samples; ++i) {
const T x = distr_full(rng);
const T y = distr_full(rng);
if (!is_div_defined(x, y)) continue;
if (!verify(x, y)) {
std::cout << "failed for (" << x << " / " << y << ")\n";
std::exit(1);
}
}
std::cout << "OK" << std::endl;
}
template<class T>
void fuzz_test_rem_euclid(std::string_view name) {
std::cout << name << " ... " << std::flush;
std::default_random_engine rng{12345};
std::uniform_int_distribution<T> distr_full;
for (int i = 0; i < full_samples; ++i) {
T x = distr_full(rng);
T y = distr_full(rng);
if (!is_div_defined(x, y)) continue;
const auto result = div_rem_euclid(x, y);
T r = rem_euclid(x, y);
if (r != result.remainder) {
std::cout << "failure for (" << x << " rem_euclid " << y << ") = " << r << '\n';
std::exit(1);
}
if (!is_valid_euclid_remainder(y, r)) {
std::cout << "failure for (" << x << " rem_euclid " << y << ") = " << r << '\n';
std::exit(1);
}
}
std::cout << "OK" << std::endl;
}
#define RUN_TEST(type, div_rem, verify) fuzz_test<type, div_rem, verify>(#div_rem "<" #type ">")
int main() {
RUN_TEST(int, div_rem_to_zero, is_valid_division_to_zero);
RUN_TEST(int, div_rem_away_zero, is_valid_division_away_zero);
RUN_TEST(int, div_rem_to_pos_inf, is_valid_division_to_pos_inf);
RUN_TEST(int, div_rem_to_neg_inf, is_valid_division_to_neg_inf);
RUN_TEST(int, div_rem_euclid, is_valid_division_euclid);
RUN_TEST(int, div_rem_to_odd, is_valid_division_to_odd);
RUN_TEST(int, div_rem_to_even, is_valid_division_to_even);
RUN_TEST(int, div_rem_ties_to_zero, is_valid_division_ties_to_zero);
RUN_TEST(int, div_rem_ties_away_zero, is_valid_division_ties_away_zero);
RUN_TEST(int, div_rem_ties_to_pos_inf, is_valid_division_ties_to_pos_inf);
RUN_TEST(int, div_rem_ties_to_neg_inf, is_valid_division_ties_to_neg_inf);
RUN_TEST(int, div_rem_ties_to_odd, is_valid_division_ties_to_odd);
RUN_TEST(int, div_rem_ties_to_even, is_valid_division_ties_to_even);
RUN_TEST(unsigned, div_rem_to_zero, is_valid_division_to_zero);
RUN_TEST(unsigned, div_rem_away_zero, is_valid_division_away_zero);
RUN_TEST(unsigned, div_rem_to_pos_inf, is_valid_division_to_pos_inf);
RUN_TEST(unsigned, div_rem_to_neg_inf, is_valid_division_to_neg_inf);
RUN_TEST(unsigned, div_rem_euclid, is_valid_division_euclid);
RUN_TEST(unsigned, div_rem_to_odd, is_valid_division_to_odd);
RUN_TEST(unsigned, div_rem_to_even, is_valid_division_to_even);
RUN_TEST(unsigned, div_rem_ties_to_zero, is_valid_division_ties_to_zero);
RUN_TEST(unsigned, div_rem_ties_away_zero, is_valid_division_ties_away_zero);
RUN_TEST(unsigned, div_rem_ties_to_pos_inf, is_valid_division_ties_to_pos_inf);
RUN_TEST(unsigned, div_rem_ties_to_neg_inf, is_valid_division_ties_to_neg_inf);
RUN_TEST(unsigned, div_rem_ties_to_odd, is_valid_division_ties_to_odd);
RUN_TEST(unsigned, div_rem_ties_to_even, is_valid_division_ties_to_even);
fuzz_test_mod<int>("mod<int>");
fuzz_test_mod<unsigned>("mod<unsigned>");
fuzz_test_euclid_projection<int>("euclid projections<int>");
fuzz_test_euclid_projection<unsigned>("euclid projections<unsigned>");
fuzz_test_rem_euclid<int>("rem_euclid<int>");
fuzz_test_rem_euclid<unsigned>("rem_euclid<unsigned>");
}