15 #include "absl/numeric/int128.h"
20 #include <type_traits>
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"
29 #if defined(_MSC_VER) && _MSC_VER == 1900
32 #pragma warning(disable:4146)
52 typedef ::testing::Types<float, double, long double> FloatingPointTypes;
56 TYPED_TEST(Uint128IntegerTraitsTest, ConstructAssignTest) {
58 "absl::uint128 must be constructible from TypeParam");
60 "absl::uint128 must be assignable from TypeParam");
62 "TypeParam must not be assignable from absl::uint128");
67 TYPED_TEST(Uint128FloatTraitsTest, ConstructAssignTest) {
69 "absl::uint128 must be constructible from TypeParam");
71 "absl::uint128 must not be assignable from TypeParam");
73 "TypeParam must not be assignable from absl::uint128");
76 #ifdef ABSL_HAVE_INTRINSIC_INT128
79 TEST(Uint128, IntrinsicTypeTraitsTest) {
81 "absl::uint128 must be constructible from __int128");
83 "absl::uint128 must be assignable from __int128");
85 "__int128 must not be assignable from absl::uint128");
88 "absl::uint128 must be constructible from unsigned __int128");
90 "absl::uint128 must be assignable from unsigned __int128");
92 "unsigned __int128 must not be assignable from absl::uint128");
94 #endif // ABSL_HAVE_INTRINSIC_INT128
96 TEST(Uint128, TrivialTraitsTest) {
105 TEST(Uint128, AllTests) {
237 TEST(Uint128, ConversionTests) {
240 #ifdef ABSL_HAVE_INTRINSIC_INT128
241 unsigned __int128 intrinsic =
242 (
static_cast<unsigned __int128
>(0x3a5b76c209de76f6) << 64) +
251 #endif // ABSL_HAVE_INTRINSIC_INT128
256 double precise_double = 0x530e * std::pow(2.0, 64.0) + 0xda74000000000000;
260 EXPECT_EQ(from_precise_double, from_precise_ints);
263 double approx_double = 0xffffeeeeddddcccc * std::pow(2.0, 64.0) +
268 double round_to_zero = 0.7;
269 double round_to_five = 5.8;
270 double round_to_nine = 9.3;
276 ~
absl::uint128{} >> (128 - std::numeric_limits<long double>::digits);
277 EXPECT_EQ(highest_precision_in_long_double,
279 static_cast<long double>(highest_precision_in_long_double)));
283 EXPECT_EQ(highest_precision_in_long_double & arbitrary_mask,
285 highest_precision_in_long_double & arbitrary_mask)));
290 TEST(Uint128, OperatorAssignReturnRef) {
296 TEST(Uint128, Multiply) {
317 for (
int i = 0;
i < 64; ++
i) {
318 for (
int j = 0;
j < 64; ++
j) {
343 TEST(Uint128, AliasTests) {
355 TEST(Uint128, DivideAndMod) {
419 TEST(Uint128, DivideAndModRandomInputs) {
420 const int kNumIters = 1 << 18;
422 std::uniform_int_distribution<uint64_t> uniform_uint64;
423 for (
int i = 0;
i < kNumIters; ++
i) {
437 TEST(Uint128, ConstexprTest) {
446 TEST(Uint128, NumericLimitsTest) {
450 EXPECT_EQ(
static_cast<int>(128 * std::log10(2)),
483 TEST(Int128Uint128, ConversionTest) {
495 assigned_value =
value;
500 -1, -0x1234567890abcdef,
507 assigned_value =
value;
512 template <
typename T>
517 TYPED_TEST(Int128IntegerTraitsTest, ConstructAssignTest) {
519 "absl::int128 must be constructible from TypeParam");
521 "absl::int128 must be assignable from TypeParam");
523 "TypeParam must not be assignable from absl::int128");
526 template <
typename T>
531 TYPED_TEST(Int128FloatTraitsTest, ConstructAssignTest) {
533 "absl::int128 must be constructible from TypeParam");
535 "absl::int128 must not be assignable from TypeParam");
537 "TypeParam must not be assignable from absl::int128");
540 #ifdef ABSL_HAVE_INTRINSIC_INT128
543 TEST(Int128, IntrinsicTypeTraitsTest) {
545 "absl::int128 must be constructible from __int128");
547 "absl::int128 must be assignable from __int128");
549 "__int128 must not be assignable from absl::int128");
552 "absl::int128 must be constructible from unsigned __int128");
554 "absl::int128 must be assignable from unsigned __int128");
556 "unsigned __int128 must not be assignable from absl::int128");
558 #endif // ABSL_HAVE_INTRINSIC_INT128
560 TEST(Int128, TrivialTraitsTest) {
568 TEST(Int128, BoolConversionTest) {
570 for (
int i = 0;
i < 64; ++
i) {
573 for (
int i = 0;
i < 63; ++
i) {
582 template <
typename T>
587 TYPED_TEST(Int128IntegerConversionTest, RoundTripTest) {
590 static_cast<TypeParam
>(
593 static_cast<TypeParam
>(
597 template <
typename T>
602 TYPED_TEST(Int128FloatConversionTest, ConstructAndCastTest) {
605 for (
int i = 0;
i < 110; ++
i) {
608 TypeParam float_value = std::ldexp(
static_cast<TypeParam
>(0x9f5b),
i);
611 EXPECT_EQ(float_value,
static_cast<TypeParam
>(int_value));
612 EXPECT_EQ(-float_value,
static_cast<TypeParam
>(-int_value));
620 0x2c43407433ba3fd1, 0x3b574ec668df6b55,
623 for (
int i = 0;
i <= 64; ++
i) {
625 <<
"value = " <<
value <<
"; i = " <<
i);
627 TypeParam fvalue = std::ldexp(
static_cast<TypeParam
>(
value),
i);
645 value >>= (127 - std::numeric_limits<TypeParam>::digits);
648 for (
int i = 0;
i < 127 - std::numeric_limits<TypeParam>::digits; ++
i) {
651 static_cast<absl::int128>(
static_cast<TypeParam
>(int_value)));
653 static_cast<absl::int128>(
static_cast<TypeParam
>(-int_value)));
664 TypeParam just_lt_one = std::nexttoward(TypeParam(1), TypeParam(0));
666 TypeParam just_gt_minus_one = std::nexttoward(TypeParam(-1), TypeParam(0));
676 TEST(Int128, FactoryTest) {
687 TEST(Int128, HighLowTest) {
692 HighLowPair
values[]{{0, 0}, {0, 1}, {1, 0}, {123, 456}, {-654, 321}};
700 TEST(Int128, LimitsTest) {
706 #if defined(ABSL_HAVE_INTRINSIC_INT128)
707 TEST(Int128, IntrinsicConversionTest) {
709 (
static_cast<__int128
>(0x3a5b76c209de76f6) << 64) + 0x1f25e1d63a2b46c5;
716 #endif // ABSL_HAVE_INTRINSIC_INT128
718 TEST(Int128, ConstexprTest) {
731 TEST(Int128, ComparisonTest) {
744 <<
"; pair.larger = " <<
pair.larger);
772 TEST(Int128, UnaryNegationTest) {
773 int64_t values64[] = {0, 1, 12345, 0x4000000000000000,
785 TEST(Int128, LogicalNotTest) {
787 for (
int i = 0;
i < 64; ++
i) {
790 for (
int i = 0;
i < 63; ++
i) {
795 TEST(Int128, AdditionSubtractionTest) {
798 std::pair<int64_t, int64_t> cases[]{
804 {89023982312461, 98346012567134},
805 {-63454234568239, -23456235230773},
806 {98263457263502, -21428561935925},
807 {-88235237438467, 15923659234573},
809 for (
const auto&
pair : cases) {
811 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
844 TEST(Int128, IncrementDecrementTest) {
856 TEST(Int128, MultiplicationTest) {
858 for (
int i = 0;
i < 64; ++
i) {
859 for (
int j = 0;
j < 127 -
i; ++
j) {
878 std::pair<int64_t, int64_t> small_values[] = {
879 {0x5e61, 0xf29f79ca14b4},
880 {0x3e033b, -0x612c0ee549},
881 {-0x052ce7e8, 0x7c728f0f},
882 {-0x3af7054626, -0xfb1e1d},
884 for (
const std::pair<int64_t, int64_t>&
pair : small_values) {
886 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
901 std::pair<int64_t, int64_t> small_values2[] = {
902 {0x1bb0a110, 0x31487671},
903 {0x4792784e, 0x28add7d7},
904 {0x7b66553a, 0x11dff8ef},
906 for (
const std::pair<int64_t, int64_t>&
pair : small_values2) {
908 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
971 TEST(Int128, DivisionAndModuloTest) {
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},
980 for (
const std::pair<int64_t, int64_t>&
pair : small_pairs) {
982 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
991 EXPECT_EQ(remainder, dividend % divisor);
1036 for (
int i = 0;
i < 127; ++
i) {
1038 <<
"value = " <<
value <<
"; i = " <<
i);
1050 struct DivisionModCase {
1056 DivisionModCase manual_cases[] = {
1068 for (
const DivisionModCase test_case : manual_cases) {
1069 EXPECT_EQ(test_case.quotient, test_case.dividend / test_case.divisor);
1071 absl::int128(test_case.dividend) /= test_case.divisor);
1072 EXPECT_EQ(test_case.remainder, test_case.dividend % test_case.divisor);
1074 absl::int128(test_case.dividend) %= test_case.divisor);
1078 TEST(Int128, BitwiseLogicTest) {
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},
1115 for (
const std::pair<int64_t, int64_t>&
pair : pairs64) {
1117 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
1158 TEST(Int128, BitwiseShiftTest) {
1159 for (
int i = 0;
i < 64; ++
i) {
1160 for (
int j = 0;
j <=
i; ++
j) {
1167 for (
int i = 0;
i < 63; ++
i) {
1168 for (
int j = 0;
j < 64; ++
j) {
1176 for (
int j = 0;
j <=
i; ++
j) {
1186 for (
int i = 0;
i < 64; ++
i) {
1187 for (
int j =
i;
j < 64; ++
j) {
1193 for (
int j = 0;
j < 63; ++
j) {
1202 for (
int i = 0;
i < 63; ++
i) {
1203 for (
int j =
i;
j < 63; ++
j) {
1214 TEST(Int128, NumericLimitsTest) {
1218 EXPECT_EQ(
static_cast<int>(127 * std::log10(2)),