Click or drag to resize
Welcome to the GMP Native Interface for .NET Library

The GMP Native Interface for .NET Library exposes to .NET (through P-Invoke and .NET types) all of the functionality of the GNU MP Library (version 6.1.2). It automatically loads at runtime the 32-bit or 64-bit GNU MP library that matches the current CPU architecture, thus allowing building Visual Studio Projects for Any CPU, x86, or x64. It is based on the GNU MP "fat" build which automatically detects the current CPU type, and selects any available assembly language code optimization for that CPU, thus providing best performance.

Overview

The gmp_lib class has a static method for each one of the GNU MP functions. Other types are defined to mimic struct's and typedef's of the GNU MP and C libraries, as well as C language constructs such as char * and void *.

The GMP Native Interface for .NET Library relies on pre-built 32-bit and 64-bit versions of the GNU MP Library. Instructions for building the GNU MP Library on Windows are given below.

For convenience, this help file has been created from the GNU MP manual version 6.1.2. It shows with examples how each GNU MP function is called in .NET. For an introduction to GNU MP, refer to the GNU MP Manual.

Functions Categories
  • Global Variable and Constants:

  • Integer Functions:

    • Initializing Integers:

      mpz_init - Initialize x, and set its value to 0.
      mpz_inits - Initialize a NULL-terminated list of mpz_t variables, and set their values to 0.
      mpz_init2 - Initialize x, with space for n-bit numbers, and set its value to 0.
      mpz_clear - Free the space occupied by x.
      mpz_clears - Free the space occupied by a NULL-terminated list of mpz_t variables.
      mpz_realloc2 - Change the space allocated for x to n bits.

    • Assigning Integers:

      mpz_set - Set the value of rop from op.
      mpz_set_ui - Set the value of rop from op.
      mpz_set_si - Set the value of rop from op.
      mpz_set_d - Set the value of rop from op.
      mpz_set_q - Set the value of rop from op.
      mpz_set_f - Set the value of rop from op.
      mpz_set_str - Set the value of rop from str, a null-terminated C string in base base.
      mpz_swap - Swap the values rop1 and rop2 efficiently.

    • Simultaneous Integer Init & Assign:

      mpz_init_set - Initialize rop with limb space and set the initial numeric value from op.
      mpz_init_set_ui - Initialize rop with limb space and set the initial numeric value from op.
      mpz_init_set_si - Initialize rop with limb space and set the initial numeric value from op.
      mpz_init_set_d - Initialize rop with limb space and set the initial numeric value from op.
      mpz_init_set_str - Initialize rop and set its value like mpz_set_str.

    • Converting Integers:

      mpz_get_ui - Return the value of op as an unsigned long.
      mpz_get_si - Return the value of op as an signed long.
      mpz_get_d - Convert op to a double, truncating if necessary (i.e. rounding towards zero).
      mpz_get_d_2exp - Convert op to a double, truncating if necessary (i.e. rounding towards zero), and returning the exponent separately.
      mpz_get_str - Convert op to a string of digits in base base.

    • Integer Arithmetic:

      mpz_add - Set rop to op1 + op2.
      mpz_add_ui - Set rop to op1 + op2.
      mpz_sub - Set rop to op1 - op2.
      mpz_sub_ui - Set rop to op1 - op2.
      mpz_ui_sub - Set rop to op1 - op2.
      mpz_mul - Set rop to op1 * op2.
      mpz_mul_si - Set rop to op1 * op2.
      mpz_mul_ui - Set rop to op1 * op2.
      mpz_addmul - Set rop to rop + op1 * op2.
      mpz_addmul_ui - Set rop to rop + op1 * op2.
      mpz_submul - Set rop to rop - op1 * op2.
      mpz_submul_ui - Set rop to rop - op1 * op2.
      mpz_mul_2exp - Set rop to op1 * 2^op2.
      mpz_neg - Set rop to -op.
      mpz_abs - Set rop to the absolute value of op.

    • Integer Division:

      mpz_cdiv_q - Set the quotient q to ceiling(n / d).
      mpz_cdiv_r - Set the remainder r to n - q * d where q = ceiling(n / d).
      mpz_cdiv_qr - Set the quotient q to ceiling(n / d), and set the remainder r to n - q * d.
      mpz_cdiv_q_ui - Set the quotient q to ceiling(n / d), and return the remainder r = | n - q * d |.
      mpz_cdiv_r_ui - Set the remainder r to n - q * d where q = ceiling(n / d), and return | r |.
      mpz_cdiv_qr_ui - Set quotient q to ceiling(n / d), set the remainder r to n - q * d, and return | r |.
      mpz_cdiv_ui - Return the remainder | r | where r = n - q * d, and where q = ceiling(n / d).
      mpz_cdiv_q_2exp - Set the quotient q to ceiling(n / 2^b).
      mpz_cdiv_r_2exp - Set the remainder r to n - q * 2^b where q = ceiling(n / 2^b).
      mpz_fdiv_q - Set the quotient q to floor(n / d).
      mpz_fdiv_r - Set the remainder r to n - q * d where q = floor(n / d).
      mpz_fdiv_qr - Set the quotient q to floor(n / d), and set the remainder r to n - q * d.
      mpz_fdiv_q_ui - Set the quotient q to floor(n / d), and return the remainder r = | n - q * d |.
      mpz_fdiv_r_ui - Set the remainder r to n - q * d where q = floor(n / d), and return | r |.
      mpz_fdiv_qr_ui - Set quotient q to floor(n / d), set the remainder r to n - q * d, and return | r |.
      mpz_fdiv_ui - Return the remainder | r | where r = n - q * d, and where q = floor(n / d).
      mpz_fdiv_q_2exp - Set the quotient q to floor(n / 2^b).
      mpz_fdiv_r_2exp - Set the remainder r to n - q * 2^b where q = floor(n / 2^b).
      mpz_tdiv_q - Set the quotient q to trunc(n / d).
      mpz_tdiv_r - Set the remainder r to n - q * d where q = trunc(n / d).
      mpz_tdiv_qr - Set the quotient q to trunc(n / d), and set the remainder r to n - q * d.
      mpz_tdiv_q_ui - Set the quotient q to trunc(n / d), and return the remainder r = | n - q * d |.
      mpz_tdiv_r_ui - Set the remainder r to n - q * d where q = trunc(n / d), and return | r |.
      mpz_tdiv_qr_ui - Set quotient q to trunc(n / d), set the remainder r to n - q * d, and return | r |.
      mpz_tdiv_ui - Return the remainder | r | where r = n - q * d, and where q = trunc(n / d).
      mpz_tdiv_q_2exp - Set the quotient q to trunc(n / 2^b).
      mpz_tdiv_r_2exp - Set the remainder r to n - q * 2^b where q = trunc(n / 2^b).
      mpz_mod - Set r to n mod d.
      mpz_mod_ui - Set r to n mod d.
      mpz_divexact - Set q to n / d when it is known in advance that d divides n.
      mpz_divexact_ui - Set q to n / d when it is known in advance that d divides n.
      mpz_divisible_p - Return non-zero if n is exactly divisible by d.
      mpz_divisible_ui_p - Return non-zero if n is exactly divisible by d.
      mpz_divisible_2exp_p - Return non-zero if n is exactly divisible by 2^b.
      mpz_congruent_p - Return non-zero if n is congruent to c modulo d.
      mpz_congruent_ui_p - Return non-zero if n is congruent to c modulo d.
      mpz_congruent_2exp_p - Return non-zero if n is congruent to c modulo 2^b.

    • Integer Exponentiation:

      mpz_powm - Set rop to (base^exp) modulo mod.
      mpz_powm_ui - Set rop to (base^exp) modulo mod.
      mpz_powm_sec - Set rop to (base^exp) modulo mod.
      mpz_pow_ui - Set rop to base^exp. The case 0^0 yields 1.
      mpz_ui_pow_ui - Set rop to base^exp. The case 0^0 yields 1.

    • Integer Roots:

      mpz_root - Set rop to the truncated integer part of the nth root of op.
      mpz_rootrem - Set root to the truncated integer part of the nth root of u. Set rem to the remainder, u - root^n.
      mpz_sqrt - Set rop to the truncated integer part of the square root of op.
      mpz_sqrtrem - Set rop1 to the truncated integer part of the square root of op, like mpz_sqrt. Set rop2 to the remainder op - rop1 * rop1, which will be zero if op is a perfect square.
      mpz_perfect_power_p - Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b.
      mpz_perfect_square_p - Return non-zero if op is a perfect square, i.e., if the square root of op is an integer.

    • Number Theoretic Functions:

      mpz_probab_prime_p - Determine whether n is prime.
      mpz_nextprime - Set rop to the next prime greater than op.
      mpz_gcd - Set rop to the greatest common divisor of op1 and op2.
      mpz_gcd_ui - Compute the greatest common divisor of op1 and op2. If rop is not null, store the result there.
      mpz_gcdext - Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying a * s + b * t = g.
      mpz_lcm - Set rop to the least common multiple of op1 and op2.
      mpz_lcm_ui - Set rop to the least common multiple of op1 and op2.
      mpz_invert - Compute the inverse of op1 modulo op2 and put the result in rop.
      mpz_jacobi - Calculate the Jacobi symbol (a/b).
      mpz_legendre - Calculate the Legendre symbol (a/p).
      mpz_kronecker - Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2) = (2/a) when a odd, or (a/2) = 0 when a even.
      mpz_kronecker_si - Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2) = (2/a) when a odd, or (a/2) = 0 when a even.
      mpz_kronecker_ui - Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2) = (2/a) when a odd, or (a/2) = 0 when a even.
      mpz_si_kronecker - Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2) = (2/a) when a odd, or (a/2) = 0 when a even.
      mpz_ui_kronecker - Calculate the Jacobi symbol (a/b) with the Kronecker extension (a/2) = (2/a) when a odd, or (a/2) = 0 when a even.
      mpz_remove - Remove all occurrences of the factor f from op and store the result in rop.
      mpz_fac_ui - Set rop to the factorial n!.
      mpz_2fac_ui - Set rop to the double-factorial n!!.
      mpz_mfac_uiui - Set rop to the m-multi-factorial n!^(m)n.
      mpz_primorial_ui - Set rop to the primorial of n, i.e. the product of all positive prime numbers ≤ n.
      mpz_bin_ui - Compute the binomial coefficient n over k and store the result in rop.
      mpz_bin_uiui - Compute the binomial coefficient n over k and store the result in rop.
      mpz_fib_ui - Sets fn to to F[n], the n’th Fibonacci number.
      mpz_fib2_ui - Sets fn to F[n], and fnsub1 to F[n - 1].
      mpz_lucnum_ui - Sets ln to to L[n], the n’th Lucas number.
      mpz_lucnum2_ui - Sets ln to L[n], and lnsub1 to L[n - 1].
      mpz_millerrabin - An implementation of the probabilistic primality test found in Knuth's Seminumerical Algorithms book.

    • Integer Comparisons:

      mpz_cmp - Compare op1 and op2.
      mpz_cmp_d - Compare op1 and op2.
      mpz_cmp_si - Compare op1 and op2.
      mpz_cmp_ui - Compare op1 and op2.
      mpz_cmpabs - Compare the absolute values of op1 and op2.
      mpz_cmpabs_d - Compare the absolute values of op1 and op2.
      mpz_cmpabs_ui - Compare the absolute values of op1 and op2.
      mpz_sgn - Return +1 if op > 0, 0 if op = 0, and -1 if op < 0.

    • Integer Logic and Bit Fiddling:

      mpz_and - Set rop to op1 bitwise-and op2.
      mpz_ior - Set rop to op1 bitwise inclusive-or op2.
      mpz_xor - Set rop to op1 bitwise exclusive-or op2.
      mpz_com - Set rop to the one’s complement of op.
      mpz_popcount - Return the population count of op.
      mpz_hamdist - Return the hamming distance between the two operands.
      mpz_scan0 - Scan op for 0 bit.
      mpz_scan1 - Scan op for 1 bit.
      mpz_setbit - Set bit bit_index in rop.
      mpz_clrbit - Clear bit bit_index in rop.
      mpz_combit - Complement bit bit_index in rop.
      mpz_tstbit - Test bit bit_index in op and return 0 or 1 accordingly.

    • I/O of Integers:

      mpz_out_str - Output op on stdio stream stream, as a string of digits in base base.
      mpz_inp_str - Input a possibly white-space preceded string in base base from stdio stream stream, and put the read integer in rop.
      mpz_out_raw - Output op on stdio stream stream, in raw binary format.
      mpz_inp_raw - Input from stdio stream stream in the format written by mpz_out_raw, and put the result in rop.

    • Integer Random Numbers:

      mpz_urandomb - Generate a uniformly distributed random integer in the range 0 to 2^n - 1, inclusive.
      mpz_urandomm - Generate a uniform random integer in the range 0 to n - 1, inclusive.
      mpz_rrandomb - Generate a random integer with long strings of zeros and ones in the binary representation.
      mpz_random - Generate a random integer of at most max_size limbs.
      mpz_random2 - Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation.

    • Integer Import and Export:

      mpz_import - Set rop from an array of word data at op.
      mpz_export - Fill rop with word data from op.

    • Miscellaneous Integer Functions:

      mpz_fits_sint_p - Return non-zero iff the value of op fits in a signed 32-bit integer. Otherwise, return zero.
      mpz_fits_slong_p - Return non-zero iff the value of op fits in a signed 32-bit integer. Otherwise, return zero.
      mpz_fits_sshort_p - Return non-zero iff the value of op fits in a signed 16-bit integer. Otherwise, return zero.
      mpz_fits_uint_p - Return non-zero iff the value of op fits in an unsigned 32-bit integer. Otherwise, return zero.
      mpz_fits_ulong_p - Return non-zero iff the value of op fits in an unsigned 32-bit integer. Otherwise, return zero.
      mpz_fits_ushort_p - Return non-zero iff the value of op fits in an unsigned 16-bit integer. Otherwise, return zero.
      mpz_sizeinbase - Return the size of op measured in number of digits in the given base.
      mpz_even_p - Determine whether op is even.
      mpz_odd_p - Determine whether op is odd.

    • Integer Special Functions:

      _mpz_realloc - Change the space for integer to new_alloc limbs.
      mpz_getlimbn - Return limb number n from op.
      mpz_size - Return the size of op measured in number of limbs.
      mpz_limbs_read - Return a pointer to the limb array representing the absolute value of x.
      mpz_limbs_write - Return a pointer to the limb array of x, intended for write access.
      mpz_limbs_modify - Return a pointer to the limb array of x, intended for write access.
      mpz_limbs_finish - Updates the internal size field of x.
      mpz_roinit_n - Special initialization of x, using the given limb array and size.

  • Rational Number Functions:

    • Initializing Rationals:

      mpq_canonicalize - Remove any factors that are common to the numerator and denominator of op, and make the denominator positive.
      mpq_init - Initialize x and set it to 0/1.
      mpq_inits - Initialize a NULL-terminated list of mpq_t variables, and set their values to 0/1.
      mpq_clear - Free the space occupied by x.
      mpq_clears - Free the space occupied by a NULL-terminated list of mpq_t variables.
      mpq_set - Assign rop from op.
      mpq_set_z - Assign rop from op.
      mpq_set_ui - Set the value of rop to op1 / op2.
      mpq_set_si - Set the value of rop to op1 / op2.
      mpq_set_str - Set rop from a null-terminated string str in the given base.
      mpq_swap - Swap the values rop1 and rop2 efficiently.

    • Rational Conversions:

      mpq_get_d - Convert op to a Double, truncating if necessary (i.e. rounding towards zero).
      mpq_set_d - Set rop to the value of op. There is no rounding, this conversion is exact.
      mpq_set_f - Set rop to the value of op. There is no rounding, this conversion is exact.
      mpq_get_str - Convert op to a string of digits in base base.

    • Rational Arithmetic:

      mpq_add - Set sum to addend1 + addend2.
      mpq_sub - Set difference to minuend - subtrahend.
      mpq_mul - Set product to multiplier * multiplicand.
      mpq_mul_2exp - Set rop to op1 * 2*op2.
      mpq_div - Set quotient to dividend / divisor.
      mpq_div_2exp - Set rop to op1 / 2^op2.
      mpq_neg - Set negated_operand to -operand.
      mpq_abs - Set rop to the absolute value of op.
      mpq_inv - Set inverted_number to 1 / number.

    • Comparing Rationals:

      mpq_cmp - Compare op1 and op2.
      mpq_cmp_z - Compare op1 and op2.
      mpq_cmp_ui - Compare op1 and num2 / den2.
      mpq_cmp_si - Compare op1 and num2 / den2.
      mpq_sgn - Return +1 if op > 0, 0 if op = 0, and -1 if op < 0.
      mpq_equal - Return non-zero if op1 and op2 are equal, zero if they are non-equal.

    • Applying Integer Functions:

      mpq_numref - Return a reference to the numerator op.
      mpq_denref - Return a reference to the denominator op.
      mpq_get_num - Set numerator to the numerator of rational.
      mpq_get_den - Set denominator to the denominator of rational.
      mpq_set_num - Set the numerator of rational to numerator.
      mpq_set_den - Set the denominator of rational to denominator.

    • I/O of Rationals:

      mpq_out_str - Output op on stdio stream stream, as a string of digits in base base.
      mpq_inp_str - Read a string of digits from stream and convert them to a rational in rop.

  • Floating-point Functions:

    • Initializing Floats:

      mpf_set_default_prec - Set the default precision to be at least prec bits.
      mpf_get_default_prec - Return the default precision actually used.
      mpf_init - Initialize x to 0.
      mpf_init2 - Initialize x to 0 and set its precision to be at least prec bits.
      mpf_inits - Initialize a NULL-terminated list of mpf_t variables, and set their values to 0.
      mpf_clear - Free the space occupied by x.
      mpf_clears - Free the space occupied by a NULL-terminated list of mpf_t variables.
      mpf_get_prec - Return the current precision of op, in bits.
      mpf_set_prec - Set the precision of rop to be at least prec bits.
      mpf_set_prec_raw - Set the precision of rop to be at least prec bits, without changing the memory allocated.
      mpf_size - Return the number of limbs currently in use.

    • Assigning Floats:

      mpf_set - Set the value of rop from op.
      mpf_set_ui - Set the value of rop from op.
      mpf_set_si - Set the value of rop from op.
      mpf_set_d - Set the value of rop from op.
      mpf_set_z - Set the value of rop from op.
      mpf_set_q - Set the value of rop from op.
      mpf_set_str - Set the value of rop from the string in str.
      mpf_swap - Swap rop1 and rop2 efficiently.

    • Simultaneous Float Init & Assign:

      mpf_init_set - Initialize rop and set its value from op.
      mpf_init_set_ui - Initialize rop and set its value from op.
      mpf_init_set_si - Initialize rop and set its value from op.
      mpf_init_set_d - Initialize rop and set its value from op.
      mpf_init_set_str - Initialize rop and set its value from the string in str.

    • Converting Floats:

      mpf_get_d - Convert op to a Double, truncating if necessary (i.e. rounding towards zero).
      mpf_get_d_2exp - Convert op to a double, truncating if necessary (i.e. rounding towards zero), and with an exponent returned separately.
      mpf_get_si - Convert op to a 32-bit integer, truncating any fraction part.
      mpf_get_ui - Convert op to an unsigned 32-bit integer, truncating any fraction part.
      mpf_get_str - Convert op to a string of digits in base base.

    • Float Arithmetic:

      mpf_add - Set rop to op1 + op2.
      mpf_add_ui - Set rop to op1 + op2.
      mpf_sub - Set rop to op1 - op2.
      mpf_ui_sub - Set rop to op1 - op2.
      mpf_sub_ui - Set rop to op1 - op2.
      mpf_mul - Set rop to op1 * op2.
      mpf_mul_ui - Set rop to op1 * op2.
      mpf_div - Set rop to op1 / op2.
      mpf_ui_div - Set rop to op1 / op2.
      mpf_div_ui - Set rop to op1 / op2.
      mpf_sqrt - Set rop to the square root of op.
      mpf_sqrt_ui - Set rop to the square root of op.
      mpf_pow_ui - Set rop to op1^op2.
      mpf_neg - Set rop to -op.
      mpf_abs - Set rop to | op |.
      mpf_mul_2exp - Set rop to op1 * 2^op2.
      mpf_div_2exp - Set rop to op1 / 2^op2.

    • Float Comparison:

      mpf_cmp - Compare op1 and op2.
      mpf_cmp_z - Compare op1 and op2.
      mpf_cmp_d - Compare op1 and op2.
      mpf_cmp_ui - Compare op1 and op2.
      mpf_cmp_si - Compare op1 and op2.
      mpf_reldiff - Compute the relative difference between op1 and op2 and store the result in rop. This is | op1 - op2 | / op1.
      mpf_sgn - Return +1 if op > 0, 0 if op = 0, and -1 if op < 0.

    • I/O of Floats:

      mpf_out_str - Print op to stream, as a string of digits.
      mpf_inp_str - Read a string in base base from stream, and put the read float in rop.

    • Miscellaneous Float Functions:

      mpf_ceil - Set rop to op rounded to the next higher integer.
      mpf_floor - Set rop to op rounded to the next lower integer.
      mpf_trunc - Set rop to op rounded to the integer towards zero.
      mpf_integer_p - Return non-zero if op is an integer.
      mpf_fits_ulong_p - Return non-zero if op fits in an unsigned 32-bit integer, when truncated to an integer.
      mpf_fits_slong_p - Return non-zero if op fits in a 32-bit integer, when truncated to an integer.
      mpf_fits_uint_p - Return non-zero if op fits in an unsigned 32-bit integer, when truncated to an integer.
      mpf_fits_sint_p - Return non-zero if op fits in a 32-bit integer, when truncated to an integer.
      mpf_fits_sshort_p - Return non-zero if op fits in a 16-bit integer, when truncated to an integer.
      mpf_fits_ushort_p - Return non-zero if op fits in an unsigned 16-bit integer, when truncated to an integer.
      mpf_urandomb - Generate a uniformly distributed random float in rop, such that 0 ≤ rop < 1, with nbits significant bits in the mantissa or less if the precision of rop is smaller.
      mpf_random2 - Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation.

  • Low-level Functions:

    • mpn_add_n - Add {s1p, n} and {s2p, n}, and write the n least significant limbs of the result to rp.
      mpn_add_1 - Add {s1p, n} and s2limb, and write the n least significant limbs of the result to rp.
      mpn_add - Add {s1p, s1n} and {s2p, s2n}, and write the s1n least significant limbs of the result to rp.
      mpn_sub_n - Subtract {s2p, n} from {s1p, n}, and write the n least significant limbs of the result to rp.
      mpn_sub_1 - Subtract s2limb from {s1p, n}, and write the n least significant limbs of the result to rp.
      mpn_sub - Subtract {s2p, s2n} from {s1p, s1n}, and write the s1n least significant limbs of the result to rp.
      mpn_neg - Perform the negation of {sp, n}, and write the result to {rp, n}.
      mpn_mul_n - Multiply {s1p, n} and {s2p, n}, and write the (2 * n)-limb result to rp.
      mpn_mul - Multiply {s1p, s1n} and {s2p, s2n}, and write the (s1n + s2n)-limb result to rp.
      mpn_sqr - Compute the square of {s1p, n} and write the (2 * n)-limb result to rp.
      mpn_mul_1 - Multiply {s1p, n} by s2limb, and write the n least significant limbs of the product to rp.
      mpn_addmul_1 - Multiply {s1p, n} and s2limb, and add the n least significant limbs of the product to {rp, n} and write the result to rp.
      mpn_submul_1 - Multiply {s1p, n} and s2limb, and subtract the n least significant limbs of the product from {rp, n} and write the result to rp.
      mpn_tdiv_qr - Divide {np, nn} by {dp, dn} and put the quotient at {qp, nn - dn + 1} and the remainder at {rp, dn}.
      mpn_divrem_1 - Divide {s2p, s2n} by s3limb, and write the quotient at r1p.
      mpn_divmod_1 - Divide {s2p, s2n} by s3limb, and write the quotient at r1p.
      mpn_divexact_1 - Divide {sp, n} by d, expecting it to divide exactly, and writing the result to {rrp, n}.
      mpn_divexact_by3 - Divide {sp, n} by 3, expecting it to divide exactly, and writing the result to {rp, n}.
      mpn_divexact_by3c - Divide {sp, n} by 3, expecting it to divide exactly, and writing the result to {rp, n}.
      mpn_mod_1 - Divide {s1p, s1n} by s2limb, and return the remainder.
      mpn_lshift - Shift {sp, n} left by count bits, and write the result to {rp, n}.
      mpn_rshift - Shift {sp, n} right by count bits, and write the result to {rp, n}.
      mpn_cmp - Compare {s1p, n} and {s2p, n}.
      mpn_zero_p - Test {sp, n} and return 1 if the operand is zero, 0 otherwise.
      mpn_gcd - Set {rp, retval} to the greatest common divisor of {xp, xn} and {yp, yn}.
      mpn_gcd_1 - Return the greatest common divisor of {xp, xn} and ylimb.
      mpn_gcdext - Compute the greatest common divisor G of U and V. Compute a cofactor S such that G = US + VT.
      mpn_sqrtrem - Compute the square root of {sp, n} and put the result at {r1p, ceil(n / 2)} and the remainder at {r2p, retval}.
      mpn_sizeinbase - Return the size of {xp, n} measured in number of digits in the given base.
      mpn_get_str - Convert {s1p, s1n} to a raw unsigned char array at str in base base, and return the number of characters produced.
      mpn_set_str - Convert bytes {str, strsize} in the given base to limbs at rp.
      mpn_scan0 - Scan s1p from bit position bit for the next clear bit.
      mpn_scan1 - Scan s1p from bit position bit for the next set bit.
      mpn_random - Generate a random number of length r1n and store it at r1p.
      mpn_random2 - Generate a random number of length r1n and store it at r1p.
      mpn_popcount - Count the number of set bits in {s1p, n}.
      mpn_hamdist - Compute the hamming distance between {s1p, n} and {s2p, n}, which is the number of bit positions where the two operands have different bit values.
      mpn_perfect_square_p - Return non-zero iff {s1p, n} is a perfect square.
      mpn_perfect_power_p - Return non-zero iff {sp, n} is a perfect power.
      mpn_and_n - Perform the bitwise logical and of {s1p, n} and {s2p, n}, and write the result to {rp, n}.
      mpn_ior_n - Perform the bitwise logical inclusive or of {s1p, n} and {s2p, n}, and write the result to {rp, n}.
      mpn_xor_n - Perform the bitwise logical exclusive or of {s1p, n} and {s2p, n}, and write the result to {rp, n}.
      mpn_andn_n - Perform the bitwise logical and of {s1p, n} and the bitwise complement of {s2p, n}, and write the result to {rp, n}.
      mpn_iorn_n - Perform the bitwise logical inclusive or of {s1p, n} and the bitwise complement of {s2p, n}, and write the result to {rp, n}.
      mpn_nand_n - Perform the bitwise logical and of {s1p, n} and {s2p, n}, and write the bitwise complement of the result to {rp, n}.
      mpn_nior_n - Perform the bitwise logical inclusive or of {s1p, n} and {s2p, n}, and write the bitwise complement of the result to {rp, n}.
      mpn_xnor_n - Perform the bitwise logical exclusive or of {s1p, n} and {s2p, n}, and write the bitwise complement of the result to {rp, n}.
      mpn_com - Perform the bitwise complement of {sp, n}, and write the result to {rp, n}.
      mpn_copyi - Copy from {s1p, n} to {rp, n}, increasingly.
      mpn_copyd - Copy from {s1p, n} to {rp, n}, decreasingly.
      mpn_zero - Zero {rp, n}.

    • Low-level functions for cryptography:

      mpn_cnd_add_n - If cnd is non-zero, it produces the same result as a regular mpn_add_n, and if cnd is zero, it copies {s1p, n} to the result area and returns zero.
      mpn_cnd_sub_n - If cnd is non-zero, it produces the same result as a regular mpn_sub_n, and if cnd is zero, it copies {s1p, n} to the result area and returns zero.
      mpn_sec_add_1 - Set R to A + b, where R = {rp, n}, A = {ap, n}, and b is a single limb.
      mpn_sec_add_1_itch - Return the scratch space in number of limbs required by the function mpn_sec_add_1.
      mpn_sec_sub_1 - Set R to A - b, where R = {rp, n}, A = {ap, n}, and b is a single limb.
      mpn_sec_sub_1_itch - Return the scratch space in number of limbs required by the function mpn_sec_sub_1.
      mpn_cnd_swap - If cnd is non-zero, swaps the contents of the areas {ap, n} and {bp, n}. Otherwise, the areas are left unmodified.
      mpn_sec_mul - Set R to A * B, where A = {ap, an}, B = {bp, bn}, and R = {rp, an + bn}.
      mpn_sec_mul_itch - Return the scratch space in number of limbs required by the function mpn_sec_mul.
      mpn_sec_sqr - Set R to A^2, where A = {ap, an}, and R = {rp, 2 * an}.
      mpn_sec_sqr_itch - Return the scratch space in number of limbs required by the function mpn_sec_sqr.
      mpn_sec_powm - Set R to (B^E) modulo M, where R = {rp, n}, M = {mp, n}, and E = {ep, ceil(enb / mp_bits_per_limb)}.
      mpn_sec_powm_itch - Return the scratch space in number of limbs required by the function mpn_sec_powm.
      mpn_sec_tabselect - Select entry which from table tab, which has nents entries, each n limbs. Store the selected entry at rp.
      mpn_sec_div_qr - Set Q to the truncated quotient N / D and R to N modulo D, where N = {np, nn}, D = {dp, dn}, Q’s most significant limb is the function return value and the remaining limbs are {qp, nn - dn}, and R = {np, dn}.
      mpn_sec_div_qr_itch - Return the scratch space in number of limbs required by the function mpn_sec_div_qr.
      mpn_sec_div_r - Set R to N modulo D, where N = {np, nn}, D = {dp, dn}, and R = {np, dn}.
      mpn_sec_div_r_itch - Return the scratch space in number of limbs required by the function mpn_sec_div_r.
      mpn_sec_invert - Set R to the inverse of A modulo M, where R = {rp, n}, A = {ap, n}, and M = {mp, n}. This function’s interface is preliminary.
      mpn_sec_invert_itch - Return the scratch space in number of limbs required by the function mpn_sec_invert.

  • Random Number Functions:

  • Formatted Output:

    • Formatted Output Functions:

      gmp_printf - Print to the standard output stdout.
      gmp_vprintf - Print to the standard output stdout.
      gmp_fprintf - Print to the stream fp.
      gmp_vfprintf - Print to the stream fp.
      gmp_sprintf - Form a null-terminated string in buf.
      gmp_vsprintf - Form a null-terminated string in buf.
      gmp_snprintf - Form a null-terminated string in buf.
      gmp_vsnprintf - Form a null-terminated string in buf.
      gmp_asprintf - Form a null-terminated string in a block of memory obtained from the current memory allocation function.
      gmp_vasprintf - Form a null-terminated string in a block of memory obtained from the current memory allocation function.

  • Formatted Input:

  • Custom Allocation:

    • mp_set_memory_functions - Replace the current allocation functions from the arguments.
      mp_get_memory_functions - Get the current allocation functions, storing function pointers to the locations given by the arguments.
      allocate - Return a pointer to newly allocated space with at least alloc_size bytes.
      reallocate - Resize a previously allocated block ptr of old_size bytes to be new_size bytes.
      free - De-allocate the space pointed to by ptrs.
      free - De-allocate the space pointed to by ptr.
      free - De-allocate the space pointed to by ptr.
      free - De-allocate the space pointed to by ptr.
      free - De-allocate the space pointed to by ptr.
      ZeroMemory - The ZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled.

