-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfixdiv_tests.cpp
More file actions
284 lines (212 loc) · 8.73 KB
/
fixdiv_tests.cpp
File metadata and controls
284 lines (212 loc) · 8.73 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
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Simple Long Integer Math for C++
// version 2.0
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2020-2026 Yury Kalmykov <y_kalmykov@mail.ru>.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "test_type_sets.h"
#include <slimcpplib/long_fixdiv.h>
#include <gtest/gtest.h>
using namespace slim::literals;
namespace slim
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// standalone functions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename uint_t>
void run_128_unsigned_divide_by_one_tests()
{
// divisor = 1: identity for all values
const auto divider = make_fixed_divider(uint_t(1));
const uint_t large_value = uint_t(12345) + (uint_t(67890) << 32) + (uint_t(13579) << 64) + (uint_t(24680) << 96);
ASSERT_EQ(uint_t(0) / divider, uint_t(0));
ASSERT_EQ(uint_t(1) / divider, uint_t(1));
ASSERT_EQ(uint_t(2) / divider, uint_t(2));
ASSERT_EQ(large_value / divider, large_value);
ASSERT_EQ(uint_t(-1) / divider, uint_t(-1));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename uint_t>
void run_128_unsigned_divide_by_power_of_two_tests()
{
// power-of-2 divisors use shift-only optimization branch
const auto divider_2 = make_fixed_divider(uint_t(2));
const auto divider_256 = make_fixed_divider(uint_t(256));
const auto divider_2_pow_32 = make_fixed_divider(uint_t(1) << 32);
const uint_t value = uint_t(12345) + (uint_t(67890) << 32) + (uint_t(13579) << 64) + (uint_t(24680) << 96);
ASSERT_EQ(value / divider_2, value / uint_t(2));
ASSERT_EQ(value / divider_256, value / uint_t(256));
ASSERT_EQ(value / divider_2_pow_32, value / (uint_t(1) << 32));
// boundary values
ASSERT_EQ(uint_t(255) / divider_256, uint_t(0));
ASSERT_EQ(uint_t(256) / divider_256, uint_t(1));
ASSERT_EQ(uint_t(512) / divider_256, uint_t(2));
ASSERT_EQ(uint_t(513) / divider_256, uint_t(2));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename uint_t>
void run_128_unsigned_consistency_sweep_tests()
{
// verify fixed divider matches operator/ for representative divisors
const uint_t divisors[] = {
uint_t(3),
uint_t(7),
uint_t(1234567890u),
uint_t(1) << 32,
(uint_t(1) << 64) + uint_t(3)
};
const uint_t dividends[] = {
uint_t(0),
uint_t(1),
uint_t(999),
uint_t(12345) + (uint_t(67890) << 32) + (uint_t(13579) << 64) + (uint_t(24680) << 96),
uint_t(-1)
};
for (const auto& divisor : divisors) {
const auto divider = make_fixed_divider(divisor);
for (const auto& dividend : dividends)
ASSERT_EQ(dividend / divider, dividend / divisor);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename uint_t>
void run_128_unsigned_swap_tests()
{
// prepare representative dividends and dividers with different division paths
const auto dividend1 = uint_t(12345) + (uint_t(67890) << 32) + (uint_t(13579) << 64) + (uint_t(24680) << 96);
const auto dividend2 = uint_t(-1);
const uint_t divisor1 = 7;
const uint_t divisor2 = (uint_t(1) << 64) + uint_t(3);
auto divider1 = make_fixed_divider(divisor1);
auto divider2 = make_fixed_divider(divisor2);
// swap divider internals
divider1.swap(divider2);
// verify that swapped dividers now behave as the opposite divisors
ASSERT_EQ(dividend1 / divider1, dividend1 / divisor2);
ASSERT_EQ(dividend2 / divider1, dividend2 / divisor2);
ASSERT_EQ(dividend1 / divider2, dividend1 / divisor1);
ASSERT_EQ(dividend2 / divider2, dividend2 / divisor1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename uint_t>
void run_256_unsigned_fixed_divider_boundary_tests()
{
// representative dividends cover zero, low values and high-bit boundaries
const uint_t dividends[] = {
uint_t(0),
uint_t(1),
uint_t(2),
uint_t(-1),
(uint_t(1) << 255) - 1,
uint_t(1) << 255,
(uint_t(1) << 255) + 1,
uint_t(0x0123456789abcdefull) + (uint_t(0xfedcba9876543210ull) << 64) + (uint_t(0x0f1e2d3c4b5a6978ull) << 128) + (uint_t(0x8877665544332211ull) << 192)
};
// representative divisors cover trivial, native-width and top-bit cases
const uint_t divisors[] = {
uint_t(1),
uint_t(2),
uint_t(3),
uint_t(native_word_t<uint_t>(-1)),
uint_t(1) << 64,
(uint_t(1) << 64) - 1,
(uint_t(1) << 64) + 1,
uint_t(1) << 255,
(uint_t(1) << 255) - 1
};
for (const auto& divisor : divisors) {
const auto divider = make_fixed_divider(divisor);
// fixed-divider quotients must match operator/ and preserve div/mod invariants
for (const auto& dividend : dividends) {
const uint_t quotient = dividend / divider;
const uint_t remainder = dividend % divisor;
ASSERT_EQ(quotient, dividend / divisor);
ASSERT_EQ(quotient * divisor + remainder, dividend);
ASSERT_LT(remainder, divisor);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// fixdiv_tests
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, family_128_unsigned_divide_by_one)
{
run_128_unsigned_divide_by_one_tests<uint128_t>();
run_128_unsigned_divide_by_one_tests<uint32x4_t>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, family_128_unsigned_divide_by_power_of_two)
{
run_128_unsigned_divide_by_power_of_two_tests<uint128_t>();
run_128_unsigned_divide_by_power_of_two_tests<uint32x4_t>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, divide_by_large_prime)
{
// prime divisor: exercises the general Granlund-Montgomery branch
// 2^64 - 59 is prime
constexpr uint128_t prime = uint128_t(0xffffffffffffffc5ull);
const auto fdp = make_fixed_divider(prime);
const uint128_t values[] = {
uint128_t(0),
uint128_t(1),
prime - 1,
prime,
prime + 1,
prime * 2,
prime * 2 + 1,
0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128,
uint128_t(-1)
};
for (const auto& val : values)
ASSERT_EQ(val / fdp, val / prime);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, divide_by_large_divisor)
{
// divisor > 2^63: requires the addition != 0 path in divide()
constexpr uint128_t big = uint128_t(0x8000000000000001ull); // 2^63 + 1
const auto fdb = make_fixed_divider(big);
const uint128_t values[] = {
uint128_t(0),
uint128_t(1),
big - 1,
big,
big + 1,
big * 2,
0xf473e8e5'f6e812c3'fde4523b'51b6d251_ui128,
uint128_t(-1)
};
for (const auto& val : values)
ASSERT_EQ(val / fdb, val / big);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, family_128_unsigned_consistency_sweep)
{
run_128_unsigned_consistency_sweep_tests<uint128_t>();
run_128_unsigned_consistency_sweep_tests<uint32x4_t>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, family_128_unsigned_swap)
{
run_128_unsigned_swap_tests<uint128_t>();
run_128_unsigned_swap_tests<uint32x4_t>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(fixdiv_tests, family_256_unsigned_fixed_divider_boundary_values)
{
run_256_unsigned_fixed_divider_boundary_tests<uint256_t>();
run_256_unsigned_fixed_divider_boundary_tests<uint32x8_t>();
run_256_unsigned_fixed_divider_boundary_tests<uint64x4_t>();
}
} // namespace slim
////////////////////////////////////////////////////////////////////////////////////////////////////
// fixdiv_tests.cpp
////////////////////////////////////////////////////////////////////////////////////////////////////