This chapter describes low-level MP functions, used to implement the high-level MP 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.

The `mpn`

functions is the base for the implementation of the `mpz_`

,
`mpf_`

, and `mpq_`

functions.

This example adds the number beginning at `src1_ptr` and the number
beginning at `src2_ptr` and writes the sum at `dest_ptr`. All areas
have `size` limbs.

cy = mpn_add_n (dest_ptr, src1_ptr, src2_ptr, 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, {s1_ptr, s1_size}.

__Function:__mp_limb_t**mpn_add_n***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, const mp_limb_t *`src2_ptr`, mp_size_t`size`)-
Add {
`src1_ptr`,`size`} and {`src2_ptr`,`size`}, and write the`size`least significant limbs of the result to`dest_ptr`. 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.,

`src1_ptr`equals`src2_ptr`, use`mpn_lshift`

with a count of 1 for optimal speed.

__Function:__mp_limb_t**mpn_add_1***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`size`, mp_limb_t`src2_limb`)-
Add {
`src1_ptr`,`size`} and`src2_limb`, and write the`size`least significant limbs of the result to`dest_ptr`. Return carry, either 0 or 1.

__Function:__mp_limb_t**mpn_add***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`src1_size`, const mp_limb_t *`src2_ptr`, mp_size_t`src2_size`)-
Add {
`src1_ptr`,`src1_size`} and {`src2_ptr`,`src2_size`}, and write the`src1_size`least significant limbs of the result to`dest_ptr`. Return carry, either 0 or 1.This function requires that

`src1_size`is greater than or equal to`src2_size`.

__Function:__mp_limb_t**mpn_sub_n***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, const mp_limb_t *`src2_ptr`, mp_size_t`size`)-
Subtract {
`src2_ptr`,`src2_size`} from {`src1_ptr`,`size`}, and write the`size`least significant limbs of the result to`dest_ptr`. 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 **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`size`, mp_limb_t`src2_limb`)-
Subtract
`src2_limb`from {`src1_ptr`,`size`}, and write the`size`least significant limbs of the result to`dest_ptr`. Return borrow, either 0 or 1.

__Function:__mp_limb_t**mpn_sub***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`src1_size`, const mp_limb_t *`src2_ptr`, mp_size_t`src2_size`)-
Subtract {
`src2_ptr`,`src2_size`} from {`src1_ptr`,`src1_size`}, and write the`src1_size`least significant limbs of the result to`dest_ptr`. Return borrow, either 0 or 1.This function requires that

`src1_size`is greater than or equal to`src2_size`.

__Function:__void**mpn_mul_n***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, const mp_limb_t *`src2_ptr`, mp_size_t`size`)-
Multiply {
`src1_ptr`,`size`} and {`src2_ptr`,`size`}, and write the**entire**result to`dest_ptr`.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 **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`size`, mp_limb_t`src2_limb`)-
Multiply {
`src1_ptr`,`size`} and`src2_limb`, and write the`size`least significant limbs of the product to`dest_ptr`. 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 MP. It is written in assembly for most targets.

Don't call this function if

`src2_limb`is a power of 2; use`mpn_lshift`

with a count equal to the logarithm of`src2_limb`instead, for optimal speed.

__Function:__mp_limb_t**mpn_addmul_1***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`size`, mp_limb_t`src2_limb`)-
Multiply {
`src1_ptr`,`size`} and`src2_limb`, and add the`size`least significant limbs of the product to {`dest_ptr`,`size`} and write the result to`dest_ptr``dest_ptr`. 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 MP. It is written in assembly for most targets.

__Function:__mp_limb_t**mpn_submul_1***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`size`, mp_limb_t`src2_limb`)-
Multiply {
`src1_ptr`,`size`} and`src2_limb`, and subtract the`size`least significant limbs of the product from {`dest_ptr`,`size`} and write the result to`dest_ptr`. 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 MP. It is written in assembly for most targets.