C and .NET Types Equivalence

The table below shows how each C type maps to .NET. Note that the mp_limb_t and size_t C types map to the CPU word, i.e., 32 or 64 bits. In particular, because mp_limb_t is the type of the integers that make up multi-precision numbers, matching the CPU word size ensures maximum performance. Unless you intend to use low-level (mpn) functions, you do not need to take into account the CPU word size, and can build for the "Any CPU" platform.

C Types

.NET Types

short

Int16

int

Int32

long

Int32

long long

Int64

mp_bitcnt_t

UInt32

mp_exp_t

Int32

mp_size_t

Int32

mp_limb_t

UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)

size_t

UInt32 (on 32-bit CPU) / UInt64 (on 64-bit CPU)

Building the GNU MP Library on Windows
  1. Install MSYS2.

    On a 64-bit computer, install msys2-x86_64-20161025.exe, and on a 32-bit computer, install msys2-i686-20161025.exe. You can also check for a more recent version of MSYS2 here. Install MSYS2 to its default location.

    After installation, you need to updates MSYS2 packages. From the Windows Start Menu, start MSYS2 MSYS. In the shell command window, enter the command:

    • pacman -Syuu

    and follow instructions. You will have to close the command window, reopen a new one, and reenter the command pacman -Syuu.

    Finally, in order to build software, you need to install a number of packages with the command:

    • pacman -S --needed base-devel mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain git subversion mercurial mingw-w64-i686-cmake mingw-w64-x86_64-cmake

    run from the same command window as in the previous step.

    To build 32-bit software, use the MSYS2 MinGW 32-bit command from the Windows Start Menu, and for 64-bit software, use MSYS2 MinGW 64-bit.

  2. Install yasm.

    On a 64-bit computer, copy yasm-1.3.0-win64.exe to C:\msys64\usr\bin, and rename it to yasm.exe.

    Similarly on a 32-bit computer, copy yasm-1.3.0-win32.exe to C:\msys32\usr\bin, and rename it to yasm.exe.

  3. Build GNU MP.

    Create folders C:\Temp\x86 and C:\Temp\x64. These are the folder where the compiled 32-bit and 64-bit versions of GNU MP will be installed. Unzip gmp-6.1.2.tar.bz2 in folder C:\Temp. This puts GNU MP in subfolder gmp-6.1.2.

    In each one of the command windows openend with the commands MSYS2 MinGW 32-bit and MSYS2 MinGW 64-bit from the Windows Start Menu, run the commands below:

    • cd /c/Temp/gmp-6.1.2
      ./configure --enable-fat --disable-static --enable-shared --prefix=/c/Temp/x86 or x64
      make
      make check
      make install

    The --prefix specifies the install folder. Note that the Windows C:\ drive is specified as the root /C/ folder in the MinGW window. Note also that the configure and make commands are to be run against a fresly uncompressed GNU MP source. The make install command creates libgmp-10.dll in the C:\Temp\x86 and C:\Temp\x64 folders. These two compiled versions of the GNU MP library are to be copied to the x86 and x64 folders of the Math.Gmp.Native Visual Studio projects. They can also be copied directly into the x86 and x64 folders of the bin/Debug or bin/Release folders.

    The 32-bit and 64-bit make check commands generate some warnings, but all tests passed successfully.

Building the GNU MP Library for a Specific CPU Type on Windows

The --enable-fat build option above creates a library where optimized low level subroutines are chosen at runtime according to the CPU detected. By using instead the --host option, you can build a library for a specific CPU type. You will end up with a library that runs only on that CPU type, but the library will be samller. See the Build Options from the GNU MP Manual for the supported CPU types.

Using the GNU MP Library in a Visual Studio C++ Project

Although our main goal was to compile GNU MP in order to use it from .NET, the compiled 32-bit and 64-bit GNU MP libraries may be used directly in Visual Studio C++ projects. For example, create a default Visual Studio C++ Console Application. Set the Platform to x64. Copy from the C:\Temp\x64 folder the files include\gmp.h, bin\libgmp-10.dll, and lib\libgmp.dll.a to the Visual Studio C++ project folder. Include gmp.h in your C++ source file. In the Linker, Input Property Page of the project, add libgmp.dll.a to the Additional Dependencies. Build your C++ project, and copy libgmp-10.dll to the output bin folder. Run your application.

See ConsoleApplication12.zip for a sample Visual Studio C++ project.

See Also