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