__Function:__mp_limb_t**mpn_mul***(mp_limb_t **`dest_ptr`, const mp_limb_t *`src1_ptr`, mp_size_t`src1_size`, const mp_limb_t *`src2_ptr`, mp_size_t`src2_size`)-
Multiply {
`src1_ptr`,`src1_size`} and {`src2_ptr`,`src2_size`}, and write the result to`dest_ptr`. Return the most significant limb of the result.The destination has to have space for

`src1_size`+`src1_size`limbs, even if the result might be one limb smaller.This function requires that

`src1_size`is greater than or equal to`src2_size`. The destination must be distinct from either input operands.

__Function:__mp_size_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`)-
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`.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`)-
Divide {
`s2p`,`s2size`} by`s3limb`, and write the quotient at`r1p`. Return the remainder.In addition to an integer quotient,

`xsize`fraction limbs are developed, and stored after the integral limbs. For most usages,`xsize`will be zero.The areas at

`r1p`and`s2p`have to be identical or completely separate, not partially overlapping.

__Function:__mp_size_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.

__Function:__mp_limb_t**mpn_divmod_1***(mp_limb_t **`r1p`, mp_limb_t *`s2p`, mp_size_t`s2size`, mp_limb_t`s3limb`)-
**This interface is obsolete. It will disappear from future releases. Use**`mpn_divrem_1`

in its stead.

__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.

__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 **`dest_ptr`, 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`dest_ptr`, 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`dest_ptr`<=`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 **`dest_ptr`, 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`dest_ptr`.`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

`dest_ptr`>=`src_ptr`.This function is written in assembly for most targets.

__Function:__mp_limp_t**mpn_rshift***(mp_limb_t **`dest_ptr`, 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`dest_ptr`.`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

`dest_ptr`<=`src_ptr`.This function is written in assembly for most targets.

__Function:__int**mpn_cmp***(const mp_limb_t **`src1_ptr`, const mp_limb_t *`src2_ptr`, mp_size_t`size`)-
Compare {
`src1_ptr`,`size`} and {`src2_ptr`,`size`} and return a positive value if src1 > src2, 0 of they are equal, and a negative value if src1 < src2.

__Function:__mp_size_t**mpn_gcd***(mp_limb_t **`dest_ptr`, mp_limb_t *`src1_ptr`, mp_size_t`src1_size`, mp_limb_t *`src2_ptr`, mp_size_t`src2_size`)-
Puts at
`dest_ptr`the greatest common divisor of {`src1_ptr`,`src1_size`} and {`src2_ptr`,`src2_size`}; both source operands are destroyed by the operation. The size in limbs of the greatest common divisor is returned.{

`src1_ptr`,`src1_size`} must be odd, and {`src2_ptr`,`src2_size`} must have at least as many bits as {`src1_ptr`,`src1_size`}.**This interface is preliminary. It might change incompatibly in future revisions.**

__Function:__mp_limb_t**mpn_gcd_1***(const mp_limb_t **`src1_ptr`, mp_size_t`src1_size`, mp_limb_t`src2_limb`)-
Return the greatest common divisor of {
`src1_ptr`,`src1_size`} and`src2_limb`, where`src2_limb`(as well as`src1_size`) must be different from 0.

__Function:__mp_size_t**mpn_gcdext***(mp_limb_t **`r1p`, mp_limb_t *`r2p`, mp_limb_t *`s1p`, mp_size_t`s1size`, mp_limb_t *`s2p`, mp_size_t`s2size`)-
Puts at
`r1p`the greatest common divisor of {`s1p`,`s1size`} and {`s2p`,`s2size`}. The first cofactor is written at`r2p`. Both source operands are destroyed by the operation. The size in limbs of the greatest common divisor is returned.**This interface is preliminary. It might change incompatibly in future revisions.**

__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.The area at

`r2p`needs to be`size`limbs large.**This interface is preliminary. It might change incompatibly in future revisions.**

__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.**This interface is preliminary. It might change incompatibly in future revisions.**

__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.**This interface is preliminary. It might change incompatibly in future revisions.**

__Function:__void**mpn_random2***(mp_limb_t **`r1p`, mp_size_t`r1size`)-
Generate a random number of length
`r1size`with long strings of zeros and ones in the binary representation, and store it at`r1p`.The generated random numbers are intended for testing the correctness of the implementation 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.