2#define CLICK_BIGINT_HH 1
59template <
typename L,
typename Lhalf = L>
73 limb_high_bit =
limb_type(1) << (limb_bits - 1),
74 limb_half =
limb_type(1) << half_limb_bits,
75 limb_low_mask = limb_half - 1,
83 return x & limb_low_mask;
88 return x >> half_limb_bits;
104 x1 = a1 + b1 + (x0 < a0);
126 if (has_fast_int_multiply<limb_type>::value)
136 x1 = (r2 < r1 ? limb_half : limb_zero) +
high(r2);
137 x0 = (r2 << half_limb_bits) +
low(r0);
154 r1 = r1 * limb_half |
high(y0);
157 if (r1 >= x && r1 < m)
165 r0 = r0 * limb_half |
low(y0);
168 if (r0 >= x && r0 < m)
172 return q1 * limb_half | q0;
207 carry = (x0 < carry) + x1;
233 carry = (x0 < carry) + x1;
257 carry = (x0 < carry) + x1;
280 if (b & limb_high_bit) {
290 preinverted_divide(*x, r, r, *a, b, b_inverse);
312 r |= (a1 >> (limb_bits - norm));
316 preinverted_divide(*x, r, r, ((a1 << norm) | (a0 >> (limb_bits - norm))), b, b_inverse);
320 preinverted_divide(*x, r, r, a1 << norm, b, b_inverse);
337 int div = (base >= 16 ? 4 : (base >= 8 ? 3 : 1));
339 char *q =
const_cast<char *
>(s.
end());
340 assert(base >= 2 && base <= 36);
342 while (n > 0 && x[n - 1] == 0)
346 int r =
divide(x, x, n, base);
354 assert(q >= s.
begin());
372 limb_type a0_mask = (a0 & limb_high_bit ? ~limb_zero : limb_zero);
373 limb_type a0_adjusted = a0 + (b & a0_mask);
375 multiply(x1, x0, b_inverse, a1 - a0_mask);
376 add(x1, x0, x1, x0, a1, a0_adjusted);
379 add(x1, x0, x1, x0, a1, a0);
Bigint< uint64_t > bigint
Typical Bigint usage with uint64_t limb_type.
Definition bigint.hh:388
Template for multiple-precision integer arithmetic.
Definition bigint.hh:60
static limb_type multiply_half(limb_type *x, const limb_type *a, int n, half_limb_type b, limb_type carry=0)
Multiply n-limb integer a by 1/2-limb integer b, add 1-limb integer carry, and store the result in n-...
Definition bigint.hh:251
Lhalf half_limb_type
Type of an arithmetic unit no smaller than one-half limb_type.
Definition bigint.hh:66
static void add(limb_type &x1, limb_type &x0, limb_type a1, limb_type a0, limb_type b1, limb_type b0)
Add two-limb integers [a1,a0] and [b1,b0], storing the result in [x1,x0].
Definition bigint.hh:101
static void multiply(limb_type &x1, limb_type &x0, limb_type a, limb_type b)
Multiply one-limb integers a and b, storing the result in [x1,x0].
Definition bigint.hh:113
static half_limb_type high(limb_type x)
Return the more significant half of x.
Definition bigint.hh:87
static void multiply_half(limb_type &x1, limb_type &x0, limb_type a, half_limb_type b)
Multiply one-limb integer a by half-limb integer b, storing the result in [x1,x0].
Definition bigint.hh:124
static limb_type inverse(limb_type x)
Return the inverse of limb x.
Definition bigint.hh:146
static limb_type multiply_add(limb_type *x, const limb_type *a, int n, limb_type b)
Multiply n-limb integer a by 1-limb integer b and add the result to n-limb integer x.
Definition bigint.hh:200
static limb_type divide(limb_type *x, const limb_type *a, int n, limb_type b)
Divide n-limb integer a by 1-limb integer b and store the result in n-limb integer x.
Definition bigint.hh:274
static half_limb_type low(limb_type x)
Return the less significant half of x.
Definition bigint.hh:82
L limb_type
Type of a limb, the unit of single arithmetic.
Definition bigint.hh:63
static limb_type multiply(limb_type *x, const limb_type *a, int n, limb_type b, limb_type carry=0)
Multiply n-limb integer a by 1-limb integer b, add 1-limb integer carry, and store the result in n-li...
Definition bigint.hh:227
static String unparse_clear(limb_type *x, int n, int base=10, bool uppercase=false)
Return a string representation of n-limb integer x and set x to 0.
Definition bigint.hh:334
static click_uintmax_t set(limb_type *a, int n, click_uintmax_t b)
Set n-limb integer a to the value b.
Definition bigint.hh:180
A string of characters.
Definition string.hh:22
String substring(const char *begin, const char *end) const
Return a substring of the current string starting at first and ending before last.
Definition string.hh:550
const_iterator end() const
Return an iterator for the end of the string.
Definition string.hh:584
static String make_uninitialized(int len)
Return a String containing len unknown characters.
Definition string.hh:444
const_iterator begin() const
Return an iterator for the first character in the string.
Definition string.hh:576
Functions for manipulating integers.
void int_multiply(T a, T b, T &xlow, T &xhigh)
Multiply a * b, placing the low-order bits of the result in xlow and the high-order bits in xhigh.
Definition integers.hh:395
int ffs_msb(unsigned x)
Return the index of the most significant bit set in x.
Definition integers.hh:210