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) {
242 TEST(Int128, RightShiftOfNegativeNumbers) {
247 if ((-6 >> 1) == -3) {
260 TEST(Uint128, ConversionTests) {
263 #ifdef ABSL_HAVE_INTRINSIC_INT128
264 unsigned __int128 intrinsic =
265 (
static_cast<unsigned __int128
>(0x3a5b76c209de76f6) << 64) +
274 #endif // ABSL_HAVE_INTRINSIC_INT128
279 double precise_double = 0x530e * std::pow(2.0, 64.0) + 0xda74000000000000;
283 EXPECT_EQ(from_precise_double, from_precise_ints);
286 double approx_double = 0xffffeeeeddddcccc * std::pow(2.0, 64.0) +
291 double round_to_zero = 0.7;
292 double round_to_five = 5.8;
293 double round_to_nine = 9.3;
299 ~
absl::uint128{} >> (128 - std::numeric_limits<long double>::digits);
300 EXPECT_EQ(highest_precision_in_long_double,
302 static_cast<long double>(highest_precision_in_long_double)));
306 EXPECT_EQ(highest_precision_in_long_double & arbitrary_mask,
308 highest_precision_in_long_double & arbitrary_mask)));
313 TEST(Uint128, OperatorAssignReturnRef) {
319 TEST(Uint128, Multiply) {
340 for (
int i = 0;
i < 64; ++
i) {
341 for (
int j = 0;
j < 64; ++
j) {
366 TEST(Uint128, AliasTests) {
378 TEST(Uint128, DivideAndMod) {
442 TEST(Uint128, DivideAndModRandomInputs) {
443 const int kNumIters = 1 << 18;
445 std::uniform_int_distribution<uint64_t> uniform_uint64;
446 for (
int i = 0;
i < kNumIters; ++
i) {
460 TEST(Uint128, ConstexprTest) {
469 TEST(Uint128, NumericLimitsTest) {
473 EXPECT_EQ(
static_cast<int>(128 * std::log10(2)),
506 TEST(Int128Uint128, ConversionTest) {
518 assigned_value =
value;
523 -1, -0x1234567890abcdef,
530 assigned_value =
value;
535 template <
typename T>
540 TYPED_TEST(Int128IntegerTraitsTest, ConstructAssignTest) {
542 "absl::int128 must be constructible from TypeParam");
544 "absl::int128 must be assignable from TypeParam");
546 "TypeParam must not be assignable from absl::int128");
549 template <
typename T>
554 TYPED_TEST(Int128FloatTraitsTest, ConstructAssignTest) {
556 "absl::int128 must be constructible from TypeParam");
558 "absl::int128 must not be assignable from TypeParam");
560 "TypeParam must not be assignable from absl::int128");
563 #ifdef ABSL_HAVE_INTRINSIC_INT128
566 TEST(Int128, IntrinsicTypeTraitsTest) {
568 "absl::int128 must be constructible from __int128");
570 "absl::int128 must be assignable from __int128");
572 "__int128 must not be assignable from absl::int128");
575 "absl::int128 must be constructible from unsigned __int128");
577 "absl::int128 must be assignable from unsigned __int128");
579 "unsigned __int128 must not be assignable from absl::int128");
581 #endif // ABSL_HAVE_INTRINSIC_INT128
583 TEST(Int128, TrivialTraitsTest) {
591 TEST(Int128, BoolConversionTest) {
593 for (
int i = 0;
i < 64; ++
i) {
596 for (
int i = 0;
i < 63; ++
i) {
605 template <
typename T>
610 TYPED_TEST(Int128IntegerConversionTest, RoundTripTest) {
613 static_cast<TypeParam
>(
616 static_cast<TypeParam
>(
620 template <
typename T>
625 TYPED_TEST(Int128FloatConversionTest, ConstructAndCastTest) {
628 for (
int i = 0;
i < 110; ++
i) {
631 TypeParam float_value = std::ldexp(
static_cast<TypeParam
>(0x9f5b),
i);
634 EXPECT_EQ(float_value,
static_cast<TypeParam
>(int_value));
635 EXPECT_EQ(-float_value,
static_cast<TypeParam
>(-int_value));
643 0x2c43407433ba3fd1, 0x3b574ec668df6b55,
646 for (
int i = 0;
i <= 64; ++
i) {
648 <<
"value = " <<
value <<
"; i = " <<
i);
650 TypeParam fvalue = std::ldexp(
static_cast<TypeParam
>(
value),
i);
668 value >>= (127 - std::numeric_limits<TypeParam>::digits);
671 for (
int i = 0;
i < 127 - std::numeric_limits<TypeParam>::digits; ++
i) {
674 static_cast<absl::int128>(
static_cast<TypeParam
>(int_value)));
676 static_cast<absl::int128>(
static_cast<TypeParam
>(-int_value)));
687 TypeParam just_lt_one = std::nexttoward(TypeParam(1), TypeParam(0));
689 TypeParam just_gt_minus_one = std::nexttoward(TypeParam(-1), TypeParam(0));
699 TEST(Int128, FactoryTest) {
710 TEST(Int128, HighLowTest) {
715 HighLowPair
values[]{{0, 0}, {0, 1}, {1, 0}, {123, 456}, {-654, 321}};
723 TEST(Int128, LimitsTest) {
729 #if defined(ABSL_HAVE_INTRINSIC_INT128)
730 TEST(Int128, IntrinsicConversionTest) {
732 (
static_cast<__int128
>(0x3a5b76c209de76f6) << 64) + 0x1f25e1d63a2b46c5;
739 #endif // ABSL_HAVE_INTRINSIC_INT128
741 TEST(Int128, ConstexprTest) {
754 TEST(Int128, ComparisonTest) {
767 <<
"; pair.larger = " <<
pair.larger);
795 TEST(Int128, UnaryPlusTest) {
796 int64_t values64[] = {0, 1, 12345, 0x4000000000000000,
808 TEST(Int128, UnaryNegationTest) {
809 int64_t values64[] = {0, 1, 12345, 0x4000000000000000,
821 TEST(Int128, LogicalNotTest) {
823 for (
int i = 0;
i < 64; ++
i) {
826 for (
int i = 0;
i < 63; ++
i) {
831 TEST(Int128, AdditionSubtractionTest) {
834 std::pair<int64_t, int64_t> cases[]{
840 {89023982312461, 98346012567134},
841 {-63454234568239, -23456235230773},
842 {98263457263502, -21428561935925},
843 {-88235237438467, 15923659234573},
845 for (
const auto&
pair : cases) {
847 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
880 TEST(Int128, IncrementDecrementTest) {
892 TEST(Int128, MultiplicationTest) {
894 for (
int i = 0;
i < 64; ++
i) {
895 for (
int j = 0;
j < 127 -
i; ++
j) {
914 std::pair<int64_t, int64_t> small_values[] = {
915 {0x5e61, 0xf29f79ca14b4},
916 {0x3e033b, -0x612c0ee549},
917 {-0x052ce7e8, 0x7c728f0f},
918 {-0x3af7054626, -0xfb1e1d},
920 for (
const std::pair<int64_t, int64_t>&
pair : small_values) {
922 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
937 std::pair<int64_t, int64_t> small_values2[] = {
938 {0x1bb0a110, 0x31487671},
939 {0x4792784e, 0x28add7d7},
940 {0x7b66553a, 0x11dff8ef},
942 for (
const std::pair<int64_t, int64_t>&
pair : small_values2) {
944 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
1007 TEST(Int128, DivisionAndModuloTest) {
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},
1016 for (
const std::pair<int64_t, int64_t>&
pair : small_pairs) {
1018 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
1025 EXPECT_EQ(quotient, dividend / divisor);
1027 EXPECT_EQ(remainder, dividend % divisor);
1072 for (
int i = 0;
i < 127; ++
i) {
1074 <<
"value = " <<
value <<
"; i = " <<
i);
1086 struct DivisionModCase {
1092 DivisionModCase manual_cases[] = {
1104 for (
const DivisionModCase test_case : manual_cases) {
1105 EXPECT_EQ(test_case.quotient, test_case.dividend / test_case.divisor);
1107 absl::int128(test_case.dividend) /= test_case.divisor);
1108 EXPECT_EQ(test_case.remainder, test_case.dividend % test_case.divisor);
1110 absl::int128(test_case.dividend) %= test_case.divisor);
1114 TEST(Int128, BitwiseLogicTest) {
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},
1151 for (
const std::pair<int64_t, int64_t>&
pair : pairs64) {
1153 <<
"pair = {" <<
pair.first <<
", " <<
pair.second <<
'}');
1194 TEST(Int128, BitwiseShiftTest) {
1195 for (
int i = 0;
i < 64; ++
i) {
1196 for (
int j = 0;
j <=
i; ++
j) {
1203 for (
int i = 0;
i < 63; ++
i) {
1204 for (
int j = 0;
j < 64; ++
j) {
1212 for (
int j = 0;
j <=
i; ++
j) {
1222 for (
int i = 0;
i < 64; ++
i) {
1223 for (
int j =
i;
j < 64; ++
j) {
1229 for (
int j = 0;
j < 63; ++
j) {
1238 for (
int i = 0;
i < 63; ++
i) {
1239 for (
int j =
i;
j < 63; ++
j) {
1250 TEST(Int128, NumericLimitsTest) {
1254 EXPECT_EQ(
static_cast<int>(127 * std::log10(2)),