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