7#include "barretenberg/common/serialize.hpp"
12class alignas(32) uint128_t {
16 constexpr uint128_t(
const uint64_t a = 0)
17 : data{ static_cast<uint32_t>(a), static_cast<uint32_t>(a >> 32), 0, 0 }
20 constexpr uint128_t(
const uint32_t a,
const uint32_t b,
const uint32_t c,
const uint32_t d)
24 constexpr uint128_t(
const uint128_t& other)
25 : data{ other.data[0], other.data[1], other.data[2], other.data[3] }
27 constexpr uint128_t(uint128_t&& other) =
default;
29 static constexpr uint128_t from_uint64(
const uint64_t a)
31 return {
static_cast<uint32_t
>(a),
static_cast<uint32_t
>(a >> 32), 0, 0 };
34 constexpr explicit operator uint64_t() {
return (
static_cast<uint64_t
>(data[1]) << 32) + data[0]; }
36 constexpr uint128_t& operator=(
const uint128_t& other) =
default;
37 constexpr uint128_t& operator=(uint128_t&& other) =
default;
38 constexpr ~uint128_t() =
default;
39 explicit constexpr operator bool()
const {
return static_cast<bool>(data[0]); };
41 template <std::
integral T>
explicit constexpr operator T()
const {
return static_cast<T
>(data[0]); };
43 [[nodiscard]]
constexpr bool get_bit(uint64_t bit_index)
const;
44 [[nodiscard]]
constexpr uint64_t get_msb()
const;
46 [[nodiscard]]
constexpr uint128_t slice(uint64_t start, uint64_t end)
const;
47 [[nodiscard]]
constexpr uint128_t pow(
const uint128_t& exponent)
const;
49 constexpr uint128_t operator+(
const uint128_t& other)
const;
50 constexpr uint128_t operator-(
const uint128_t& other)
const;
51 constexpr uint128_t operator-()
const;
53 constexpr uint128_t operator*(
const uint128_t& other)
const;
54 constexpr uint128_t operator/(
const uint128_t& other)
const;
55 constexpr uint128_t operator%(
const uint128_t& other)
const;
57 constexpr uint128_t operator>>(
const uint128_t& other)
const;
58 constexpr uint128_t operator<<(
const uint128_t& other)
const;
60 constexpr uint128_t operator&(
const uint128_t& other)
const;
61 constexpr uint128_t operator^(
const uint128_t& other)
const;
62 constexpr uint128_t operator|(
const uint128_t& other)
const;
63 constexpr uint128_t operator~()
const;
65 constexpr bool operator==(
const uint128_t& other)
const;
66 constexpr bool operator!=(
const uint128_t& other)
const;
67 constexpr bool operator!()
const;
69 constexpr bool operator>(
const uint128_t& other)
const;
70 constexpr bool operator<(
const uint128_t& other)
const;
71 constexpr bool operator>=(
const uint128_t& other)
const;
72 constexpr bool operator<=(
const uint128_t& other)
const;
74 static constexpr size_t length() {
return 128; }
76 constexpr uint128_t& operator+=(
const uint128_t& other)
78 *
this = *
this + other;
81 constexpr uint128_t& operator-=(
const uint128_t& other)
83 *
this = *
this - other;
86 constexpr uint128_t& operator*=(
const uint128_t& other)
88 *
this = *
this * other;
91 constexpr uint128_t& operator/=(
const uint128_t& other)
93 *
this = *
this / other;
96 constexpr uint128_t& operator%=(
const uint128_t& other)
98 *
this = *
this % other;
102 constexpr uint128_t& operator++()
104 *
this += uint128_t(1);
107 constexpr uint128_t& operator--()
109 *
this -= uint128_t(1);
113 constexpr uint128_t& operator&=(
const uint128_t& other)
115 *
this = *
this & other;
118 constexpr uint128_t& operator^=(
const uint128_t& other)
120 *
this = *
this ^ other;
123 constexpr uint128_t& operator|=(
const uint128_t& other)
125 *
this = *
this | other;
129 constexpr uint128_t& operator>>=(
const uint128_t& other)
131 *
this = *
this >> other;
134 constexpr uint128_t& operator<<=(
const uint128_t& other)
136 *
this = *
this << other;
140 [[nodiscard]]
constexpr std::pair<uint128_t, uint128_t> mul_extended(
const uint128_t& other)
const;
142 [[nodiscard]]
constexpr std::pair<uint128_t, uint128_t> divmod(
const uint128_t& b)
const;
145 [[nodiscard]]
static constexpr std::pair<uint32_t, uint32_t> mul_wide(uint32_t a, uint32_t b);
146 [[nodiscard]]
static constexpr std::pair<uint32_t, uint32_t> addc(uint32_t a, uint32_t b, uint32_t carry_in);
147 [[nodiscard]]
static constexpr uint32_t addc_discard_hi(uint32_t a, uint32_t b, uint32_t carry_in);
148 [[nodiscard]]
static constexpr uint32_t sbb_discard_hi(uint32_t a, uint32_t b, uint32_t borrow_in);
150 [[nodiscard]]
static constexpr std::pair<uint32_t, uint32_t> sbb(uint32_t a, uint32_t b, uint32_t borrow_in);
151 [[nodiscard]]
static constexpr uint32_t mac_discard_hi(uint32_t a, uint32_t b, uint32_t c, uint32_t carry_in);
152 [[nodiscard]]
static constexpr std::pair<uint32_t, uint32_t> mac(uint32_t a,
158inline std::ostream& operator<<(std::ostream& os, uint128_t
const& a)
160 std::ios_base::fmtflags f(os.flags());
161 os << std::hex <<
"0x" << std::setfill(
'0') << std::setw(8) << a.data[3] << std::setw(8) << a.data[2]
162 << std::setw(8) << a.data[1] << std::setw(8) << a.data[0];
167template <
typename B>
inline void read(B& it, uint128_t& value)
169 using serialize::read;
178 value = uint128_t(a, b, c, d);
181template <
typename B>
inline void write(B& it, uint128_t
const& value)
183 using serialize::write;
184 write(it, value.data[3]);
185 write(it, value.data[2]);
186 write(it, value.data[1]);
187 write(it, value.data[0]);
192#include "./uint128_impl.hpp"
196using numeric::uint128_t;
198__extension__
using uint128_t =
unsigned __int128;
203inline std::ostream& operator<<(std::ostream& os, uint128_t
const& a)
205 std::ios_base::fmtflags f(os.flags());
206 os << std::hex <<
"0x" << std::setfill(
'0') << std::setw(16) <<
static_cast<uint64_t
>(a >> 64) << std::setw(16)
207 <<
static_cast<uint64_t
>(a);
Definition: field2_declarations.hpp:6