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