9#include "../bitop/get_msb.hpp"
16 const uint32_t a_lo =
a & 0xffffULL;
17 const uint32_t a_hi =
a >> 16ULL;
18 const uint32_t b_lo =
b & 0xffffULL;
19 const uint32_t b_hi =
b >> 16ULL;
21 const uint32_t lo_lo = a_lo * b_lo;
22 const uint32_t hi_lo = a_hi * b_lo;
23 const uint32_t lo_hi = a_lo * b_hi;
24 const uint32_t hi_hi = a_hi * b_hi;
26 const uint32_t cross = (lo_lo >> 16) + (hi_lo & 0xffffULL) + lo_hi;
28 return { (cross << 16ULL) | (lo_lo & 0xffffULL), (hi_lo >> 16ULL) + (cross >> 16ULL) + hi_hi };
34 const uint32_t
sum =
a +
b;
35 const auto carry_temp =
static_cast<uint32_t
>(
sum <
a);
36 const uint32_t r =
sum + carry_in;
37 const uint32_t carry_out = carry_temp +
static_cast<unsigned int>(r < carry_in);
38 return { r, carry_out };
41constexpr uint32_t uint128_t::addc_discard_hi(
const uint32_t
a,
const uint32_t
b,
const uint32_t carry_in)
43 return a +
b + carry_in;
48 const uint32_t t_1 =
a - (borrow_in >> 31ULL);
49 const auto borrow_temp_1 =
static_cast<uint32_t
>(t_1 >
a);
50 const uint32_t t_2 = t_1 -
b;
51 const auto borrow_temp_2 =
static_cast<uint32_t
>(t_2 > t_1);
53 return { t_2, 0ULL - (borrow_temp_1 | borrow_temp_2) };
56constexpr uint32_t uint128_t::sbb_discard_hi(
const uint32_t
a,
const uint32_t
b,
const uint32_t borrow_in)
58 return a -
b - (borrow_in >> 31ULL);
65 const uint32_t carry_in)
69 const auto overflow_c =
static_cast<uint32_t
>(result.first <
a);
70 result.first += carry_in;
71 const auto overflow_carry =
static_cast<uint32_t
>(result.first < carry_in);
72 result.second += (overflow_c + overflow_carry);
76constexpr uint32_t uint128_t::mac_discard_hi(
const uint32_t
a,
79 const uint32_t carry_in)
81 return (
b * c +
a + carry_in);
86 if (*
this == 0 ||
b == 0) {
102 uint64_t bit_difference =
get_msb() -
b.get_msb();
108 if (divisor > remainder) {
115 while (remainder >=
b) {
119 if (remainder >= divisor) {
120 remainder -= divisor;
123 quotient |= accumulator;
129 return { quotient, remainder };
134 const auto [r0, t0] = mul_wide(
data[0], other.data[0]);
135 const auto [q0, t1] = mac(t0,
data[0], other.data[1], 0);
136 const auto [q1, t2] = mac(t1,
data[0], other.data[2], 0);
137 const auto [q2, z0] = mac(t2,
data[0], other.data[3], 0);
139 const auto [r1, t3] = mac(q0,
data[1], other.data[0], 0);
140 const auto [q3, t4] = mac(q1,
data[1], other.data[1], t3);
141 const auto [q4, t5] = mac(q2,
data[1], other.data[2], t4);
142 const auto [q5, z1] = mac(z0,
data[1], other.data[3], t5);
144 const auto [r2, t6] = mac(q3,
data[2], other.data[0], 0);
145 const auto [q6, t7] = mac(q4,
data[2], other.data[1], t6);
146 const auto [q7, t8] = mac(q5,
data[2], other.data[2], t7);
147 const auto [q8, z2] = mac(z1,
data[2], other.data[3], t8);
149 const auto [r3, t9] = mac(q6,
data[3], other.data[0], 0);
150 const auto [r4, t10] = mac(q7,
data[3], other.data[1], t9);
151 const auto [r5, t11] = mac(q8,
data[3], other.data[2], t10);
152 const auto [r6, r7] = mac(z2,
data[3], other.data[3], t11);
164constexpr uint128_t uint128_t::slice(
const uint64_t start,
const uint64_t end)
const
166 const uint64_t range = end - start;
168 return ((*
this) >> start) & mask;
175 const uint64_t maximum_set_bit = exponent.get_msb();
177 for (
int i =
static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
178 accumulator *= accumulator;
179 if (exponent.get_bit(
static_cast<uint64_t
>(i))) {
180 accumulator *= to_mul;
191constexpr bool uint128_t::get_bit(
const uint64_t bit_index)
const
194 if (bit_index > 127) {
197 const auto idx =
static_cast<size_t>(bit_index >> 5);
198 const size_t shift = bit_index & 31;
199 return static_cast<bool>((
data[idx] >> shift) & 1);
202constexpr uint64_t uint128_t::get_msb()
const
213 const auto [r0, t0] = addc(
data[0], other.data[0], 0);
214 const auto [r1, t1] = addc(
data[1], other.data[1], t0);
215 const auto [r2, t2] = addc(
data[2], other.data[2], t1);
216 const auto r3 = addc_discard_hi(
data[3], other.data[3], t2);
217 return { r0, r1, r2, r3 };
223 const auto [r0, t0] = sbb(
data[0], other.data[0], 0);
224 const auto [r1, t1] = sbb(
data[1], other.data[1], t0);
225 const auto [r2, t2] = sbb(
data[2], other.data[2], t1);
226 const auto r3 = sbb_discard_hi(
data[3], other.data[3], t2);
227 return { r0, r1, r2, r3 };
230constexpr uint128_t uint128_t::operator-()
const
237 const auto [r0, t0] = mac(0,
data[0], other.data[0], 0ULL);
238 const auto [q0, t1] = mac(0,
data[0], other.data[1], t0);
239 const auto [q1, t2] = mac(0,
data[0], other.data[2], t1);
240 const auto q2 = mac_discard_hi(0,
data[0], other.data[3], t2);
242 const auto [r1, t3] = mac(q0,
data[1], other.data[0], 0ULL);
243 const auto [q3, t4] = mac(q1,
data[1], other.data[1], t3);
244 const auto q4 = mac_discard_hi(q2,
data[1], other.data[2], t4);
246 const auto [r2, t5] = mac(q3,
data[2], other.data[0], 0ULL);
247 const auto q5 = mac_discard_hi(q4,
data[2], other.data[1], t5);
249 const auto r3 = mac_discard_hi(q5,
data[3], other.data[0], 0ULL);
251 return { r0, r1, r2, r3 };
256 return divmod(other).first;
261 return divmod(other).second;
266 return {
data[0] & other.data[0],
data[1] & other.data[1],
data[2] & other.data[2],
data[3] & other.data[3] };
271 return {
data[0] ^ other.data[0],
data[1] ^ other.data[1],
data[2] ^ other.data[2],
data[3] ^ other.data[3] };
276 return {
data[0] | other.data[0],
data[1] | other.data[1],
data[2] | other.data[2],
data[3] | other.data[3] };
279constexpr uint128_t uint128_t::operator~()
const
281 return { ~data[0], ~data[1], ~data[2], ~data[3] };
284constexpr bool uint128_t::operator==(
const uint128_t& other)
const
286 return data[0] == other.data[0] &&
data[1] == other.data[1] &&
data[2] == other.data[2] &&
data[3] == other.data[3];
289constexpr bool uint128_t::operator!=(
const uint128_t& other)
const
291 return !(*
this == other);
294constexpr bool uint128_t::operator!()
const
299constexpr bool uint128_t::operator>(
const uint128_t& other)
const
301 bool t0 =
data[3] > other.data[3];
302 bool t1 =
data[3] == other.data[3] &&
data[2] > other.data[2];
303 bool t2 =
data[3] == other.data[3] &&
data[2] == other.data[2] &&
data[1] > other.data[1];
305 data[3] == other.data[3] &&
data[2] == other.data[2] &&
data[1] == other.data[1] &&
data[0] > other.data[0];
306 return t0 || t1 || t2 || t3;
309constexpr bool uint128_t::operator>=(
const uint128_t& other)
const
311 return (*
this > other) || (*
this == other);
314constexpr bool uint128_t::operator<(
const uint128_t& other)
const
316 return other > *
this;
319constexpr bool uint128_t::operator<=(
const uint128_t& other)
const
321 return (*
this < other) || (*
this == other);
326 uint32_t total_shift = other.data[0];
328 if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
332 if (total_shift == 0) {
336 uint32_t num_shifted_limbs = total_shift >> 5ULL;
337 uint32_t limb_shift = total_shift & 31ULL;
339 std::array<uint32_t, 4> shifted_limbs = { 0, 0, 0, 0 };
341 if (limb_shift == 0) {
342 shifted_limbs[0] =
data[0];
343 shifted_limbs[1] =
data[1];
344 shifted_limbs[2] =
data[2];
345 shifted_limbs[3] =
data[3];
347 uint32_t remainder_shift = 32ULL - limb_shift;
349 shifted_limbs[3] =
data[3] >> limb_shift;
351 uint32_t remainder = (
data[3]) << remainder_shift;
353 shifted_limbs[2] = (
data[2] >> limb_shift) + remainder;
355 remainder = (
data[2]) << remainder_shift;
357 shifted_limbs[1] = (
data[1] >> limb_shift) + remainder;
359 remainder = (
data[1]) << remainder_shift;
361 shifted_limbs[0] = (
data[0] >> limb_shift) + remainder;
365 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
366 result.data[i] = shifted_limbs[
static_cast<size_t>(i + num_shifted_limbs)];
374 uint32_t total_shift = other.data[0];
376 if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
380 if (total_shift == 0) {
383 uint32_t num_shifted_limbs = total_shift >> 5ULL;
384 uint32_t limb_shift = total_shift & 31ULL;
386 std::array<uint32_t, 4> shifted_limbs{ 0, 0, 0, 0 };
388 if (limb_shift == 0) {
389 shifted_limbs[0] =
data[0];
390 shifted_limbs[1] =
data[1];
391 shifted_limbs[2] =
data[2];
392 shifted_limbs[3] =
data[3];
394 uint32_t remainder_shift = 32ULL - limb_shift;
396 shifted_limbs[0] =
data[0] << limb_shift;
398 uint32_t remainder =
data[0] >> remainder_shift;
400 shifted_limbs[1] = (
data[1] << limb_shift) + remainder;
402 remainder =
data[1] >> remainder_shift;
404 shifted_limbs[2] = (
data[2] << limb_shift) + remainder;
406 remainder =
data[2] >> remainder_shift;
408 shifted_limbs[3] = (
data[3] << limb_shift) + remainder;
412 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
413 result.data[
static_cast<size_t>(i + num_shifted_limbs)] = shifted_limbs[i];
#define ASSERT_IN_CONSTEXPR(expression,...)
const std::vector< FF > data
constexpr uint64_t get_msb64(const uint64_t in)
constexpr T get_msb(const T in)
Inner sum(Cont< Inner, Args... > const &in)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
unsigned __int128 uint128_t