VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
uint128.h 文件参考

Portable 128-bit unsigned integer with full arithmetic, bitwise, and comparison operators. 更多...

#include <cstdint>
#include <iostream>
#include <type_traits>
#include <utility>
#include "./macros.h"
uint128.h 的引用(Include)关系图:

浏览该文件的源代码.

 128-bit unsigned integer stored as two 64-bit halves with full operator support. 更多...
 std::hash specialisation for vlink::Uint128. 更多...

命名空间

namespace  std
 STL namespace

类型定义

using vlink::uint128_t = Uint128

详细描述

Portable 128-bit unsigned integer with full arithmetic, bitwise, and comparison operators.

Uint128 represents a 128-bit unsigned value as two uint64_t halves (high_ and low_). On platforms that provide __uint128_t (GCC/Clang with 64-bit targets), multiplication is delegated to the compiler's native type for maximum performance. On other platforms a portable fallback algorithm (mul_u128_fallback) is used.

Supported operations:

Category Operators Notes
Arithmetic + - * / % += -= *= /= %= / and % throw on divisor zero

| Bitwise | | & ^ ~ << >> |= &= ^= <<= >>= | Shifts clamped to [0, 127] | | Comparison | == != < > <= >= | Lexicographic (high then low) | | Increment | ++ – (prefix and postfix) | 128-bit carry propagated | | Stream | operator<<(ostream) | Hexadecimal output |

Implicit conversion from integral types
A non-explicit single-argument constructor accepts any integral type T:
  • If T is __uint128_t (when available), high and low halves are extracted.
  • Otherwise the value is zero-extended into the low 64 bits.
Conversion to __uint128_t
On platforms that support it, an explicit operator __uint128_t() is provided for interoperability with compiler-native 128-bit arithmetic.
std::hash specialisation
A std::hash<vlink::Uint128> specialisation is defined at the bottom of this file, enabling Uint128 to be used as a key in std::unordered_map / std::unordered_set.
Type alias
using uint128_t = Uint128;
Example
vlink::Uint128 a(0, UINT64_MAX); // 0xFFFFFFFFFFFFFFFF
vlink::Uint128 b(1, 0); // 0x10000000000000000
vlink::Uint128 c = a + vlink::Uint128(0, 1); // carry propagates to high word
assert(c == b);
// Hash map usage:
std::unordered_map<vlink::uint128_t, std::string> map;
map[vlink::Uint128(0xDEAD, 0xBEEF)] = "key";