absl::uint128

An unsigned 128-bit integer type.

Synopsis

Declared in <absl/numeric/int128.h>

class uint128;

Description

The API is meant to mimic an intrinsic type as closely as is practical, including exhibiting undefined behavior in analogous cases (e.g. division by zero). This type is intended to be a drop-in replacement once C++ supports an intrinsic uint128_t type; when that occurs, existing well-behaved uses of uint128 will continue to work using that new type.

Note: code written with this type will continue to compile once uint128_t is introduced, provided the replacement helper functions Uint128(Low|High)64() and MakeUint128() are made.

A uint128 supports the following:

* Implicit construction from integral types * Explicit conversion to integral types

Additionally, if your compiler supports __int128, uint128 is interoperable with that type. (Abseil checks for this compatibility through the ABSL_HAVE_INTRINSIC_INT128 macro.)

However, a uint128 differs from intrinsic integral types in the following ways:

* Errors on implicit conversions that do not preserve value (such as loss of precision when converting to float values). * Requires explicit construction from and conversion to floating point types. * Conversion to integral types requires an explicit static_cast() to mimic use of the -Wnarrowing compiler flag. * The alignment requirement of uint128 may differ from that of an intrinsic 128-bit integer type depending on platform and build configuration.

Example:

float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly // converted to float.

absl::uint128 v; uint64_t i = v; // Error uint64_t i = static_cast<uint64_t>(v); // OK

Member Functions

NameDescription
uint128 [constructor]Constructors
operator= Assignment operators
operator%= Divides this value by other and stores the remainder in place.
operator&= Applies a bitwise AND with other and stores the result in place.
operator*= Multiplies this value by other and stores the result in place.
operator++ Increment operators
operator+= Adds other to this value and stores the result in place.
operator-- Decrement operators
operator-= Subtracts other from this value and stores the result in place.
operator/= Divides this value by other and stores the quotient in place.
operator<<= Shifts this value left by the given number of bits in place.
operator>>= Shifts this value right by the given number of bits in place.
operator^= Applies a bitwise XOR with other and stores the result in place.
operator|= Applies a bitwise OR with other and stores the result in place.
operator __int128 Converts this uint128 to a signed __int128.
operator bool Converts this uint128 to a bool.
operator char Converts this uint128 to a char, keeping the low bits.
operator char16_t Converts this uint128 to a char16_t, keeping the low bits.
operator char32_t Converts this uint128 to a char32_t, keeping the low bits.
operator double Converts this uint128 to a double.
operator float Converts this uint128 to a float.
operator int Converts this uint128 to an int, keeping the low bits.
operator long Converts this uint128 to a long, keeping the low bits.
operator long double Converts this uint128 to a long double.
operator long long Converts this uint128 to a long long, keeping the low bits.
operator short Converts this uint128 to a short, keeping the low bits.
operator signed char Converts this uint128 to a signed char, keeping the low bits.
operator unsigned __int128 Converts this uint128 to an unsigned __int128.
operator unsigned char Converts this uint128 to an unsigned char, keeping the low bits.
operator unsigned int Converts this uint128 to an unsigned int, keeping the low bits.
operator unsigned long Converts this uint128 to an unsigned long, keeping the low bits.
operator unsigned long long Converts this uint128 to an unsigned long long, keeping the low bits.
operator unsigned short Converts this uint128 to an unsigned short, keeping the low bits.
operator wchar_t Converts this uint128 to a wchar_t, keeping the low bits.

Friends

NameDescription
absl::AbslStringifyAppends the decimal representation of this value to a sink, supporting absl::StrCat() and related functions.
absl::AbslHashValueCombines this value into a hash state, supporting absl::Hash.
absl::Uint128MaxReturns the highest value for a 128-bit unsigned integer.
absl::MakeUint128Constructs a uint128 numeric value from two 64-bit unsigned integers.
absl::Uint128High64Returns the higher 64-bit value of a uint128 value.
absl::Uint128Low64Returns the lower 64-bit value of a uint128 value.

Non-Member Functions

NameDescription
operator!Returns the logical negation of val.
operator!=Determines whether lhs and rhs are unequal.
operator%Returns the remainder of lhs divided by rhs.
operator&Returns the bitwise AND of lhs and rhs.
operator*Returns the product of lhs and rhs.
operator+Returns the unary plus of val, which is val unchanged.
operator+Returns the sum of lhs and rhs.
operator-Returns the difference of lhs and rhs.
operator-Returns the arithmetic negation of val.
operator/Returns the quotient of lhs divided by rhs.
operator<Determines whether lhs is less than rhs.
operator<<Shifts lhs left by the given number of bits.
operator<=Determines whether lhs is less than or equal to rhs.
operator<=>Performs a three-way comparison of lhs and rhs.
operator==Determines whether lhs and rhs are equal.
operator>Determines whether lhs is greater than rhs.
operator>=Determines whether lhs is greater than or equal to rhs.
operator>>Shifts lhs right by the given number of bits.
operator^Returns the bitwise XOR of lhs and rhs.
operator|Returns the bitwise OR of lhs and rhs.
operator~Returns the bitwise complement of val.