summaryrefslogtreecommitdiff
path: root/abseil-cpp/absl/numeric/int128.h
diff options
context:
space:
mode:
Diffstat (limited to 'abseil-cpp/absl/numeric/int128.h')
-rw-r--r--abseil-cpp/absl/numeric/int128.h282
1 files changed, 176 insertions, 106 deletions
diff --git a/abseil-cpp/absl/numeric/int128.h b/abseil-cpp/absl/numeric/int128.h
index 0dd814a..7530a79 100644
--- a/abseil-cpp/absl/numeric/int128.h
+++ b/abseil-cpp/absl/numeric/int128.h
@@ -18,6 +18,10 @@
// -----------------------------------------------------------------------------
//
// This header file defines 128-bit integer types, `uint128` and `int128`.
+//
+// TODO(absl-team): This module is inconsistent as many inline `uint128` methods
+// are defined in this file, while many inline `int128` methods are defined in
+// the `int128_*_intrinsic.inc` files.
#ifndef ABSL_NUMERIC_INT128_H_
#define ABSL_NUMERIC_INT128_H_
@@ -28,6 +32,7 @@
#include <cstring>
#include <iosfwd>
#include <limits>
+#include <string>
#include <utility>
#include "absl/base/config.h"
@@ -40,7 +45,7 @@
// builtin type. We need to make sure not to define operator wchar_t()
// alongside operator unsigned short() in these instances.
#define ABSL_INTERNAL_WCHAR_T __wchar_t
-#if defined(_M_X64)
+#if defined(_M_X64) && !defined(_M_ARM64EC)
#include <intrin.h>
#pragma intrinsic(_umul128)
#endif // defined(_M_X64)
@@ -115,8 +120,8 @@ class
#ifdef ABSL_HAVE_INTRINSIC_INT128
constexpr uint128(__int128 v); // NOLINT(runtime/explicit)
constexpr uint128(unsigned __int128 v); // NOLINT(runtime/explicit)
-#endif // ABSL_HAVE_INTRINSIC_INT128
- constexpr uint128(int128 v); // NOLINT(runtime/explicit)
+#endif // ABSL_HAVE_INTRINSIC_INT128
+ constexpr uint128(int128 v); // NOLINT(runtime/explicit)
explicit uint128(float v);
explicit uint128(double v);
explicit uint128(long double v);
@@ -213,9 +218,17 @@ class
return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
}
+ // Support for absl::StrCat() etc.
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, uint128 v) {
+ sink.Append(v.ToString());
+ }
+
private:
constexpr uint128(uint64_t high, uint64_t low);
+ std::string ToString() const;
+
// TODO(strel) Update implementation to use __int128 once all users of
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add
// alignas(16) to class definition to keep alignment consistent across
@@ -282,9 +295,9 @@ class numeric_limits<absl::uint128> {
#endif // ABSL_HAVE_INTRINSIC_INT128
static constexpr bool tinyness_before = false;
- static constexpr absl::uint128 (min)() { return 0; }
+ static constexpr absl::uint128(min)() { return 0; }
static constexpr absl::uint128 lowest() { return 0; }
- static constexpr absl::uint128 (max)() { return absl::Uint128Max(); }
+ static constexpr absl::uint128(max)() { return absl::Uint128Max(); }
static constexpr absl::uint128 epsilon() { return 0; }
static constexpr absl::uint128 round_error() { return 0; }
static constexpr absl::uint128 infinity() { return 0; }
@@ -450,9 +463,17 @@ class int128 {
return H::combine(std::move(h), Int128High64(v), Int128Low64(v));
}
+ // Support for absl::StrCat() etc.
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, int128 v) {
+ sink.Append(v.ToString());
+ }
+
private:
constexpr int128(int64_t high, uint64_t low);
+ std::string ToString() const;
+
#if defined(ABSL_HAVE_INTRINSIC_INT128)
__int128 v_;
#else // ABSL_HAVE_INTRINSIC_INT128
@@ -517,9 +538,9 @@ class numeric_limits<absl::int128> {
#endif // ABSL_HAVE_INTRINSIC_INT128
static constexpr bool tinyness_before = false;
- static constexpr absl::int128 (min)() { return absl::Int128Min(); }
+ static constexpr absl::int128(min)() { return absl::Int128Min(); }
static constexpr absl::int128 lowest() { return absl::Int128Min(); }
- static constexpr absl::int128 (max)() { return absl::Int128Max(); }
+ static constexpr absl::int128(max)() { return absl::Int128Max(); }
static constexpr absl::int128 epsilon() { return 0; }
static constexpr absl::int128 round_error() { return 0; }
static constexpr absl::int128 infinity() { return 0; }
@@ -557,9 +578,7 @@ inline uint128& uint128::operator=(unsigned long v) {
}
// NOLINTNEXTLINE(runtime/int)
-inline uint128& uint128::operator=(long long v) {
- return *this = uint128(v);
-}
+inline uint128& uint128::operator=(long long v) { return *this = uint128(v); }
// NOLINTNEXTLINE(runtime/int)
inline uint128& uint128::operator=(unsigned long long v) {
@@ -567,25 +586,21 @@ inline uint128& uint128::operator=(unsigned long long v) {
}
#ifdef ABSL_HAVE_INTRINSIC_INT128
-inline uint128& uint128::operator=(__int128 v) {
- return *this = uint128(v);
-}
+inline uint128& uint128::operator=(__int128 v) { return *this = uint128(v); }
inline uint128& uint128::operator=(unsigned __int128 v) {
return *this = uint128(v);
}
#endif // ABSL_HAVE_INTRINSIC_INT128
-inline uint128& uint128::operator=(int128 v) {
- return *this = uint128(v);
-}
+inline uint128& uint128::operator=(int128 v) { return *this = uint128(v); }
// Arithmetic operators.
-uint128 operator<<(uint128 lhs, int amount);
-uint128 operator>>(uint128 lhs, int amount);
-uint128 operator+(uint128 lhs, uint128 rhs);
-uint128 operator-(uint128 lhs, uint128 rhs);
+constexpr uint128 operator<<(uint128 lhs, int amount);
+constexpr uint128 operator>>(uint128 lhs, int amount);
+constexpr uint128 operator+(uint128 lhs, uint128 rhs);
+constexpr uint128 operator-(uint128 lhs, uint128 rhs);
uint128 operator*(uint128 lhs, uint128 rhs);
uint128 operator/(uint128 lhs, uint128 rhs);
uint128 operator%(uint128 lhs, uint128 rhs);
@@ -633,8 +648,7 @@ constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; }
#if defined(ABSL_IS_LITTLE_ENDIAN)
-constexpr uint128::uint128(uint64_t high, uint64_t low)
- : lo_{low}, hi_{high} {}
+constexpr uint128::uint128(uint64_t high, uint64_t low) : lo_{low}, hi_{high} {}
constexpr uint128::uint128(int v)
: lo_{static_cast<uint64_t>(v)},
@@ -666,8 +680,7 @@ constexpr uint128::uint128(int128 v)
#elif defined(ABSL_IS_BIG_ENDIAN)
-constexpr uint128::uint128(uint64_t high, uint64_t low)
- : hi_{high}, lo_{low} {}
+constexpr uint128::uint128(uint64_t high, uint64_t low) : hi_{high}, lo_{low} {}
constexpr uint128::uint128(int v)
: hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0},
@@ -782,16 +795,19 @@ inline uint128::operator long double() const {
// Comparison operators.
-inline bool operator==(uint128 lhs, uint128 rhs) {
+constexpr bool operator==(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) ==
+ static_cast<unsigned __int128>(rhs);
+#else
return (Uint128Low64(lhs) == Uint128Low64(rhs) &&
Uint128High64(lhs) == Uint128High64(rhs));
+#endif
}
-inline bool operator!=(uint128 lhs, uint128 rhs) {
- return !(lhs == rhs);
-}
+constexpr bool operator!=(uint128 lhs, uint128 rhs) { return !(lhs == rhs); }
-inline bool operator<(uint128 lhs, uint128 rhs) {
+constexpr bool operator<(uint128 lhs, uint128 rhs) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) <
static_cast<unsigned __int128>(rhs);
@@ -802,118 +818,165 @@ inline bool operator<(uint128 lhs, uint128 rhs) {
#endif
}
-inline bool operator>(uint128 lhs, uint128 rhs) { return rhs < lhs; }
+constexpr bool operator>(uint128 lhs, uint128 rhs) { return rhs < lhs; }
-inline bool operator<=(uint128 lhs, uint128 rhs) { return !(rhs < lhs); }
+constexpr bool operator<=(uint128 lhs, uint128 rhs) { return !(rhs < lhs); }
-inline bool operator>=(uint128 lhs, uint128 rhs) { return !(lhs < rhs); }
+constexpr bool operator>=(uint128 lhs, uint128 rhs) { return !(lhs < rhs); }
// Unary operators.
-inline uint128 operator-(uint128 val) {
- uint64_t hi = ~Uint128High64(val);
- uint64_t lo = ~Uint128Low64(val) + 1;
- if (lo == 0) ++hi; // carry
- return MakeUint128(hi, lo);
+constexpr inline uint128 operator+(uint128 val) { return val; }
+
+constexpr inline int128 operator+(int128 val) { return val; }
+
+constexpr uint128 operator-(uint128 val) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return -static_cast<unsigned __int128>(val);
+#else
+ return MakeUint128(
+ ~Uint128High64(val) + static_cast<unsigned long>(Uint128Low64(val) == 0),
+ ~Uint128Low64(val) + 1);
+#endif
}
-inline bool operator!(uint128 val) {
+constexpr inline bool operator!(uint128 val) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return !static_cast<unsigned __int128>(val);
+#else
return !Uint128High64(val) && !Uint128Low64(val);
+#endif
}
// Logical operators.
-inline uint128 operator~(uint128 val) {
+constexpr inline uint128 operator~(uint128 val) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return ~static_cast<unsigned __int128>(val);
+#else
return MakeUint128(~Uint128High64(val), ~Uint128Low64(val));
+#endif
}
-inline uint128 operator|(uint128 lhs, uint128 rhs) {
+constexpr inline uint128 operator|(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) |
+ static_cast<unsigned __int128>(rhs);
+#else
return MakeUint128(Uint128High64(lhs) | Uint128High64(rhs),
- Uint128Low64(lhs) | Uint128Low64(rhs));
+ Uint128Low64(lhs) | Uint128Low64(rhs));
+#endif
}
-inline uint128 operator&(uint128 lhs, uint128 rhs) {
+constexpr inline uint128 operator&(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) &
+ static_cast<unsigned __int128>(rhs);
+#else
return MakeUint128(Uint128High64(lhs) & Uint128High64(rhs),
- Uint128Low64(lhs) & Uint128Low64(rhs));
+ Uint128Low64(lhs) & Uint128Low64(rhs));
+#endif
}
-inline uint128 operator^(uint128 lhs, uint128 rhs) {
+constexpr inline uint128 operator^(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) ^
+ static_cast<unsigned __int128>(rhs);
+#else
return MakeUint128(Uint128High64(lhs) ^ Uint128High64(rhs),
- Uint128Low64(lhs) ^ Uint128Low64(rhs));
+ Uint128Low64(lhs) ^ Uint128Low64(rhs));
+#endif
}
inline uint128& uint128::operator|=(uint128 other) {
- hi_ |= other.hi_;
- lo_ |= other.lo_;
+ *this = *this | other;
return *this;
}
inline uint128& uint128::operator&=(uint128 other) {
- hi_ &= other.hi_;
- lo_ &= other.lo_;
+ *this = *this & other;
return *this;
}
inline uint128& uint128::operator^=(uint128 other) {
- hi_ ^= other.hi_;
- lo_ ^= other.lo_;
+ *this = *this ^ other;
return *this;
}
// Arithmetic operators.
-inline uint128 operator<<(uint128 lhs, int amount) {
+constexpr uint128 operator<<(uint128 lhs, int amount) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) << amount;
#else
// uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing.
- if (amount < 64) {
- if (amount != 0) {
- return MakeUint128(
- (Uint128High64(lhs) << amount) | (Uint128Low64(lhs) >> (64 - amount)),
- Uint128Low64(lhs) << amount);
- }
- return lhs;
- }
- return MakeUint128(Uint128Low64(lhs) << (amount - 64), 0);
+ return amount >= 64 ? MakeUint128(Uint128Low64(lhs) << (amount - 64), 0)
+ : amount == 0 ? lhs
+ : MakeUint128((Uint128High64(lhs) << amount) |
+ (Uint128Low64(lhs) >> (64 - amount)),
+ Uint128Low64(lhs) << amount);
#endif
}
-inline uint128 operator>>(uint128 lhs, int amount) {
+constexpr uint128 operator>>(uint128 lhs, int amount) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) >> amount;
#else
// uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing.
- if (amount < 64) {
- if (amount != 0) {
- return MakeUint128(Uint128High64(lhs) >> amount,
- (Uint128Low64(lhs) >> amount) |
- (Uint128High64(lhs) << (64 - amount)));
- }
- return lhs;
- }
- return MakeUint128(0, Uint128High64(lhs) >> (amount - 64));
+ return amount >= 64 ? MakeUint128(0, Uint128High64(lhs) >> (amount - 64))
+ : amount == 0 ? lhs
+ : MakeUint128(Uint128High64(lhs) >> amount,
+ (Uint128Low64(lhs) >> amount) |
+ (Uint128High64(lhs) << (64 - amount)));
#endif
}
-inline uint128 operator+(uint128 lhs, uint128 rhs) {
- uint128 result = MakeUint128(Uint128High64(lhs) + Uint128High64(rhs),
- Uint128Low64(lhs) + Uint128Low64(rhs));
- if (Uint128Low64(result) < Uint128Low64(lhs)) { // check for carry
- return MakeUint128(Uint128High64(result) + 1, Uint128Low64(result));
- }
- return result;
+#if !defined(ABSL_HAVE_INTRINSIC_INT128)
+namespace int128_internal {
+constexpr uint128 AddResult(uint128 result, uint128 lhs) {
+ // check for carry
+ return (Uint128Low64(result) < Uint128Low64(lhs))
+ ? MakeUint128(Uint128High64(result) + 1, Uint128Low64(result))
+ : result;
}
+} // namespace int128_internal
+#endif
-inline uint128 operator-(uint128 lhs, uint128 rhs) {
- uint128 result = MakeUint128(Uint128High64(lhs) - Uint128High64(rhs),
- Uint128Low64(lhs) - Uint128Low64(rhs));
- if (Uint128Low64(lhs) < Uint128Low64(rhs)) { // check for carry
- return MakeUint128(Uint128High64(result) - 1, Uint128Low64(result));
- }
- return result;
+constexpr uint128 operator+(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) +
+ static_cast<unsigned __int128>(rhs);
+#else
+ return int128_internal::AddResult(
+ MakeUint128(Uint128High64(lhs) + Uint128High64(rhs),
+ Uint128Low64(lhs) + Uint128Low64(rhs)),
+ lhs);
+#endif
+}
+
+#if !defined(ABSL_HAVE_INTRINSIC_INT128)
+namespace int128_internal {
+constexpr uint128 SubstructResult(uint128 result, uint128 lhs, uint128 rhs) {
+ // check for carry
+ return (Uint128Low64(lhs) < Uint128Low64(rhs))
+ ? MakeUint128(Uint128High64(result) - 1, Uint128Low64(result))
+ : result;
+}
+} // namespace int128_internal
+#endif
+
+constexpr uint128 operator-(uint128 lhs, uint128 rhs) {
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+ return static_cast<unsigned __int128>(lhs) -
+ static_cast<unsigned __int128>(rhs);
+#else
+ return int128_internal::SubstructResult(
+ MakeUint128(Uint128High64(lhs) - Uint128High64(rhs),
+ Uint128Low64(lhs) - Uint128Low64(rhs)),
+ lhs, rhs);
+#endif
}
inline uint128 operator*(uint128 lhs, uint128 rhs) {
@@ -922,7 +985,7 @@ inline uint128 operator*(uint128 lhs, uint128 rhs) {
// can be used for uint128 storage.
return static_cast<unsigned __int128>(lhs) *
static_cast<unsigned __int128>(rhs);
-#elif defined(_MSC_VER) && defined(_M_X64)
+#elif defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
uint64_t carry;
uint64_t low = _umul128(Uint128Low64(lhs), Uint128Low64(rhs), &carry);
return MakeUint128(Uint128Low64(lhs) * Uint128High64(rhs) +
@@ -943,6 +1006,18 @@ inline uint128 operator*(uint128 lhs, uint128 rhs) {
#endif // ABSL_HAVE_INTRINSIC128
}
+#if defined(ABSL_HAVE_INTRINSIC_INT128)
+inline uint128 operator/(uint128 lhs, uint128 rhs) {
+ return static_cast<unsigned __int128>(lhs) /
+ static_cast<unsigned __int128>(rhs);
+}
+
+inline uint128 operator%(uint128 lhs, uint128 rhs) {
+ return static_cast<unsigned __int128>(lhs) %
+ static_cast<unsigned __int128>(rhs);
+}
+#endif
+
// Increment/decrement operators.
inline uint128 uint128::operator++(int) {
@@ -972,27 +1047,19 @@ constexpr int128 MakeInt128(int64_t high, uint64_t low) {
}
// Assignment from integer types.
-inline int128& int128::operator=(int v) {
- return *this = int128(v);
-}
+inline int128& int128::operator=(int v) { return *this = int128(v); }
-inline int128& int128::operator=(unsigned int v) {
- return *this = int128(v);
-}
+inline int128& int128::operator=(unsigned int v) { return *this = int128(v); }
inline int128& int128::operator=(long v) { // NOLINT(runtime/int)
return *this = int128(v);
}
// NOLINTNEXTLINE(runtime/int)
-inline int128& int128::operator=(unsigned long v) {
- return *this = int128(v);
-}
+inline int128& int128::operator=(unsigned long v) { return *this = int128(v); }
// NOLINTNEXTLINE(runtime/int)
-inline int128& int128::operator=(long long v) {
- return *this = int128(v);
-}
+inline int128& int128::operator=(long long v) { return *this = int128(v); }
// NOLINTNEXTLINE(runtime/int)
inline int128& int128::operator=(unsigned long long v) {
@@ -1000,17 +1067,17 @@ inline int128& int128::operator=(unsigned long long v) {
}
// Arithmetic operators.
-
-int128 operator+(int128 lhs, int128 rhs);
-int128 operator-(int128 lhs, int128 rhs);
+constexpr int128 operator-(int128 v);
+constexpr int128 operator+(int128 lhs, int128 rhs);
+constexpr int128 operator-(int128 lhs, int128 rhs);
int128 operator*(int128 lhs, int128 rhs);
int128 operator/(int128 lhs, int128 rhs);
int128 operator%(int128 lhs, int128 rhs);
-int128 operator|(int128 lhs, int128 rhs);
-int128 operator&(int128 lhs, int128 rhs);
-int128 operator^(int128 lhs, int128 rhs);
-int128 operator<<(int128 lhs, int amount);
-int128 operator>>(int128 lhs, int amount);
+constexpr int128 operator|(int128 lhs, int128 rhs);
+constexpr int128 operator&(int128 lhs, int128 rhs);
+constexpr int128 operator^(int128 lhs, int128 rhs);
+constexpr int128 operator<<(int128 lhs, int amount);
+constexpr int128 operator>>(int128 lhs, int amount);
inline int128& int128::operator+=(int128 other) {
*this = *this + other;
@@ -1062,6 +1129,9 @@ inline int128& int128::operator>>=(int amount) {
return *this;
}
+// Forward declaration for comparison operators.
+constexpr bool operator!=(int128 lhs, int128 rhs);
+
namespace int128_internal {
// Casts from unsigned to signed while preserving the underlying binary