Skip to content
Snippets Groups Projects
Commit be40fdf1 authored by Abseil Team's avatar Abseil Team Committed by Derek Mauro
Browse files

Changes imported from Abseil "staging" branch:

  - c01cbbc23470f60e54aa28f88faa49df17d26c9c Add constexpr function for max uint128 to replace global ... by Alex Strelnikov <strel@google.com>
  - 07491549940e51fb6a0c8aad0ce101cb2830ee8e fix __GNUC__ typo in ABSL_ATTRIBUTE_RETURNS_NONNULL test by Abseil Team <absl-team@google.com>
  - 1bc29bafa53c387b0e097856cffb2fb9ce56c331 Remove redundant uint128 function cast expressions. by Alex Strelnikov <strel@google.com>
  - e3dddbd7be9e85e1bac77648af35ddccdba4500e Add ABSL_ATTRIBUTE_RETURNS_NONNULL macro. by Chris Kennelly <ckennelly@google.com>

GitOrigin-RevId: c01cbbc23470f60e54aa28f88faa49df17d26c9c
Change-Id: I7a5f608f58fb53bf013cdf56018f5b39842427fc
parent c742b723
No related branches found
No related tags found
No related merge requests found
...@@ -281,6 +281,18 @@ ...@@ -281,6 +281,18 @@
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
#endif #endif
// ABSL_ATTRIBUTE_RETURNS_NONNULL
//
// Tells the compiler that a particular function never returns a null pointer.
#if ABSL_HAVE_ATTRIBUTE(returns_nonnull) || \
(defined(__GNUC__) && \
(__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
!defined(__clang__))
#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
#else
#define ABSL_ATTRIBUTE_RETURNS_NONNULL
#endif
// ABSL_HAVE_ATTRIBUTE_SECTION // ABSL_HAVE_ATTRIBUTE_SECTION
// //
// Indicates whether labeled sections are supported. Labeled sections are not // Indicates whether labeled sections are supported. Labeled sections are not
......
...@@ -71,8 +71,8 @@ namespace absl { ...@@ -71,8 +71,8 @@ namespace absl {
// //
// Example: // Example:
// //
// float y = absl::kuint128max; // Error. uint128 cannot be implicitly // float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly
// // converted to float. // // converted to float.
// //
// absl::uint128 v; // absl::uint128 v;
// absl::uint64_t i = v; // Error // absl::uint64_t i = v; // Error
...@@ -177,6 +177,11 @@ class alignas(16) uint128 { ...@@ -177,6 +177,11 @@ class alignas(16) uint128 {
// absl::uint128 big = absl::MakeUint128(1, 0); // absl::uint128 big = absl::MakeUint128(1, 0);
friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low); 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: private:
constexpr uint128(uint64_t high, uint64_t low); constexpr uint128(uint64_t high, uint64_t low);
...@@ -195,6 +200,9 @@ class alignas(16) uint128 { ...@@ -195,6 +200,9 @@ class alignas(16) uint128 {
#endif // byte order #endif // byte order
}; };
// Prefer to use the constexpr `Uint128Max()`.
//
// TODO(absl-team) deprecate kuint128max once migration tool is released.
extern const uint128 kuint128max; extern const uint128 kuint128max;
// allow uint128 to be logged // allow uint128 to be logged
...@@ -212,6 +220,11 @@ constexpr uint128 MakeUint128(uint64_t high, uint64_t low) { ...@@ -212,6 +220,11 @@ constexpr uint128 MakeUint128(uint64_t high, uint64_t low) {
return uint128(high, 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. // Assignment from integer types.
inline uint128& uint128::operator=(int v) { return *this = uint128(v); } inline uint128& uint128::operator=(int v) { return *this = uint128(v); }
...@@ -251,33 +264,19 @@ inline uint128& uint128::operator=(unsigned __int128 v) { ...@@ -251,33 +264,19 @@ inline uint128& uint128::operator=(unsigned __int128 v) {
// Shift and arithmetic operators. // Shift and arithmetic operators.
inline uint128 operator<<(uint128 lhs, int amount) { inline uint128 operator<<(uint128 lhs, int amount) { return lhs <<= amount; }
return uint128(lhs) <<= amount;
}
inline uint128 operator>>(uint128 lhs, int amount) { inline uint128 operator>>(uint128 lhs, int amount) { return lhs >>= amount; }
return uint128(lhs) >>= amount;
}
inline uint128 operator+(uint128 lhs, uint128 rhs) { inline uint128 operator+(uint128 lhs, uint128 rhs) { return lhs += rhs; }
return uint128(lhs) += rhs;
}
inline uint128 operator-(uint128 lhs, uint128 rhs) { inline uint128 operator-(uint128 lhs, uint128 rhs) { return lhs -= rhs; }
return uint128(lhs) -= rhs;
}
inline uint128 operator*(uint128 lhs, uint128 rhs) { inline uint128 operator*(uint128 lhs, uint128 rhs) { return lhs *= rhs; }
return uint128(lhs) *= rhs;
}
inline uint128 operator/(uint128 lhs, uint128 rhs) { inline uint128 operator/(uint128 lhs, uint128 rhs) { return lhs /= rhs; }
return uint128(lhs) /= rhs;
}
inline uint128 operator%(uint128 lhs, uint128 rhs) { inline uint128 operator%(uint128 lhs, uint128 rhs) { return lhs %= rhs; }
return uint128(lhs) %= rhs;
}
constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; } constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; }
......
...@@ -110,7 +110,7 @@ TEST(Uint128, AllTests) { ...@@ -110,7 +110,7 @@ TEST(Uint128, AllTests) {
absl::uint128 big = absl::MakeUint128(2000, 2); absl::uint128 big = absl::MakeUint128(2000, 2);
absl::uint128 big_minus_one = absl::MakeUint128(2000, 1); absl::uint128 big_minus_one = absl::MakeUint128(2000, 1);
absl::uint128 bigger = absl::MakeUint128(2001, 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 high_low = absl::MakeUint128(1, 0);
absl::uint128 low_high = absl::uint128 low_high =
absl::MakeUint128(0, std::numeric_limits<uint64_t>::max()); absl::MakeUint128(0, std::numeric_limits<uint64_t>::max());
...@@ -227,8 +227,10 @@ TEST(Uint128, AllTests) { ...@@ -227,8 +227,10 @@ TEST(Uint128, AllTests) {
EXPECT_EQ(big, -(-big)); EXPECT_EQ(big, -(-big));
EXPECT_EQ(two, -((-one) - 1)); EXPECT_EQ(two, -((-one) - 1));
EXPECT_EQ(absl::kuint128max, -one); EXPECT_EQ(absl::Uint128Max(), -one);
EXPECT_EQ(zero, -zero); EXPECT_EQ(zero, -zero);
EXPECT_EQ(absl::Uint128Max(), absl::kuint128max);
} }
TEST(Uint128, ConversionTests) { TEST(Uint128, ConversionTests) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment