1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/numeric/int128.h"
16
17 #include <algorithm>
18 #include <limits>
19 #include <random>
20 #include <tuple>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24
25 #include "gtest/gtest.h"
26 #include "absl/base/casts.h"
27 #include "absl/base/internal/cycleclock.h"
28 #include "absl/hash/hash_testing.h"
29 #include "absl/meta/type_traits.h"
30 #include "absl/types/compare.h"
31
32 #define MAKE_INT128(HI, LO) absl::MakeInt128(static_cast<int64_t>(HI), LO)
33
34 namespace {
35
36 template <typename T>
37 class Uint128IntegerTraitsTest : public ::testing::Test {};
38 typedef ::testing::Types<bool, char, signed char, unsigned char, char16_t,
39 char32_t, wchar_t,
40 short, // NOLINT(runtime/int)
41 unsigned short, // NOLINT(runtime/int)
42 int, unsigned int,
43 long, // NOLINT(runtime/int)
44 unsigned long, // NOLINT(runtime/int)
45 long long, // NOLINT(runtime/int)
46 unsigned long long> // NOLINT(runtime/int)
47 IntegerTypes;
48
49 template <typename T>
50 class Uint128FloatTraitsTest : public ::testing::Test {};
51 typedef ::testing::Types<float, double, long double> FloatingPointTypes;
52
53 TYPED_TEST_SUITE(Uint128IntegerTraitsTest, IntegerTypes);
54
TYPED_TEST(Uint128IntegerTraitsTest,ConstructAssignTest)55 TYPED_TEST(Uint128IntegerTraitsTest, ConstructAssignTest) {
56 static_assert(std::is_constructible<absl::uint128, TypeParam>::value,
57 "absl::uint128 must be constructible from TypeParam");
58 static_assert(std::is_assignable<absl::uint128&, TypeParam>::value,
59 "absl::uint128 must be assignable from TypeParam");
60 static_assert(!std::is_assignable<TypeParam&, absl::uint128>::value,
61 "TypeParam must not be assignable from absl::uint128");
62 }
63
64 TYPED_TEST_SUITE(Uint128FloatTraitsTest, FloatingPointTypes);
65
TYPED_TEST(Uint128FloatTraitsTest,ConstructAssignTest)66 TYPED_TEST(Uint128FloatTraitsTest, ConstructAssignTest) {
67 static_assert(std::is_constructible<absl::uint128, TypeParam>::value,
68 "absl::uint128 must be constructible from TypeParam");
69 static_assert(!std::is_assignable<absl::uint128&, TypeParam>::value,
70 "absl::uint128 must not be assignable from TypeParam");
71 static_assert(!std::is_assignable<TypeParam&, absl::uint128>::value,
72 "TypeParam must not be assignable from absl::uint128");
73 }
74
75 #ifdef ABSL_HAVE_INTRINSIC_INT128
76 // These type traits done separately as TYPED_TEST requires typeinfo, and not
77 // all platforms have this for __int128 even though they define the type.
TEST(Uint128,IntrinsicTypeTraitsTest)78 TEST(Uint128, IntrinsicTypeTraitsTest) {
79 static_assert(std::is_constructible<absl::uint128, __int128>::value,
80 "absl::uint128 must be constructible from __int128");
81 static_assert(std::is_assignable<absl::uint128&, __int128>::value,
82 "absl::uint128 must be assignable from __int128");
83 static_assert(!std::is_assignable<__int128&, absl::uint128>::value,
84 "__int128 must not be assignable from absl::uint128");
85
86 static_assert(std::is_constructible<absl::uint128, unsigned __int128>::value,
87 "absl::uint128 must be constructible from unsigned __int128");
88 static_assert(std::is_assignable<absl::uint128&, unsigned __int128>::value,
89 "absl::uint128 must be assignable from unsigned __int128");
90 static_assert(!std::is_assignable<unsigned __int128&, absl::uint128>::value,
91 "unsigned __int128 must not be assignable from absl::uint128");
92 }
93 #endif // ABSL_HAVE_INTRINSIC_INT128
94
TEST(Uint128,TrivialTraitsTest)95 TEST(Uint128, TrivialTraitsTest) {
96 static_assert(absl::is_trivially_default_constructible<absl::uint128>::value,
97 "");
98 static_assert(absl::is_trivially_copy_constructible<absl::uint128>::value,
99 "");
100 static_assert(absl::is_trivially_copy_assignable<absl::uint128>::value, "");
101 static_assert(std::is_trivially_destructible<absl::uint128>::value, "");
102 }
103
TEST(Uint128,AllTests)104 TEST(Uint128, AllTests) {
105 absl::uint128 zero = 0;
106 absl::uint128 one = 1;
107 absl::uint128 one_2arg = absl::MakeUint128(0, 1);
108 absl::uint128 two = 2;
109 absl::uint128 three = 3;
110 absl::uint128 big = absl::MakeUint128(2000, 2);
111 absl::uint128 big_minus_one = absl::MakeUint128(2000, 1);
112 absl::uint128 bigger = absl::MakeUint128(2001, 1);
113 absl::uint128 biggest = absl::Uint128Max();
114 absl::uint128 high_low = absl::MakeUint128(1, 0);
115 absl::uint128 low_high =
116 absl::MakeUint128(0, std::numeric_limits<uint64_t>::max());
117 EXPECT_LT(one, two);
118 EXPECT_GT(two, one);
119 EXPECT_LT(one, big);
120 EXPECT_LT(one, big);
121 EXPECT_EQ(one, one_2arg);
122 EXPECT_NE(one, two);
123 EXPECT_GT(big, one);
124 EXPECT_GE(big, two);
125 EXPECT_GE(big, big_minus_one);
126 EXPECT_GT(big, big_minus_one);
127 EXPECT_LT(big_minus_one, big);
128 EXPECT_LE(big_minus_one, big);
129 EXPECT_NE(big_minus_one, big);
130 EXPECT_LT(big, biggest);
131 EXPECT_LE(big, biggest);
132 EXPECT_GT(biggest, big);
133 EXPECT_GE(biggest, big);
134 EXPECT_EQ(big, ~~big);
135 EXPECT_EQ(one, one | one);
136 EXPECT_EQ(big, big | big);
137 EXPECT_EQ(one, one | zero);
138 EXPECT_EQ(one, one & one);
139 EXPECT_EQ(big, big & big);
140 EXPECT_EQ(zero, one & zero);
141 EXPECT_EQ(zero, big & ~big);
142 EXPECT_EQ(zero, one ^ one);
143 EXPECT_EQ(zero, big ^ big);
144 EXPECT_EQ(one, one ^ zero);
145
146 // Shift operators.
147 EXPECT_EQ(big, big << 0);
148 EXPECT_EQ(big, big >> 0);
149 EXPECT_GT(big << 1, big);
150 EXPECT_LT(big >> 1, big);
151 EXPECT_EQ(big, (big << 10) >> 10);
152 EXPECT_EQ(big, (big >> 1) << 1);
153 EXPECT_EQ(one, (one << 80) >> 80);
154 EXPECT_EQ(zero, (one >> 80) << 80);
155
156 // Shift assignments.
157 absl::uint128 big_copy = big;
158 EXPECT_EQ(big << 0, big_copy <<= 0);
159 big_copy = big;
160 EXPECT_EQ(big >> 0, big_copy >>= 0);
161 big_copy = big;
162 EXPECT_EQ(big << 1, big_copy <<= 1);
163 big_copy = big;
164 EXPECT_EQ(big >> 1, big_copy >>= 1);
165 big_copy = big;
166 EXPECT_EQ(big << 10, big_copy <<= 10);
167 big_copy = big;
168 EXPECT_EQ(big >> 10, big_copy >>= 10);
169 big_copy = big;
170 EXPECT_EQ(big << 64, big_copy <<= 64);
171 big_copy = big;
172 EXPECT_EQ(big >> 64, big_copy >>= 64);
173 big_copy = big;
174 EXPECT_EQ(big << 73, big_copy <<= 73);
175 big_copy = big;
176 EXPECT_EQ(big >> 73, big_copy >>= 73);
177
178 EXPECT_EQ(absl::Uint128High64(biggest), std::numeric_limits<uint64_t>::max());
179 EXPECT_EQ(absl::Uint128Low64(biggest), std::numeric_limits<uint64_t>::max());
180 EXPECT_EQ(zero + one, one);
181 EXPECT_EQ(one + one, two);
182 EXPECT_EQ(big_minus_one + one, big);
183 EXPECT_EQ(one - one, zero);
184 EXPECT_EQ(one - zero, one);
185 EXPECT_EQ(zero - one, biggest);
186 EXPECT_EQ(big - big, zero);
187 EXPECT_EQ(big - one, big_minus_one);
188 EXPECT_EQ(big + std::numeric_limits<uint64_t>::max(), bigger);
189 EXPECT_EQ(biggest + 1, zero);
190 EXPECT_EQ(zero - 1, biggest);
191 EXPECT_EQ(high_low - one, low_high);
192 EXPECT_EQ(low_high + one, high_low);
193 EXPECT_EQ(absl::Uint128High64((absl::uint128(1) << 64) - 1), 0);
194 EXPECT_EQ(absl::Uint128Low64((absl::uint128(1) << 64) - 1),
195 std::numeric_limits<uint64_t>::max());
196 EXPECT_TRUE(!!one);
197 EXPECT_TRUE(!!high_low);
198 EXPECT_FALSE(!!zero);
199 EXPECT_FALSE(!one);
200 EXPECT_FALSE(!high_low);
201 EXPECT_TRUE(!zero);
202 EXPECT_TRUE(zero == 0); // NOLINT(readability/check)
203 EXPECT_FALSE(zero != 0); // NOLINT(readability/check)
204 EXPECT_FALSE(one == 0); // NOLINT(readability/check)
205 EXPECT_TRUE(one != 0); // NOLINT(readability/check)
206 EXPECT_FALSE(high_low == 0); // NOLINT(readability/check)
207 EXPECT_TRUE(high_low != 0); // NOLINT(readability/check)
208
209 absl::uint128 test = zero;
210 EXPECT_EQ(++test, one);
211 EXPECT_EQ(test, one);
212 EXPECT_EQ(test++, one);
213 EXPECT_EQ(test, two);
214 EXPECT_EQ(test -= 2, zero);
215 EXPECT_EQ(test, zero);
216 EXPECT_EQ(test += 2, two);
217 EXPECT_EQ(test, two);
218 EXPECT_EQ(--test, one);
219 EXPECT_EQ(test, one);
220 EXPECT_EQ(test--, one);
221 EXPECT_EQ(test, zero);
222 EXPECT_EQ(test |= three, three);
223 EXPECT_EQ(test &= one, one);
224 EXPECT_EQ(test ^= three, two);
225 EXPECT_EQ(test >>= 1, one);
226 EXPECT_EQ(test <<= 1, two);
227
228 EXPECT_EQ(big, +big);
229 EXPECT_EQ(two, +two);
230 EXPECT_EQ(absl::Uint128Max(), +absl::Uint128Max());
231 EXPECT_EQ(zero, +zero);
232
233 EXPECT_EQ(big, -(-big));
234 EXPECT_EQ(two, -((-one) - 1));
235 EXPECT_EQ(absl::Uint128Max(), -one);
236 EXPECT_EQ(zero, -zero);
237 }
238
TEST(Int128,RightShiftOfNegativeNumbers)239 TEST(Int128, RightShiftOfNegativeNumbers) {
240 absl::int128 minus_six = -6;
241 absl::int128 minus_three = -3;
242 absl::int128 minus_two = -2;
243 absl::int128 minus_one = -1;
244 if ((-6 >> 1) == -3) {
245 // Right shift is arithmetic (sign propagates)
246 EXPECT_EQ(minus_six >> 1, minus_three);
247 EXPECT_EQ(minus_six >> 2, minus_two);
248 EXPECT_EQ(minus_six >> 65, minus_one);
249 } else {
250 // Right shift is logical (zeros shifted in at MSB)
251 EXPECT_EQ(minus_six >> 1, absl::int128(absl::uint128(minus_six) >> 1));
252 EXPECT_EQ(minus_six >> 2, absl::int128(absl::uint128(minus_six) >> 2));
253 EXPECT_EQ(minus_six >> 65, absl::int128(absl::uint128(minus_six) >> 65));
254 }
255 }
256
TEST(Uint128,ConversionTests)257 TEST(Uint128, ConversionTests) {
258 EXPECT_TRUE(absl::MakeUint128(1, 0));
259
260 #ifdef ABSL_HAVE_INTRINSIC_INT128
261 unsigned __int128 intrinsic =
262 (static_cast<unsigned __int128>(0x3a5b76c209de76f6) << 64) +
263 0x1f25e1d63a2b46c5;
264 absl::uint128 custom =
265 absl::MakeUint128(0x3a5b76c209de76f6, 0x1f25e1d63a2b46c5);
266
267 EXPECT_EQ(custom, absl::uint128(intrinsic));
268 EXPECT_EQ(custom, absl::uint128(static_cast<__int128>(intrinsic)));
269 EXPECT_EQ(intrinsic, static_cast<unsigned __int128>(custom));
270 EXPECT_EQ(intrinsic, static_cast<__int128>(custom));
271 #endif // ABSL_HAVE_INTRINSIC_INT128
272
273 // verify that an integer greater than 2**64 that can be stored precisely
274 // inside a double is converted to a absl::uint128 without loss of
275 // information.
276 double precise_double = 0x530e * std::pow(2.0, 64.0) + 0xda74000000000000;
277 absl::uint128 from_precise_double(precise_double);
278 absl::uint128 from_precise_ints =
279 absl::MakeUint128(0x530e, 0xda74000000000000);
280 EXPECT_EQ(from_precise_double, from_precise_ints);
281 EXPECT_DOUBLE_EQ(static_cast<double>(from_precise_ints), precise_double);
282
283 double approx_double =
284 static_cast<double>(0xffffeeeeddddcccc) * std::pow(2.0, 64.0) +
285 static_cast<double>(0xbbbbaaaa99998888);
286 absl::uint128 from_approx_double(approx_double);
287 EXPECT_DOUBLE_EQ(static_cast<double>(from_approx_double), approx_double);
288
289 double round_to_zero = 0.7;
290 double round_to_five = 5.8;
291 double round_to_nine = 9.3;
292 EXPECT_EQ(static_cast<absl::uint128>(round_to_zero), 0);
293 EXPECT_EQ(static_cast<absl::uint128>(round_to_five), 5);
294 EXPECT_EQ(static_cast<absl::uint128>(round_to_nine), 9);
295
296 absl::uint128 highest_precision_in_long_double =
297 ~absl::uint128{} >> (128 - std::numeric_limits<long double>::digits);
298 EXPECT_EQ(highest_precision_in_long_double,
299 static_cast<absl::uint128>(
300 static_cast<long double>(highest_precision_in_long_double)));
301 // Apply a mask just to make sure all the bits are the right place.
302 const absl::uint128 arbitrary_mask =
303 absl::MakeUint128(0xa29f622677ded751, 0xf8ca66add076f468);
304 EXPECT_EQ(highest_precision_in_long_double & arbitrary_mask,
305 static_cast<absl::uint128>(static_cast<long double>(
306 highest_precision_in_long_double & arbitrary_mask)));
307
308 EXPECT_EQ(static_cast<absl::uint128>(-0.1L), 0);
309 }
310
TEST(Uint128,OperatorAssignReturnRef)311 TEST(Uint128, OperatorAssignReturnRef) {
312 absl::uint128 v(1);
313 (v += 4) -= 3;
314 EXPECT_EQ(2, v);
315 }
316
TEST(Uint128,Multiply)317 TEST(Uint128, Multiply) {
318 absl::uint128 a, b, c;
319
320 // Zero test.
321 a = 0;
322 b = 0;
323 c = a * b;
324 EXPECT_EQ(0, c);
325
326 // Max carries.
327 a = absl::uint128(0) - 1;
328 b = absl::uint128(0) - 1;
329 c = a * b;
330 EXPECT_EQ(1, c);
331
332 // Self-operation with max carries.
333 c = absl::uint128(0) - 1;
334 c *= c;
335 EXPECT_EQ(1, c);
336
337 // 1-bit x 1-bit.
338 for (int i = 0; i < 64; ++i) {
339 for (int j = 0; j < 64; ++j) {
340 a = absl::uint128(1) << i;
341 b = absl::uint128(1) << j;
342 c = a * b;
343 EXPECT_EQ(absl::uint128(1) << (i + j), c);
344 }
345 }
346
347 // Verified with dc.
348 a = absl::MakeUint128(0xffffeeeeddddcccc, 0xbbbbaaaa99998888);
349 b = absl::MakeUint128(0x7777666655554444, 0x3333222211110000);
350 c = a * b;
351 EXPECT_EQ(absl::MakeUint128(0x530EDA741C71D4C3, 0xBF25975319080000), c);
352 EXPECT_EQ(0, c - b * a);
353 EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
354
355 // Verified with dc.
356 a = absl::MakeUint128(0x0123456789abcdef, 0xfedcba9876543210);
357 b = absl::MakeUint128(0x02468ace13579bdf, 0xfdb97531eca86420);
358 c = a * b;
359 EXPECT_EQ(absl::MakeUint128(0x97a87f4f261ba3f2, 0x342d0bbf48948200), c);
360 EXPECT_EQ(0, c - b * a);
361 EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
362 }
363
TEST(Uint128,AliasTests)364 TEST(Uint128, AliasTests) {
365 absl::uint128 x1 = absl::MakeUint128(1, 2);
366 absl::uint128 x2 = absl::MakeUint128(2, 4);
367 x1 += x1;
368 EXPECT_EQ(x2, x1);
369
370 absl::uint128 x3 = absl::MakeUint128(1, static_cast<uint64_t>(1) << 63);
371 absl::uint128 x4 = absl::MakeUint128(3, 0);
372 x3 += x3;
373 EXPECT_EQ(x4, x3);
374 }
375
TEST(Uint128,DivideAndMod)376 TEST(Uint128, DivideAndMod) {
377 using std::swap;
378
379 // a := q * b + r
380 absl::uint128 a, b, q, r;
381
382 // Zero test.
383 a = 0;
384 b = 123;
385 q = a / b;
386 r = a % b;
387 EXPECT_EQ(0, q);
388 EXPECT_EQ(0, r);
389
390 a = absl::MakeUint128(0x530eda741c71d4c3, 0xbf25975319080000);
391 q = absl::MakeUint128(0x4de2cab081, 0x14c34ab4676e4bab);
392 b = absl::uint128(0x1110001);
393 r = absl::uint128(0x3eb455);
394 ASSERT_EQ(a, q * b + r); // Sanity-check.
395
396 absl::uint128 result_q, result_r;
397 result_q = a / b;
398 result_r = a % b;
399 EXPECT_EQ(q, result_q);
400 EXPECT_EQ(r, result_r);
401
402 // Try the other way around.
403 swap(q, b);
404 result_q = a / b;
405 result_r = a % b;
406 EXPECT_EQ(q, result_q);
407 EXPECT_EQ(r, result_r);
408 // Restore.
409 swap(b, q);
410
411 // Dividend < divisor; result should be q:0 r:<dividend>.
412 swap(a, b);
413 result_q = a / b;
414 result_r = a % b;
415 EXPECT_EQ(0, result_q);
416 EXPECT_EQ(a, result_r);
417 // Try the other way around.
418 swap(a, q);
419 result_q = a / b;
420 result_r = a % b;
421 EXPECT_EQ(0, result_q);
422 EXPECT_EQ(a, result_r);
423 // Restore.
424 swap(q, a);
425 swap(b, a);
426
427 // Try a large remainder.
428 b = a / 2 + 1;
429 absl::uint128 expected_r =
430 absl::MakeUint128(0x29876d3a0e38ea61, 0xdf92cba98c83ffff);
431 // Sanity checks.
432 ASSERT_EQ(a / 2 - 1, expected_r);
433 ASSERT_EQ(a, b + expected_r);
434 result_q = a / b;
435 result_r = a % b;
436 EXPECT_EQ(1, result_q);
437 EXPECT_EQ(expected_r, result_r);
438 }
439
TEST(Uint128,DivideAndModRandomInputs)440 TEST(Uint128, DivideAndModRandomInputs) {
441 const int kNumIters = 1 << 18;
442 std::minstd_rand random(testing::UnitTest::GetInstance()->random_seed());
443 std::uniform_int_distribution<uint64_t> uniform_uint64;
444 for (int i = 0; i < kNumIters; ++i) {
445 const absl::uint128 a =
446 absl::MakeUint128(uniform_uint64(random), uniform_uint64(random));
447 const absl::uint128 b =
448 absl::MakeUint128(uniform_uint64(random), uniform_uint64(random));
449 if (b == 0) {
450 continue; // Avoid a div-by-zero.
451 }
452 const absl::uint128 q = a / b;
453 const absl::uint128 r = a % b;
454 ASSERT_EQ(a, b * q + r);
455 }
456 }
457
TEST(Uint128,ConstexprTest)458 TEST(Uint128, ConstexprTest) {
459 constexpr absl::uint128 zero = absl::uint128();
460 constexpr absl::uint128 one = 1;
461 constexpr absl::uint128 minus_two = -2;
462 EXPECT_EQ(zero, absl::uint128(0));
463 EXPECT_EQ(one, absl::uint128(1));
464 EXPECT_EQ(minus_two, absl::MakeUint128(-1, -2));
465 }
466
TEST(Uint128,NumericLimitsTest)467 TEST(Uint128, NumericLimitsTest) {
468 static_assert(std::numeric_limits<absl::uint128>::is_specialized, "");
469 static_assert(!std::numeric_limits<absl::uint128>::is_signed, "");
470 static_assert(std::numeric_limits<absl::uint128>::is_integer, "");
471 EXPECT_EQ(static_cast<int>(128 * std::log10(2)),
472 std::numeric_limits<absl::uint128>::digits10);
473 EXPECT_EQ(0, std::numeric_limits<absl::uint128>::min());
474 EXPECT_EQ(0, std::numeric_limits<absl::uint128>::lowest());
475 EXPECT_EQ(absl::Uint128Max(), std::numeric_limits<absl::uint128>::max());
476 }
477
478 // Some arbitrary constant to test hashing. The first hex digits of pi.
479 constexpr absl::uint128 kPi = (absl::uint128(0x3243f6a8885a308d) << 64) |
480 absl::uint128(0x313198a2e0370734);
481
TEST(Uint128,Hash)482 TEST(Uint128, Hash) {
483 #if defined(ABSL_HAVE_INTRINSIC_INT128)
484 using Ext128 = unsigned __int128;
485 #endif
486 // Make the tuple outside the EXPECT_TRUE because putting the #if inside the
487 // macro argument is not ok.
488 const auto values = std::make_tuple(
489 // Some simple values
490 absl::uint128{0}, absl::uint128{1}, ~absl::uint128{},
491 // 64 bit limits
492 absl::uint128{std::numeric_limits<int64_t>::max()},
493 absl::uint128{std::numeric_limits<uint64_t>::max()} + 0,
494 absl::uint128{std::numeric_limits<uint64_t>::max()} + 1,
495 absl::uint128{std::numeric_limits<uint64_t>::max()} + 2,
496 // Keeping high same
497 absl::uint128{1} << 62, absl::uint128{1} << 63,
498 // Keeping low same
499 absl::uint128{1} << 64, absl::uint128{1} << 65,
500 // 128 bit limits
501 std::numeric_limits<absl::uint128>::max(),
502 std::numeric_limits<absl::uint128>::max() - 1,
503 std::numeric_limits<absl::uint128>::min() + 1,
504 std::numeric_limits<absl::uint128>::min(),
505 // arbitrary constant
506 kPi
507 #if defined(ABSL_HAVE_INTRINSIC_INT128)
508 // Same but with the intrinsic to verify that they match
509 ,
510 Ext128{0}, Ext128{1}, ~Ext128{},
511 Ext128{std::numeric_limits<int64_t>::max()},
512 Ext128{std::numeric_limits<uint64_t>::max()} + 0,
513 Ext128{std::numeric_limits<uint64_t>::max()} + 1,
514 Ext128{std::numeric_limits<uint64_t>::max()} + 2, Ext128{1} << 62,
515 Ext128{1} << 63, Ext128{1} << 64, Ext128{1} << 65,
516 std::numeric_limits<Ext128>::max(),
517 std::numeric_limits<Ext128>::max() - 1,
518 std::numeric_limits<Ext128>::min() + 1,
519 std::numeric_limits<Ext128>::min(), static_cast<Ext128>(kPi)
520 #endif
521 );
522 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(values));
523 }
524
525
TEST(Int128Uint128,ConversionTest)526 TEST(Int128Uint128, ConversionTest) {
527 absl::int128 nonnegative_signed_values[] = {
528 0,
529 1,
530 0xffeeddccbbaa9988,
531 absl::MakeInt128(0x7766554433221100, 0),
532 absl::MakeInt128(0x1234567890abcdef, 0xfedcba0987654321),
533 absl::Int128Max()};
534 for (absl::int128 value : nonnegative_signed_values) {
535 EXPECT_EQ(value, absl::int128(absl::uint128(value)));
536
537 absl::uint128 assigned_value;
538 assigned_value = value;
539 EXPECT_EQ(value, absl::int128(assigned_value));
540 }
541
542 absl::int128 negative_values[] = {
543 -1, -0x1234567890abcdef,
544 absl::MakeInt128(-0x5544332211ffeedd, 0),
545 -absl::MakeInt128(0x76543210fedcba98, 0xabcdef0123456789)};
546 for (absl::int128 value : negative_values) {
547 EXPECT_EQ(absl::uint128(-value), -absl::uint128(value));
548
549 absl::uint128 assigned_value;
550 assigned_value = value;
551 EXPECT_EQ(absl::uint128(-value), -assigned_value);
552 }
553 }
554
555 template <typename T>
556 class Int128IntegerTraitsTest : public ::testing::Test {};
557
558 TYPED_TEST_SUITE(Int128IntegerTraitsTest, IntegerTypes);
559
TYPED_TEST(Int128IntegerTraitsTest,ConstructAssignTest)560 TYPED_TEST(Int128IntegerTraitsTest, ConstructAssignTest) {
561 static_assert(std::is_constructible<absl::int128, TypeParam>::value,
562 "absl::int128 must be constructible from TypeParam");
563 static_assert(std::is_assignable<absl::int128&, TypeParam>::value,
564 "absl::int128 must be assignable from TypeParam");
565 static_assert(!std::is_assignable<TypeParam&, absl::int128>::value,
566 "TypeParam must not be assignable from absl::int128");
567 }
568
569 template <typename T>
570 class Int128FloatTraitsTest : public ::testing::Test {};
571
572 TYPED_TEST_SUITE(Int128FloatTraitsTest, FloatingPointTypes);
573
TYPED_TEST(Int128FloatTraitsTest,ConstructAssignTest)574 TYPED_TEST(Int128FloatTraitsTest, ConstructAssignTest) {
575 static_assert(std::is_constructible<absl::int128, TypeParam>::value,
576 "absl::int128 must be constructible from TypeParam");
577 static_assert(!std::is_assignable<absl::int128&, TypeParam>::value,
578 "absl::int128 must not be assignable from TypeParam");
579 static_assert(!std::is_assignable<TypeParam&, absl::int128>::value,
580 "TypeParam must not be assignable from absl::int128");
581 }
582
583 #ifdef ABSL_HAVE_INTRINSIC_INT128
584 // These type traits done separately as TYPED_TEST requires typeinfo, and not
585 // all platforms have this for __int128 even though they define the type.
TEST(Int128,IntrinsicTypeTraitsTest)586 TEST(Int128, IntrinsicTypeTraitsTest) {
587 static_assert(std::is_constructible<absl::int128, __int128>::value,
588 "absl::int128 must be constructible from __int128");
589 static_assert(std::is_assignable<absl::int128&, __int128>::value,
590 "absl::int128 must be assignable from __int128");
591 static_assert(!std::is_assignable<__int128&, absl::int128>::value,
592 "__int128 must not be assignable from absl::int128");
593
594 static_assert(std::is_constructible<absl::int128, unsigned __int128>::value,
595 "absl::int128 must be constructible from unsigned __int128");
596 static_assert(!std::is_assignable<absl::int128&, unsigned __int128>::value,
597 "absl::int128 must be assignable from unsigned __int128");
598 static_assert(!std::is_assignable<unsigned __int128&, absl::int128>::value,
599 "unsigned __int128 must not be assignable from absl::int128");
600 }
601 #endif // ABSL_HAVE_INTRINSIC_INT128
602
TEST(Int128,TrivialTraitsTest)603 TEST(Int128, TrivialTraitsTest) {
604 static_assert(absl::is_trivially_default_constructible<absl::int128>::value,
605 "");
606 static_assert(absl::is_trivially_copy_constructible<absl::int128>::value, "");
607 static_assert(absl::is_trivially_copy_assignable<absl::int128>::value, "");
608 static_assert(std::is_trivially_destructible<absl::int128>::value, "");
609 }
610
TEST(Int128,BoolConversionTest)611 TEST(Int128, BoolConversionTest) {
612 EXPECT_FALSE(absl::int128(0));
613 for (int i = 0; i < 64; ++i) {
614 EXPECT_TRUE(absl::MakeInt128(0, uint64_t{1} << i));
615 }
616 for (int i = 0; i < 63; ++i) {
617 EXPECT_TRUE(absl::MakeInt128(int64_t{1} << i, 0));
618 }
619 EXPECT_TRUE(absl::Int128Min());
620
621 EXPECT_EQ(absl::int128(1), absl::int128(true));
622 EXPECT_EQ(absl::int128(0), absl::int128(false));
623 }
624
625 template <typename T>
626 class Int128IntegerConversionTest : public ::testing::Test {};
627
628 TYPED_TEST_SUITE(Int128IntegerConversionTest, IntegerTypes);
629
TYPED_TEST(Int128IntegerConversionTest,RoundTripTest)630 TYPED_TEST(Int128IntegerConversionTest, RoundTripTest) {
631 EXPECT_EQ(TypeParam{0}, static_cast<TypeParam>(absl::int128(0)));
632 EXPECT_EQ(std::numeric_limits<TypeParam>::min(),
633 static_cast<TypeParam>(
634 absl::int128(std::numeric_limits<TypeParam>::min())));
635 EXPECT_EQ(std::numeric_limits<TypeParam>::max(),
636 static_cast<TypeParam>(
637 absl::int128(std::numeric_limits<TypeParam>::max())));
638 }
639
640 template <typename T>
641 class Int128FloatConversionTest : public ::testing::Test {};
642
643 TYPED_TEST_SUITE(Int128FloatConversionTest, FloatingPointTypes);
644
TYPED_TEST(Int128FloatConversionTest,ConstructAndCastTest)645 TYPED_TEST(Int128FloatConversionTest, ConstructAndCastTest) {
646 // Conversions where the floating point values should be exactly the same.
647 // 0x9f5b is a randomly chosen small value.
648 for (int i = 0; i < 110; ++i) { // 110 = 126 - #bits in 0x9f5b
649 SCOPED_TRACE(::testing::Message() << "i = " << i);
650
651 TypeParam float_value = std::ldexp(static_cast<TypeParam>(0x9f5b), i);
652 absl::int128 int_value = absl::int128(0x9f5b) << i;
653
654 EXPECT_EQ(float_value, static_cast<TypeParam>(int_value));
655 EXPECT_EQ(-float_value, static_cast<TypeParam>(-int_value));
656 EXPECT_EQ(int_value, absl::int128(float_value));
657 EXPECT_EQ(-int_value, absl::int128(-float_value));
658 }
659
660 // Round trip conversions with a small sample of randomly generated uint64_t
661 // values (less than int64_t max so that value * 2^64 fits into int128).
662 uint64_t values[] = {0x6d4492c24fb86199, 0x26ead65e4cb359b5,
663 0x2c43407433ba3fd1, 0x3b574ec668df6b55,
664 0x1c750e55a29f4f0f};
665 for (uint64_t value : values) {
666 for (int i = 0; i <= 64; ++i) {
667 SCOPED_TRACE(::testing::Message()
668 << "value = " << value << "; i = " << i);
669
670 TypeParam fvalue = std::ldexp(static_cast<TypeParam>(value), i);
671 EXPECT_DOUBLE_EQ(fvalue, static_cast<TypeParam>(absl::int128(fvalue)));
672 EXPECT_DOUBLE_EQ(-fvalue, static_cast<TypeParam>(-absl::int128(fvalue)));
673 EXPECT_DOUBLE_EQ(-fvalue, static_cast<TypeParam>(absl::int128(-fvalue)));
674 EXPECT_DOUBLE_EQ(fvalue, static_cast<TypeParam>(-absl::int128(-fvalue)));
675 }
676 }
677
678 // Round trip conversions with a small sample of random large positive values.
679 absl::int128 large_values[] = {
680 absl::MakeInt128(0x5b0640d96c7b3d9f, 0xb7a7189e51d18622),
681 absl::MakeInt128(0x34bed042c6f65270, 0x73b236570669a089),
682 absl::MakeInt128(0x43deba9e6da12724, 0xf7f0f83da686797d),
683 absl::MakeInt128(0x71e8d383be4e5589, 0x75c3f96fb00752b6)};
684 for (absl::int128 value : large_values) {
685 // Make value have as many significant bits as can be represented by
686 // the mantissa, also making sure the highest and lowest bit in the range
687 // are set.
688 value >>= (127 - std::numeric_limits<TypeParam>::digits);
689 value |= absl::int128(1) << (std::numeric_limits<TypeParam>::digits - 1);
690 value |= 1;
691 for (int i = 0; i < 127 - std::numeric_limits<TypeParam>::digits; ++i) {
692 absl::int128 int_value = value << i;
693 EXPECT_EQ(int_value,
694 static_cast<absl::int128>(static_cast<TypeParam>(int_value)));
695 EXPECT_EQ(-int_value,
696 static_cast<absl::int128>(static_cast<TypeParam>(-int_value)));
697 }
698 }
699
700 // Small sample of checks that rounding is toward zero
701 EXPECT_EQ(0, absl::int128(TypeParam(0.1)));
702 EXPECT_EQ(17, absl::int128(TypeParam(17.8)));
703 EXPECT_EQ(0, absl::int128(TypeParam(-0.8)));
704 EXPECT_EQ(-53, absl::int128(TypeParam(-53.1)));
705 EXPECT_EQ(0, absl::int128(TypeParam(0.5)));
706 EXPECT_EQ(0, absl::int128(TypeParam(-0.5)));
707 TypeParam just_lt_one = std::nexttoward(TypeParam(1), TypeParam(0));
708 EXPECT_EQ(0, absl::int128(just_lt_one));
709 TypeParam just_gt_minus_one = std::nexttoward(TypeParam(-1), TypeParam(0));
710 EXPECT_EQ(0, absl::int128(just_gt_minus_one));
711
712 // Check limits
713 EXPECT_DOUBLE_EQ(std::ldexp(static_cast<TypeParam>(1), 127),
714 static_cast<TypeParam>(absl::Int128Max()));
715 EXPECT_DOUBLE_EQ(-std::ldexp(static_cast<TypeParam>(1), 127),
716 static_cast<TypeParam>(absl::Int128Min()));
717 }
718
TEST(Int128,FactoryTest)719 TEST(Int128, FactoryTest) {
720 EXPECT_EQ(absl::int128(-1), absl::MakeInt128(-1, -1));
721 EXPECT_EQ(absl::int128(-31), absl::MakeInt128(-1, -31));
722 EXPECT_EQ(absl::int128(std::numeric_limits<int64_t>::min()),
723 absl::MakeInt128(-1, std::numeric_limits<int64_t>::min()));
724 EXPECT_EQ(absl::int128(0), absl::MakeInt128(0, 0));
725 EXPECT_EQ(absl::int128(1), absl::MakeInt128(0, 1));
726 EXPECT_EQ(absl::int128(std::numeric_limits<int64_t>::max()),
727 absl::MakeInt128(0, std::numeric_limits<int64_t>::max()));
728 }
729
TEST(Int128,HighLowTest)730 TEST(Int128, HighLowTest) {
731 struct HighLowPair {
732 int64_t high;
733 uint64_t low;
734 };
735 HighLowPair values[]{{0, 0}, {0, 1}, {1, 0}, {123, 456}, {-654, 321}};
736 for (const HighLowPair& pair : values) {
737 absl::int128 value = absl::MakeInt128(pair.high, pair.low);
738 EXPECT_EQ(pair.low, absl::Int128Low64(value));
739 EXPECT_EQ(pair.high, absl::Int128High64(value));
740 }
741 }
742
TEST(Int128,LimitsTest)743 TEST(Int128, LimitsTest) {
744 EXPECT_EQ(absl::MakeInt128(0x7fffffffffffffff, 0xffffffffffffffff),
745 absl::Int128Max());
746 EXPECT_EQ(absl::Int128Max(), ~absl::Int128Min());
747 }
748
749 #if defined(ABSL_HAVE_INTRINSIC_INT128)
TEST(Int128,IntrinsicConversionTest)750 TEST(Int128, IntrinsicConversionTest) {
751 __int128 intrinsic =
752 (static_cast<__int128>(0x3a5b76c209de76f6) << 64) + 0x1f25e1d63a2b46c5;
753 absl::int128 custom =
754 absl::MakeInt128(0x3a5b76c209de76f6, 0x1f25e1d63a2b46c5);
755
756 EXPECT_EQ(custom, absl::int128(intrinsic));
757 EXPECT_EQ(intrinsic, static_cast<__int128>(custom));
758 }
759 #endif // ABSL_HAVE_INTRINSIC_INT128
760
TEST(Int128,ConstexprTest)761 TEST(Int128, ConstexprTest) {
762 constexpr absl::int128 zero = absl::int128();
763 constexpr absl::int128 one = 1;
764 constexpr absl::int128 minus_two = -2;
765 constexpr absl::int128 min = absl::Int128Min();
766 constexpr absl::int128 max = absl::Int128Max();
767 EXPECT_EQ(zero, absl::int128(0));
768 EXPECT_EQ(one, absl::int128(1));
769 EXPECT_EQ(minus_two, absl::MakeInt128(-1, -2));
770 EXPECT_GT(max, one);
771 EXPECT_LT(min, minus_two);
772 }
773
TEST(Int128,ComparisonTest)774 TEST(Int128, ComparisonTest) {
775 struct TestCase {
776 absl::int128 smaller;
777 absl::int128 larger;
778 };
779 TestCase cases[] = {
780 {absl::int128(0), absl::int128(123)},
781 {absl::MakeInt128(-12, 34), absl::MakeInt128(12, 34)},
782 {absl::MakeInt128(1, 1000), absl::MakeInt128(1000, 1)},
783 {absl::MakeInt128(-1000, 1000), absl::MakeInt128(-1, 1)},
784 };
785 for (const TestCase& pair : cases) {
786 SCOPED_TRACE(::testing::Message() << "pair.smaller = " << pair.smaller
787 << "; pair.larger = " << pair.larger);
788
789 EXPECT_TRUE(pair.smaller == pair.smaller); // NOLINT(readability/check)
790 EXPECT_TRUE(pair.larger == pair.larger); // NOLINT(readability/check)
791 EXPECT_FALSE(pair.smaller == pair.larger); // NOLINT(readability/check)
792
793 EXPECT_TRUE(pair.smaller != pair.larger); // NOLINT(readability/check)
794 EXPECT_FALSE(pair.smaller != pair.smaller); // NOLINT(readability/check)
795 EXPECT_FALSE(pair.larger != pair.larger); // NOLINT(readability/check)
796
797 EXPECT_TRUE(pair.smaller < pair.larger); // NOLINT(readability/check)
798 EXPECT_FALSE(pair.larger < pair.smaller); // NOLINT(readability/check)
799
800 EXPECT_TRUE(pair.larger > pair.smaller); // NOLINT(readability/check)
801 EXPECT_FALSE(pair.smaller > pair.larger); // NOLINT(readability/check)
802
803 EXPECT_TRUE(pair.smaller <= pair.larger); // NOLINT(readability/check)
804 EXPECT_FALSE(pair.larger <= pair.smaller); // NOLINT(readability/check)
805 EXPECT_TRUE(pair.smaller <= pair.smaller); // NOLINT(readability/check)
806 EXPECT_TRUE(pair.larger <= pair.larger); // NOLINT(readability/check)
807
808 EXPECT_TRUE(pair.larger >= pair.smaller); // NOLINT(readability/check)
809 EXPECT_FALSE(pair.smaller >= pair.larger); // NOLINT(readability/check)
810 EXPECT_TRUE(pair.smaller >= pair.smaller); // NOLINT(readability/check)
811 EXPECT_TRUE(pair.larger >= pair.larger); // NOLINT(readability/check)
812
813 #ifdef __cpp_impl_three_way_comparison
814 EXPECT_EQ(pair.smaller <=> pair.larger, absl::strong_ordering::less);
815 EXPECT_EQ(pair.larger <=> pair.smaller, absl::strong_ordering::greater);
816 EXPECT_EQ(pair.smaller <=> pair.smaller, absl::strong_ordering::equal);
817 EXPECT_EQ(pair.larger <=> pair.larger, absl::strong_ordering::equal);
818 #endif
819 }
820 }
821
TEST(Int128,UnaryPlusTest)822 TEST(Int128, UnaryPlusTest) {
823 int64_t values64[] = {0, 1, 12345, 0x4000000000000000,
824 std::numeric_limits<int64_t>::max()};
825 for (int64_t value : values64) {
826 SCOPED_TRACE(::testing::Message() << "value = " << value);
827
828 EXPECT_EQ(absl::int128(value), +absl::int128(value));
829 EXPECT_EQ(absl::int128(-value), +absl::int128(-value));
830 EXPECT_EQ(absl::MakeInt128(value, 0), +absl::MakeInt128(value, 0));
831 EXPECT_EQ(absl::MakeInt128(-value, 0), +absl::MakeInt128(-value, 0));
832 }
833 }
834
TEST(Int128,UnaryNegationTest)835 TEST(Int128, UnaryNegationTest) {
836 int64_t values64[] = {0, 1, 12345, 0x4000000000000000,
837 std::numeric_limits<int64_t>::max()};
838 for (int64_t value : values64) {
839 SCOPED_TRACE(::testing::Message() << "value = " << value);
840
841 EXPECT_EQ(absl::int128(-value), -absl::int128(value));
842 EXPECT_EQ(absl::int128(value), -absl::int128(-value));
843 EXPECT_EQ(absl::MakeInt128(-value, 0), -absl::MakeInt128(value, 0));
844 EXPECT_EQ(absl::MakeInt128(value, 0), -absl::MakeInt128(-value, 0));
845 }
846 }
847
TEST(Int128,LogicalNotTest)848 TEST(Int128, LogicalNotTest) {
849 EXPECT_TRUE(!absl::int128(0));
850 for (int i = 0; i < 64; ++i) {
851 EXPECT_FALSE(!absl::MakeInt128(0, uint64_t{1} << i));
852 }
853 for (int i = 0; i < 63; ++i) {
854 EXPECT_FALSE(!absl::MakeInt128(int64_t{1} << i, 0));
855 }
856 }
857
TEST(Int128,AdditionSubtractionTest)858 TEST(Int128, AdditionSubtractionTest) {
859 // 64 bit pairs that will not cause overflow / underflow. These test negative
860 // carry; positive carry must be checked separately.
861 std::pair<int64_t, int64_t> cases[]{
862 {0, 0}, // 0, 0
863 {0, 2945781290834}, // 0, +
864 {1908357619234, 0}, // +, 0
865 {0, -1204895918245}, // 0, -
866 {-2957928523560, 0}, // -, 0
867 {89023982312461, 98346012567134}, // +, +
868 {-63454234568239, -23456235230773}, // -, -
869 {98263457263502, -21428561935925}, // +, -
870 {-88235237438467, 15923659234573}, // -, +
871 };
872 for (const auto& pair : cases) {
873 SCOPED_TRACE(::testing::Message()
874 << "pair = {" << pair.first << ", " << pair.second << '}');
875
876 EXPECT_EQ(absl::int128(pair.first + pair.second),
877 absl::int128(pair.first) + absl::int128(pair.second));
878 EXPECT_EQ(absl::int128(pair.second + pair.first),
879 absl::int128(pair.second) += absl::int128(pair.first));
880
881 EXPECT_EQ(absl::int128(pair.first - pair.second),
882 absl::int128(pair.first) - absl::int128(pair.second));
883 EXPECT_EQ(absl::int128(pair.second - pair.first),
884 absl::int128(pair.second) -= absl::int128(pair.first));
885
886 EXPECT_EQ(
887 absl::MakeInt128(pair.second + pair.first, 0),
888 absl::MakeInt128(pair.second, 0) + absl::MakeInt128(pair.first, 0));
889 EXPECT_EQ(
890 absl::MakeInt128(pair.first + pair.second, 0),
891 absl::MakeInt128(pair.first, 0) += absl::MakeInt128(pair.second, 0));
892
893 EXPECT_EQ(
894 absl::MakeInt128(pair.second - pair.first, 0),
895 absl::MakeInt128(pair.second, 0) - absl::MakeInt128(pair.first, 0));
896 EXPECT_EQ(
897 absl::MakeInt128(pair.first - pair.second, 0),
898 absl::MakeInt128(pair.first, 0) -= absl::MakeInt128(pair.second, 0));
899 }
900
901 // check positive carry
902 EXPECT_EQ(absl::MakeInt128(31, 0),
903 absl::MakeInt128(20, 1) +
904 absl::MakeInt128(10, std::numeric_limits<uint64_t>::max()));
905 }
906
TEST(Int128,IncrementDecrementTest)907 TEST(Int128, IncrementDecrementTest) {
908 absl::int128 value = 0;
909 EXPECT_EQ(0, value++);
910 EXPECT_EQ(1, value);
911 EXPECT_EQ(1, value--);
912 EXPECT_EQ(0, value);
913 EXPECT_EQ(-1, --value);
914 EXPECT_EQ(-1, value);
915 EXPECT_EQ(0, ++value);
916 EXPECT_EQ(0, value);
917 }
918
TEST(Int128,MultiplicationTest)919 TEST(Int128, MultiplicationTest) {
920 // 1 bit x 1 bit, and negative combinations
921 for (int i = 0; i < 64; ++i) {
922 for (int j = 0; j < 127 - i; ++j) {
923 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
924 absl::int128 a = absl::int128(1) << i;
925 absl::int128 b = absl::int128(1) << j;
926 absl::int128 c = absl::int128(1) << (i + j);
927
928 EXPECT_EQ(c, a * b);
929 EXPECT_EQ(-c, -a * b);
930 EXPECT_EQ(-c, a * -b);
931 EXPECT_EQ(c, -a * -b);
932
933 EXPECT_EQ(c, absl::int128(a) *= b);
934 EXPECT_EQ(-c, absl::int128(-a) *= b);
935 EXPECT_EQ(-c, absl::int128(a) *= -b);
936 EXPECT_EQ(c, absl::int128(-a) *= -b);
937 }
938 }
939
940 // Pairs of random values that will not overflow signed 64-bit multiplication
941 std::pair<int64_t, int64_t> small_values[] = {
942 {0x5e61, 0xf29f79ca14b4}, // +, +
943 {0x3e033b, -0x612c0ee549}, // +, -
944 {-0x052ce7e8, 0x7c728f0f}, // -, +
945 {-0x3af7054626, -0xfb1e1d}, // -, -
946 };
947 for (const std::pair<int64_t, int64_t>& pair : small_values) {
948 SCOPED_TRACE(::testing::Message()
949 << "pair = {" << pair.first << ", " << pair.second << '}');
950
951 EXPECT_EQ(absl::int128(pair.first * pair.second),
952 absl::int128(pair.first) * absl::int128(pair.second));
953 EXPECT_EQ(absl::int128(pair.first * pair.second),
954 absl::int128(pair.first) *= absl::int128(pair.second));
955
956 EXPECT_EQ(absl::MakeInt128(pair.first * pair.second, 0),
957 absl::MakeInt128(pair.first, 0) * absl::int128(pair.second));
958 EXPECT_EQ(absl::MakeInt128(pair.first * pair.second, 0),
959 absl::MakeInt128(pair.first, 0) *= absl::int128(pair.second));
960 }
961
962 // Pairs of positive random values that will not overflow 64-bit
963 // multiplication and can be left shifted by 32 without overflow
964 std::pair<int64_t, int64_t> small_values2[] = {
965 {0x1bb0a110, 0x31487671},
966 {0x4792784e, 0x28add7d7},
967 {0x7b66553a, 0x11dff8ef},
968 };
969 for (const std::pair<int64_t, int64_t>& pair : small_values2) {
970 SCOPED_TRACE(::testing::Message()
971 << "pair = {" << pair.first << ", " << pair.second << '}');
972
973 absl::int128 a = absl::int128(pair.first << 32);
974 absl::int128 b = absl::int128(pair.second << 32);
975 absl::int128 c = absl::MakeInt128(pair.first * pair.second, 0);
976
977 EXPECT_EQ(c, a * b);
978 EXPECT_EQ(-c, -a * b);
979 EXPECT_EQ(-c, a * -b);
980 EXPECT_EQ(c, -a * -b);
981
982 EXPECT_EQ(c, absl::int128(a) *= b);
983 EXPECT_EQ(-c, absl::int128(-a) *= b);
984 EXPECT_EQ(-c, absl::int128(a) *= -b);
985 EXPECT_EQ(c, absl::int128(-a) *= -b);
986 }
987
988 // check 0, 1, and -1 behavior with large values
989 absl::int128 large_values[] = {
990 {absl::MakeInt128(0xd66f061af02d0408, 0x727d2846cb475b53)},
991 {absl::MakeInt128(0x27b8d5ed6104452d, 0x03f8a33b0ee1df4f)},
992 {-absl::MakeInt128(0x621b6626b9e8d042, 0x27311ac99df00938)},
993 {-absl::MakeInt128(0x34e0656f1e95fb60, 0x4281cfd731257a47)},
994 };
995 for (absl::int128 value : large_values) {
996 EXPECT_EQ(0, 0 * value);
997 EXPECT_EQ(0, value * 0);
998 EXPECT_EQ(0, absl::int128(0) *= value);
999 EXPECT_EQ(0, value *= 0);
1000
1001 EXPECT_EQ(value, 1 * value);
1002 EXPECT_EQ(value, value * 1);
1003 EXPECT_EQ(value, absl::int128(1) *= value);
1004 EXPECT_EQ(value, value *= 1);
1005
1006 EXPECT_EQ(-value, -1 * value);
1007 EXPECT_EQ(-value, value * -1);
1008 EXPECT_EQ(-value, absl::int128(-1) *= value);
1009 EXPECT_EQ(-value, value *= -1);
1010 }
1011
1012 // Manually calculated random large value cases
1013 EXPECT_EQ(absl::MakeInt128(0xcd0efd3442219bb, 0xde47c05bcd9df6e1),
1014 absl::MakeInt128(0x7c6448, 0x3bc4285c47a9d253) * 0x1a6037537b);
1015 EXPECT_EQ(-absl::MakeInt128(0x1f8f149850b1e5e6, 0x1e50d6b52d272c3e),
1016 -absl::MakeInt128(0x23, 0x2e68a513ca1b8859) * 0xe5a434cd14866e);
1017 EXPECT_EQ(-absl::MakeInt128(0x55cae732029d1fce, 0xca6474b6423263e4),
1018 0xa9b98a8ddf66bc * -absl::MakeInt128(0x81, 0x672e58231e2469d7));
1019 EXPECT_EQ(absl::MakeInt128(0x19c8b7620b507dc4, 0xfec042b71a5f29a4),
1020 -0x3e39341147 * -absl::MakeInt128(0x6a14b2, 0x5ed34cca42327b3c));
1021
1022 EXPECT_EQ(absl::MakeInt128(0xcd0efd3442219bb, 0xde47c05bcd9df6e1),
1023 absl::MakeInt128(0x7c6448, 0x3bc4285c47a9d253) *= 0x1a6037537b);
1024 EXPECT_EQ(-absl::MakeInt128(0x1f8f149850b1e5e6, 0x1e50d6b52d272c3e),
1025 -absl::MakeInt128(0x23, 0x2e68a513ca1b8859) *= 0xe5a434cd14866e);
1026 EXPECT_EQ(-absl::MakeInt128(0x55cae732029d1fce, 0xca6474b6423263e4),
1027 absl::int128(0xa9b98a8ddf66bc) *=
1028 -absl::MakeInt128(0x81, 0x672e58231e2469d7));
1029 EXPECT_EQ(absl::MakeInt128(0x19c8b7620b507dc4, 0xfec042b71a5f29a4),
1030 absl::int128(-0x3e39341147) *=
1031 -absl::MakeInt128(0x6a14b2, 0x5ed34cca42327b3c));
1032 }
1033
TEST(Int128,DivisionAndModuloTest)1034 TEST(Int128, DivisionAndModuloTest) {
1035 // Check against 64 bit division and modulo operators with a sample of
1036 // randomly generated pairs.
1037 std::pair<int64_t, int64_t> small_pairs[] = {
1038 {0x15f2a64138, 0x67da05}, {0x5e56d194af43045f, 0xcf1543fb99},
1039 {0x15e61ed052036a, -0xc8e6}, {0x88125a341e85, -0xd23fb77683},
1040 {-0xc06e20, 0x5a}, {-0x4f100219aea3e85d, 0xdcc56cb4efe993},
1041 {-0x168d629105, -0xa7}, {-0x7b44e92f03ab2375, -0x6516},
1042 };
1043 for (const std::pair<int64_t, int64_t>& pair : small_pairs) {
1044 SCOPED_TRACE(::testing::Message()
1045 << "pair = {" << pair.first << ", " << pair.second << '}');
1046
1047 absl::int128 dividend = pair.first;
1048 absl::int128 divisor = pair.second;
1049 int64_t quotient = pair.first / pair.second;
1050 int64_t remainder = pair.first % pair.second;
1051
1052 EXPECT_EQ(quotient, dividend / divisor);
1053 EXPECT_EQ(quotient, absl::int128(dividend) /= divisor);
1054 EXPECT_EQ(remainder, dividend % divisor);
1055 EXPECT_EQ(remainder, absl::int128(dividend) %= divisor);
1056 }
1057
1058 // Test behavior with 0, 1, and -1 with a sample of randomly generated large
1059 // values.
1060 absl::int128 values[] = {
1061 absl::MakeInt128(0x63d26ee688a962b2, 0x9e1411abda5c1d70),
1062 absl::MakeInt128(0x152f385159d6f986, 0xbf8d48ef63da395d),
1063 -absl::MakeInt128(0x3098d7567030038c, 0x14e7a8a098dc2164),
1064 -absl::MakeInt128(0x49a037aca35c809f, 0xa6a87525480ef330),
1065 };
1066 for (absl::int128 value : values) {
1067 SCOPED_TRACE(::testing::Message() << "value = " << value);
1068
1069 EXPECT_EQ(0, 0 / value);
1070 EXPECT_EQ(0, absl::int128(0) /= value);
1071 EXPECT_EQ(0, 0 % value);
1072 EXPECT_EQ(0, absl::int128(0) %= value);
1073
1074 EXPECT_EQ(value, value / 1);
1075 EXPECT_EQ(value, absl::int128(value) /= 1);
1076 EXPECT_EQ(0, value % 1);
1077 EXPECT_EQ(0, absl::int128(value) %= 1);
1078
1079 EXPECT_EQ(-value, value / -1);
1080 EXPECT_EQ(-value, absl::int128(value) /= -1);
1081 EXPECT_EQ(0, value % -1);
1082 EXPECT_EQ(0, absl::int128(value) %= -1);
1083 }
1084
1085 // Min and max values
1086 EXPECT_EQ(0, absl::Int128Max() / absl::Int128Min());
1087 EXPECT_EQ(absl::Int128Max(), absl::Int128Max() % absl::Int128Min());
1088 EXPECT_EQ(-1, absl::Int128Min() / absl::Int128Max());
1089 EXPECT_EQ(-1, absl::Int128Min() % absl::Int128Max());
1090
1091 // Power of two division and modulo of random large dividends
1092 absl::int128 positive_values[] = {
1093 absl::MakeInt128(0x21e1a1cc69574620, 0xe7ac447fab2fc869),
1094 absl::MakeInt128(0x32c2ff3ab89e66e8, 0x03379a613fd1ce74),
1095 absl::MakeInt128(0x6f32ca786184dcaf, 0x046f9c9ecb3a9ce1),
1096 absl::MakeInt128(0x1aeb469dd990e0ee, 0xda2740f243cd37eb),
1097 };
1098 for (absl::int128 value : positive_values) {
1099 for (int i = 0; i < 127; ++i) {
1100 SCOPED_TRACE(::testing::Message()
1101 << "value = " << value << "; i = " << i);
1102 absl::int128 power_of_two = absl::int128(1) << i;
1103
1104 EXPECT_EQ(value >> i, value / power_of_two);
1105 EXPECT_EQ(value >> i, absl::int128(value) /= power_of_two);
1106 EXPECT_EQ(value & (power_of_two - 1), value % power_of_two);
1107 EXPECT_EQ(value & (power_of_two - 1),
1108 absl::int128(value) %= power_of_two);
1109 }
1110 }
1111
1112 // Manually calculated cases with random large dividends
1113 struct DivisionModCase {
1114 absl::int128 dividend;
1115 absl::int128 divisor;
1116 absl::int128 quotient;
1117 absl::int128 remainder;
1118 };
1119 DivisionModCase manual_cases[] = {
1120 {absl::MakeInt128(0x6ada48d489007966, 0x3c9c5c98150d5d69),
1121 absl::MakeInt128(0x8bc308fb, 0x8cb9cc9a3b803344), 0xc3b87e08,
1122 absl::MakeInt128(0x1b7db5e1, 0xd9eca34b7af04b49)},
1123 {absl::MakeInt128(0xd6946511b5b, 0x4886c5c96546bf5f),
1124 -absl::MakeInt128(0x263b, 0xfd516279efcfe2dc), -0x59cbabf0,
1125 absl::MakeInt128(0x622, 0xf462909155651d1f)},
1126 {-absl::MakeInt128(0x33db734f9e8d1399, 0x8447ac92482bca4d), 0x37495078240,
1127 -absl::MakeInt128(0xf01f1, 0xbc0368bf9a77eae8), -0x21a508f404d},
1128 {-absl::MakeInt128(0x13f837b409a07e7d, 0x7fc8e248a7d73560), -0x1b9f,
1129 absl::MakeInt128(0xb9157556d724, 0xb14f635714d7563e), -0x1ade},
1130 };
1131 for (const DivisionModCase test_case : manual_cases) {
1132 EXPECT_EQ(test_case.quotient, test_case.dividend / test_case.divisor);
1133 EXPECT_EQ(test_case.quotient,
1134 absl::int128(test_case.dividend) /= test_case.divisor);
1135 EXPECT_EQ(test_case.remainder, test_case.dividend % test_case.divisor);
1136 EXPECT_EQ(test_case.remainder,
1137 absl::int128(test_case.dividend) %= test_case.divisor);
1138 }
1139 }
1140
TEST(Int128,BitwiseLogicTest)1141 TEST(Int128, BitwiseLogicTest) {
1142 EXPECT_EQ(absl::int128(-1), ~absl::int128(0));
1143
1144 absl::int128 values[]{
1145 0, -1, 0xde400bee05c3ff6b, absl::MakeInt128(0x7f32178dd81d634a, 0),
1146 absl::MakeInt128(0xaf539057055613a9, 0x7d104d7d946c2e4d)};
1147 for (absl::int128 value : values) {
1148 EXPECT_EQ(value, ~~value);
1149
1150 EXPECT_EQ(value, value | value);
1151 EXPECT_EQ(value, value & value);
1152 EXPECT_EQ(0, value ^ value);
1153
1154 EXPECT_EQ(value, absl::int128(value) |= value);
1155 EXPECT_EQ(value, absl::int128(value) &= value);
1156 EXPECT_EQ(0, absl::int128(value) ^= value);
1157
1158 EXPECT_EQ(value, value | 0);
1159 EXPECT_EQ(0, value & 0);
1160 EXPECT_EQ(value, value ^ 0);
1161
1162 EXPECT_EQ(absl::int128(-1), value | absl::int128(-1));
1163 EXPECT_EQ(value, value & absl::int128(-1));
1164 EXPECT_EQ(~value, value ^ absl::int128(-1));
1165 }
1166
1167 // small sample of randomly generated int64_t's
1168 std::pair<int64_t, int64_t> pairs64[]{
1169 {0x7f86797f5e991af4, 0x1ee30494fb007c97},
1170 {0x0b278282bacf01af, 0x58780e0a57a49e86},
1171 {0x059f266ccb93a666, 0x3d5b731bae9286f5},
1172 {0x63c0c4820f12108c, 0x58166713c12e1c3a},
1173 {0x381488bb2ed2a66e, 0x2220a3eb76a3698c},
1174 {0x2a0a0dfb81e06f21, 0x4b60585927f5523c},
1175 {0x555b1c3a03698537, 0x25478cd19d8e53cb},
1176 {0x4750f6f27d779225, 0x16397553c6ff05fc},
1177 };
1178 for (const std::pair<int64_t, int64_t>& pair : pairs64) {
1179 SCOPED_TRACE(::testing::Message()
1180 << "pair = {" << pair.first << ", " << pair.second << '}');
1181
1182 EXPECT_EQ(absl::MakeInt128(~pair.first, ~pair.second),
1183 ~absl::MakeInt128(pair.first, pair.second));
1184
1185 EXPECT_EQ(absl::int128(pair.first & pair.second),
1186 absl::int128(pair.first) & absl::int128(pair.second));
1187 EXPECT_EQ(absl::int128(pair.first | pair.second),
1188 absl::int128(pair.first) | absl::int128(pair.second));
1189 EXPECT_EQ(absl::int128(pair.first ^ pair.second),
1190 absl::int128(pair.first) ^ absl::int128(pair.second));
1191
1192 EXPECT_EQ(absl::int128(pair.first & pair.second),
1193 absl::int128(pair.first) &= absl::int128(pair.second));
1194 EXPECT_EQ(absl::int128(pair.first | pair.second),
1195 absl::int128(pair.first) |= absl::int128(pair.second));
1196 EXPECT_EQ(absl::int128(pair.first ^ pair.second),
1197 absl::int128(pair.first) ^= absl::int128(pair.second));
1198
1199 EXPECT_EQ(
1200 absl::MakeInt128(pair.first & pair.second, 0),
1201 absl::MakeInt128(pair.first, 0) & absl::MakeInt128(pair.second, 0));
1202 EXPECT_EQ(
1203 absl::MakeInt128(pair.first | pair.second, 0),
1204 absl::MakeInt128(pair.first, 0) | absl::MakeInt128(pair.second, 0));
1205 EXPECT_EQ(
1206 absl::MakeInt128(pair.first ^ pair.second, 0),
1207 absl::MakeInt128(pair.first, 0) ^ absl::MakeInt128(pair.second, 0));
1208
1209 EXPECT_EQ(
1210 absl::MakeInt128(pair.first & pair.second, 0),
1211 absl::MakeInt128(pair.first, 0) &= absl::MakeInt128(pair.second, 0));
1212 EXPECT_EQ(
1213 absl::MakeInt128(pair.first | pair.second, 0),
1214 absl::MakeInt128(pair.first, 0) |= absl::MakeInt128(pair.second, 0));
1215 EXPECT_EQ(
1216 absl::MakeInt128(pair.first ^ pair.second, 0),
1217 absl::MakeInt128(pair.first, 0) ^= absl::MakeInt128(pair.second, 0));
1218 }
1219 }
1220
TEST(Int128,BitwiseShiftTest)1221 TEST(Int128, BitwiseShiftTest) {
1222 for (int i = 0; i < 64; ++i) {
1223 for (int j = 0; j <= i; ++j) {
1224 // Left shift from j-th bit to i-th bit.
1225 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1226 EXPECT_EQ(uint64_t{1} << i, absl::int128(uint64_t{1} << j) << (i - j));
1227 EXPECT_EQ(uint64_t{1} << i, absl::int128(uint64_t{1} << j) <<= (i - j));
1228 }
1229 }
1230 for (int i = 0; i < 63; ++i) {
1231 for (int j = 0; j < 64; ++j) {
1232 // Left shift from j-th bit to (i + 64)-th bit.
1233 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1234 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1235 absl::int128(uint64_t{1} << j) << (i + 64 - j));
1236 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1237 absl::int128(uint64_t{1} << j) <<= (i + 64 - j));
1238 }
1239 for (int j = 0; j <= i; ++j) {
1240 // Left shift from (j + 64)-th bit to (i + 64)-th bit.
1241 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1242 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1243 absl::MakeInt128(uint64_t{1} << j, 0) << (i - j));
1244 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1245 absl::MakeInt128(uint64_t{1} << j, 0) <<= (i - j));
1246 }
1247 }
1248
1249 for (int i = 0; i < 64; ++i) {
1250 for (int j = i; j < 64; ++j) {
1251 // Right shift from j-th bit to i-th bit.
1252 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1253 EXPECT_EQ(uint64_t{1} << i, absl::int128(uint64_t{1} << j) >> (j - i));
1254 EXPECT_EQ(uint64_t{1} << i, absl::int128(uint64_t{1} << j) >>= (j - i));
1255 }
1256 for (int j = 0; j < 63; ++j) {
1257 // Right shift from (j + 64)-th bit to i-th bit.
1258 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1259 EXPECT_EQ(uint64_t{1} << i,
1260 absl::MakeInt128(uint64_t{1} << j, 0) >> (j + 64 - i));
1261 EXPECT_EQ(uint64_t{1} << i,
1262 absl::MakeInt128(uint64_t{1} << j, 0) >>= (j + 64 - i));
1263 }
1264 }
1265 for (int i = 0; i < 63; ++i) {
1266 for (int j = i; j < 63; ++j) {
1267 // Right shift from (j + 64)-th bit to (i + 64)-th bit.
1268 SCOPED_TRACE(::testing::Message() << "i = " << i << "; j = " << j);
1269 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1270 absl::MakeInt128(uint64_t{1} << j, 0) >> (j - i));
1271 EXPECT_EQ(absl::MakeInt128(uint64_t{1} << i, 0),
1272 absl::MakeInt128(uint64_t{1} << j, 0) >>= (j - i));
1273 }
1274 }
1275
1276 // Manually calculated cases with shift count for positive (val1) and negative
1277 // (val2) values
1278 absl::int128 val1 = MAKE_INT128(0x123456789abcdef0, 0x123456789abcdef0);
1279 absl::int128 val2 = MAKE_INT128(0xfedcba0987654321, 0xfedcba0987654321);
1280
1281 EXPECT_EQ(val1 << 63, MAKE_INT128(0x91a2b3c4d5e6f78, 0x0));
1282 EXPECT_EQ(val1 << 64, MAKE_INT128(0x123456789abcdef0, 0x0));
1283 EXPECT_EQ(val2 << 63, MAKE_INT128(0xff6e5d04c3b2a190, 0x8000000000000000));
1284 EXPECT_EQ(val2 << 64, MAKE_INT128(0xfedcba0987654321, 0x0));
1285
1286 EXPECT_EQ(val1 << 126, MAKE_INT128(0x0, 0x0));
1287 EXPECT_EQ(val2 << 126, MAKE_INT128(0x4000000000000000, 0x0));
1288
1289 EXPECT_EQ(val1 >> 63, MAKE_INT128(0x0, 0x2468acf13579bde0));
1290 EXPECT_EQ(val1 >> 64, MAKE_INT128(0x0, 0x123456789abcdef0));
1291 EXPECT_EQ(val2 >> 63, MAKE_INT128(0xffffffffffffffff, 0xfdb974130eca8643));
1292 EXPECT_EQ(val2 >> 64, MAKE_INT128(0xffffffffffffffff, 0xfedcba0987654321));
1293
1294 EXPECT_EQ(val1 >> 126, MAKE_INT128(0x0, 0x0));
1295 EXPECT_EQ(val2 >> 126, MAKE_INT128(0xffffffffffffffff, 0xffffffffffffffff));
1296 }
1297
TEST(Int128,NumericLimitsTest)1298 TEST(Int128, NumericLimitsTest) {
1299 static_assert(std::numeric_limits<absl::int128>::is_specialized, "");
1300 static_assert(std::numeric_limits<absl::int128>::is_signed, "");
1301 static_assert(std::numeric_limits<absl::int128>::is_integer, "");
1302 EXPECT_EQ(static_cast<int>(127 * std::log10(2)),
1303 std::numeric_limits<absl::int128>::digits10);
1304 EXPECT_EQ(absl::Int128Min(), std::numeric_limits<absl::int128>::min());
1305 EXPECT_EQ(absl::Int128Min(), std::numeric_limits<absl::int128>::lowest());
1306 EXPECT_EQ(absl::Int128Max(), std::numeric_limits<absl::int128>::max());
1307 }
1308
TEST(Int128,BitCastable)1309 TEST(Int128, BitCastable) {
1310 // NOTE: This test is not intended to be an example that demonstrate usages of
1311 // `static_cast` and `std::bit_cast`, rather it is here simply to verify
1312 // behavior. When deciding whether you should use `static_cast` or
1313 // `std::bit_cast` when converting between `absl::int128` and `absl::uint128`,
1314 // use your best judgement. As a rule of thumb, use the same cast that you
1315 // would use when converting between the signed and unsigned counterparts of a
1316 // builtin integral type.
1317
1318 // Verify bit casting between signed and unsigned works with regards to two's
1319 // complement. This verifies we exhibit the same behavior as a theoretical
1320 // builtin int128_t and uint128_t in C++20 onwards.
1321 EXPECT_EQ(absl::bit_cast<absl::uint128>(absl::int128(-1)),
1322 std::numeric_limits<absl::uint128>::max());
1323 EXPECT_EQ(
1324 absl::bit_cast<absl::int128>(std::numeric_limits<absl::uint128>::max()),
1325 absl::int128(-1));
1326 EXPECT_EQ(
1327 absl::bit_cast<absl::uint128>(std::numeric_limits<absl::int128>::min()),
1328 absl::uint128(1) << 127);
1329 EXPECT_EQ(absl::bit_cast<absl::int128>(absl::uint128(1) << 127),
1330 std::numeric_limits<absl::int128>::min());
1331 EXPECT_EQ(
1332 absl::bit_cast<absl::uint128>(std::numeric_limits<absl::int128>::max()),
1333 (absl::uint128(1) << 127) - 1);
1334 EXPECT_EQ(absl::bit_cast<absl::int128>((absl::uint128(1) << 127) - 1),
1335 std::numeric_limits<absl::int128>::max());
1336
1337 // Also verify static casting has the same behavior as bit casting.
1338 EXPECT_EQ(static_cast<absl::uint128>(absl::int128(-1)),
1339 std::numeric_limits<absl::uint128>::max());
1340 EXPECT_EQ(
1341 static_cast<absl::int128>(std::numeric_limits<absl::uint128>::max()),
1342 absl::int128(-1));
1343 EXPECT_EQ(
1344 static_cast<absl::uint128>(std::numeric_limits<absl::int128>::min()),
1345 absl::uint128(1) << 127);
1346 EXPECT_EQ(static_cast<absl::int128>(absl::uint128(1) << 127),
1347 std::numeric_limits<absl::int128>::min());
1348 EXPECT_EQ(
1349 static_cast<absl::uint128>(std::numeric_limits<absl::int128>::max()),
1350 (absl::uint128(1) << 127) - 1);
1351 EXPECT_EQ(static_cast<absl::int128>((absl::uint128(1) << 127) - 1),
1352 std::numeric_limits<absl::int128>::max());
1353 }
1354
1355 } // namespace
1356