3#include "../bitop/get_msb.hpp"
4#include "./uint128.hpp"
5#include "barretenberg/common/assert.hpp"
8constexpr std::pair<uint32_t, uint32_t> uint128_t::mul_wide(
const uint32_t a,
const uint32_t b)
10 const uint32_t a_lo = a & 0xffffULL;
11 const uint32_t a_hi = a >> 16ULL;
12 const uint32_t b_lo = b & 0xffffULL;
13 const uint32_t b_hi = b >> 16ULL;
15 const uint32_t lo_lo = a_lo * b_lo;
16 const uint32_t hi_lo = a_hi * b_lo;
17 const uint32_t lo_hi = a_lo * b_hi;
18 const uint32_t hi_hi = a_hi * b_hi;
20 const uint32_t cross = (lo_lo >> 16) + (hi_lo & 0xffffULL) + lo_hi;
22 return { (cross << 16ULL) | (lo_lo & 0xffffULL), (hi_lo >> 16ULL) + (cross >> 16ULL) + hi_hi };
26constexpr std::pair<uint32_t, uint32_t> uint128_t::addc(
const uint32_t a,
const uint32_t b,
const uint32_t carry_in)
28 const uint32_t sum = a + b;
29 const auto carry_temp =
static_cast<uint32_t
>(sum < a);
30 const uint32_t r = sum + carry_in;
31 const uint32_t carry_out = carry_temp +
static_cast<unsigned int>(r < carry_in);
32 return { r, carry_out };
35constexpr uint32_t uint128_t::addc_discard_hi(
const uint32_t a,
const uint32_t b,
const uint32_t carry_in)
37 return a + b + carry_in;
40constexpr std::pair<uint32_t, uint32_t> uint128_t::sbb(
const uint32_t a,
const uint32_t b,
const uint32_t borrow_in)
42 const uint32_t t_1 = a - (borrow_in >> 31ULL);
43 const auto borrow_temp_1 =
static_cast<uint32_t
>(t_1 > a);
44 const uint32_t t_2 = t_1 - b;
45 const auto borrow_temp_2 =
static_cast<uint32_t
>(t_2 > t_1);
47 return { t_2, 0ULL - (borrow_temp_1 | borrow_temp_2) };
50constexpr uint32_t uint128_t::sbb_discard_hi(
const uint32_t a,
const uint32_t b,
const uint32_t borrow_in)
52 return a - b - (borrow_in >> 31ULL);
56constexpr std::pair<uint32_t, uint32_t> uint128_t::mac(
const uint32_t a,
59 const uint32_t carry_in)
61 std::pair<uint32_t, uint32_t> result = mul_wide(b, c);
63 const auto overflow_c =
static_cast<uint32_t
>(result.first < a);
64 result.first += carry_in;
65 const auto overflow_carry =
static_cast<uint32_t
>(result.first < carry_in);
66 result.second += (overflow_c + overflow_carry);
70constexpr uint32_t uint128_t::mac_discard_hi(
const uint32_t a,
73 const uint32_t carry_in)
75 return (b * c + a + carry_in);
78constexpr std::pair<uint128_t, uint128_t> uint128_t::divmod(
const uint128_t& b)
const
80 if (*
this == 0 || b == 0) {
93 uint128_t quotient = 0;
94 uint128_t remainder = *
this;
96 uint64_t bit_difference = get_msb() - b.get_msb();
98 uint128_t divisor = b << bit_difference;
99 uint128_t accumulator = uint128_t(1) << bit_difference;
102 if (divisor > remainder) {
109 while (remainder >= b) {
113 if (remainder >= divisor) {
114 remainder -= divisor;
117 quotient |= accumulator;
123 return { quotient, remainder };
126constexpr std::pair<uint128_t, uint128_t> uint128_t::mul_extended(
const uint128_t& other)
const
128 const auto [r0, t0] = mul_wide(data[0], other.data[0]);
129 const auto [q0, t1] = mac(t0, data[0], other.data[1], 0);
130 const auto [q1, t2] = mac(t1, data[0], other.data[2], 0);
131 const auto [q2, z0] = mac(t2, data[0], other.data[3], 0);
133 const auto [r1, t3] = mac(q0, data[1], other.data[0], 0);
134 const auto [q3, t4] = mac(q1, data[1], other.data[1], t3);
135 const auto [q4, t5] = mac(q2, data[1], other.data[2], t4);
136 const auto [q5, z1] = mac(z0, data[1], other.data[3], t5);
138 const auto [r2, t6] = mac(q3, data[2], other.data[0], 0);
139 const auto [q6, t7] = mac(q4, data[2], other.data[1], t6);
140 const auto [q7, t8] = mac(q5, data[2], other.data[2], t7);
141 const auto [q8, z2] = mac(z1, data[2], other.data[3], t8);
143 const auto [r3, t9] = mac(q6, data[3], other.data[0], 0);
144 const auto [r4, t10] = mac(q7, data[3], other.data[1], t9);
145 const auto [r5, t11] = mac(q8, data[3], other.data[2], t10);
146 const auto [r6, r7] = mac(z2, data[3], other.data[3], t11);
148 uint128_t lo(r0, r1, r2, r3);
149 uint128_t hi(r4, r5, r6, r7);
158constexpr uint128_t uint128_t::slice(
const uint64_t start,
const uint64_t end)
const
160 const uint64_t range = end - start;
161 const uint128_t mask = (range == 128) ? -uint128_t(1) : (uint128_t(1) << range) - 1;
162 return ((*
this) >> start) & mask;
165constexpr uint128_t uint128_t::pow(
const uint128_t& exponent)
const
167 uint128_t accumulator{ data[0], data[1], data[2], data[3] };
168 uint128_t to_mul{ data[0], data[1], data[2], data[3] };
169 const uint64_t maximum_set_bit = exponent.get_msb();
171 for (
int i =
static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
172 accumulator *= accumulator;
173 if (exponent.get_bit(
static_cast<uint64_t
>(i))) {
174 accumulator *= to_mul;
177 if (exponent == uint128_t(0)) {
178 accumulator = uint128_t(1);
179 }
else if (*
this == uint128_t(0)) {
180 accumulator = uint128_t(0);
185constexpr bool uint128_t::get_bit(
const uint64_t bit_index)
const
187 ASSERT(bit_index < 128);
188 if (bit_index > 127) {
191 const auto idx =
static_cast<size_t>(bit_index >> 5);
192 const size_t shift = bit_index & 31;
193 return static_cast<bool>((data[idx] >> shift) & 1);
196constexpr uint64_t uint128_t::get_msb()
const
198 uint64_t idx = numeric::get_msb64(data[3]);
199 idx = (idx == 0 && data[3] == 0) ? numeric::get_msb64(data[2]) : idx + 32;
200 idx = (idx == 0 && data[2] == 0) ? numeric::get_msb64(data[1]) : idx + 32;
201 idx = (idx == 0 && data[1] == 0) ? numeric::get_msb64(data[0]) : idx + 32;
205constexpr uint128_t uint128_t::operator+(
const uint128_t& other)
const
207 const auto [r0, t0] = addc(data[0], other.data[0], 0);
208 const auto [r1, t1] = addc(data[1], other.data[1], t0);
209 const auto [r2, t2] = addc(data[2], other.data[2], t1);
210 const auto r3 = addc_discard_hi(data[3], other.data[3], t2);
211 return { r0, r1, r2, r3 };
214constexpr uint128_t uint128_t::operator-(
const uint128_t& other)
const
217 const auto [r0, t0] = sbb(data[0], other.data[0], 0);
218 const auto [r1, t1] = sbb(data[1], other.data[1], t0);
219 const auto [r2, t2] = sbb(data[2], other.data[2], t1);
220 const auto r3 = sbb_discard_hi(data[3], other.data[3], t2);
221 return { r0, r1, r2, r3 };
224constexpr uint128_t uint128_t::operator-()
const
226 return uint128_t(0) - *
this;
229constexpr uint128_t uint128_t::operator*(
const uint128_t& other)
const
231 const auto [r0, t0] = mac(0, data[0], other.data[0], 0ULL);
232 const auto [q0, t1] = mac(0, data[0], other.data[1], t0);
233 const auto [q1, t2] = mac(0, data[0], other.data[2], t1);
234 const auto q2 = mac_discard_hi(0, data[0], other.data[3], t2);
236 const auto [r1, t3] = mac(q0, data[1], other.data[0], 0ULL);
237 const auto [q3, t4] = mac(q1, data[1], other.data[1], t3);
238 const auto q4 = mac_discard_hi(q2, data[1], other.data[2], t4);
240 const auto [r2, t5] = mac(q3, data[2], other.data[0], 0ULL);
241 const auto q5 = mac_discard_hi(q4, data[2], other.data[1], t5);
243 const auto r3 = mac_discard_hi(q5, data[3], other.data[0], 0ULL);
245 return { r0, r1, r2, r3 };
248constexpr uint128_t uint128_t::operator/(
const uint128_t& other)
const
250 return divmod(other).first;
253constexpr uint128_t uint128_t::operator%(
const uint128_t& other)
const
255 return divmod(other).second;
258constexpr uint128_t uint128_t::operator&(
const uint128_t& other)
const
260 return { data[0] & other.data[0], data[1] & other.data[1], data[2] & other.data[2], data[3] & other.data[3] };
263constexpr uint128_t uint128_t::operator^(
const uint128_t& other)
const
265 return { data[0] ^ other.data[0], data[1] ^ other.data[1], data[2] ^ other.data[2], data[3] ^ other.data[3] };
268constexpr uint128_t uint128_t::operator|(
const uint128_t& other)
const
270 return { data[0] | other.data[0], data[1] | other.data[1], data[2] | other.data[2], data[3] | other.data[3] };
273constexpr uint128_t uint128_t::operator~()
const
275 return { ~data[0], ~data[1], ~data[2], ~data[3] };
278constexpr bool uint128_t::operator==(
const uint128_t& other)
const
280 return data[0] == other.data[0] && data[1] == other.data[1] && data[2] == other.data[2] && data[3] == other.data[3];
283constexpr bool uint128_t::operator!=(
const uint128_t& other)
const
285 return !(*
this == other);
288constexpr bool uint128_t::operator!()
const
290 return *
this == uint128_t(0ULL);
293constexpr bool uint128_t::operator>(
const uint128_t& other)
const
295 bool t0 = data[3] > other.data[3];
296 bool t1 = data[3] == other.data[3] && data[2] > other.data[2];
297 bool t2 = data[3] == other.data[3] && data[2] == other.data[2] && data[1] > other.data[1];
299 data[3] == other.data[3] && data[2] == other.data[2] && data[1] == other.data[1] && data[0] > other.data[0];
300 return t0 || t1 || t2 || t3;
303constexpr bool uint128_t::operator>=(
const uint128_t& other)
const
305 return (*
this > other) || (*
this == other);
308constexpr bool uint128_t::operator<(
const uint128_t& other)
const
310 return other > *
this;
313constexpr bool uint128_t::operator<=(
const uint128_t& other)
const
315 return (*
this < other) || (*
this == other);
318constexpr uint128_t uint128_t::operator>>(
const uint128_t& other)
const
320 uint32_t total_shift = other.data[0];
322 if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
326 if (total_shift == 0) {
330 uint32_t num_shifted_limbs = total_shift >> 5ULL;
331 uint32_t limb_shift = total_shift & 31ULL;
333 std::array<uint32_t, 4> shifted_limbs = { 0, 0, 0, 0 };
335 if (limb_shift == 0) {
336 shifted_limbs[0] = data[0];
337 shifted_limbs[1] = data[1];
338 shifted_limbs[2] = data[2];
339 shifted_limbs[3] = data[3];
341 uint32_t remainder_shift = 32ULL - limb_shift;
343 shifted_limbs[3] = data[3] >> limb_shift;
345 uint32_t remainder = (data[3]) << remainder_shift;
347 shifted_limbs[2] = (data[2] >> limb_shift) + remainder;
349 remainder = (data[2]) << remainder_shift;
351 shifted_limbs[1] = (data[1] >> limb_shift) + remainder;
353 remainder = (data[1]) << remainder_shift;
355 shifted_limbs[0] = (data[0] >> limb_shift) + remainder;
359 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
360 result.data[i] = shifted_limbs[
static_cast<size_t>(i + num_shifted_limbs)];
366constexpr uint128_t uint128_t::operator<<(
const uint128_t& other)
const
368 uint32_t total_shift = other.data[0];
370 if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
374 if (total_shift == 0) {
377 uint32_t num_shifted_limbs = total_shift >> 5ULL;
378 uint32_t limb_shift = total_shift & 31ULL;
380 std::array<uint32_t, 4> shifted_limbs{ 0, 0, 0, 0 };
382 if (limb_shift == 0) {
383 shifted_limbs[0] = data[0];
384 shifted_limbs[1] = data[1];
385 shifted_limbs[2] = data[2];
386 shifted_limbs[3] = data[3];
388 uint32_t remainder_shift = 32ULL - limb_shift;
390 shifted_limbs[0] = data[0] << limb_shift;
392 uint32_t remainder = data[0] >> remainder_shift;
394 shifted_limbs[1] = (data[1] << limb_shift) + remainder;
396 remainder = data[1] >> remainder_shift;
398 shifted_limbs[2] = (data[2] << limb_shift) + remainder;
400 remainder = data[2] >> remainder_shift;
402 shifted_limbs[3] = (data[3] << limb_shift) + remainder;
406 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
407 result.data[
static_cast<size_t>(i + num_shifted_limbs)] = shifted_limbs[i];
Definition: field2_declarations.hpp:6