about summary refs log tree commit diff
path: root/absl/numeric
diff options
context:
space:
mode:
Diffstat (limited to 'absl/numeric')
-rw-r--r--absl/numeric/int128.h45
-rw-r--r--absl/numeric/int128_test.cc6
2 files changed, 26 insertions, 25 deletions
diff --git a/absl/numeric/int128.h b/absl/numeric/int128.h
index a204ac4d9c3f..bd1b553ab2dc 100644
--- a/absl/numeric/int128.h
+++ b/absl/numeric/int128.h
@@ -71,8 +71,8 @@ namespace absl {
 //
 // Example:
 //
-//     float y = absl::kuint128max;  // Error. uint128 cannot be implicitly
-//                                   // converted to float.
+//     float y = absl::Uint128Max();  // Error. uint128 cannot be implicitly
+//                                    // converted to float.
 //
 //     absl::uint128 v;
 //     absl::uint64_t i = v;                         // Error
@@ -177,6 +177,11 @@ class alignas(16) uint128 {
   //   absl::uint128 big = absl::MakeUint128(1, 0);
   friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low);
 
+  // Uint128Max()
+  //
+  // Returns the highest value for a 128-bit unsigned integer.
+  friend constexpr uint128 Uint128Max();
+
  private:
   constexpr uint128(uint64_t high, uint64_t low);
 
@@ -195,6 +200,9 @@ class alignas(16) uint128 {
 #endif  // byte order
 };
 
+// Prefer to use the constexpr `Uint128Max()`.
+//
+// TODO(absl-team) deprecate kuint128max once migration tool is released.
 extern const uint128 kuint128max;
 
 // allow uint128 to be logged
@@ -212,6 +220,11 @@ constexpr uint128 MakeUint128(uint64_t high, uint64_t low) {
   return uint128(high, low);
 }
 
+constexpr uint128 Uint128Max() {
+  return uint128(std::numeric_limits<uint64_t>::max(),
+                 std::numeric_limits<uint64_t>::max());
+}
+
 // Assignment from integer types.
 
 inline uint128& uint128::operator=(int v) { return *this = uint128(v); }
@@ -251,33 +264,19 @@ inline uint128& uint128::operator=(unsigned __int128 v) {
 
 // Shift and arithmetic operators.
 
-inline uint128 operator<<(uint128 lhs, int amount) {
-  return uint128(lhs) <<= amount;
-}
+inline uint128 operator<<(uint128 lhs, int amount) { return lhs <<= amount; }
 
-inline uint128 operator>>(uint128 lhs, int amount) {
-  return uint128(lhs) >>= amount;
-}
+inline uint128 operator>>(uint128 lhs, int amount) { return lhs >>= amount; }
 
-inline uint128 operator+(uint128 lhs, uint128 rhs) {
-  return uint128(lhs) += rhs;
-}
+inline uint128 operator+(uint128 lhs, uint128 rhs) { return lhs += rhs; }
 
-inline uint128 operator-(uint128 lhs, uint128 rhs) {
-  return uint128(lhs) -= rhs;
-}
+inline uint128 operator-(uint128 lhs, uint128 rhs) { return lhs -= rhs; }
 
-inline uint128 operator*(uint128 lhs, uint128 rhs) {
-  return uint128(lhs) *= rhs;
-}
+inline uint128 operator*(uint128 lhs, uint128 rhs) { return lhs *= rhs; }
 
-inline uint128 operator/(uint128 lhs, uint128 rhs) {
-  return uint128(lhs) /= rhs;
-}
+inline uint128 operator/(uint128 lhs, uint128 rhs) { return lhs /= rhs; }
 
-inline uint128 operator%(uint128 lhs, uint128 rhs) {
-  return uint128(lhs) %= rhs;
-}
+inline uint128 operator%(uint128 lhs, uint128 rhs) { return lhs %= rhs; }
 
 constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; }
 
diff --git a/absl/numeric/int128_test.cc b/absl/numeric/int128_test.cc
index d674cb125f5e..79bcca907ae9 100644
--- a/absl/numeric/int128_test.cc
+++ b/absl/numeric/int128_test.cc
@@ -110,7 +110,7 @@ TEST(Uint128, AllTests) {
   absl::uint128 big = absl::MakeUint128(2000, 2);
   absl::uint128 big_minus_one = absl::MakeUint128(2000, 1);
   absl::uint128 bigger = absl::MakeUint128(2001, 1);
-  absl::uint128 biggest = absl::kuint128max;
+  absl::uint128 biggest = absl::Uint128Max();
   absl::uint128 high_low = absl::MakeUint128(1, 0);
   absl::uint128 low_high =
       absl::MakeUint128(0, std::numeric_limits<uint64_t>::max());
@@ -227,8 +227,10 @@ TEST(Uint128, AllTests) {
 
   EXPECT_EQ(big, -(-big));
   EXPECT_EQ(two, -((-one) - 1));
-  EXPECT_EQ(absl::kuint128max, -one);
+  EXPECT_EQ(absl::Uint128Max(), -one);
   EXPECT_EQ(zero, -zero);
+
+  EXPECT_EQ(absl::Uint128Max(), absl::kuint128max);
 }
 
 TEST(Uint128, ConversionTests) {