Go to the previous, next section.

Low-level Functions

The next release of the GNU MP library (2.0) will include changes to some mpn functions. Programs that use these functions according to the descriptions below will therefore not work with the next release.

The low-level function layer is designed to be as fast as possible, not to provide a coherent calling interface. The different functions have similar interfaces, but there are variations that might be confusing. 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 responsability of the caller to ensure that the destination has enough space for storing the result.

With this way of specifying source operands, it is possible to perform computations on subranges of an argument, and store the result into a subrange of a destination.

All these functions require that the operands are normalized in the sense that the most significant limb must be non-zero. (A future release of might drop this requirement.)

The low-level layer is the base for the implementation of the mpz_ and mpq_ layers.

The code below adds the number beginning at src1_ptr and the number beginning at src2_ptr and writes the sum at dest_ptr. A constraint for mpn_add is that src1_size must not be smaller that src2_size.

mpn_add (dest_ptr, src1_ptr, src1_size, src2_ptr, src2_size)

In the description below, a source operand is identified by the pointer to the least significant limb, and the limb count in braces.

Function: mp_size mpn_add (mp_ptr dest_ptr, mp_srcptr src1_ptr, mp_size src1_size, mp_srcptr src2_ptr, mp_size 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. Carry-out, either 0 or 1, is returned.

This function requires that src1_size is greater than or equal to src2_size.

Function: mp_size mpn_sub (mp_ptr dest_ptr, mp_srcptr src1_ptr, mp_size src1_size, mp_srcptr src2_ptr, mp_size src2_size)

Subtarct {src2_ptr, src2_size} from {src1_ptr, src1_size}, and write the result to dest_ptr.

Return 1 if the minuend < the subtrahend. Otherwise, return the negative difference between the number of words in the result and the minuend. I.e. return 0 if the result has src1_size words, -1 if it has src1_size - 1 words, etc.

This function requires that src1_size is greater than or equal to src2_size.

Function: mp_size mpn_mul (mp_ptr dest_ptr, mp_srcptr src1_ptr, mp_size src1_size, mp_srcptr src2_ptr, mp_size src2_size)

Multiply {src1_ptr, src1_size} and {src2_ptr, src2_size}, and write the result to dest_ptr. The exact size of the result is returned.

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 mpn_div (mp_ptr dest_ptr, mp_ptr src1_ptr, mp_size src1_size, mp_srcptr src2_ptr, mp_size src2_size)

Divide {src1_ptr, src1_size} by {src2_ptr, src2_size}, and write the quotient to dest_ptr, and the remainder to src1_ptr.

Return 0 if the quotient size is at most (src1_size - src2_size), and 1 if the quotient size is at most (src1_size - src2_size + 1). The caller has to check the most significant limb to find out the exact size.

The most significant bit of the most significant limb of the divisor has to be set.

This function requires that src1_size is greater than or equal to src2_size. The quotient, pointed to by dest_ptr, must be distinct from either input operands.

Function: mp_limb mpn_lshift (mp_ptr dest_ptr, mp_srcptr src_ptr, mp_size 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 limb shifted out is returned.

Overlapping of the destination space and the source space is allowed in this function, provdied dest_ptr >= src_ptr.

Function: mp_size mpn_rshift (mp_ptr dest_ptr, mp_srcptr src_ptr, mp_size src_size, unsigned long int count)

Shift {src_ptr, src_size} count bits to the right, 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 size of the result is returned.

Overlaping of the destination space and the source space is allowed in this function, provdied dest_ptr <= src_ptr.

Function: mp_size mpn_rshiftci (mp_ptr dest_ptr, mp_srcptr src_ptr, mp_size src_size, unsigned long int count, mp_limb inlimb)

Like mpn_rshift, but use inlimb to feed the least significant end of the destination.

Function: int mpn_cmp (mp_srcptr src1_ptr, mp_srcptr src2_ptr, mp_size 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.

Go to the previous, next section.