Go to the first, previous, next, last section, table of contents.
This chapter describes low-level GMP functions, used to implement the high-level
GMP functions, but also intended for time-critical user code.
These functions start with the prefix mpn_
.
The mpn
functions are designed to be as fast as possible, not
to provide a coherent calling interface. The different functions have somewhat
similar interfaces, but there are variations that make them hard to use. These
functions do as little as possible apart from the real multiple precision
computation, so that no time is spent on things that not all callers need.
A source operand is specified by a pointer to the least significant limb and a
limb count. A destination operand is specified by just a pointer. It is the
responsibility of the caller to ensure that the destination has enough space
for storing the result.
With this way of specifying operands, it is possible to perform computations
on subranges of an argument, and store the result into a subrange of a
destination.
A common requirement for all functions is that each source area needs at
least one limb. No size argument may be zero. Unless otherwise stated,
in-place operations are allowed where source and destination are the
same, but not where they only partly overlap.
The mpn
functions are the base for the implementation of the
mpz_
, mpf_
, and mpq_
functions.
This example adds the number beginning at s1p and the number
beginning at s2p and writes the sum at destp. All areas
have size limbs.
cy = mpn_add_n (destp, s1p, s2p, size)
In the notation used here, a source operand is identified by the pointer to
the least significant limb, and the limb count in braces. For example,
{s1p, s1size}.
- Function: mp_limb_t mpn_add_n (mp_limb_t *rp, const mp_limb_t *s1p, const mp_limb_t *s2p, mp_size_t size)
-
Add {s1p, size} and {s2p, size}, and
write the size least significant limbs of the result to rp.
Return carry, either 0 or 1.
This is the lowest-level function for addition. It is the preferred function
for addition, since it is written in assembly for most targets. For addition
of a variable to itself (i.e., s1p equals s2p, use
mpn_lshift
with a count of 1 for optimal speed.
- Function: mp_limb_t mpn_add_1 (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t size, mp_limb_t s2limb)
-
Add {s1p, size} and s2limb, and write the
size least significant limbs of the result to rp. Return
carry, either 0 or 1.
- Function: mp_limb_t mpn_add (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t s1size, const mp_limb_t *s2p, mp_size_t s2size)
-
Add {s1p, s1size} and {s2p,
s2size}, and write the s1size least significant limbs of
the result to rp. Return carry, either 0 or 1.
This function requires that s1size is greater than or equal to
s2size.
- Function: mp_limb_t mpn_sub_n (mp_limb_t *rp, const mp_limb_t *s1p, const mp_limb_t *s2p, mp_size_t size)
-
Subtract {s2p, s2size} from {s1p,
size}, and write the size least significant limbs of the result
to rp. Return borrow, either 0 or 1.
This is the lowest-level function for subtraction. It is the preferred
function for subtraction, since it is written in assembly for most targets.
- Function: mp_limb_t mpn_sub_1 (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t size, mp_limb_t s2limb)
-
Subtract s2limb from {s1p, size}, and write the
size least significant limbs of the result to rp. Return
borrow, either 0 or 1.
- Function: mp_limb_t mpn_sub (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t s1size, const mp_limb_t *s2p, mp_size_t s2size)
-
Subtract {s2p, s2size} from {s1p,
s1size}, and write the s1size least significant limbs of
the result to rp. Return borrow, either 0 or 1.
This function requires that s1size is greater than or equal to
s2size.
- Function: void mpn_mul_n (mp_limb_t *rp, const mp_limb_t *s1p, const mp_limb_t *s2p, mp_size_t size)
-
Multiply {s1p, size} and {s2p, size},
and write the entire result to rp.
The destination has to have space for 2*size limbs, even if the
significant result might be one limb smaller.
- Function: mp_limb_t mpn_mul_1 (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t size, mp_limb_t s2limb)
-
Multiply {s1p, size} and s2limb, and write the
size least significant limbs of the product to rp. Return
the most significant limb of the product.
This is a low-level function that is a building block for general
multiplication as well as other operations in GMP. It is written in assembly
for most targets.
Don't call this function if s2limb is a power of 2; use
mpn_lshift
with a count equal to the logarithm of s2limb
instead, for optimal speed.
- Function: mp_limb_t mpn_addmul_1 (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t size, mp_limb_t s2limb)
-
Multiply {s1p, size} and s2limb, and add the
size least significant limbs of the product to {rp,
size} and write the result to rp. Return
the most significant limb of the product, plus carry-out from the addition.
This is a low-level function that is a building block for general
multiplication as well as other operations in GMP. It is written in assembly
for most targets.
- Function: mp_limb_t mpn_submul_1 (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t size, mp_limb_t s2limb)
-
Multiply {s1p, size} and s2limb, and subtract the
size least significant limbs of the product from {rp,
size} and write the result to rp. Return the most
significant limb of the product, minus borrow-out from the subtraction.
This is a low-level function that is a building block for general
multiplication and division as well as other operations in GMP. It is written
in assembly for most targets.
- Function: mp_limb_t mpn_mul (mp_limb_t *rp, const mp_limb_t *s1p, mp_size_t s1size, const mp_limb_t *s2p, mp_size_t s2size)
-
Multiply {s1p, s1size} and {s2p,
s2size}, and write the result to rp. Return the most
significant limb of the result.
The destination has to have space for s1size + s2size
limbs, even if the result might be one limb smaller.
This function requires that s1size is greater than or equal to
s2size. The destination must be distinct from either input operands.
- Function: void mpn_tdiv_qr (mp_limb_t *qp, mp_limb_t *rp, mp_size_t qxn, const mp_limb_t *np, mp_size_t nn, const mp_limb_t *dp, mp_size_t dn)
-
Divide {np, nn} by {dp, dn}. Write the quotient
at qp and the remainder at rp.
The quotient written at qp will be nn - dn + 1 limbs.
The remainder written at rp will be dn limbs.
It is required that nn is greater than or equal to dn. The
qxn operand must be zero.
The quotient is rounded towards 0.
No overlap between arguments is permitted.
- Function: mp_limb_t mpn_divrem (mp_limb_t *r1p, mp_size_t xsize, mp_limb_t *rs2p, mp_size_t rs2size, const mp_limb_t *s3p, mp_size_t s3size)
-
[This function is obsolete. Please call
mpn_tdiv_qr
instead for
best performance.]
Divide {rs2p, rs2size} by {s3p, s3size}, and write
the quotient at r1p, with the exception of the most significant limb,
which is returned. The remainder replaces the dividend at rs2p; it will
be s3size limbs long (i.e., as many limbs as the divisor).
In addition to an integer quotient, xsize fraction limbs are developed,
and stored after the integral limbs. For most usages, xsize will be
zero.
It is required that rs2size is greater than or equal to s3size.
It is required that the most significant bit of the divisor is set.
If the quotient is not needed, pass rs2p + s3size as r1p.
Aside from that special case, no overlap between arguments is permitted.
Return the most significant limb of the quotient, either 0 or 1.
The area at r1p needs to be rs2size - s3size +
xsize limbs large.
- Function: mp_limb_t mpn_divrem_1 (mp_limb_t *r1p, mp_size_t xsize, mp_limb_t *s2p, mp_size_t s2size, mp_limb_t s3limb)
-
- Macro: mp_limb_t mpn_divmod_1 (mp_limb_t *r1p, mp_limb_t *s2p, mp_size_t s2size, mp_limb_t s3limb)
-
Divide {s2p, s2size} by s3limb, and write the quotient
at r1p. Return the remainder.
The integer quotient is written to {r1p+xsize, s2size} and
in addition xsize fraction limbs are developed and written to
{r1p, xsize}. Either or both s2size and xsize can
be zero. For most usages, xsize will be zero.
mpn_divmod_1
exists for upward source compatibility and is simply a
macro calling mpn_divrem_1
with an xsize of 0.
The areas at r1p and s2p have to be identical or completely
separate, not partially overlapping.
- Function: mp_limb_t mpn_divmod (mp_limb_t *r1p, mp_limb_t *rs2p, mp_size_t rs2size, const mp_limb_t *s3p, mp_size_t s3size)
-
This interface is obsolete. It will disappear from future releases.
Use
mpn_divrem
in its stead.
- Macro: mp_limb_t mpn_divexact_by3 (mp_limb_t *rp, mp_limb_t *sp, mp_size_t size)
-
- Function: mp_limb_t mpn_divexact_by3c (mp_limb_t *rp, mp_limb_t *sp, mp_size_t size, mp_limb_t carry)
-
Divide {sp, size} by 3, expecting it to divide exactly, and
writing the result to {rp, size}. If 3 divides exactly, the
return value is zero and the result is the quotient. If not, the return value
is non-zero and the result won't be anything useful.
mpn_divexact_by3c
takes an initial carry parameter, which can be the
return value from a previous call, so a large calculation can be done piece by
piece. mpn_divexact_by3
is simply a macro calling
mpn_divexact_by3c
with a 0 carry parameter.
These routines use a multiply-by-inverse and will be faster than
mpn_divrem_1
on CPUs with fast multiplication but slow division.
The source a, result q, size n, initial carry i,
and return value c satisfy
@ifnottex
c*b^n + a-i = 3*q,
where b is the size of a limb
@ifnottex
(2^32 or 2^64).
c is always 0, 1 or 2, and the initial carry must also be 0, 1 or 2
(these are both borrows really). When c=0, clearly q=(a-i)/3.
When
@ifnottex
c!=0, the remainder (a-i) mod 3
is given by 3-c, because
@ifnottex
b == 1 mod 3.
- Function: mp_limb_t mpn_mod_1 (mp_limb_t *s1p, mp_size_t s1size, mp_limb_t s2limb)
-
Divide {s1p, s1size} by s2limb, and return the remainder.
s1size can be zero.
- Function: mp_limb_t mpn_preinv_mod_1 (mp_limb_t *s1p, mp_size_t s1size, mp_limb_t s2limb, mp_limb_t s3limb)
-
This interface is obsolete. It will disappear from future releases.
Use
mpn_mod_1
in its stead.
- Function: mp_limb_t mpn_bdivmod (mp_limb_t *rp, mp_limb_t *s1p, mp_size_t s1size, const mp_limb_t *s2p, mp_size_t s2size, unsigned long int d)
-
The function puts the low [d/BITS_PER_MP_LIMB] limbs of
q =
{s1p, s1size}/{s2p, s2size}
mod 2^d
at rp,
and returns the high d mod BITS_PER_MP_LIMB bits of q.
{s1p, s1size} - q * {s2p, s2size}
mod 2^(s1size*BITS_PER_MP_LIMB)
is placed at s1p.
Since the low [d/BITS_PER_MP_LIMB] limbs of
this difference are zero, it is possible to overwrite the low limbs at
s1p with this difference,
provided rp <= s1p.
This function requires that s1size * BITS_PER_MP_LIMB >= D,
and that {s2p, s2size} is odd.
This interface is preliminary. It might change incompatibly in
future revisions.
- Function: mp_limb_t mpn_lshift (mp_limb_t *rp, const mp_limb_t *src_ptr, mp_size_t src_size, unsigned long int count)
-
Shift {src_ptr, src_size} count bits to the left, and
write the src_size least significant limbs of the result to
rp. count might be in the range 1 to n - 1, on an
n-bit machine. The bits shifted out to the left are returned.
Overlapping of the destination space and the source space is allowed in this
function, provided rp >= src_ptr.
This function is written in assembly for most targets.
- Function: mp_limp_t mpn_rshift (mp_limb_t *rp, const mp_limb_t *src_ptr, mp_size_t src_size, unsigned long int count)
-
Shift {src_ptr, src_size} count bits to the right, and
write the src_size most significant limbs of the result to
rp. count might be in the range 1 to n - 1, on an
n-bit machine. The bits shifted out to the right are returned.
Overlapping of the destination space and the source space is allowed in this
function, provided rp <= src_ptr.
This function is written in assembly for most targets.
- Function: int mpn_cmp (const mp_limb_t *s1p, const mp_limb_t *s2p, mp_size_t size)
-
Compare {s1p, size} and {s2p, size} and
return a positive value if s1 > src2, 0 of they are equal, and a negative
value if s1 < src2.
- Function: mp_size_t mpn_gcd (mp_limb_t *rp, mp_limb_t *s1p, mp_size_t s1size, mp_limb_t *s2p, mp_size_t s2size)
-
Puts at rp the greatest common divisor of {s1p,
s1size} and {s2p, s2size}; both source
operands are destroyed by the operation. The size in limbs of the greatest
common divisor is returned.
{s1p, s1size} must have at least as many bits as
{s2p, s2size}, and {s2p, s2size} must be odd.
- Function: mp_limb_t mpn_gcd_1 (const mp_limb_t *s1p, mp_size_t s1size, mp_limb_t s2limb)
-
Return the greatest common divisor of {s1p, s1size}
and s2limb, where s2limb (as well as s1size)
must be different from 0.
- Function: mp_size_t mpn_gcdext (mp_limb_t *r1p, mp_limb_t *r2p, mp_size_t *r2size, mp_limb_t *s1p, mp_size_t s1size, mp_limb_t *s2p, mp_size_t s2size)
-
Compute the greatest common divisor of {s1p, s1size} and
{s2p, s2size}. Store the gcd at r1p and return its size
in limbs. Write the first cofactor at r2p and store its size in
*r2size. If the cofactor is negative, *r2size is negative and
r2p is the absolute value of the cofactor.
{s1p, s1size} must be greater than or equal to {s2p,
s2size}. Neither operand may equal 0. Both source operands are
destroyed, plus one limb past the end of each, ie. {s1p,
s1size+1} and {s2p, s2size+1}.
- Function: mp_size_t mpn_sqrtrem (mp_limb_t *r1p, mp_limb_t *r2p, const mp_limb_t *sp, mp_size_t size)
-
Compute the square root of {sp, size} and put the result at
r1p. Write the remainder at r2p, unless r2p is
NULL
.
Return the size of the remainder, whether r2p was NULL
or non-NULL
.
Iff the operand was a perfect square, the return value will be 0.
The areas at r1p and sp have to be distinct. The areas at
r2p and sp have to be identical or completely separate, not
partially overlapping.
@ifnottex
The area at r1p needs to have space for ceil(size/2) limbs.
The area at r2p needs to be size limbs large.
- Function: mp_size_t mpn_get_str (unsigned char *str, int base, mp_limb_t *s1p, mp_size_t s1size)
-
Convert {s1p, s1size} to a raw unsigned char array in base
base. The string is not in ASCII; to convert it to printable format,
add the ASCII codes for `0' or `A', depending on the base and
range. There may be leading zeros in the string.
The area at s1p is clobbered.
Return the number of characters in str.
The area at str has to have space for the largest possible number
represented by a s1size long limb array, plus one extra character.
- Function: mp_size_t mpn_set_str (mp_limb_t *r1p, const char *str, size_t strsize, int base)
-
Convert the raw unsigned char array at str of length strsize to
a limb array {s1p, s1size}. The base of str is
base.
Return the number of limbs stored in r1p.
- Function: unsigned long int mpn_scan0 (const mp_limb_t *s1p, unsigned long int bit)
-
Scan s1p from bit position bit for the next clear bit.
It is required that there be a clear bit within the area at s1p at or
beyond bit position bit, so that the function has something to return.
- Function: unsigned long int mpn_scan1 (const mp_limb_t *s1p, unsigned long int bit)
-
Scan s1p from bit position bit for the next set bit.
It is required that there be a set bit within the area at s1p at or
beyond bit position bit, so that the function has something to return.
- Function: void mpn_random (mp_limb_t *r1p, mp_size_t r1size)
-
- Function: void mpn_random2 (mp_limb_t *r1p, mp_size_t r1size)
-
Generate a random number of length r1size and store it at r1p.
The most significant limb is always non-zero.
mpn_random
generates
uniformly distributed limb data, mpn_random2
generates long strings of
zeros and ones in the binary representation.
mpn_random2
is intended for testing the correctness of the mpn
routines.
- Function: unsigned long int mpn_popcount (const mp_limb_t *s1p, unsigned long int size)
-
Count the number of set bits in {s1p, size}.
- Function: unsigned long int mpn_hamdist (const mp_limb_t *s1p, const mp_limb_t *s2p, unsigned long int size)
-
Compute the hamming distance between {s1p, size} and
{s2p, size}.
- Function: int mpn_perfect_square_p (const mp_limb_t *s1p, mp_size_t size)
-
Return non-zero iff {s1p, size} is a perfect square.
Go to the first, previous, next, last section, table of contents.