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