Go forward to BSD Compatible Functions.
Go backward to Floating-point Functions.
Go up to Top.

Low-level Functions
*******************

   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 2SIZE 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 R1P needs to have space for ceil(SIZE/2) limbs.  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.