int128_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/numeric/int128.h"
00016 
00017 #include <algorithm>
00018 #include <limits>
00019 #include <random>
00020 #include <type_traits>
00021 #include <utility>
00022 #include <vector>
00023 
00024 #include "gtest/gtest.h"
00025 #include "absl/base/internal/cycleclock.h"
00026 #include "absl/hash/hash_testing.h"
00027 #include "absl/meta/type_traits.h"
00028 
00029 #if defined(_MSC_VER) && _MSC_VER == 1900
00030 // Disable "unary minus operator applied to unsigned type" warnings in Microsoft
00031 // Visual C++ 14 (2015).
00032 #pragma warning(disable:4146)
00033 #endif
00034 
00035 namespace {
00036 
00037 template <typename T>
00038 class Uint128IntegerTraitsTest : public ::testing::Test {};
00039 typedef ::testing::Types<bool, char, signed char, unsigned char, char16_t,
00040                          char32_t, wchar_t,
00041                          short,           // NOLINT(runtime/int)
00042                          unsigned short,  // NOLINT(runtime/int)
00043                          int, unsigned int,
00044                          long,                // NOLINT(runtime/int)
00045                          unsigned long,       // NOLINT(runtime/int)
00046                          long long,           // NOLINT(runtime/int)
00047                          unsigned long long>  // NOLINT(runtime/int)
00048     IntegerTypes;
00049 
00050 template <typename T>
00051 class Uint128FloatTraitsTest : public ::testing::Test {};
00052 typedef ::testing::Types<float, double, long double> FloatingPointTypes;
00053 
00054 TYPED_TEST_SUITE(Uint128IntegerTraitsTest, IntegerTypes);
00055 
00056 TYPED_TEST(Uint128IntegerTraitsTest, ConstructAssignTest) {
00057   static_assert(std::is_constructible<absl::uint128, TypeParam>::value,
00058                 "absl::uint128 must be constructible from TypeParam");
00059   static_assert(std::is_assignable<absl::uint128&, TypeParam>::value,
00060                 "absl::uint128 must be assignable from TypeParam");
00061   static_assert(!std::is_assignable<TypeParam&, absl::uint128>::value,
00062                 "TypeParam must not be assignable from absl::uint128");
00063 }
00064 
00065 TYPED_TEST_SUITE(Uint128FloatTraitsTest, FloatingPointTypes);
00066 
00067 TYPED_TEST(Uint128FloatTraitsTest, ConstructAssignTest) {
00068   static_assert(std::is_constructible<absl::uint128, TypeParam>::value,
00069                 "absl::uint128 must be constructible from TypeParam");
00070   static_assert(!std::is_assignable<absl::uint128&, TypeParam>::value,
00071                 "absl::uint128 must not be assignable from TypeParam");
00072   static_assert(!std::is_assignable<TypeParam&, absl::uint128>::value,
00073                 "TypeParam must not be assignable from absl::uint128");
00074 }
00075 
00076 #ifdef ABSL_HAVE_INTRINSIC_INT128
00077 // These type traits done separately as TYPED_TEST requires typeinfo, and not
00078 // all platforms have this for __int128 even though they define the type.
00079 TEST(Uint128, IntrinsicTypeTraitsTest) {
00080   static_assert(std::is_constructible<absl::uint128, __int128>::value,
00081                 "absl::uint128 must be constructible from __int128");
00082   static_assert(std::is_assignable<absl::uint128&, __int128>::value,
00083                 "absl::uint128 must be assignable from __int128");
00084   static_assert(!std::is_assignable<__int128&, absl::uint128>::value,
00085                 "__int128 must not be assignable from absl::uint128");
00086 
00087   static_assert(std::is_constructible<absl::uint128, unsigned __int128>::value,
00088                 "absl::uint128 must be constructible from unsigned __int128");
00089   static_assert(std::is_assignable<absl::uint128&, unsigned __int128>::value,
00090                 "absl::uint128 must be assignable from unsigned __int128");
00091   static_assert(!std::is_assignable<unsigned __int128&, absl::uint128>::value,
00092                 "unsigned __int128 must not be assignable from absl::uint128");
00093 }
00094 #endif  // ABSL_HAVE_INTRINSIC_INT128
00095 
00096 TEST(Uint128, TrivialTraitsTest) {
00097   static_assert(absl::is_trivially_default_constructible<absl::uint128>::value,
00098                 "");
00099   static_assert(absl::is_trivially_copy_constructible<absl::uint128>::value,
00100                 "");
00101   static_assert(absl::is_trivially_copy_assignable<absl::uint128>::value, "");
00102   static_assert(std::is_trivially_destructible<absl::uint128>::value, "");
00103 }
00104 
00105 TEST(Uint128, AllTests) {
00106   absl::uint128 zero = 0;
00107   absl::uint128 one = 1;
00108   absl::uint128 one_2arg = absl::MakeUint128(0, 1);
00109   absl::uint128 two = 2;
00110   absl::uint128 three = 3;
00111   absl::uint128 big = absl::MakeUint128(2000, 2);
00112   absl::uint128 big_minus_one = absl::MakeUint128(2000, 1);
00113   absl::uint128 bigger = absl::MakeUint128(2001, 1);
00114   absl::uint128 biggest = absl::Uint128Max();
00115   absl::uint128 high_low = absl::MakeUint128(1, 0);
00116   absl::uint128 low_high =
00117       absl::MakeUint128(0, std::numeric_limits<uint64_t>::max());
00118   EXPECT_LT(one, two);
00119   EXPECT_GT(two, one);
00120   EXPECT_LT(one, big);
00121   EXPECT_LT(one, big);
00122   EXPECT_EQ(one, one_2arg);
00123   EXPECT_NE(one, two);
00124   EXPECT_GT(big, one);
00125   EXPECT_GE(big, two);
00126   EXPECT_GE(big, big_minus_one);
00127   EXPECT_GT(big, big_minus_one);
00128   EXPECT_LT(big_minus_one, big);
00129   EXPECT_LE(big_minus_one, big);
00130   EXPECT_NE(big_minus_one, big);
00131   EXPECT_LT(big, biggest);
00132   EXPECT_LE(big, biggest);
00133   EXPECT_GT(biggest, big);
00134   EXPECT_GE(biggest, big);
00135   EXPECT_EQ(big, ~~big);
00136   EXPECT_EQ(one, one | one);
00137   EXPECT_EQ(big, big | big);
00138   EXPECT_EQ(one, one | zero);
00139   EXPECT_EQ(one, one & one);
00140   EXPECT_EQ(big, big & big);
00141   EXPECT_EQ(zero, one & zero);
00142   EXPECT_EQ(zero, big & ~big);
00143   EXPECT_EQ(zero, one ^ one);
00144   EXPECT_EQ(zero, big ^ big);
00145   EXPECT_EQ(one, one ^ zero);
00146 
00147   // Shift operators.
00148   EXPECT_EQ(big, big << 0);
00149   EXPECT_EQ(big, big >> 0);
00150   EXPECT_GT(big << 1, big);
00151   EXPECT_LT(big >> 1, big);
00152   EXPECT_EQ(big, (big << 10) >> 10);
00153   EXPECT_EQ(big, (big >> 1) << 1);
00154   EXPECT_EQ(one, (one << 80) >> 80);
00155   EXPECT_EQ(zero, (one >> 80) << 80);
00156 
00157   // Shift assignments.
00158   absl::uint128 big_copy = big;
00159   EXPECT_EQ(big << 0, big_copy <<= 0);
00160   big_copy = big;
00161   EXPECT_EQ(big >> 0, big_copy >>= 0);
00162   big_copy = big;
00163   EXPECT_EQ(big << 1, big_copy <<= 1);
00164   big_copy = big;
00165   EXPECT_EQ(big >> 1, big_copy >>= 1);
00166   big_copy = big;
00167   EXPECT_EQ(big << 10, big_copy <<= 10);
00168   big_copy = big;
00169   EXPECT_EQ(big >> 10, big_copy >>= 10);
00170   big_copy = big;
00171   EXPECT_EQ(big << 64, big_copy <<= 64);
00172   big_copy = big;
00173   EXPECT_EQ(big >> 64, big_copy >>= 64);
00174   big_copy = big;
00175   EXPECT_EQ(big << 73, big_copy <<= 73);
00176   big_copy = big;
00177   EXPECT_EQ(big >> 73, big_copy >>= 73);
00178 
00179   EXPECT_EQ(absl::Uint128High64(biggest), std::numeric_limits<uint64_t>::max());
00180   EXPECT_EQ(absl::Uint128Low64(biggest), std::numeric_limits<uint64_t>::max());
00181   EXPECT_EQ(zero + one, one);
00182   EXPECT_EQ(one + one, two);
00183   EXPECT_EQ(big_minus_one + one, big);
00184   EXPECT_EQ(one - one, zero);
00185   EXPECT_EQ(one - zero, one);
00186   EXPECT_EQ(zero - one, biggest);
00187   EXPECT_EQ(big - big, zero);
00188   EXPECT_EQ(big - one, big_minus_one);
00189   EXPECT_EQ(big + std::numeric_limits<uint64_t>::max(), bigger);
00190   EXPECT_EQ(biggest + 1, zero);
00191   EXPECT_EQ(zero - 1, biggest);
00192   EXPECT_EQ(high_low - one, low_high);
00193   EXPECT_EQ(low_high + one, high_low);
00194   EXPECT_EQ(absl::Uint128High64((absl::uint128(1) << 64) - 1), 0);
00195   EXPECT_EQ(absl::Uint128Low64((absl::uint128(1) << 64) - 1),
00196             std::numeric_limits<uint64_t>::max());
00197   EXPECT_TRUE(!!one);
00198   EXPECT_TRUE(!!high_low);
00199   EXPECT_FALSE(!!zero);
00200   EXPECT_FALSE(!one);
00201   EXPECT_FALSE(!high_low);
00202   EXPECT_TRUE(!zero);
00203   EXPECT_TRUE(zero == 0);       // NOLINT(readability/check)
00204   EXPECT_FALSE(zero != 0);      // NOLINT(readability/check)
00205   EXPECT_FALSE(one == 0);       // NOLINT(readability/check)
00206   EXPECT_TRUE(one != 0);        // NOLINT(readability/check)
00207   EXPECT_FALSE(high_low == 0);  // NOLINT(readability/check)
00208   EXPECT_TRUE(high_low != 0);   // NOLINT(readability/check)
00209 
00210   absl::uint128 test = zero;
00211   EXPECT_EQ(++test, one);
00212   EXPECT_EQ(test, one);
00213   EXPECT_EQ(test++, one);
00214   EXPECT_EQ(test, two);
00215   EXPECT_EQ(test -= 2, zero);
00216   EXPECT_EQ(test, zero);
00217   EXPECT_EQ(test += 2, two);
00218   EXPECT_EQ(test, two);
00219   EXPECT_EQ(--test, one);
00220   EXPECT_EQ(test, one);
00221   EXPECT_EQ(test--, one);
00222   EXPECT_EQ(test, zero);
00223   EXPECT_EQ(test |= three, three);
00224   EXPECT_EQ(test &= one, one);
00225   EXPECT_EQ(test ^= three, two);
00226   EXPECT_EQ(test >>= 1, one);
00227   EXPECT_EQ(test <<= 1, two);
00228 
00229   EXPECT_EQ(big, -(-big));
00230   EXPECT_EQ(two, -((-one) - 1));
00231   EXPECT_EQ(absl::Uint128Max(), -one);
00232   EXPECT_EQ(zero, -zero);
00233 
00234   EXPECT_EQ(absl::Uint128Max(), absl::kuint128max);
00235 }
00236 
00237 TEST(Uint128, ConversionTests) {
00238   EXPECT_TRUE(absl::MakeUint128(1, 0));
00239 
00240 #ifdef ABSL_HAVE_INTRINSIC_INT128
00241   unsigned __int128 intrinsic =
00242       (static_cast<unsigned __int128>(0x3a5b76c209de76f6) << 64) +
00243       0x1f25e1d63a2b46c5;
00244   absl::uint128 custom =
00245       absl::MakeUint128(0x3a5b76c209de76f6, 0x1f25e1d63a2b46c5);
00246 
00247   EXPECT_EQ(custom, absl::uint128(intrinsic));
00248   EXPECT_EQ(custom, absl::uint128(static_cast<__int128>(intrinsic)));
00249   EXPECT_EQ(intrinsic, static_cast<unsigned __int128>(custom));
00250   EXPECT_EQ(intrinsic, static_cast<__int128>(custom));
00251 #endif  // ABSL_HAVE_INTRINSIC_INT128
00252 
00253   // verify that an integer greater than 2**64 that can be stored precisely
00254   // inside a double is converted to a absl::uint128 without loss of
00255   // information.
00256   double precise_double = 0x530e * std::pow(2.0, 64.0) + 0xda74000000000000;
00257   absl::uint128 from_precise_double(precise_double);
00258   absl::uint128 from_precise_ints =
00259       absl::MakeUint128(0x530e, 0xda74000000000000);
00260   EXPECT_EQ(from_precise_double, from_precise_ints);
00261   EXPECT_DOUBLE_EQ(static_cast<double>(from_precise_ints), precise_double);
00262 
00263   double approx_double = 0xffffeeeeddddcccc * std::pow(2.0, 64.0) +
00264                          0xbbbbaaaa99998888;
00265   absl::uint128 from_approx_double(approx_double);
00266   EXPECT_DOUBLE_EQ(static_cast<double>(from_approx_double), approx_double);
00267 
00268   double round_to_zero = 0.7;
00269   double round_to_five = 5.8;
00270   double round_to_nine = 9.3;
00271   EXPECT_EQ(static_cast<absl::uint128>(round_to_zero), 0);
00272   EXPECT_EQ(static_cast<absl::uint128>(round_to_five), 5);
00273   EXPECT_EQ(static_cast<absl::uint128>(round_to_nine), 9);
00274 
00275   absl::uint128 highest_precision_in_long_double =
00276       ~absl::uint128{} >> (128 - std::numeric_limits<long double>::digits);
00277   EXPECT_EQ(highest_precision_in_long_double,
00278             static_cast<absl::uint128>(
00279                 static_cast<long double>(highest_precision_in_long_double)));
00280   // Apply a mask just to make sure all the bits are the right place.
00281   const absl::uint128 arbitrary_mask =
00282       absl::MakeUint128(0xa29f622677ded751, 0xf8ca66add076f468);
00283   EXPECT_EQ(highest_precision_in_long_double & arbitrary_mask,
00284             static_cast<absl::uint128>(static_cast<long double>(
00285                 highest_precision_in_long_double & arbitrary_mask)));
00286 
00287   EXPECT_EQ(static_cast<absl::uint128>(-0.1L), 0);
00288 }
00289 
00290 TEST(Uint128, OperatorAssignReturnRef) {
00291   absl::uint128 v(1);
00292   (v += 4) -= 3;
00293   EXPECT_EQ(2, v);
00294 }
00295 
00296 TEST(Uint128, Multiply) {
00297   absl::uint128 a, b, c;
00298 
00299   // Zero test.
00300   a = 0;
00301   b = 0;
00302   c = a * b;
00303   EXPECT_EQ(0, c);
00304 
00305   // Max carries.
00306   a = absl::uint128(0) - 1;
00307   b = absl::uint128(0) - 1;
00308   c = a * b;
00309   EXPECT_EQ(1, c);
00310 
00311   // Self-operation with max carries.
00312   c = absl::uint128(0) - 1;
00313   c *= c;
00314   EXPECT_EQ(1, c);
00315 
00316   // 1-bit x 1-bit.
00317   for (int i = 0; i < 64; ++i) {
00318     for (int j = 0; j < 64; ++j) {
00319       a = absl::uint128(1) << i;
00320       b = absl::uint128(1) << j;
00321       c = a * b;
00322       EXPECT_EQ(absl::uint128(1) << (i + j), c);
00323     }
00324   }
00325 
00326   // Verified with dc.
00327   a = absl::MakeUint128(0xffffeeeeddddcccc, 0xbbbbaaaa99998888);
00328   b = absl::MakeUint128(0x7777666655554444, 0x3333222211110000);
00329   c = a * b;
00330   EXPECT_EQ(absl::MakeUint128(0x530EDA741C71D4C3, 0xBF25975319080000), c);
00331   EXPECT_EQ(0, c - b * a);
00332   EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
00333 
00334   // Verified with dc.
00335   a = absl::MakeUint128(0x0123456789abcdef, 0xfedcba9876543210);
00336   b = absl::MakeUint128(0x02468ace13579bdf, 0xfdb97531eca86420);
00337   c = a * b;
00338   EXPECT_EQ(absl::MakeUint128(0x97a87f4f261ba3f2, 0x342d0bbf48948200), c);
00339   EXPECT_EQ(0, c - b * a);
00340   EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
00341 }
00342 
00343 TEST(Uint128, AliasTests) {
00344   absl::uint128 x1 = absl::MakeUint128(1, 2);
00345   absl::uint128 x2 = absl::MakeUint128(2, 4);
00346   x1 += x1;
00347   EXPECT_EQ(x2, x1);
00348 
00349   absl::uint128 x3 = absl::MakeUint128(1, static_cast<uint64_t>(1) << 63);
00350   absl::uint128 x4 = absl::MakeUint128(3, 0);
00351   x3 += x3;
00352   EXPECT_EQ(x4, x3);
00353 }
00354 
00355 TEST(Uint128, DivideAndMod) {
00356   using std::swap;
00357 
00358   // a := q * b + r
00359   absl::uint128 a, b, q, r;
00360 
00361   // Zero test.
00362   a = 0;
00363   b = 123;
00364   q = a / b;
00365   r = a % b;
00366   EXPECT_EQ(0, q);
00367   EXPECT_EQ(0, r);
00368 
00369   a = absl::MakeUint128(0x530eda741c71d4c3, 0xbf25975319080000);
00370   q = absl::MakeUint128(0x4de2cab081, 0x14c34ab4676e4bab);
00371   b = absl::uint128(0x1110001);
00372   r = absl::uint128(0x3eb455);
00373   ASSERT_EQ(a, q * b + r);  // Sanity-check.
00374 
00375   absl::uint128 result_q, result_r;
00376   result_q = a / b;
00377   result_r = a % b;
00378   EXPECT_EQ(q, result_q);
00379   EXPECT_EQ(r, result_r);
00380 
00381   // Try the other way around.
00382   swap(q, b);
00383   result_q = a / b;
00384   result_r = a % b;
00385   EXPECT_EQ(q, result_q);
00386   EXPECT_EQ(r, result_r);
00387   // Restore.
00388   swap(b, q);
00389 
00390   // Dividend < divisor; result should be q:0 r:<dividend>.
00391   swap(a, b);
00392   result_q = a / b;
00393   result_r = a % b;
00394   EXPECT_EQ(0, result_q);
00395   EXPECT_EQ(a, result_r);
00396   // Try the other way around.
00397   swap(a, q);
00398   result_q = a / b;
00399   result_r = a % b;
00400   EXPECT_EQ(0, result_q);
00401   EXPECT_EQ(a, result_r);
00402   // Restore.
00403   swap(q, a);
00404   swap(b, a);
00405 
00406   // Try a large remainder.
00407   b = a / 2 + 1;
00408   absl::uint128 expected_r =
00409       absl::MakeUint128(0x29876d3a0e38ea61, 0xdf92cba98c83ffff);
00410   // Sanity checks.
00411   ASSERT_EQ(a / 2 - 1, expected_r);
00412   ASSERT_EQ(a, b + expected_r);
00413   result_q = a / b;
00414   result_r = a % b;
00415   EXPECT_EQ(1, result_q);
00416   EXPECT_EQ(expected_r, result_r);
00417 }
00418 
00419 TEST(Uint128, DivideAndModRandomInputs) {
00420   const int kNumIters = 1 << 18;
00421   std::minstd_rand random(testing::UnitTest::GetInstance()->random_seed());
00422   std::uniform_int_distribution<uint64_t> uniform_uint64;
00423   for (int i = 0; i < kNumIters; ++i) {
00424     const absl::uint128 a =
00425         absl::MakeUint128(uniform_uint64(random), uniform_uint64(random));
00426     const absl::uint128 b =
00427         absl::MakeUint128(uniform_uint64(random), uniform_uint64(random));
00428     if (b == 0) {
00429       continue;  // Avoid a div-by-zero.
00430     }
00431     const absl::uint128 q = a / b;
00432     const absl::uint128 r = a % b;
00433     ASSERT_EQ(a, b * q + r);
00434   }
00435 }
00436 
00437 TEST(Uint128, ConstexprTest) {
00438   constexpr absl::uint128 zero = absl::uint128();
00439   constexpr absl::uint128 one = 1;
00440   constexpr absl::uint128 minus_two = -2;
00441   EXPECT_EQ(zero, absl::uint128(0));
00442   EXPECT_EQ(one, absl::uint128(1));
00443   EXPECT_EQ(minus_two, absl::MakeUint128(-1, -2));
00444 }
00445 
00446 TEST(Uint128, NumericLimitsTest) {
00447   static_assert(std::numeric_limits<absl::uint128>::is_specialized, "");
00448   static_assert(!std::numeric_limits<absl::uint128>::is_signed, "");
00449   static_assert(std::numeric_limits<absl::uint128>::is_integer, "");
00450   EXPECT_EQ(static_cast<int>(128 * std::log10(2)),
00451             std::numeric_limits<absl::uint128>::digits10);
00452   EXPECT_EQ(0, std::numeric_limits<absl::uint128>::min());
00453   EXPECT_EQ(0, std::numeric_limits<absl::uint128>::lowest());
00454   EXPECT_EQ(absl::Uint128Max(), std::numeric_limits<absl::uint128>::max());
00455 }
00456 
00457 TEST(Uint128, Hash) {
00458   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({
00459       // Some simple values
00460       absl::uint128{0},
00461       absl::uint128{1},
00462       ~absl::uint128{},
00463       // 64 bit limits
00464       absl::uint128{std::numeric_limits<int64_t>::max()},
00465       absl::uint128{std::numeric_limits<uint64_t>::max()} + 0,
00466       absl::uint128{std::numeric_limits<uint64_t>::max()} + 1,
00467       absl::uint128{std::numeric_limits<uint64_t>::max()} + 2,
00468       // Keeping high same
00469       absl::uint128{1} << 62,
00470       absl::uint128{1} << 63,
00471       // Keeping low same
00472       absl::uint128{1} << 64,
00473       absl::uint128{1} << 65,
00474       // 128 bit limits
00475       std::numeric_limits<absl::uint128>::max(),
00476       std::numeric_limits<absl::uint128>::max() - 1,
00477       std::numeric_limits<absl::uint128>::min() + 1,
00478       std::numeric_limits<absl::uint128>::min(),
00479   }));
00480 }
00481 
00482 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15