|
Click
|
Template for multiple-precision integer arithmetic. More...
#include <bigint.hh>
Public Types | |
| enum | { limb_bits = sizeof(limb_type) * 8 , half_limb_bits = sizeof(limb_type) * 4 } |
| enum | { limb_high_bit = limb_type(1) << (limb_bits - 1) , limb_half = limb_type(1) << half_limb_bits , limb_low_mask = limb_half - 1 , limb_zero = limb_type(0) } |
| typedef L | limb_type |
| Type of a limb, the unit of single arithmetic. | |
| typedef Lhalf | half_limb_type |
| Type of an arithmetic unit no smaller than one-half limb_type. | |
Static Public Member Functions | |
| static half_limb_type | low (limb_type x) |
| Return the less significant half of x. | |
| static half_limb_type | high (limb_type x) |
| Return the more significant half of x. | |
| 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]. | |
| 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]. | |
| 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]. | |
| static limb_type | inverse (limb_type x) |
| Return the inverse of limb x. | |
| static click_uintmax_t | set (limb_type *a, int n, click_uintmax_t b) |
| Set n-limb integer a to the value b. | |
| 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. | |
| 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-limb integer x. | |
| 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-limb integer x. | |
| 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. | |
| 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. | |
Template for multiple-precision integer arithmetic.
The Bigint template provides a couple simple, but useful, functions for multiple-precision integer arithmetic. It is a port of a very limited subset of the GNU Multiple Precision Arithmetic library's mpn functionality (architecture-generic functions that work on unsigned integers of known size).
A multiple-precision integer is an array of "limbs," or machine integers. The multiply_add() and divide() functions take arrays of limbs as arguments. In such arrays, the least significant limb is stored at array index 0.
For example, here's Bigint speak for "x = 0x123_4567_89AB_CDEF * 0x45":
Bigint is not optimized for speed.
Bigint's template parameter is the type of a limb. Most users will use bigint, which is equivalent to Bigint<uint32_t>.
| typedef L Bigint< L, Lhalf >::limb_type |
Type of a limb, the unit of single arithmetic.
| typedef Lhalf Bigint< L, Lhalf >::half_limb_type |
Type of an arithmetic unit no smaller than one-half limb_type.
|
inlinestatic |
Return the less significant half of x.
|
inlinestatic |
Return the more significant half of x.
|
inlinestatic |
Add two-limb integers [a1,a0] and [b1,b0], storing the result in [x1,x0].
| [out] | x1 | most significant limb of result |
| [out] | x0 | least significant limb of result |
| a1 | most significant limb of addend | |
| a0 | least significant limb of addend | |
| b1 | most significant limb of addend | |
| b0 | least significant limb of addend |
Handles carry in the low order limbs.
|
inlinestatic |
Multiply one-limb integers a and b, storing the result in [x1,x0].
| [out] | x1 | most significant limb of result |
| [out] | x0 | least significant limb of result |
| a | multiplicand | |
| b | multiplicand |
|
inlinestatic |
Multiply one-limb integer a by half-limb integer b, storing the result in [x1,x0].
| [out] | x1 | most significant limb of result |
| [out] | x0 | least significant limb of result |
| a | multiplicand | |
| b | multiplicand |
|
inlinestatic |
Return the inverse of limb x.
Returns the limb y that is the largest limb not larger than (2**(2*limb_bits))/x - (2**limb_bits). If this would yield overflow, y is the largest possible number (i.e., only ones).
|
inlinestatic |
Set n-limb integer a to the value b.
| [out] | a | points to n-limb result |
| n | number of limbs in a | |
| b | input value |
|
inlinestatic |
Multiply n-limb integer a by 1-limb integer b and add the result to n-limb integer x.
| [in,out] | x | points to n-limb addend and result |
| a | points to n-limb multiplicand | |
| n | number of limbs in x and a | |
| b | 1-limb multiplicand |
Like x += a * b. Both x and a must have n limbs. It is safe for x and a to point to exactly the same memory, but they must not otherwise overlap.
|
inlinestatic |
Multiply n-limb integer a by 1-limb integer b, add 1-limb integer carry, and store the result in n-limb integer x.
| [in,out] | x | points to n-limb result |
| a | points to n-limb multiplicand | |
| n | number of limbs in x and a | |
| b | 1-limb multiplicand | |
| carry | 1-limb initial carry |
Like x = (a * b) + carry. Both x and a must have n limbs. It is safe for x and a to point to exactly the same memory, but they must not otherwise overlap.
|
inlinestatic |
Multiply n-limb integer a by 1/2-limb integer b, add 1-limb integer carry, and store the result in n-limb integer x.
| [in,out] | x | points to n-limb result |
| a | points to n-limb multiplicand | |
| n | number of limbs in x and a | |
| b | 1/2-limb multiplicand | |
| carry | 1-limb initial carry |
Like x = (a * b) + carry. Both x and a must have n limbs. It is safe for x and a to point to exactly the same memory, but they must not otherwise overlap.
|
inlinestatic |
Divide n-limb integer a by 1-limb integer b and store the result in n-limb integer x.
| [out] | x | points to n-limb result |
| a | points to n-limb dividend | |
| n | number of limbs in x and a | |
| b | 1-limb divisor |
Like x = a / b. Both x and a must have n limbs. It is safe for x and a to point to exactly the same memory, but they must not otherwise overlap.
|
inlinestatic |
Return a string representation of n-limb integer x and set x to 0.
| [in,out] | x | n-limb input, set to zero on output |
| n | number of limbs in x | |
| base | base (between 2 and 36) | |
| uppercase | if true, use uppercase letters for digits >= 10 |