-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_bit.cpp
More file actions
317 lines (256 loc) · 7.42 KB
/
test_bit.cpp
File metadata and controls
317 lines (256 loc) · 7.42 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
// Copyright 2025 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/lightweight_test.hpp>
#ifndef BOOST_INT128_BUILD_MODULE
#include <boost/int128/int128.hpp>
#include <boost/int128/bit.hpp>
#include <boost/int128/iostream.hpp>
#else
import boost.int128;
#endif
void test_has_single_bit()
{
boost::int128::uint128_t x {0};
BOOST_TEST(!boost::int128::has_single_bit(x));
++x;
for (unsigned i {1}; i < 128U; ++i)
{
BOOST_TEST(boost::int128::has_single_bit(x));
x <<= 1;
}
x = 3;
for (unsigned i {1}; i < 128U; ++i)
{
BOOST_TEST(!boost::int128::has_single_bit(x));
x <<= 1;
}
}
void test_countl_zero()
{
BOOST_TEST_EQ(boost::int128::countl_zero(0), 128);
boost::int128::uint128_t x {1};
for (unsigned i {1}; i < 128U; ++i)
{
BOOST_TEST_EQ(boost::int128::countl_zero(x), 128 - i);
x <<= 1;
}
}
void test_bit_width()
{
BOOST_TEST_EQ(boost::int128::bit_width(boost::int128::uint128_t{0}), 0);
boost::int128::uint128_t x {1};
for (unsigned i {1}; i < 128U; ++i)
{
BOOST_TEST_EQ(boost::int128::bit_width(x), i);
x <<= 1;
}
x = 3;
for (unsigned i {2}; i < 128U; ++i)
{
BOOST_TEST_EQ(boost::int128::bit_width(x), i);
x <<= 1;
}
}
void test_bit_ceil()
{
BOOST_TEST_EQ(boost::int128::bit_ceil(0), 1U);
BOOST_TEST_EQ(boost::int128::bit_ceil(1), 1U);
BOOST_TEST_EQ(boost::int128::bit_ceil(2), 2U);
boost::int128::uint128_t x {3};
boost::int128::uint128_t y {4};
for (unsigned i {4}; i < 128U; ++i)
{
BOOST_TEST_EQ(boost::int128::bit_ceil(x), y);
x <<= 1;
y <<= 1;
}
}
void test_bit_floor()
{
BOOST_TEST_EQ(boost::int128::bit_floor(0), 0U);
boost::int128::uint128_t x {3};
boost::int128::uint128_t y {2};
for (unsigned i {2}; i < 127U; ++i)
{
BOOST_TEST_EQ(boost::int128::bit_floor(x), y);
x <<= 1;
y <<= 1;
}
}
void test_countl_one()
{
BOOST_TEST_EQ(boost::int128::countl_one(0), 0);
boost::int128::uint128_t x {UINT64_MAX, UINT64_MAX};
for (int i {128}; i >= 0; --i)
{
BOOST_TEST_EQ(boost::int128::countl_one(x), i);
x <<= 1;
}
}
void test_countr_zero()
{
BOOST_TEST_EQ(boost::int128::countr_zero(0), 128);
boost::int128::uint128_t x {0x8000000000000000ULL, 0};
for (int i {127}; i >= 0; --i)
{
BOOST_TEST_EQ(boost::int128::countr_zero(x), i);
x >>= 1;
}
}
void test_countr_one()
{
BOOST_TEST_EQ(boost::int128::countr_one(0), 0);
boost::int128::uint128_t x {UINT64_MAX, UINT64_MAX};
for (int i {128}; i >= 0; --i)
{
BOOST_TEST_EQ(boost::int128::countr_one(x), i);
x >>= 1;
}
}
void test_rotl()
{
constexpr boost::int128::uint128_t x {1};
boost::int128::uint128_t y {1};
for (int i {0}; i < 128; ++i)
{
BOOST_TEST(boost::int128::rotl(x, i) == y);
y <<= 1;
}
}
void test_rotr()
{
constexpr boost::int128::uint128_t x {0x8000000000000000ULL, 0};
boost::int128::uint128_t y {0x8000000000000000ULL, 0};
for (int i {0}; i < 128; ++i)
{
BOOST_TEST(boost::int128::rotr(x, i) == y);
y >>= 1;
}
}
void test_popcount()
{
BOOST_TEST_EQ(boost::int128::popcount(0), 0);
boost::int128::uint128_t x {0, 2};
for (int i {1}; i < 128; ++i)
{
BOOST_TEST_EQ(boost::int128::popcount(x - 1U), i);
const auto temp {x - 1U};
BOOST_TEST_EQ(boost::int128::impl::popcount_impl(temp.high) + boost::int128::impl::popcount_impl(temp.low), i);
x <<= 1;
}
constexpr boost::int128::uint128_t y {1};
static_assert(boost::int128::popcount(y) == 1, "Wrong popcount");
}
void test_byteswap()
{
// Test 1: Basic test with specific byte values
{
// Create a value with distinct byte pattern
boost::int128::uint128_t original{
0x0123456789ABCDEFULL,
0xFEDCBA9876543210ULL
};
// Expected result after byteswap
boost::int128::uint128_t expected{
0x1032547698BADCFEULL,
0xEFCDAB8967452301ULL
};
BOOST_TEST(boost::int128::byteswap(original) == expected);
BOOST_TEST(boost::int128::byteswap(expected) == original);
}
// Test 2: Verify double byteswap returns original
{
boost::int128::uint128_t values[] = {
{0, 0}, // All zeros
{~0ULL, ~0ULL}, // All ones
{0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL}, // Mixed pattern
{1ULL, 0}, // Single bit in high
{0, 1ULL} // Single bit in low
};
for (const auto& val : values) {
BOOST_TEST(boost::int128::byteswap(boost::int128::byteswap(val)) == val);
}
}
// Test 3: Verify each byte position is correctly swapped
for (int i = 0; i < 16; ++i)
{
// Set a single byte to 0xFF
boost::int128::uint128_t input{0, 0};
auto bytes = reinterpret_cast<uint8_t*>(&input);
bytes[i] = 0xFF;
// After byteswap, the 0xFF should be at position (15-i)
boost::int128::uint128_t result = boost::int128::byteswap(input);
auto result_bytes = reinterpret_cast<uint8_t*>(&result);
for (int j = 0; j < 16; ++j)
{
if (j == 15 - i)
{
BOOST_TEST(result_bytes[j] == 0xFF);
}
else
{
BOOST_TEST(result_bytes[j] == 0);
}
}
}
// Test 4: Check the backup impls
{
// Create a value with distinct byte pattern
constexpr boost::int128::uint128_t original{
0x0123456789ABCDEFULL,
0xFEDCBA9876543210ULL
};
// Expected result after byteswap
constexpr boost::int128::uint128_t expected{
0x1032547698BADCFEULL,
0xEFCDAB8967452301ULL
};
static_assert(boost::int128::byteswap(original) == expected, "wrong");
static_assert(boost::int128::byteswap(expected) == original, "wrong");
BOOST_TEST(boost::int128::impl::byteswap_impl(original) == expected);
BOOST_TEST(boost::int128::impl::byteswap_impl(expected) == original);
}
}
void test_clz()
{
unsigned int x {};
std::uint32_t y {};
BOOST_TEST_EQ(boost::int128::detail::impl::countl_impl(x), boost::int128::detail::impl::backup_countl_impl(y));
x = 1;
y = 1;
for (int i {1}; i < 16; ++i)
{
BOOST_TEST_EQ(boost::int128::detail::impl::countl_impl(x), boost::int128::detail::impl::backup_countl_impl(y));
x <<= 1;
y <<= 1;
}
}
void test_bit_scan_reverse()
{
std::uint64_t x {0x8000000000000000};
for (int i {63}; i > 0; --i)
{
BOOST_TEST_EQ(boost::int128::detail::impl::bit_scan_reverse(x), i);
x >>= 1;
}
}
int main()
{
test_has_single_bit();
test_countl_zero();
test_bit_width();
test_bit_ceil();
test_bit_floor();
test_countl_one();
test_countl_zero();
test_countr_zero();
test_countr_one();
test_rotl();
test_rotr();
test_popcount();
test_byteswap();
test_clz();
test_bit_scan_reverse();
return boost::report_errors();
}