What Are the 128-Bit Integer Types in absl::numeric?
Abseil’s absl::numeric module defines exactly two 128-bit integer types—absl::int128 (signed) and absl::uint128 (unsigned)—which are declared in absl/numeric/int128.h and implemented in absl/numeric/int128.cc.
The Abseil C++ library extends the standard type system with portable 128-bit integer arithmetic. These types fill a critical gap for applications requiring precision beyond 64 bits, offering consistent cross-platform behavior that the C++ standard library does not yet provide.
The 128-Bit Integer Types Defined
absl::int128 (Signed)
absl::int128 is a signed 128-bit integer type capable of representing values from $-2^{127}$ to $2^{127}-1$. It is defined as a class type in absl/numeric/int128.h and supports standard arithmetic operators (+, -, *, /, %) as well as bitwise operations. The type provides explicit constructors to convert from standard integer types and literals, ensuring type safety during initialization.
absl::uint128 (Unsigned)
absl::uint128 is the unsigned variant, representing values from $0$ to $2^{128}-1$. Like its signed counterpart, it is defined in absl/numeric/int128.h and implements the full suite of arithmetic and bitwise operators. Both types are designed to interact seamlessly with standard C++ integers through explicit conversion operators, preventing accidental narrowing.
Source File Locations and Implementation
According to the Abseil source code, the 128-bit integer implementation is organized across the following files:
absl/numeric/int128.h: Contains the class declarations forabsl::int128andabsl::uint128, operator overloads, type traits, and inline utility functions.absl/numeric/int128.cc: Houses the implementation of complex arithmetic operations, string conversion routines, and platform-specific optimizations.absl/numeric/int128_test.cc: Comprehensive unit tests validating arithmetic correctness, edge cases, and type conversions.absl/numeric/int128_stream_test.cc: Tests for stream insertion (operator<<) and extraction (operator>>) functionality, confirming compatibility with standard I/O streams.
Practical Usage Examples
The following example demonstrates initialization, arithmetic, and string conversion of 128-bit integers:
#include "absl/numeric/int128.h"
#include "absl/strings/str_cat.h"
#include <iostream>
int main() {
// Initialize using bit shifts to create large values
absl::int128 signed_val = absl::int128(1) << 100; // 2^100
absl::uint128 unsigned_val = absl::uint128(1) << 120; // 2^120
// Perform arithmetic operations
absl::int128 sum = signed_val + absl::int128(42);
absl::uint128 product = unsigned_val * absl::uint128(3);
// Convert to string for output (requires absl/strings/str_cat.h)
std::cout << "Signed: " << absl::StrCat(signed_val) << "\n";
std::cout << "Sum: " << absl::StrCat(sum) << "\n";
std::cout << "Product: " << absl::StrCat(product) << "\n";
return 0;
}
Clarification on Non-Standard Bit Widths
Abseil does not provide 28-bit integer types or other unusual bit-width integers (such as int28_t or uint28_t). The absl::numeric namespace focuses exclusively on the 128-bit extensions alongside standard <cstdint> types. If your application requires 28-bit specific logic, you must implement custom masking or use arbitrary-precision arithmetic libraries.
Summary
- Abseil provides two 128-bit integer types:
absl::int128(signed) andabsl::uint128(unsigned). - Both types are defined in
absl/numeric/int128.hand implemented inabsl/numeric/int128.cc. - The library includes comprehensive test coverage in
int128_test.ccandint128_stream_test.cc. - Use
absl::StrCat()(fromabsl/strings/str_cat.h) to convert 128-bit values to strings for logging or output. - No 28-bit or other non-standard fixed-width integers exist in the
absl::numericmodule.
Frequently Asked Questions
What header file defines absl::int128 and absl::uint128?
Both types are defined in absl/numeric/int128.h. You must include this header to use the 128-bit integer types, and for string conversion functions, you should also include absl/strings/str_cat.h.
Does absl::numeric provide 28-bit integer types?
No. The Abseil numeric library does not define 28-bit integer types (such as int28_t or uint28_t). The only extended-width integers provided are the 128-bit types. For other bit widths, use standard fixed-width types from <cstdint> or implement custom logic.
How do I output absl::int128 values to std::cout?
You can use absl::StrCat() from absl/strings/str_cat.h to convert the value to a string, or rely on the stream insertion operator (operator<<) which is defined in absl/numeric/int128.h and tested in absl/numeric/int128_stream_test.cc.
Are absl::int128 operations portable across 32-bit and 64-bit platforms?
Yes. Abseil implements software fallbacks for 128-bit arithmetic operations when hardware support is unavailable, ensuring identical behavior on 32-bit, 64-bit, and mixed architectures.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →