using System; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; using System.Runtime.ConstrainedExecution; using System.Security; namespace Math.Gmp.Native { /// /// Represents all of the functions of the GNU MP library. /// /// /// ///

Functions Categories

///
///

Global Variable and Constants:

/// /// gmp_errno - Gets or sets the global GMP error number. /// gmp_version - The GMP version number in the form “i.j.k”. This release is "6.1.2". /// mp_bits_per_limb - The number of bits per limb. /// mp_bytes_per_limb - The number of bytes per limb. /// mp_uint_per_limb - The number of 32-bit, unsigned integers per limb. /// ///

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_set_str - Set the value of rop from str, a null-terminated C string in base base. /// ///

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_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 System.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 System.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:

///

Random State Initialization:

/// /// gmp_randinit_default - Initialize state with a default algorithm. /// gmp_randinit_mt - Initialize state for a Mersenne Twister algorithm. /// gmp_randinit_lc_2exp - Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp. /// gmp_randinit_lc_2exp_size - Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp. /// gmp_randinit_set - Initialize rop with a copy of the algorithm and state from op. /// gmp_randclear - Free all memory occupied by state. /// ///

Random State Seeding:

/// /// gmp_randseed - Set an initial seed value into state. /// gmp_randseed_ui - Set an initial seed value into state. /// ///

Random State Miscellaneous:

/// /// gmp_urandomb_ui - Generate a uniformly distributed random number of n bits, i.e. in the range 0 to 2^n - 1 inclusive. /// gmp_urandomm_ui - Generate a uniformly distributed random number in the range 0 to n - 1, inclusive. /// ///

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:

///

Formatted Input Functions:

/// /// gmp_scanf - Read from the standard input stdin. /// gmp_vscanf - Read from the standard input stdin. /// gmp_fscanf - Read from the stream fp. /// gmp_vfscanf - Read from the stream fp. /// gmp_sscanf - Read from a null-terminated string s. /// gmp_vsscanf - Read from a null-terminated string s. /// ///

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. /// 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. /// ///
public static class gmp_lib { // Safe handle to the loaded GMP library. private static SafeHandle _gmp_lib = new SafeHandle(_load_gmp_lib()); // Delegate to GMP dynamic unmanaged memory allocation function. private static allocate_function allocate_func_ptr; // Delegate to GMP dynamic unmanaged memory reallocation function. private static reallocate_function reallocate_func_ptr; // Delegate to GMP dynamic unmanaged memory free function. private static free_function free_func_ptr; private static IntPtr _load_gmp_lib() { // Load GMP library based on current computer x86 or x64 architecture. string folderName = IntPtr.Size == 4 ? "x86" : "x64"; // Get pathname of executing assembly. string codeBase = System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase; // Get directory pathname of GMP library. string libpath = System.IO.Path.GetDirectoryName(System.Uri.UnescapeDataString((new System.UriBuilder(codeBase)).Path)) + System.IO.Path.DirectorySeparatorChar + folderName; // Add GMP library directory to DLL search paths. SafeNativeMethods.SetDllDirectory(libpath); // Load GMP library and create safe handle to it. IntPtr handle = SafeNativeMethods.LoadLibrary(@"libgmp-10.dll"); // Retrieve and cache GMP dynamic memory allocation functions. _get_memory_functions(); return handle; } #region "Global variables." /// /// Gets or sets the global GMP error number. /// /// Global Variable and Constants /// GNU MP - Useful Macros and Constants public static int gmp_errno { get { return Marshal.ReadInt32(SafeNativeMethods.GetProcAddress(_gmp_lib.Handle, "__gmp_errno")); } set { Marshal.WriteInt32(SafeNativeMethods.GetProcAddress(_gmp_lib.Handle, "__gmp_errno"), value); } } /// /// The GMP version number in the form “i.j.k”. This release is "6.1.2". /// /// Global Variable and Constants /// GNU MP - Useful Macros and Constants /// /// /// string version = gmp_lib.gmp_version; /// Assert.AreEqual(version, "6.1.2"); /// /// /// Dim version As String = gmp_lib.gmp_version /// Assert.AreEqual(version, "6.1.2") /// /// public static readonly string gmp_version = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(SafeNativeMethods.GetProcAddress(_gmp_lib.Handle, "__gmp_version"))); /// /// The number of bits per limb. /// /// mp_bytes_per_limb /// mp_uint_per_limb /// Global Variable and Constants /// GNU MP - Useful Macros and Constants /// /// /// int bitsPerLimb = gmp_lib.mp_bits_per_limb; /// Assert.AreEqual(bitsPerLimb, IntPtr.Size * 8); /// /// /// Dim bitsPerLimb As Integer = gmp_lib.mp_bits_per_limb /// Assert.AreEqual(bitsPerLimb, IntPtr.Size * 8) /// /// public static readonly int mp_bits_per_limb = Marshal.ReadInt32(SafeNativeMethods.GetProcAddress(_gmp_lib.Handle, "__gmp_bits_per_limb")); /// /// The number of bytes per limb. /// /// mp_bits_per_limb /// mp_uint_per_limb /// Global Variable and Constants /// /// /// mp_size_t bytesPerLimb = gmp_lib.mp_bytes_per_limb; /// Assert.AreEqual(bytesPerLimb, (mp_size_t)IntPtr.Size); /// /// /// Dim bytesPerLimb As mp_size_t = gmp_lib.mp_bytes_per_limb /// Assert.AreEqual(bytesPerLimb, DirectCast(IntPtr.Size, mp_size_t)) /// /// public static readonly mp_size_t mp_bytes_per_limb = mp_bits_per_limb / 8; /// /// The number of 32-bit, unsigned integers per limb. /// /// mp_bits_per_limb /// mp_bytes_per_limb /// Global Variable and Constants /// /// /// mp_size_t uintsPerLimb = gmp_lib.mp_uint_per_limb; /// Assert.AreEqual(uintsPerLimb, (mp_size_t)(IntPtr.Size / 4)); /// /// /// Dim uintsPerLimb As mp_size_t = gmp_lib.mp_uint_per_limb /// Assert.AreEqual(uintsPerLimb, DirectCast(IntPtr.Size / 4, mp_size_t)) /// /// public static readonly mp_size_t mp_uint_per_limb = mp_bits_per_limb / 32; #endregion #region "Memory allocation functions." /// /// Return a pointer to newly allocated space with at least bytes. /// /// The minimum number of bytes to allocate. /// A pointer to newly allocated space with at least bytes. /// /// free /// reallocate /// Custom Allocation /// GNU MP - Custom Allocation public static void_ptr allocate(size_t alloc_size) { return allocate_func_ptr(alloc_size); } /// /// Resize a previously allocated block of bytes to be bytes. /// /// Pointer to previously allocated block. /// Number of bytes of previously allocated block. /// New number of bytes of previously allocated block. /// A previously allocated block ptr of bytes to be bytes. /// /// /// The block may be moved if necessary or if desired, and in that case the smaller of and /// bytes must be copied to the new location. /// The return value is a pointer to the resized block, that being the new location if moved or just if not. /// /// /// is never NULL, it’s always a previously allocated block. /// may be bigger or smaller than . /// /// /// The reallocate function parameter is passed for convenience, but of course it can be ignored /// if not needed by an implementation. The default functions using malloc and friends for instance don’t use it. /// /// /// allocate /// free /// Custom Allocation /// GNU MP - Custom Allocation public static void_ptr reallocate(void_ptr ptr, size_t old_size, size_t new_size) { return reallocate_func_ptr(ptr, old_size, new_size); } /// /// De-allocate the space pointed to by . /// /// Pointers to previously allocated memory. /// /// public static void free(params mp_ptr[] ptrs) { if (ptrs == null) throw new ArgumentNullException("ptrs"); foreach (mp_ptr p in ptrs) if (p.Size > 0) free_func_ptr(new void_ptr(p.ToIntPtr()), 0); } /// /// De-allocate the space pointed to by . /// /// Pointer to previously allocated memory. /// /// public static void free(gmp_randstate_t ptr) { if (ptr == null) throw new ArgumentNullException("ptr"); free_func_ptr(new void_ptr(ptr.ToIntPtr()), 0); } /// /// De-allocate the space pointed to by . /// /// Pointer to previously allocated memory. /// /// public static void free(char_ptr ptr) { free_func_ptr(new void_ptr(ptr.ToIntPtr()), 0); } /// /// De-allocate the space pointed to by . /// /// Pointer to previously allocated memory. /// /// public static void free(void_ptr ptr) { free_func_ptr(ptr, 0); } /// /// Free the unmanaged memory at . /// /// Pointer to unmanaged memory. public static void free(IntPtr ptr) { free_func_ptr(new void_ptr(ptr), 0); } /// /// De-allocate the space pointed to by . /// /// Pointer to previously allocated block. /// Number of bytes of previously allocated block. /// /// /// The free function parameter is passed for convenience, but of course it can be ignored /// if not needed by an implementation. The default functions using malloc and friends for instance don’t use it. /// /// /// allocate /// reallocate /// Custom Allocation /// GNU MP - Custom Allocation public static void free(void_ptr ptr, size_t size) { free_func_ptr(ptr, size); } /// /// Get the current allocation functions, storing function pointers to the locations given by the arguments. /// /// The memory allocation function. /// The memory reallocation function. /// The memory de-allocation function. /// mp_set_memory_functions /// Custom Allocation /// GNU MP - Custom Allocation /// /// /// allocate_function allocate; /// reallocate_function reallocate; /// free_function free; /// /// // Retrieve the GMP memory allocation functions. /// allocate = null; /// reallocate = null; /// free = null; /// gmp_lib.mp_get_memory_functions(ref allocate, ref reallocate, ref free); /// Assert.IsTrue(allocate != null && reallocate != null && free != null); /// /// // Allocate and free memory. /// void_ptr p = allocate(100); /// free(p, 100); /// /// /// Dim allocate As allocate_function /// Dim reallocate As reallocate_function /// Dim free As free_function /// /// ' Retrieve the GMP memory allocation functions. /// allocate = Nothing /// reallocate = Nothing /// free = Nothing /// gmp_lib.mp_get_memory_functions(allocate, reallocate, free) /// Assert.IsTrue(allocate IsNot Nothing AndAlso reallocate IsNot Nothing AndAlso free IsNot Nothing) /// /// ' Allocate and free memory. /// Dim p As void_ptr = allocate(100) /// free(p, 100) /// /// public static void mp_get_memory_functions(ref allocate_function alloc_func_ptr, ref reallocate_function realloc_func_ptr, ref free_function free_func_ptr) { alloc_func_ptr = gmp_lib.allocate_func_ptr; realloc_func_ptr = gmp_lib.reallocate_func_ptr; free_func_ptr = gmp_lib.free_func_ptr; } /// /// Replace the current allocation functions from the arguments. /// /// The new memory allocation function. /// The new memory reallocation function. /// The new memory de-allocation function. /// /// /// If an argument is null (Nothing in VB.NET), the corresponding /// default function is used. /// /// /// mp_get_memory_functions /// Custom Allocation /// GNU MP - Custom Allocation /// /// /// // Retrieve GMP default memory allocation functions. /// allocate_function default_allocate = null; /// reallocate_function default_reallocate = null; /// free_function default_free = null; /// gmp_lib.mp_get_memory_functions(ref default_allocate, ref default_reallocate, ref default_free); /// /// // Create and set new memory allocation functions that count the number of times they are called. /// int counter = 0; /// allocate_function new_allocate = (size_t alloc_size) => { counter++; return default_allocate(alloc_size); }; /// reallocate_function new_reallocate = (void_ptr ptr, size_t old_size, size_t new_size) => { counter++; return default_reallocate(ptr, old_size, new_size); }; /// free_function new_free = (void_ptr ptr, size_t size) => { counter++; default_free(ptr, size); }; /// gmp_lib.mp_set_memory_functions(new_allocate, new_reallocate, new_free); /// /// // Retrieve GMP memory allocation functions. /// allocate_function allocate = null; /// reallocate_function reallocate = null; /// free_function free = null; /// gmp_lib.mp_get_memory_functions(ref allocate, ref reallocate, ref free); /// /// // Call memory function and assert calls count. /// void_ptr p = allocate(10); /// Assert.IsTrue(counter == 1); /// /// reallocate(p, 10, 20); /// Assert.IsTrue(counter == 2); /// /// free(p, 20); /// Assert.IsTrue(counter == 3); /// /// // Restore default memory allocation functions. /// gmp_lib.mp_set_memory_functions(null, null, null); /// /// /// ' Retrieve GMP default memory allocation functions. /// Dim default_allocate As allocate_function = Nothing /// Dim default_reallocate As reallocate_function = Nothing /// Dim default_free As free_function = Nothing /// gmp_lib.gmp_get_memory_functions(default_allocate, default_reallocate, default_free) /// /// ' Create and set new memory allocation functions that count the number of times they are called. /// Dim counter As Integer = 0 /// Dim new_allocate As allocate_function = /// Function(alloc_size As size_t) /// counter += 1 /// Return default_allocate(alloc_size) /// End Function /// Dim new_reallocate As reallocate_function = /// Function(ptr As void_ptr, old_size As size_t, new_size As size_t) /// counter += 1 /// Return default_reallocate(ptr, old_size, new_size) /// End Function /// Dim new_free As free_function = /// Function(ptr As void_ptr, size As size_t) /// counter += 1 /// default_free(ptr, size) /// End Function /// gmp_lib.gmp_set_memory_functions(new_allocate, new_reallocate, new_free) /// /// ' Retrieve GMP memory allocation functions. /// Dim allocate As allocate_function = Nothing /// Dim reallocate As reallocate_function = Nothing /// Dim free As free_function = Nothing /// gmp_lib.gmp_get_memory_functions(allocate, reallocate, free) /// /// ' Call memory function and assert calls count. /// Dim p As void_ptr = allocate(10) /// Assert.IsTrue(counter = 1) /// /// reallocate(p, 10, 20) /// Assert.IsTrue(counter = 2) /// /// free(p, 20) /// Assert.IsTrue(counter = 3) /// /// ' Restore default memory allocation functions. /// gmp_lib.gmp_set_memory_functions(Nothing, Nothing, Nothing) /// /// public static void mp_set_memory_functions(allocate_function alloc_func_ptr, reallocate_function realloc_func_ptr, free_function free_func_ptr) { IntPtr allocate = IntPtr.Zero; IntPtr reallocate = IntPtr.Zero; IntPtr free = IntPtr.Zero; if (IntPtr.Size == 4) { if (alloc_func_ptr != null) allocate = Marshal.GetFunctionPointerForDelegate((_allocate_function_x86)((uint alloc_size) => { return alloc_func_ptr((size_t)alloc_size).ToIntPtr(); })); if (realloc_func_ptr != null) reallocate = Marshal.GetFunctionPointerForDelegate((_reallocate_function_x86)((IntPtr ptr, uint old_size, uint new_size) => { return realloc_func_ptr(new void_ptr(ptr), (size_t)old_size, (size_t)new_size).ToIntPtr(); })); if (free_func_ptr != null) free = Marshal.GetFunctionPointerForDelegate((_free_function_x86)((IntPtr ptr, uint size) => { free_func_ptr(new void_ptr(ptr), (size_t)size); })); } else { if (alloc_func_ptr != null) allocate = Marshal.GetFunctionPointerForDelegate((_allocate_function_x64)((ulong alloc_size) => { return alloc_func_ptr((size_t)alloc_size).ToIntPtr(); })); if (realloc_func_ptr != null) reallocate = Marshal.GetFunctionPointerForDelegate((_reallocate_function_x64)((IntPtr ptr, ulong old_size, ulong new_size) => { return realloc_func_ptr(new void_ptr(ptr), (size_t)old_size, (size_t)new_size).ToIntPtr(); })); if (free_func_ptr != null) free = Marshal.GetFunctionPointerForDelegate((_free_function_x64)((IntPtr ptr, ulong size) => { free_func_ptr(new void_ptr(ptr), (size_t)size); })); } SafeNativeMethods.__gmp_set_memory_functions(allocate, reallocate, free); _get_memory_functions(); } private static void _get_memory_functions() { // Cache dynamic memory allocation functions. IntPtr allocate = IntPtr.Zero; IntPtr reallocate = IntPtr.Zero; IntPtr free = IntPtr.Zero; SafeNativeMethods.__gmp_get_memory_functions(ref allocate, ref reallocate, ref free); _get_memory_function(allocate, ref gmp_lib.allocate_func_ptr); _get_memory_function(reallocate, ref gmp_lib.reallocate_func_ptr); _get_memory_function(free, ref gmp_lib.free_func_ptr); } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr _allocate_function_x86(uint alloc_size); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr _allocate_function_x64(ulong alloc_size); private static void _get_memory_function(IntPtr allocate, ref allocate_function alloc_func_ptr) { if (IntPtr.Size == 4) alloc_func_ptr = (size_t alloc_size) => { return new void_ptr(((_allocate_function_x86)Marshal.GetDelegateForFunctionPointer(allocate, typeof(_allocate_function_x86)))((uint)alloc_size)); }; else alloc_func_ptr = (size_t alloc_size) => { return new void_ptr(((_allocate_function_x64)Marshal.GetDelegateForFunctionPointer(allocate, typeof(_allocate_function_x64)))((ulong)alloc_size)); }; } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr _reallocate_function_x86(IntPtr ptr, uint old_size, uint new_size); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr _reallocate_function_x64(IntPtr ptr, ulong old_size, ulong new_size); private static void _get_memory_function(IntPtr reallocate, ref reallocate_function realloc_func_ptr) { if (IntPtr.Size == 4) realloc_func_ptr = (ptr, old_size, new_size) => { return ptr.FromIntPtr(((_reallocate_function_x86)Marshal.GetDelegateForFunctionPointer(reallocate, typeof(_reallocate_function_x86)))(ptr.ToIntPtr(), (uint)old_size, (uint)new_size)); }; else realloc_func_ptr = (ptr, old_size, new_size) => { return ptr.FromIntPtr(((_reallocate_function_x64)Marshal.GetDelegateForFunctionPointer(reallocate, typeof(_reallocate_function_x64)))(ptr.ToIntPtr(), (ulong)old_size, (ulong)new_size)); }; } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void _free_function_x86(IntPtr ptr, uint size); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void _free_function_x64(IntPtr ptr, ulong size); private static void _get_memory_function(IntPtr free, ref free_function free_func_ptr) { if (IntPtr.Size == 4) free_func_ptr = (ptr, size) => { ((_free_function_x86)Marshal.GetDelegateForFunctionPointer(free, typeof(_free_function_x86)))(ptr.ToIntPtr(), (uint)size); }; else free_func_ptr = (ptr, size) => { ((_free_function_x64)Marshal.GetDelegateForFunctionPointer(free, typeof(_free_function_x64)))(ptr.ToIntPtr(), (ulong)size); }; } /// /// The ZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled. /// /// A pointer to the memory block to be filled with zeros. /// The number of bytes to fill with zeros. public static void ZeroMemory(IntPtr dst, int length) { SafeNativeMethods.RtlZeroMemory(dst, length); } #endregion #region "Random number routines." /// /// Initialize with a default algorithm. /// /// The state to initialize. /// /// /// This will be a compromise between speed and randomness, /// and is recommended for applications with no special requirements. /// Currently this is gmp_randinit_mt. /// /// /// gmp_randclear /// gmp_randinit_lc_2exp /// gmp_randinit_lc_2exp_size /// gmp_randinit_mt /// gmp_randinit_set /// Random State Initialization /// GNU MP - Random State Initialization /// /// /// // Create new random number generator state. /// gmp_randstate_t state = new gmp_randstate_t(); /// /// // Initialize state with default random number generator algorithm. /// gmp_lib.gmp_randinit_default(state); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create new random number generator state. /// Dim state As New gmp_randstate_t() /// /// ' Initialize state with default random number generator algorithm. /// gmp_lib.gmp_randinit_default(state) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static void gmp_randinit_default(gmp_randstate_t state) { if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmp_randinit_default(state.ToIntPtr()); } /// /// Initialize with a linear congruential algorithm X = (X + ) mod 2^. /// /// The state to initialize. /// Parameter of the algorithm. /// Parameter of the algorithm. /// Parameter of the algorithm. /// /// /// The low bits of X in this algorithm are not very random. /// The least significant bit will have a period no more than 2, and the second bit /// no more than 4, etc. For this reason only the high half of each X is actually used. /// /// /// When a random number of more than / 2 bits is to be generated, /// multiple iterations of the recurrence are used and the results concatenated. /// /// /// gmp_randclear /// gmp_randinit_default /// gmp_randinit_lc_2exp_size /// gmp_randinit_mt /// gmp_randinit_set /// Random State Initialization /// GNU MP - Random State Initialization /// /// /// // Create new random number generator state. /// gmp_randstate_t state = new gmp_randstate_t(); /// /// // Initialize state with a linear congruential random number generator algorithm. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 100000U); /// gmp_lib.gmp_randinit_lc_2exp(state, a, 13, 300); /// /// // Free all memory occupied by state and a. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpz_clear(a); /// /// /// ' Create new random number generator state. /// Dim state As New gmp_randstate_t() /// /// ' Initialize state with a linear congruential random number generator algorithm. /// Dim a As New mpz_t() /// /// gmp_lib.mpz_init_set_ui(a, 100000UI) /// gmp_lib.gmp_randinit_lc_2exp(state, a, 13, 300) /// /// ' Free all memory occupied by state and a. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpz_clear(a) /// /// public static void gmp_randinit_lc_2exp(gmp_randstate_t state, /*const*/ mpz_t a, uint /*unsigned long int*/ c, mp_bitcnt_t m2exp) { if (state == null) throw new ArgumentNullException("state"); if (a == null) throw new ArgumentNullException("a"); SafeNativeMethods.__gmp_randinit_lc_2exp(state.ToIntPtr(), a.ToIntPtr(), c, m2exp); } /// /// Initialize for a linear congruential algorithm as per gmp_randinit_lc_2exp. /// /// The state to initialize. /// /// If successful the return value is non-zero. If is bigger than the table data provides then the return value is zero. /// /// /// a, c and m2exp are selected from a table, chosen so that /// bits (or more) of each X will be used, i.e. m2exp / 2 ≥ . /// /// /// gmp_randclear /// gmp_randinit_default /// gmp_randinit_lc_2exp /// gmp_randinit_mt /// gmp_randinit_set /// Random State Initialization /// GNU MP - Random State Initialization /// /// /// // Create new random number generator state. /// gmp_randstate_t state = new gmp_randstate_t(); /// /// // Initialize state with a linear congruential random number generator algorithm. /// gmp_lib.gmp_randinit_lc_2exp_size(state, 30); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create new random number generator state. /// Dim state As New gmp_randstate_t() /// /// ' Initialize state with a linear congruential random number generator algorithm. /// gmp_lib.gmp_randinit_lc_2exp_size(state, 30) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static int gmp_randinit_lc_2exp_size(gmp_randstate_t state, mp_bitcnt_t size) { if (state == null) throw new ArgumentNullException("state"); return SafeNativeMethods.__gmp_randinit_lc_2exp_size(state.ToIntPtr(), size); } /// /// Initialize for a Mersenne Twister algorithm. /// /// The state to initialize. /// /// /// This algorithm is fast and has good randomness properties. /// /// /// gmp_randclear /// gmp_randinit_default /// gmp_randinit_lc_2exp /// gmp_randinit_lc_2exp_size /// gmp_randinit_set /// Random State Initialization /// GNU MP - Random State Initialization /// /// /// // Create new random number generator state. /// gmp_randstate_t state = new gmp_randstate_t(); /// /// // Initialize state with Mersenne Twister random number generator algorithm. /// gmp_lib.gmp_randinit_mt(state); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create new random number generator state. /// Dim state As New gmp_randstate_t() /// /// ' Initialize state with Mersenne Twister random number generator algorithm. /// gmp_lib.gmp_randinit_mt(state) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static void gmp_randinit_mt(gmp_randstate_t state) { if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmp_randinit_mt(state.ToIntPtr()); } /// /// Initialize with a copy of the algorithm and state from . /// /// The state to initialize. /// The source state. /// gmp_randclear /// gmp_randinit_default /// gmp_randinit_lc_2exp /// gmp_randinit_lc_2exp_size /// gmp_randinit_mt /// Random State Initialization /// GNU MP - Random State Initialization /// /// /// // Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// gmp_randstate_t op = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(op); /// /// // Create new random number generator state, and initialize it with the state op. /// gmp_randstate_t rop = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_set(rop, op); /// /// // Free all memory occupied by op and rop. /// gmp_lib.gmp_randclear(op); /// gmp_lib.gmp_randclear(rop); /// /// /// ' Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// Dim op As New gmp_randstate_t() /// /// gmp_lib.gmp_randinit_mt(op) /// /// ' Create new random number generator state, and initialize it with the state op. /// Dim rop As New gmp_randstate_t() /// /// gmp_lib.gmp_randinit_set(rop, op) /// /// ' Free all memory occupied by op and rop. /// gmp_lib.gmp_randclear(op) /// gmp_lib.gmp_randclear(rop) /// /// public static void gmp_randinit_set(gmp_randstate_t rop, /*const*/ gmp_randstate_t /*__gmp_randstate_struct **/ op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmp_randinit_set(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set an initial value into . /// /// The state to seed. /// The seed. /// /// /// The size of a seed determines how many different sequences of random numbers that it’s possible to /// generate. The “quality” of the seed is the randomness of a given seed compared to the previous seed /// used, and this affects the randomness of separate number sequences. The method for choosing a seed /// is critical if the generated numbers are to be used for important applications, such as generating /// cryptographic keys. /// /// /// Traditionally the system time has been used to seed, but care needs to be taken with this. If an /// application seeds often and the resolution of the system clock is low, then the same sequence of /// numbers might be repeated. Also, the system time is quite easy to guess, so if unpredictability is /// required then it should definitely not be the only source for the seed value. On some systems /// there’s a special device /dev/random which provides random data better suited for use as a seed. /// /// /// gmp_randseed_ui /// Random State Seeding /// GNU MP - Random State Seeding /// /// /// // Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// /// // Seed random number generator. /// mpz_t seed = new mpz_t(); /// gmp_lib.mpz_init_set_ui(seed, 100000U); /// gmp_lib.gmp_randseed(state, seed); /// /// // Free all memory occupied by state and seed. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpz_clear(seed); /// /// /// ' Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// Dim state As New gmp_randstate_t() /// /// gmp_lib.gmp_randinit_mt(state) /// /// ' Seed random number generator. /// Dim seed As New mpz_t() /// /// gmp_lib.mpz_init_set_ui(seed, 100000UI) /// gmp_lib.gmp_randseed(state, seed) /// /// ' Free all memory occupied by state and seed. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpz_clear(seed) /// /// public static void gmp_randseed(gmp_randstate_t state, /*const*/ mpz_t seed) { if (state == null) throw new ArgumentNullException("state"); if (seed == null) throw new ArgumentNullException("seed"); SafeNativeMethods.__gmp_randseed(state.ToIntPtr(), seed.ToIntPtr()); } /// /// Set an initial value into . /// /// The state to seed. /// The seed. /// /// /// The size of a seed determines how many different sequences of random numbers that it’s possible to /// generate. The “quality” of the seed is the randomness of a given seed compared to the previous seed /// used, and this affects the randomness of separate number sequences. The method for choosing a seed /// is critical if the generated numbers are to be used for important applications, such as generating /// cryptographic keys. /// /// /// Traditionally the system time has been used to seed, but care needs to be taken with this. If an /// application seeds often and the resolution of the system clock is low, then the same sequence of /// numbers might be repeated. Also, the system time is quite easy to guess, so if unpredictability is /// required then it should definitely not be the only source for the seed value. On some systems /// there’s a special device /dev/random which provides random data better suited for use as a seed. /// /// /// gmp_randseed /// Random State Seeding /// GNU MP - Random State Seeding /// /// /// // Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// /// // Seed random number generator. /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create new random number generator state, and initialize state with the Mersenne Twister algorithm. /// Dim state As New gmp_randstate_t() /// /// gmp_lib.gmp_randinit_mt(state) /// /// ' Seed random number generator. /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static void gmp_randseed_ui(gmp_randstate_t state, uint /*unsigned long int*/ seed) { if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmp_randseed_ui(state.ToIntPtr(), seed); } /// /// Free all memory occupied by . /// /// A state. /// gmp_randinit_default /// gmp_randinit_lc_2exp /// gmp_randinit_lc_2exp_size /// gmp_randinit_mt /// gmp_randinit_set /// Random State Initialization /// GNU MP - Random State Initialization public static void gmp_randclear(gmp_randstate_t state) { if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmp_randclear(state.ToIntPtr()); gmp_lib.free(state); } /// /// Generate a uniformly distributed random number of bits, i.e. in the range 0 to 2^ - 1 inclusive. /// /// The state of the random number generator to use. /// The numbe rof bits. /// The generated random number. /// /// /// must be less than or equal to the number of bits in an unsigned long. /// /// /// In .NET, must be less than or equal to the number of bits in an unsigned 32-bit integer. /// /// /// gmp_urandomm_ui /// Random State Miscellaneous /// GNU MP - Random State Miscellaneous /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Generate a random integer in the range [0, 2^8-1]. /// uint rand = gmp_lib.gmp_urandomb_ui(state, 8); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Generate a random integer in the range [0, 2^8-1]. /// Dim rand As UInteger = gmp_lib.gmp_urandomb_ui(state, 8) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static uint /*unsigned long int*/ gmp_urandomb_ui(gmp_randstate_t state, uint /*unsigned long int*/ n) { if (state == null) throw new ArgumentNullException("state"); return SafeNativeMethods.__gmp_urandomb_ui(state.ToIntPtr(), n); } /// /// Generate a uniformly distributed random number in the range 0 to - 1, inclusive. /// /// The state of the random number generator to use. /// The upper bound of the range. /// The generated random number. /// gmp_urandomb_ui /// Random State Miscellaneous /// GNU MP - Random State Miscellaneous /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 1000U); /// /// // Generate a random integer in the range [0, 8-1]. /// uint rand = gmp_lib.gmp_urandomm_ui(state, 8); /// /// // Free all memory occupied by state. /// gmp_lib.gmp_randclear(state); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 1000UI) /// /// ' Generate a random integer in the range [0, 8-1]. /// Dim rand As UInteger = gmp_lib.gmp_urandomm_ui(state, 8) /// /// ' Free all memory occupied by state. /// gmp_lib.gmp_randclear(state) /// /// public static uint /*unsigned long int*/ gmp_urandomm_ui(gmp_randstate_t state, uint /*unsigned long int*/ n) { if (state == null) throw new ArgumentNullException("state"); return SafeNativeMethods.__gmp_urandomm_ui(state.ToIntPtr(), n); } #endregion #region "Formatted output routines." /// /// Form a null-terminated string in a block of memory obtained from the current memory allocation function. /// /// Pointer to returned, allocated string. /// Format string. See Formatted Output Strings. /// Arguments. /// The return value is the number of characters produced, excluding the null-terminator. /// /// /// The block will be the size of the string and null-terminator. The address of the block in stored to . /// /// /// Unlike the C library asprintf, gmp_asprintf doesn’t return -1 if there’s no more memory available, /// it lets the current allocation function handle that. /// /// /// gmp_snprintf /// gmp_sprintf /// gmp_vasprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Create pointer to unmanaged character string pointer. /// ptr<char_ptr> str = new ptr<char_ptr>(); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to newly allocated unmanaged memory string. /// Assert.IsTrue(gmp_lib.gmp_asprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.Value.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str.Value); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Create pointer to unmanaged character string pointer. /// Dim str As New ptr(Of char_ptr)() /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to newly allocated unmanaged memory string. /// Assert.IsTrue(gmp_lib.gmp_asprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.Value.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str.Value) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_asprintf(ptr /*char ***/ pp, /*const*/ string /*char **/ fmt, params object[] /*...*/ args) { return gmp_vasprintf(pp, fmt, args); } /// /// Print to the stream . /// /// File stream. /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, or -1 if an error occurred. /// gmp_printf /// gmp_sprintf /// gmp_vfprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Create unique file pathname and a file pointer. /// string pathname = System.IO.Path.GetTempFileName(); /// ptr<FILE> stream = new ptr<FILE>(); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Open file stream and print to it. /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// Assert.IsTrue(gmp_lib.gmp_fprintf(stream, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// fclose(stream.Value.Value); /// Assert.IsTrue(System.IO.File.ReadAllText(pathname) == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// /// ' Create unique file pathname and a file pointer. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// Dim stream As New ptr(Of FILE)() /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Open file stream and print to it. /// _wfopen_s(stream.Value.Value, pathname, "w") /// Assert.IsTrue(gmp_lib.gmp_fprintf(stream, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// fclose(stream.Value.Value) /// Assert.IsTrue(System.IO.File.ReadAllText(pathname) = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_fprintf(ptr /*FILE **/ fp, /*const*/ string /*char **/ fmt, params object[] /*...*/ args) { return gmp_vfprintf(fp, fmt, args); } //#define gmp_obstack_printf __gmp_obstack_printf //#if defined (_GMP_H_HAVE_OBSTACK) //__GMP_DECLSPEC int gmp_obstack_printf (struct obstack *, const char *, ...); //#endif //#define gmp_obstack_vprintf __gmp_obstack_vprintf //#if defined (_GMP_H_HAVE_OBSTACK) && defined (_GMP_H_HAVE_VA_LIST) //__GMP_DECLSPEC int gmp_obstack_vprintf (struct obstack *, const char *, va_list); //#endif /// /// Print to the standard output stdout. /// /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, or -1 if an error occurred. /// gmp_fprintf /// gmp_sprintf /// gmp_vprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to standard output. /// Assert.IsTrue(gmp_lib.gmp_printf("%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to standard output. /// Assert.IsTrue(gmp_lib.gmp_printf("%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_printf(/*const*/ string /*char **/ fmt, params object[] /*...*/ args) { return gmp_vprintf(fmt, args); } /// /// Form a null-terminated string in . /// /// The string to print to. /// The maximum number of bytes to write. /// Format string. See Formatted Output Strings. /// Arguments. /// The return value is the total number of characters which ought to have been produced, excluding the terminating null. If retval ≥ then the actual output has been truncated to the first - 1 characters, and a null appended. /// /// /// No more than bytes will be written. To get the full output, must be enough for the string and null-terminator. /// /// /// No overlap is permitted between the region {,} and the string. /// /// /// Notice the return value is in ISO C99 snprintf style. This is so even if the C library vsnprintf is the older GLIBC 2.0.x style. /// /// /// gmp_asprintf /// gmp_sprintf /// gmp_vsnprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Allocate unmanaged string with 50 characters. /// char_ptr str = new char_ptr("................................................."); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to string. /// Assert.IsTrue(gmp_lib.gmp_snprintf(str, 50, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Allocate unmanaged string with 50 characters. /// Dim str As New char_ptr(".................................................") /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to string. /// Assert.IsTrue(gmp_lib.gmp_snprintf(str, 50, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_snprintf(char_ptr /*char **/ buf, size_t size, /*const*/ string /*char **/ fmt, params object[] /*...*/ args) { return gmp_vsnprintf(buf, size, fmt, args); } /// /// Form a null-terminated string in . /// /// The string to print to. /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, excluding the terminating null. /// /// /// No overlap is permitted between the space at and the string . /// /// /// These functions are not recommended, since there’s no protection against exceeding the space available at . /// /// /// gmp_asprintf /// gmp_printf /// gmp_fprintf /// gmp_snprintf /// gmp_vsprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Allocate unmanaged string with 50 characters. /// char_ptr str = new char_ptr("................................................."); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to string. /// Assert.IsTrue(gmp_lib.gmp_sprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Allocate unmanaged string with 50 characters. /// Dim str As New char_ptr(".................................................") /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to string. /// Assert.IsTrue(gmp_lib.gmp_sprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_sprintf(char_ptr /*char **/ buf, /*const*/ string /*char **/ fmt, params object[] /*...*/ args) { return gmp_vsprintf(buf, fmt, args); } /// /// Form a null-terminated string in a block of memory obtained from the current memory allocation function. /// /// /// Format string. See Formatted Output Strings. /// Arguments. /// The return value is the number of characters produced, excluding the null-terminator. /// /// /// The block will be the size of the string and null-terminator. The address of the block in stored to . /// /// /// Unlike the C library vasprintf, gmp_vasprintf doesn’t return -1 if there’s no more memory available, /// it lets the current allocation function handle that. /// /// /// gmp_asprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Create pointer to unmanaged character string pointer. /// ptr<char_ptr> str = new ptr<char_ptr>(); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to newly allocated unmanaged memory string. /// Assert.IsTrue(gmp_lib.gmp_vasprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.Value.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str.Value); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Create pointer to unmanaged character string pointer. /// Dim str As New ptr(Of char_ptr)() /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to newly allocated unmanaged memory string. /// Assert.IsTrue(gmp_lib.gmp_vasprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.Value.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str.Value) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vasprintf(ptr /*char ***/ ptr, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (ptr == null) throw new ArgumentNullException("ptr"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vasprintf(ref ptr.Value.Pointer, format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Print to the stream . /// /// File stream. /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, or -1 if an error occurred. /// gmp_fprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Create unique file pathname and a file pointer. /// string pathname = System.IO.Path.GetTempFileName(); /// ptr<FILE> stream = new ptr<FILE>(); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Open file stream and print to it. /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// Assert.IsTrue(gmp_lib.gmp_vfprintf(stream, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// fclose(stream.Value.Value); /// Assert.IsTrue(System.IO.File.ReadAllText(pathname) == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// /// ' Create unique file pathname and a file pointer. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// Dim stream As New ptr(Of FILE)() /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Open file stream and print to it. /// _wfopen_s(stream.Value.Value, pathname, "w") /// Assert.IsTrue(gmp_lib.gmp_vfprintf(stream, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// fclose(stream.Value.Value) /// Assert.IsTrue(System.IO.File.ReadAllText(pathname) = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vfprintf(ptr /*FILE **/ fp, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (fp == null) throw new ArgumentNullException("fp"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vfprintf(fp.Value.Value, format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Print to the standard output stdout. /// /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, or -1 if an error occurred. /// gmp_printf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to standard output. /// Assert.IsTrue(gmp_lib.gmp_vprintf("%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to standard output. /// Assert.IsTrue(gmp_lib.gmp_vprintf("%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vprintf(/*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vprintf(format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Form a null-terminated string in . /// /// The string to print to. /// The maximum number of bytes to write. /// Format string. See Formatted Output Strings. /// Arguments. /// The return value is the total number of characters which ought to have been produced, excluding the terminating null. If retval ≥ then the actual output has been truncated to the first - 1 characters, and a null appended. /// /// /// No more than bytes will be written. To get the full output, must be enough for the string and null-terminator. /// /// /// No overlap is permitted between the regiom {,} and the string. /// /// /// Notice the return value is in ISO C99 snprintf style. This is so even if the C library vsnprintf is the older GLIBC 2.0.x style. /// /// /// gmp_snprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Allocate unmanaged string with 50 characters. /// char_ptr str = new char_ptr("................................................."); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to string. /// Assert.IsTrue(gmp_lib.gmp_vsnprintf(str, 50, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Allocate unmanaged string with 50 characters. /// Dim str As New char_ptr(".................................................") /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to string. /// Assert.IsTrue(gmp_lib.gmp_vsnprintf(str, 50, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vsnprintf(char_ptr /*char **/ buf, size_t size, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (buf == null) throw new ArgumentNullException("buf"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result; if (IntPtr.Size == 4) result = SafeNativeMethods.__gmp_vsnprintf_x86(buf.ToIntPtr(), (uint)size, format.ToIntPtr(), va_args.ToIntPtr()); else result = SafeNativeMethods.__gmp_vsnprintf_x64(buf.ToIntPtr(), (ulong)size, format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Form a null-terminated string in . /// /// The string to print to. /// Format string. See Formatted Output Strings. /// Arguments. /// Return the number of characters written, excluding the terminating null. /// /// /// No overlap is permitted between the space at and the string . /// /// /// These functions are not recommended, since there’s no protection against exceeding the space available at . /// /// /// gmp_sprintf /// Formatted Output Functions /// GNU MP - Formatted Output Functions /// GNU MP - Formatted Output Strings /// /// /// // Allocate unmanaged string with 50 characters. /// char_ptr str = new char_ptr("................................................."); /// /// mpz_t z = "123456"; /// mpq_t q = "123/456"; /// mpf_t f = "12345e6"; /// mp_limb_t m = 123456; /// /// // Print to string. /// Assert.IsTrue(gmp_lib.gmp_vsprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) == 42); /// Assert.IsTrue(str.ToString() == "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100"); /// /// // Release unmanaged memory. /// gmp_lib.free(str); /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Allocate unmanaged string with 50 characters. /// Dim str As New char_ptr(".................................................") /// /// Dim z As mpz_t = "123456" /// Dim q As mpq_t = "123/456" /// Dim f As mpf_t = "12345e6" /// Dim m As mp_limb_t = 123456 /// /// ' Print to string. /// Assert.IsTrue(gmp_lib.gmp_vsprintf(str, "%Zd - %QX - %Fa - %Mo", z, q, f, m) = 42) /// Assert.IsTrue(str.ToString() = "123456 - 7B/1C8 - 0x2.dfd1c04p+32 - 361100") /// /// ' Release unmanaged memory. /// gmp_lib.free(str) /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vsprintf(char_ptr /*char **/ buf, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (buf == null) throw new ArgumentNullException("buf"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vsprintf(buf.ToIntPtr(), format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } #endregion #region "Formatted input routines." /// /// Read from the stream . /// /// File stream. /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_scanf /// gmp_sscanf /// gmp_vfscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// // Create unique filename and stream pointer. /// string pathname = System.IO.Path.GetTempFileName(); /// ptr<FILE> stream = new ptr<FILE>(); /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// // Write string to file, and then read values from it. /// System.IO.File.WriteAllText(pathname, "123456 7B/1C8 1.234500e+10 A 10 1.000000"); /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.gmp_fscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// fclose(stream.Value.Value); /// /// // Assert values read. /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Create unique filename and stream pointer. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// Dim stream As New ptr(Of FILE)() /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// ' Write string to file, and then read values from it. /// System.IO.File.WriteAllText(pathname, "123456 7B/1C8 1.234500e+10 A 10 1.000000") /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.gmp_fscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// fclose(stream.Value.Value) /// /// ' Assert values read. /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_fscanf(ptr /*FILE **/ fp, /*const*/ string /*char **/ fmt, params object[] /*...*/ ap) { return gmp_vfscanf(fp, fmt, ap); } /// /// Read from the standard input stdin. /// /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_fscanf /// gmp_sscanf /// gmp_vscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// // Read values from standard input. /// Assert.IsTrue(gmp_lib.gmp_scanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// /// // Assert values read. /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// ' Read values from standard input. /// Assert.IsTrue(gmp_lib.gmp_scanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// /// ' Assert values read. /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_scanf(/*const*/ string /*char **/ fmt, params object[] /*...*/ ap) { return gmp_vscanf(fmt, ap); } /// /// Read from a null-terminated string . /// /// A string. /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_fscanf /// gmp_scanf /// gmp_vsscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// Assert.IsTrue(gmp_lib.gmp_sscanf("123456 7B/1C8 1.234500e+10 A 10 1.000000", "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// Assert.IsTrue(gmp_lib.gmp_sscanf("123456 7B/1C8 1.234500e+10 A 10 1.000000", "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_sscanf(/*const*/ string /*char **/ s, /*const*/ string /*char **/ fmt, params object[] /*...*/ ap) { return gmp_vsscanf(s, fmt, ap); } /// /// Read from the stream . /// /// File stream. /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_fscanf /// gmp_vscanf /// gmp_vsscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// // Create unique filename and stream pointer. /// string pathname = System.IO.Path.GetTempFileName(); /// ptr<FILE> stream = new ptr<FILE>(); /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// // Write string to file, and then read values from it. /// System.IO.File.WriteAllText(pathname, "123456 7B/1C8 1.234500e+10 A 10 1.000000"); /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.gmp_vfscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// fclose(stream.Value.Value); /// /// // Assert values read. /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// ' Create unique filename and stream pointer. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// Dim stream As New ptr(Of FILE)() /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// ' Write string to file, and then read values from it. /// System.IO.File.WriteAllText(pathname, "123456 7B/1C8 1.234500e+10 A 10 1.000000") /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.gmp_vfscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// fclose(stream.Value.Value) /// /// ' Assert values read. /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vfscanf(ptr /*FILE **/ fp, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (fp == null) throw new ArgumentNullException("fp"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vfscanf(fp.Value.Value, format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Read from the standard input stdin. /// /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_scanf /// gmp_vfscanf /// gmp_vsscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// // Read values from standard input. /// Assert.IsTrue(gmp_lib.gmp_vscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// /// // Assert values read. /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// ' Read values from standard input. /// Assert.IsTrue(gmp_lib.gmp_vscanf(stream, "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// /// ' Assert values read. /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vscanf(/*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); int result = SafeNativeMethods.__gmp_vscanf(format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); return result; } /// /// Read from a null-terminated string . /// /// A string. /// Format string. See Formatted Input Strings. /// Arguments. /// The return value the number of fields successfully parsed and stored. ‘%n’ fields and fields read but suppressed by ‘*’ don’t count towards the return value. /// gmp_sscanf /// gmp_vfscanf /// gmp_vscanf /// Formatted Input Functions /// GNU MP - Formatted Input Functions /// GNU MP - Formatted Input Strings /// /// /// mpz_t z = "0"; /// mpq_t q = "0"; /// mpf_t f = "0"; /// ptr<Char> c = new ptr<Char>('0'); /// ptr<mp_size_t> zt = new ptr<mp_size_t>(0); /// ptr<Double> dbl = new ptr<Double>(0); /// /// Assert.IsTrue(gmp_lib.gmp_vsscanf("123456 7B/1C8 1.234500e+10 A 10 1.000000", "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) == 6); /// /// Assert.IsTrue(z.ToString() == "123456"); /// Assert.IsTrue(q.ToString() == "123/456"); /// Assert.IsTrue(f.ToString() == "0.12345e11"); /// Assert.IsTrue(c.Value == 'A'); /// Assert.IsTrue(zt.Value == 10); /// Assert.IsTrue(dbl.Value == 1.0); /// /// // Release unmanaged memory. /// gmp_lib.mpz_clear(z); /// gmp_lib.mpq_clear(q); /// gmp_lib.mpf_clear(f); /// /// /// Dim z As mpz_t = "0" /// Dim q As mpq_t = "0" /// Dim f As mpf_t = "0" /// Dim c As New ptr(Of[Char])("0"C) /// Dim zt As New ptr(Of mp_size_t)(0) /// Dim dbl As New ptr(Of[Double])(0) /// /// Assert.IsTrue(gmp_lib.gmp_vsscanf("123456 7B/1C8 1.234500e+10 A 10 1.000000", "%Zd %QX %Fe %hhc %d %lf", z, q, f, c, zt, dbl) = 6) /// /// Assert.IsTrue(z.ToString() = "123456") /// Assert.IsTrue(q.ToString() = "123/456") /// Assert.IsTrue(f.ToString() = "0.12345e11") /// Assert.IsTrue(c.Value = "A"C) /// Assert.IsTrue(zt.Value = 10) /// Assert.IsTrue(dbl.Value = 1.0) /// /// ' Release unmanaged memory. /// gmp_lib.mpz_clear(z) /// gmp_lib.mpq_clear(q) /// gmp_lib.mpf_clear(f) /// /// public static int gmp_vsscanf(/*const*/ string /*char **/ s, /*const*/ string /*char **/ fmt, params object[] /*va_list*/ ap) { if (s == null) throw new ArgumentNullException("s"); if (fmt == null) throw new ArgumentNullException("fmt"); if (ap == null) throw new ArgumentNullException("ap"); va_list va_args = new va_list(ap); char_ptr format = new char_ptr(fmt); char_ptr buf = new char_ptr(s); int result = SafeNativeMethods.__gmp_vsscanf(buf.ToIntPtr(), format.ToIntPtr(), va_args.ToIntPtr()); va_args.RetrieveArgumentValues(); gmp_lib.free(format); gmp_lib.free(buf); return result; } #endregion #region "Integer (i.e. Z) routines." /// /// Change the space for to limbs. /// /// The integer to resize. /// The new number of limbs. /// /// /// The value in is preserved if it fits, or is set to 0 if not. /// /// /// mpz_realloc2 is the preferred way to accomplish allocation changes like this. /// mpz_realloc2 and _mpz_realloc are the same except that /// _mpz_realloc takes its size in limbs. /// /// /// mpz_realloc2 /// mpz_getlimbn /// mpz_size /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_modify /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x to a 77-bit integer. /// char_ptr value = new char_ptr("1000 0000 0000 0000 0000"); /// gmp_lib.mpz_set_str(x, value, 16); /// /// // Resize x to 50 limbs, and assert that its value has not changed. /// gmp_lib._mpz_realloc(x, 50); /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 16, x); /// Assert.IsTrue(s.ToString() == "1000 0000 0000 0000 0000".Replace(" ", "")); /// /// // Resize x to 1 limb, and assert that its value has changed to 0. /// gmp_lib._mpz_realloc(x, 1); /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 0); /// /// // Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// gmp_lib.free(s); /// /// /// ' Create and initialize new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x to a 77-bit integer. /// Dim value As New char_ptr("1000 0000 0000 0000 0000") /// gmp_lib.mpz_set_str(x, value, 16) /// /// ' Resize x to 50 limbs, and assert that its value has not changed. /// gmp_lib._mpz_realloc(x, 50) /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 16, x) /// Assert.IsTrue(s.ToString() = "1000 0000 0000 0000 0000".Replace(" ", "")) /// /// ' Resize x to 1 limb, and assert that its value has changed to 0. /// gmp_lib._mpz_realloc(x, 1) /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 0) /// /// ' Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// gmp_lib.free(s) /// /// public static void _mpz_realloc(mpz_t integer, mp_size_t new_alloc) { if (integer == null) throw new ArgumentNullException("integer"); SafeNativeMethods.__gmpz_realloc(integer.ToIntPtr(), new_alloc); } /// /// Set to the absolute value of . /// /// The result integer. /// The operand integer. /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = |x|. /// gmp_lib.mpz_abs(z, x); /// /// // Assert that z is |x|. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 10000); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = |x|. /// gmp_lib.mpz_abs(z, x) /// /// ' Assert that z is |x|. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 10000) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_abs(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_abs(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to + . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add_ui /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 12222. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 12222U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x + y. /// gmp_lib.mpz_add(z, x, y); /// /// // Assert that z is the sum of x and y. /// Assert.IsTrue(gmp_lib.mpz_get_ui(z) == 22222U); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 12222. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 12222UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x + y. /// gmp_lib.mpz_add(z, x, y) /// /// ' Assert that z is the sum of x and y. /// Assert.IsTrue(gmp_lib.mpz_get_ui(z) = 22222UI) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_add(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_add(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to + . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 0. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Increment x twice by 101999. /// gmp_lib.mpz_add_ui(x, x, 101999U); /// gmp_lib.mpz_add_ui(x, x, 101999U); /// /// // Assert that x is 203998. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) == 203998U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 0. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Increment x twice by 101999. /// gmp_lib.mpz_add_ui(x, x, 101999UI) /// gmp_lib.mpz_add_ui(x, x, 101999UI) /// /// ' Assert that x is 203998. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) = 203998UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_add_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_add_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to + * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul_ui /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 12222. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 12222U); /// /// // Create, initialize, and set the value of z to 20000. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init_set_ui(z, 20000U); /// /// // Set z += x * y. /// gmp_lib.mpz_addmul(z, x, y); /// /// // Assert that z has been incremented by 10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 20000U + 10000 * 12222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 12222. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 12222UI) /// /// ' Create, initialize, and set the value of z to 20000. /// Dim z As New mpz_t() /// gmp_lib.mpz_init_set_ui(z, 20000UI) /// /// ' Set z += x * y. /// gmp_lib.mpz_addmul(z, x, y) /// /// ' Assert that z has been incremented by 10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 20000UI + 10000 * 12222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_addmul(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_addmul(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to + * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 20000. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init_set_si(z, 20000); /// /// // Set z += x * 12222. /// gmp_lib.mpz_addmul_ui(z, x, 12222U); /// /// // Assert that z has been incremented by -10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 20000 + -10000 * 12222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 20000. /// Dim z As New mpz_t() /// gmp_lib.mpz_init_set_si(z, 20000) /// /// ' Set z += x * 12222. /// gmp_lib.mpz_addmul_ui(z, x, 12222UI) /// /// ' Assert that z has been incremented by -10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 20000 + -10000 * 12222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_addmul_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_addmul_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to bitwise-and . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the bitwise and of op1 and op2. /// gmp_lib.mpz_and(rop, op1, op2); /// /// // Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 6); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the bitwise and of op1 and op2. /// gmp_lib.mpz_and(rop, op1, op2) /// /// ' Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 6) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static void mpz_and(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_and(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compute the binomial coefficient over and store the result in . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// Negative values of n are supported by mpz_bin_ui, using the identity /// bin(-, ) = (-1)^ * bin( + - 1, ), /// see Knuth volume 1 section 1.2.6 part G. /// /// /// mpz_bin_uiui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of n to 4. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 4); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the binomial coefficient (n:2). /// gmp_lib.mpz_bin_ui(rop, n, 2U); /// /// // Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 6); /// /// // Release unmanaged memory allocated for n and rop. /// gmp_lib.mpz_clears(n, rop, null); /// /// /// ' Create, initialize, and set the value of n to 4. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 4) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the binomial coefficient (n:2). /// gmp_lib.mpz_bin_ui(rop, n, 2UI) /// /// ' Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 6) /// /// ' Release unmanaged memory allocated for n and rop. /// gmp_lib.mpz_clears(n, rop, Nothing) /// /// public static void mpz_bin_ui(mpz_t rop, /*const*/ mpz_t n, uint /*unsigned long int*/ k) { if (rop == null) throw new ArgumentNullException("rop"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_bin_ui(rop.ToIntPtr(), n.ToIntPtr(), k); } /// /// Compute the binomial coefficient over and store the result in . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_bin_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the binomial coefficient (4:2). /// gmp_lib.mpz_bin_uiui(rop, 4U, 2U); /// /// // Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 6); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the binomial coefficient (4:2). /// gmp_lib.mpz_bin_uiui(rop, 4UI, 2UI) /// /// ' Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 6) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_bin_uiui(mpz_t rop, uint /*unsigned long int*/ n, uint /*unsigned long int*/ k) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_bin_uiui(rop.ToIntPtr(), n, k); } /// /// Set the quotient to ceiling( / ). /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = ceiling(n / d). /// gmp_lib.mpz_cdiv_q(q, n, d); /// /// // Assert that q is ceiling(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3334); /// /// // Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = ceiling(n / d). /// gmp_lib.mpz_cdiv_q(q, n, d) /// /// ' Assert that q is ceiling(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3334) /// /// ' Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, Nothing) /// /// public static void mpz_cdiv_q(mpz_t q, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_cdiv_q(q.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the quotient to ceiling( / 2^). /// /// The result quotient integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = ceiling(n / 2^2). /// gmp_lib.mpz_cdiv_q_2exp(q, n, 2U); /// /// // Assert that q is ceiling(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 2501); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = ceiling(n / 2^2). /// gmp_lib.mpz_cdiv_q_2exp(q, n, 2UI) /// /// ' Assert that q is ceiling(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 2501) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static void mpz_cdiv_q_2exp(mpz_t q, /*const*/ mpz_t n, mp_bitcnt_t b) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_cdiv_q_2exp(q.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the quotient to ceiling( / ), and return the remainder r = | - * |. /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// Return the remainder r = | - * |. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = ceiling(n / 3) and return r = n - 3 * q. /// // Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_cdiv_q_ui(q, n, 3U) == 2U); /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3334); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = ceiling(n / 3) and return r = n - 3 * q. /// ' Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_cdiv_q_ui(q, n, 3UI) = 2UI) /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3334) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static uint /*unsigned long int*/ mpz_cdiv_q_ui(mpz_t q, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_cdiv_q_ui(q.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the quotient to ceiling( / ), and set the remainder to - * . /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = ceiling(n / 3) and r = n - d * q. /// gmp_lib.mpz_cdiv_qr(q, r, n, d); /// /// // Assert that q is 3334, and that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3334); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == -2); /// /// // Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// Set q = ceiling(n / 3) and r = n - d * q. /// gmp_lib.mpz_cdiv_qr(q, r, n, d) /// /// ' Assert that q is 3334, and that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3334) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = -2) /// /// ' Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, Nothing) /// /// public static void mpz_cdiv_qr(mpz_t q, mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_cdiv_qr(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set quotient to ceiling( / ), set the remainder to - * , and return | |. /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = ceiling(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_cdiv_qr_ui(q, r, n, 3U) == 2U); /// /// // Assert that q is 3334, and that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3334); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == -2); /// /// // Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// ' Set q = ceiling(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_cdiv_qr_ui(q, r, n, 3UI) = 2UI) /// /// ' Assert that q is 3334, and that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3334) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = -2) /// /// ' Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, Nothing) /// /// public static uint /*unsigned long int*/ mpz_cdiv_qr_ui(mpz_t q, mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_cdiv_qr_ui(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the remainder to - q * where q = ceiling( / ). /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_q /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - d * ceiling(n / d). /// gmp_lib.mpz_cdiv_r(r, n, d); /// /// // Assert that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == -2); /// /// // Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - d * ceiling(n / d). /// gmp_lib.mpz_cdiv_r(r, n, d) /// /// ' Assert that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = -2) /// /// ' Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, Nothing) /// /// public static void mpz_cdiv_r(mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_cdiv_r(r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the remainder to - q * 2^ where q = ceiling( / 2^). /// /// The result remainder integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 2^2 * ceiling(n / 2^2) /// gmp_lib.mpz_cdiv_r_2exp(r, n, 2U); /// /// // Assert that r is -3. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == -3); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// ' Set r = n - 2^2 * ceiling(n / 2^2) /// gmp_lib.mpz_cdiv_r_2exp(r, n, 2UI) /// /// ' Assert that r is -3. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = -3) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static void mpz_cdiv_r_2exp(mpz_t r, /*const*/ mpz_t n, mp_bitcnt_t b) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_cdiv_r_2exp(r.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the remainder to - q * where q = ceiling( / ), and return | |. /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 3 * ceiling(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_cdiv_r_ui(r, n, 3U) == 2U); /// /// // Assert that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == -2); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - 3 * ceiling(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_cdiv_r_ui(r, n, 3UI) = 2UI) /// /// ' Assert that r is -2. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = -2) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static uint /*unsigned long int*/ mpz_cdiv_r_ui(mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_cdiv_r_ui(r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Return the remainder | r | where r = - q * , and where q = ceiling( / ). /// /// The numerator integer. /// The denominator integer. /// The remainder | r | where r = - q * , and where q = ceiling( / ). /// mpz_cdiv_q /// mpz_cdiv_r /// mpz_cdiv_qr /// mpz_cdiv_q_ui /// mpz_cdiv_r_ui /// mpz_cdiv_qr_ui /// mpz_cdiv_q_2exp /// mpz_cdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Assert that returned value is |n - 3 * ceiling(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_cdiv_ui(n, 3U) == 2U); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Assert that returned value is |n - 3 * ceiling(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_cdiv_ui(n, 3UI) = 2UI) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static uint mpz_cdiv_ui(/*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_cdiv_ui(n.ToIntPtr(), d); } /// /// Free the space occupied by . /// /// The integer. /// /// /// Call this function for all mpz_t variables when you are done with them. /// /// /// mpz_clears /// mpz_init /// mpz_inits /// mpz_init2 /// mpz_realloc2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Assert that the value of x is 0. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) == 0U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Assert that the value of x is 0. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) = 0UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_clear(mpz_t x) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_clear(x.ToIntPtr()); x.Clear(); } /// /// Free the space occupied by a NULL-terminated list of mpz_t variables. /// /// A NULL-terminated list of mpz_t variables. /// mpz_clear /// mpz_init /// mpz_inits /// mpz_init2 /// mpz_realloc2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create new integers x1, x2 and x3. /// mpz_t x1 = new mpz_t(); /// mpz_t x2 = new mpz_t(); /// mpz_t x3 = new mpz_t(); /// /// // Initialize the integers. /// gmp_lib.mpz_inits(x1, x2, x3, null); /// /// // Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x1) == 0); /// Assert.IsTrue(gmp_lib.mpz_get_si(x2) == 0); /// Assert.IsTrue(gmp_lib.mpz_get_si(x3) == 0); /// /// // Release unmanaged memory allocated for the integers. /// gmp_lib.mpz_clears(x1, x2, x3, null); /// /// /// ' Create new integers x1, x2 and x3. /// Dim x1 As New mpz_t() /// Dim x2 As New mpz_t() /// Dim x3 As New mpz_t() /// /// ' Initialize the integers. /// gmp_lib.mpz_inits(x1, x2, x3, Nothing) /// /// ' Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x1) = 0) /// Assert.IsTrue(gmp_lib.mpz_get_si(x2) = 0) /// Assert.IsTrue(gmp_lib.mpz_get_si(x3) = 0) /// /// ' Release unmanaged memory allocated for the integers. /// gmp_lib.mpz_clears(x1, x2, x3, Nothing) /// /// public static void mpz_clears(params mpz_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpz_t a in x) { if (a != null) mpz_clear(a); } } /// /// Clear bit in . /// /// The result integer. /// The index of the bit to clear. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of rop to 70. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init_set_si(rop, 70); /// /// // Clear bit 3 of rop. /// gmp_lib.mpz_clrbit(rop, 3U); /// /// // Assert that rop is 70. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 70); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 70. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init_set_si(rop, 70) /// /// ' Clear bit 3 of rop. /// gmp_lib.mpz_clrbit(rop, 3UI) /// /// ' Assert that rop is 70. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 70) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_clrbit(mpz_t rop, mp_bitcnt_t bit_index) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_clrbit(rop.ToIntPtr(), bit_index); } /// /// Compare and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if > , zero if = , or a negative value if < . /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_cmpabs_ui /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Assert that op1 < op2. /// Assert.IsTrue(gmp_lib.mpz_cmp(op1, op2) < 0); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Assert that op1 < op2. /// Assert.IsTrue(gmp_lib.mpz_cmp(op1, op2) < 0) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, Nothing) /// /// public static int mpz_cmp(/*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpz_cmp(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compare and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if > , zero if = , or a negative value if < . /// /// /// mpz_cmp_d can be called with an infinity (see double.PositiveInfinity or double.NegativeInfinity), /// but results are undefined for a double.NaN. /// /// /// mpz_cmp /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_cmpabs_ui /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Assert that op1 < 70.0. /// Assert.IsTrue(gmp_lib.mpz_cmp_d(op1, 70.0) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Assert that op1 < 70.0. /// Assert.IsTrue(gmp_lib.mpz_cmp_d(op1, 70.0) < 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1) /// /// public static int mpz_cmp_d(/*const*/ mpz_t op1, double op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_cmp_d(op1.ToIntPtr(), op2); } /// /// Compare and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if > , zero if = , or a negative value if < . /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_cmpabs_ui /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Assert that op1 < 70. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(op1, 70) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Assert that op1 < 70. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(op1, 70) < 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1) /// /// public static int mpz_cmp_si(/*const*/ mpz_t op1, int /*long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_cmp_si(op1.ToIntPtr(), op2); } /// /// Compare and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if > , zero if = , or a negative value if < . /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Assert that op1 < 70. /// Assert.IsTrue(gmp_lib.mpz_cmp_ui(op1, 70U) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Assert that op1 < 70. /// Assert.IsTrue(gmp_lib.mpz_cmp_ui(op1, 70UI) < 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1) /// /// public static int mpz_cmp_ui(/*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_cmp_ui(op1.ToIntPtr(), op2); } /// /// Compare the absolute values of and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if | | > | |, zero if | | = | |, or a negative value if | | < | |. /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs_d /// mpz_cmpabs_ui /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to -63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_si(op1, -63); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Assert that |op1| < |op2|. /// Assert.IsTrue(gmp_lib.mpz_cmp(op1, op2) < 0); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to -63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_si(op1, -63) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Assert that |op1| < |op2|. /// Assert.IsTrue(gmp_lib.mpz_cmp(op1, op2) < 0) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, Nothing) /// /// public static int mpz_cmpabs(/*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpz_cmpabs(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compare the absolute values of and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if | | > | |, zero if | | = | |, or a negative value if | | < | |. /// /// /// mpz_cmpabs_d can be called with an infinity (see double.PositiveInfinity or double.NegativeInfinity), /// but results are undefined for a double.NaN. /// /// /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_ui /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to -63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_si(op1, -63); /// /// // Assert that |op1| < |-70.0|. /// Assert.IsTrue(gmp_lib.mpz_cmpabs_d(op1, -70.0) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to -63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_si(op1, -63) /// /// ' Assert that |op1| < |-70.0|. /// Assert.IsTrue(gmp_lib.mpz_cmpabs_d(op1, -70.0) < 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1) /// /// public static int mpz_cmpabs_d(/*const*/ mpz_t op1, double op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_cmpabs_d(op1.ToIntPtr(), op2); } /// /// Compare the absolute values of and . /// /// The first operand integer. /// The second operand integer. /// Return a positive value if | | > | |, zero if | | = | |, or a negative value if | | < | |. /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_sgn /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op1 to -63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_si(op1, -63); /// /// // Assert that |op1| < |70|. /// Assert.IsTrue(gmp_lib.mpz_cmpabs_ui(op1, 70U) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// /// public static int mpz_cmpabs_ui(/*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_cmpabs_ui(op1.ToIntPtr(), op2); } /// /// Set to the one’s complement of . /// /// The result integer. /// The operand integer. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op to 63. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 63U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the one's complement of op. /// gmp_lib.mpz_com(rop, op); /// /// // Assert that rop is -64. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == -64); /// /// // Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, null); /// /// /// ' Create, initialize, and set the value of op to 63. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 63UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the one's complement of op. /// gmp_lib.mpz_com(rop, op) /// /// ' Assert that rop is -64. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = -64) /// /// ' Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, Nothing) /// /// public static void mpz_com(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_com(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Complement bit in . /// /// The result integer. /// The index of the bit to comlpement. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of rop to 70. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init_set_si(rop, 70); /// /// // Complement bit 3 of rop. /// gmp_lib.mpz_combit(rop, 3U); /// /// // Assert that rop is 78. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 78); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 70. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init_set_si(rop, 70) /// /// ' Complement bit 3 of rop. /// gmp_lib.mpz_combit(rop, 3UI) /// /// ' Assert that rop is 78. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 78) /// /// ' Release unmanaged memory allocated for rop. /// /// public static void mpz_combit(mpz_t rop, mp_bitcnt_t bit_index) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_combit(rop.ToIntPtr(), bit_index); } /// /// Return non-zero if is congruent to modulo . /// /// An operand integer. /// The remainder of the division by . /// The divisor operand integer. /// Non-zero if is congruent to modulo . /// /// /// is congruent to mod if there exists an integer q /// satisfying = + q * . /// Unlike the other division functions, = 0 is accepted and following the rule it can be seen /// that and are considered congruent mod 0 only when exactly equal. /// /// /// mpz_cdiv_qr /// mpz_congruent_2exp_p /// mpz_congruent_ui_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_ui(n, 10000U); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_ui(d, 3U); /// /// // Create, initialize, and set the value of c to 1. /// mpz_t c = new mpz_t(); /// gmp_lib.mpz_init_set_ui(c, 1U); /// /// // Assert that n is congruent to c mod d. /// Assert.IsTrue(gmp_lib.mpz_congruent_p(n, c, d) > 0); /// /// // Release unmanaged memory allocated for n, d, and c. /// gmp_lib.mpz_clears(n, d, c, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_ui(n, 10000UI) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_ui(d, 3UI) /// /// ' Create, initialize, and set the value of c to 1. /// Dim c As New mpz_t() /// gmp_lib.mpz_init_set_ui(c, 1UI) /// /// ' Assert that n is congruent to c mod d. /// Assert.IsTrue(gmp_lib.mpz_congruent_p(n, c, d) > 0) /// /// ' Release unmanaged memory allocated for n, d, and c. /// gmp_lib.mpz_clears(n, d, c, Nothing) /// /// public static int mpz_congruent_p(/*const*/ mpz_t n, /*const*/ mpz_t c, /*const*/ mpz_t d) { if (n == null) throw new ArgumentNullException("n"); if (c == null) throw new ArgumentNullException("c"); if (d == null) throw new ArgumentNullException("d"); return SafeNativeMethods.__gmpz_congruent_p(n.ToIntPtr(), c.ToIntPtr(), d.ToIntPtr()); } /// /// Return non-zero if is congruent to modulo 2^. /// /// An operand integer. /// The remainder of the division by 2^. /// The exponent of the power of two divisor. /// Non-zero if is congruent to modulo 2^. /// /// /// is congruent to mod 2^ if there exists an integer q /// satisfying = + q * 2^. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_congruent_ui_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_ui(n, 10001U); /// /// // Create, initialize, and set the value of b to 1. /// mpz_t c = new mpz_t(); /// gmp_lib.mpz_init_set_ui(c, 1U); /// /// // Assert that n is congruent to c mod 2^3. /// Assert.IsTrue(gmp_lib.mpz_congruent_2exp_p(n, c, 3U) > 0); /// /// // Release unmanaged memory allocated for n and c. /// gmp_lib.mpz_clears(n, c, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_ui(n, 10001UI) /// /// ' Create, initialize, and set the value of b to 1. /// Dim c As New mpz_t() /// gmp_lib.mpz_init_set_ui(c, 1UI) /// /// ' Assert that n is congruent to c mod 2^3. /// Assert.IsTrue(gmp_lib.mpz_congruent_2exp_p(n, c, 3UI) > 0) /// /// ' Release unmanaged memory allocated for n and c. /// gmp_lib.mpz_clears(n, c, Nothing) /// /// public static int mpz_congruent_2exp_p(/*const*/ mpz_t n, /*const*/ mpz_t c, mp_bitcnt_t b) { if (n == null) throw new ArgumentNullException("n"); if (c == null) throw new ArgumentNullException("c"); return SafeNativeMethods.__gmpz_congruent_2exp_p(n.ToIntPtr(), c.ToIntPtr(), b); } /// /// Return non-zero if is congruent to modulo . /// /// An operand integer. /// The remainder of the division by . /// The divisor operand integer. /// Non-zero if is congruent to modulo . /// /// /// is congruent to mod if there exists an integer q /// satisfying = + q * . /// Unlike the other division functions, = 0 is accepted and following the rule it can be seen /// that and are considered congruent mod 0 only when exactly equal. /// /// /// mpz_cdiv_qr /// mpz_congruent_2exp_p /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_ui(n, 10000U); /// /// // Assert that n is congruent to 1 mod 3. /// Assert.IsTrue(gmp_lib.mpz_congruent_ui_p(n, 1U, 3U) > 0); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_ui(n, 10000UI) /// /// ' Assert that n is congruent to 1 mod 3. /// Assert.IsTrue(gmp_lib.mpz_congruent_ui_p(n, 1UI, 3UI) > 0) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static int mpz_congruent_ui_p(/*const*/ mpz_t n, uint /*unsigned long int*/ c, uint /*unsigned long int*/ d) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_congruent_ui_p(n.ToIntPtr(), c, d); } /// /// Set to / when it is known in advance that divides . /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact_ui /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 5. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 5U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x / y. /// gmp_lib.mpz_divexact(z, x, y); /// /// // Assert that z is 2000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 2000); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 5. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 5UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x / y. /// gmp_lib.mpz_divexact(z, x, y) /// /// ' Assert that z is 2000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 2000) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_divexact(mpz_t q, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_divexact(q.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set to / when it is known in advance that divides . /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x / 5. /// gmp_lib.mpz_divexact_ui(z, x, 5U); /// /// // Assert that z is 2000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 2000); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x / 5. /// gmp_lib.mpz_divexact_ui(z, x, 5UI) /// /// ' Assert that z is 2000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 2000) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_divexact_ui(mpz_t q, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_divexact_ui(q.ToIntPtr(), n.ToIntPtr(), d); } /// /// Return non-zero if is exactly divisible by . /// /// The numerator integer. /// The denominator integer. /// Non-zero if is exactly divisible by . /// /// /// is divisible by if there exists an integer q /// satisfying = q * . Unlike the other division functions, /// = 0 is accepted and following the rule it can be seen that only 0 is /// considered divisible by 0. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_2exp_p /// mpz_divisible_ui_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 5. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 5U); /// /// // Assert that x is divisible by y. /// Assert.IsTrue(gmp_lib.mpz_divisible_p(x, y) > 0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 5. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 5UI) /// /// ' Assert that x is divisible by y. /// Assert.IsTrue(gmp_lib.mpz_divisible_p(x, y) > 0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, Nothing) /// /// public static int mpz_divisible_p(/*const*/ mpz_t n, /*const*/ mpz_t d) { if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); return SafeNativeMethods.__gmpz_divisible_p(n.ToIntPtr(), d.ToIntPtr()); } /// /// Return non-zero if is exactly divisible by . /// /// The numerator integer. /// The denominator integer. /// Non-zero if is exactly divisible by . /// /// /// is divisible by if there exists an integer q /// satisfying = q * . Unlike the other division functions, /// = 0 is accepted and following the rule it can be seen that only 0 is /// considered divisible by 0. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_2exp_p /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Assert that x is divisible by 5. /// Assert.IsTrue(gmp_lib.mpz_divisible_ui_p(x, 5U) > 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Assert that x is divisible by 5. /// Assert.IsTrue(gmp_lib.mpz_divisible_ui_p(x, 5UI) > 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static int mpz_divisible_ui_p(/*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_divisible_ui_p(n.ToIntPtr(), d); } /// /// Return non-zero if is exactly divisible by 2^. /// /// The numerator integer. /// The exponent of the power of two denominator integer. /// Non-zero if is exactly divisible by 2^. /// /// /// is divisible by 2^ if there exists an integer q /// satisfying = q * 2^. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_divisible_ui_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// Assert.IsTrue(gmp_lib.mpz_divisible_2exp_p(x, 2U) > 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// Assert.IsTrue(gmp_lib.mpz_divisible_2exp_p(x, 2UI) > 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static int mpz_divisible_2exp_p(/*const*/ mpz_t n, mp_bitcnt_t b) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_divisible_2exp_p(n.ToIntPtr(), b); } /// /// Determine whether is even. /// /// The operand integer. /// Return non-zero if even, zero if odd. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op to 427295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 427295); /// /// // Assert that op is not even but odd. /// Assert.IsTrue(gmp_lib.mpz_even_p(op) == 0); /// Assert.IsTrue(gmp_lib.mpz_odd_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 427295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 427295) /// /// ' Assert that op is not even but odd. /// Assert.IsTrue(gmp_lib.mpz_even_p(op) = 0) /// Assert.IsTrue(gmp_lib.mpz_odd_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_even_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_size != 0 && (op._mp_d[0] & 1) == 0 ? 1 : 0; } /// /// Fill with word data from . /// /// The result integer. /// The number of words produced. /// 1 for most significant word first or -1 for least significant first. /// The number of bytes in each word. /// 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. /// The number of most significant bits to skip. /// The operand integer. /// Either or the allocated block. /// /// /// The parameters specify the format of the data produced. /// Each word will be bytes and can be 1 for most significant word first /// or -1 for least significant first. /// Within each word can be 1 for most significant byte first, /// -1 for least significant first, or 0 for the native endianness of the host CPU. /// The most significant bits of each word are unused and set to zero, /// this can be 0 to produce full words. /// /// /// The number of words produced is written to , or can be NULL to discard the count. /// must have enough space for the data, or if is NULL then a result array of the necessary /// size is allocated using the current GMP allocation function /// (see GNU MP - Custom Allocation). /// In either case the return value is the destination used, either or the allocated block. /// /// /// If is non-zero then the most significant word produced will be non-zero. /// If is zero then the count returned will be zero and nothing written to . /// If is NULL in this case, no block is allocated, just NULL is returned. /// /// /// The sign of is ignored, just the absolute value is exported. /// An application can use mpz_sgn to get the sign and handle it as desired. /// (see GNU MP - Integer Comparisons) /// /// /// There are no data alignment restrictions on , any address is allowed. /// /// /// When an application is allocating space itself the required size can be determined with a calculation like the following. /// Since mpz_sizeinbase always returns at least 1, count here will be at least one, which avoids any portability /// problems with malloc(0), though if z is zero no space at all is actually needed (or written). /// /// /// numb = 8 * size - nail; /// count = (mpz_sizeinbase(z, 2) + numb - 1) / numb; /// p = malloc(count * size); /// /// /// mpz_import /// Integer Import and Export /// GNU MP - Integer Import and Export /// /// /// // Create, initialize, and set the value of op to 0x800000000000000000000001. /// mpz_t op = new mpz_t(); /// char_ptr value = new char_ptr("800000000000000000000001"); /// gmp_lib.mpz_init_set_str(op, value, 16); /// /// // Export op as 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// void_ptr data = gmp_lib.allocate(12); /// size_t countp = 0; /// gmp_lib.mpz_export(data, ref countp, -1, 4, 1, 0, op); /// /// // Assert the result. /// byte[] result = new byte[12]; /// Marshal.Copy(data.ToIntPtr(), result, 0, 12); /// Assert.IsTrue(result[0] == 0x00); /// Assert.IsTrue(result[1] == 0x00); /// Assert.IsTrue(result[2] == 0x00); /// Assert.IsTrue(result[3] == 0x01); /// Assert.IsTrue(result[4] == 0x00); /// Assert.IsTrue(result[5] == 0x00); /// Assert.IsTrue(result[6] == 0x00); /// Assert.IsTrue(result[7] == 0x00); /// Assert.IsTrue(result[8] == 0x80); /// Assert.IsTrue(result[9] == 0x00); /// Assert.IsTrue(result[10] == 0x00); /// Assert.IsTrue(result[11] == 0x00); /// /// // Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(op); /// gmp_lib.free(data); /// gmp_lib.free(value); /// /// /// ' Create, initialize, and set the value of op to 0x800000000000000000000001. /// Dim op As New mpz_t() /// Dim value As New char_ptr("800000000000000000000001") /// gmp_lib.mpz_init_set_str(op, value, 16) /// /// ' Export op as 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// Dim data As void_ptr = gmp_lib.allocate(12) /// Dim countp As size_t = 0 /// gmp_lib.mpz_export(data, countp, -1, 4, 1, 0, op) /// /// ' Assert the result. /// Dim result As Byte() = New Byte(11) { } /// Marshal.Copy(data.ToIntPtr(), result, 0, 12) /// Assert.IsTrue(result(0) = &H0) /// Assert.IsTrue(result(1) = &H0) /// Assert.IsTrue(result(2) = &H0) /// Assert.IsTrue(result(3) = &H1) /// Assert.IsTrue(result(4) = &H0) /// Assert.IsTrue(result(5) = &H0) /// Assert.IsTrue(result(6) = &H0) /// Assert.IsTrue(result(7) = &H0) /// Assert.IsTrue(result(8) = &H80) /// Assert.IsTrue(result(9) = &H0) /// Assert.IsTrue(result(10) = &H0) /// Assert.IsTrue(result(11) = &H0) /// /// ' Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(op) /// gmp_lib.free(data) /// gmp_lib.free(value) /// /// public static void_ptr mpz_export(void_ptr rop, ref size_t countp, int order, size_t size, int endian, size_t nails, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); if (IntPtr.Size == 4) { uint temp = (uint)countp; IntPtr result = SafeNativeMethods.__gmpz_export_x86(rop.ToIntPtr(), ref temp, order, (uint)size, endian, (uint)nails, op.ToIntPtr()); countp = temp; return new void_ptr(result); } else { ulong temp = countp; IntPtr result = SafeNativeMethods.__gmpz_export_x64(rop.ToIntPtr(), ref temp, order, size, endian, nails, op.ToIntPtr()); countp = temp; return new void_ptr(result); } } /// /// Fill with word data from . /// /// The result integer. /// The number of words produced. /// 1 for most significant word first or -1 for least significant first. /// The number of bytes in each word. /// 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. /// The number of most significant bits to skip. /// The operand integer. /// Either or the allocated block. /// /// /// The parameters specify the format of the data produced. /// Each word will be bytes and can be 1 for most significant word first /// or -1 for least significant first. /// Within each word can be 1 for most significant byte first, /// -1 for least significant first, or 0 for the native endianness of the host CPU. /// The most significant bits of each word are unused and set to zero, /// this can be 0 to produce full words. /// /// /// The number of words produced is written to , or can be NULL to discard the count. /// must have enough space for the data, or if is NULL then a result array of the necessary /// size is allocated using the current GMP allocation function /// (see GNU MP - Custom Allocation). /// In either case the return value is the destination used, either or the allocated block. /// /// /// If is non-zero then the most significant word produced will be non-zero. /// If is zero then the count returned will be zero and nothing written to . /// If is NULL in this case, no block is allocated, just NULL is returned. /// /// /// The sign of is ignored, just the absolute value is exported. /// An application can use mpz_sgn to get the sign and handle it as desired. /// (see GNU MP - Integer Comparisons) /// /// /// There are no data alignment restrictions on , any address is allowed. /// /// /// When an application is allocating space itself the required size can be determined with a calculation like the following. /// Since mpz_sizeinbase always returns at least 1, count here will be at least one, which avoids any portability /// problems with malloc(0), though if z is zero no space at all is actually needed (or written). /// /// /// numb = 8 * size - nail; /// count = (mpz_sizeinbase(z, 2) + numb - 1) / numb; /// p = malloc(count * size); /// /// /// mpz_import /// Integer Import and Export /// GNU MP - Integer Import and Export /// /// /// // Create, initialize, and set the value of op to 0x800000000000000000000001. /// mpz_t op = new mpz_t(); /// char_ptr value = new char_ptr("800000000000000000000001"); /// gmp_lib.mpz_init_set_str(op, value, 16); /// /// // Export op as 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// void_ptr data = gmp_lib.allocate(12); /// ptr<size_t> countp = new ptr<size_t>(0); /// gmp_lib.mpz_export(data, countp, -1, 4, 1, 0, op); /// /// // Assert the result. /// byte[] result = new byte[12]; /// Marshal.Copy(data.ToIntPtr(), result, 0, 12); /// Assert.IsTrue(result[0] == 0x00); /// Assert.IsTrue(result[1] == 0x00); /// Assert.IsTrue(result[2] == 0x00); /// Assert.IsTrue(result[3] == 0x01); /// Assert.IsTrue(result[4] == 0x00); /// Assert.IsTrue(result[5] == 0x00); /// Assert.IsTrue(result[6] == 0x00); /// Assert.IsTrue(result[7] == 0x00); /// Assert.IsTrue(result[8] == 0x80); /// Assert.IsTrue(result[9] == 0x00); /// Assert.IsTrue(result[10] == 0x00); /// Assert.IsTrue(result[11] == 0x00); /// /// // Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(op); /// gmp_lib.free(data); /// gmp_lib.free(value); /// /// /// ' Create, initialize, and set the value of op to 0x800000000000000000000001. /// Dim op As New mpz_t() /// Dim value As New char_ptr("800000000000000000000001") /// gmp_lib.mpz_init_set_str(op, value, 16) /// /// ' Export op as 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// Dim data As void_ptr = gmp_lib.allocate(12) /// Dim countp As New ptr(Of size_t)(0) /// gmp_lib.mpz_export(data, countp, -1, 4, 1, 0, op) /// /// ' Assert the result. /// Dim result As Byte() = New Byte(11) { } /// Marshal.Copy(data.ToIntPtr(), result, 0, 12) /// Assert.IsTrue(result(0) = &H0) /// Assert.IsTrue(result(1) = &H0) /// Assert.IsTrue(result(2) = &H0) /// Assert.IsTrue(result(3) = &H1) /// Assert.IsTrue(result(4) = &H0) /// Assert.IsTrue(result(5) = &H0) /// Assert.IsTrue(result(6) = &H0) /// Assert.IsTrue(result(7) = &H0) /// Assert.IsTrue(result(8) = &H80) /// Assert.IsTrue(result(9) = &H0) /// Assert.IsTrue(result(10) = &H0) /// Assert.IsTrue(result(11) = &H0) /// /// ' Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(op) /// gmp_lib.free(data) /// gmp_lib.free(value) /// /// public static void_ptr mpz_export(void_ptr rop, ptr countp, int order, size_t size, int endian, size_t nails, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); if (countp == null) throw new ArgumentNullException("countp"); if (IntPtr.Size == 4) { uint temp = (uint)countp.Value; IntPtr result = SafeNativeMethods.__gmpz_export_x86(rop.ToIntPtr(), ref temp, order, (uint)size, endian, (uint)nails, op.ToIntPtr()); countp.Value = temp; return new void_ptr(result); } else { ulong temp = countp.Value; IntPtr result = SafeNativeMethods.__gmpz_export_x64(rop.ToIntPtr(), ref temp, order, size, endian, nails, op.ToIntPtr()); countp.Value = temp; return new void_ptr(result); } } /// /// Set to the factorial !. /// /// The result integer. /// The operand integer. /// mpz_2fac_ui /// mpz_mfac_uiui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = 3!. /// gmp_lib.mpz_fac_ui(rop, 3U); /// /// // Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 6); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = 3!. /// gmp_lib.mpz_fac_ui(rop, 3UI) /// /// ' Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 6) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_fac_ui(mpz_t rop, uint /*unsigned long int*/ n) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_fac_ui(rop.ToIntPtr(), n); } /// /// Set to the double-factorial !!. /// /// The result integer. /// The operand integer. /// mpz_fac_ui /// mpz_mfac_uiui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = 9!!. /// gmp_lib.mpz_2fac_ui(rop, 9U); /// /// // Assert that rop is 945. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 945); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = 9!!. /// gmp_lib.mpz_2fac_ui(rop, 9UI) /// /// ' Assert that rop is 945. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 945) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_2fac_ui(mpz_t rop, uint /*unsigned long int*/ n) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_2fac_ui(rop.ToIntPtr(), n); } /// /// Set to the m-multi-factorial !^()n. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_fac_ui /// mpz_2fac_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = 10!^(4). /// gmp_lib.mpz_mfac_uiui(rop, 10U, 4U); /// /// // Assert that rop is 945. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 120); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = 10!^(4). /// gmp_lib.mpz_mfac_uiui(rop, 10UI, 4UI) /// /// ' Assert that rop is 945. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 120) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_mfac_uiui(mpz_t rop, uint /*unsigned long int*/ n, uint /*unsigned long int*/ m) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_mfac_uiui(rop.ToIntPtr(), n, m); } /// /// Set to the primorial of , i.e. the product of all positive prime numbers ≤ . /// /// The result integer. /// The operand integer. /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = 7 * 5 * 3 * 2 = 210. /// gmp_lib.mpz_primorial_ui(rop, 9U); /// /// // Assert that rop is 210. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 210); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = 7 * 5 * 3 * 2 = 210. /// gmp_lib.mpz_primorial_ui(rop, 9UI) /// /// ' Assert that rop is 210. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 210) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_primorial_ui(mpz_t rop, uint /*unsigned long int*/ n) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_primorial_ui(rop.ToIntPtr(), n); } /// /// Set the quotient to floor( / ). /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = floor(n / d). /// gmp_lib.mpz_fdiv_q(q, n, d); /// /// // Assert that q is floor(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// /// // Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = floor(n / d). /// gmp_lib.mpz_fdiv_q(q, n, d) /// /// ' Assert that q is floor(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// /// ' Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, Nothing) /// /// public static void mpz_fdiv_q(mpz_t q, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_fdiv_q(q.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the quotient to floor( / 2^). /// /// The result quotient integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = floor(n / 2^2). /// gmp_lib.mpz_fdiv_q_2exp(q, n, 2U); /// /// // Assert that q is floor(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 2500); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = floor(n / 2^2). /// gmp_lib.mpz_fdiv_q_2exp(q, n, 2UI) /// /// ' Assert that q is floor(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 2500) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static void mpz_fdiv_q_2exp(mpz_t q, /*const*/ mpz_t n, mp_bitcnt_t b) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_fdiv_q_2exp(q.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the quotient to floor( / ), and return the remainder r = | - * |. /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// Return the remainder r = | - * |. /// mpz_cdiv_qr /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = floor(n / 3) and return r = n - 3 * q. /// // Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_fdiv_q_ui(q, n, 3U) == 1U); /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = floor(n / 3) and return r = n - 3 * q. /// ' Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_fdiv_q_ui(q, n, 3UI) = 1UI) /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static long mpz_fdiv_q_ui(mpz_t q, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_fdiv_q_ui(q.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the quotient to floor( / ), and set the remainder to - * . /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = floor(n / 3) and r = n - d * q. /// gmp_lib.mpz_fdiv_qr(q, r, n, d); /// /// // Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// ' Set q = floor(n / 3) and r = n - d * q. /// gmp_lib.mpz_fdiv_qr(q, r, n, d) /// /// /// ' Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, Nothing) /// /// public static void mpz_fdiv_qr(mpz_t q, mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_fdiv_qr(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set quotient to floor( / ), set the remainder to - * , and return | |. /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = floor(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_fdiv_qr_ui(q, r, n, 3U) == 1U); /// /// // Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// ' Set q = floor(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_fdiv_qr_ui(q, r, n, 3UI) = 1UI) /// /// ' Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, Nothing) /// /// public static uint /*unsigned long int*/ mpz_fdiv_qr_ui(mpz_t q, mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_fdiv_qr_ui(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the remainder to - q * where q = floor( / ). /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - d * floor(n / d). /// gmp_lib.mpz_fdiv_r(r, n, d); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - d * floor(n / d). /// gmp_lib.mpz_fdiv_r(r, n, d) /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, Nothing) /// /// public static void mpz_fdiv_r(mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_fdiv_r(r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the remainder to - q * 2^ where q = floor( / 2^). /// /// The result remainder integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 2^2 * floor(n / 2^2) /// gmp_lib.mpz_fdiv_r_2exp(r, n, 2U); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - 2^2 * floor(n / 2^2) /// gmp_lib.mpz_fdiv_r_2exp(r, n, 2UI) /// /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static void mpz_fdiv_r_2exp(mpz_t r, /*const*/ mpz_t n, mp_bitcnt_t b) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_fdiv_r_2exp(r.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the remainder to - q * where q = floor( / ), and return | |. /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 3 * floor(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_fdiv_r_ui(r, n, 3U) == 1U); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - 3 * floor(n / 3), and return |r|. /// Set r = n - 3 * floor(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_fdiv_r_ui(r, n, 3UI) = 1UI) /// /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static uint mpz_fdiv_r_ui(mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_fdiv_r_ui(r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Return the remainder | r | where r = - q * , and where q = floor( / ). /// /// The numerator integer. /// The denominator integer. /// The remainder | r | where r = - q * , and where q = floor( / ). /// mpz_cdiv_qr /// mpz_fdiv_q /// mpz_fdiv_r /// mpz_fdiv_qr /// mpz_fdiv_q_ui /// mpz_fdiv_r_ui /// mpz_fdiv_qr_ui /// mpz_fdiv_q_2exp /// mpz_fdiv_r_2exp /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Assert that returned value is |n - 3 * floor(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_fdiv_ui(n, 3U) == 1U); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Assert that returned value is |n - 3 * floor(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_fdiv_ui(n, 3UI) = 1UI) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static long mpz_fdiv_ui(/*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_fdiv_ui(n.ToIntPtr(), d); } /// /// Sets to to F[], the ’th Fibonacci number. /// /// The F[] result. /// The operand integer. /// /// /// The Fibonacci numbers and Lucas numbers are related sequences, so it’s never necessary to call both /// mpz_fib2_ui and mpz_lucnum2_ui. /// The formulas for going from Fibonacci to Lucas can be found in /// GNU MP - Lucas Numbers Algorithm, /// the reverse is straightforward too. /// /// /// mpz_fib2_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of fn to 0. /// mpz_t fn = new mpz_t(); /// gmp_lib.mpz_init(fn); /// /// // Set fn to the n'th Fibonacci number. /// gmp_lib.mpz_fib_ui(fn, 20U); /// /// // Assert that fn is 6765. /// Assert.IsTrue(gmp_lib.mpz_get_si(fn) == 6765); /// /// // Release unmanaged memory allocated for fn. /// gmp_lib.mpz_clear(fn); /// /// /// ' Create, initialize, and set the value of fn to 0. /// Dim fn As New mpz_t() /// gmp_lib.mpz_init(fn) /// /// ' Set fn to the n'th Fibonacci number. /// gmp_lib.mpz_fib_ui(fn, 20UI) /// /// ' Assert that fn is 6765. /// Assert.IsTrue(gmp_lib.mpz_get_si(fn) = 6765) /// /// ' Release unmanaged memory allocated for fn. /// gmp_lib.mpz_clear(fn) /// /// public static void mpz_fib_ui(mpz_t fn, uint /*unsigned long int*/ n) { if (fn == null) throw new ArgumentNullException("fn"); SafeNativeMethods.__gmpz_fib_ui(fn.ToIntPtr(), n); } /// /// Sets to F[], and to F[ - 1]. /// /// The F[] result. /// The F[ - 1] result. /// The operand integer. /// /// /// This function is designed for calculating isolated Fibonacci numbers. /// When a sequence of values is wanted it’s best to start with mpz_fib2_ui /// and iterate the defining F[n + 1] = F[n] + F[n - 1] or similar. /// /// /// The Fibonacci numbers and Lucas numbers are related sequences, so it’s never necessary to call both /// mpz_fib2_ui and mpz_lucnum2_ui. /// The formulas for going from Fibonacci to Lucas can be found in /// GNU MP - Lucas Numbers Algorithm, /// the reverse is straightforward too. /// /// /// mpz_fib_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the values of fn and fnsub1 to 0. /// mpz_t fn = new mpz_t(); /// mpz_t fnsub1 = new mpz_t(); /// gmp_lib.mpz_inits(fn, fnsub1, null); /// /// // Set fnsub1 and fn to the 19'th and 20'th Fibonacci numbers respectively. /// gmp_lib.mpz_fib2_ui(fn, fnsub1, 20U); /// /// // Assert that fnsub1 and fn are respectively 4181 and 6765. /// Assert.IsTrue(gmp_lib.mpz_get_si(fnsub1) == 4181); /// Assert.IsTrue(gmp_lib.mpz_get_si(fn) == 6765); /// /// // Release unmanaged memory allocated for fn and fnsub1. /// gmp_lib.mpz_clears(fn, fnsub1, null); /// /// /// ' Create, initialize, and set the values of fn and fnsub1 to 0. /// Dim fn As New mpz_t() /// Dim fnsub1 As New mpz_t() /// gmp_lib.mpz_inits(fn, fnsub1, Nothing) /// /// ' Set fnsub1 and fn to the 19'th and 20'th Fibonacci numbers respectively. /// gmp_lib.mpz_fib2_ui(fn, fnsub1, 20UI) /// /// ' Assert that fnsub1 and fn are respectively 4181 and 6765. /// Assert.IsTrue(gmp_lib.mpz_get_si(fnsub1) = 4181) /// Assert.IsTrue(gmp_lib.mpz_get_si(fn) = 6765) /// /// ' Release unmanaged memory allocated for fn and fnsub1. /// gmp_lib.mpz_clears(fn, fnsub1, Nothing) /// /// public static void mpz_fib2_ui(mpz_t fn, mpz_t fnsub1, uint /*unsigned long int*/ n) { if (fn == null) throw new ArgumentNullException("fn"); if (fnsub1 == null) throw new ArgumentNullException("fnsub1"); SafeNativeMethods.__gmpz_fib2_ui(fn.ToIntPtr(), fnsub1.ToIntPtr(), n); } /// /// Return non-zero iff the value of fits in a signed 32-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in a signed 32-bit integer. Otherwise, return zero. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpz_fits_sint_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpz_fits_sint_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_sint_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_sint_p(op.ToIntPtr()); } /// /// Return non-zero iff the value of fits in a signed 32-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in a signed 32-bit integer. Otherwise, return zero. /// mpz_fits_ulong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in long. /// Assert.IsTrue(gmp_lib.mpz_fits_slong_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in long. /// Assert.IsTrue(gmp_lib.mpz_fits_slong_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_slong_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_slong_p(op.ToIntPtr()); } /// /// Return non-zero iff the value of fits in a signed 16-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in a signed 16-bit integer. Otherwise, return zero. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in short. /// Assert.IsTrue(gmp_lib.mpz_fits_sshort_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in short. /// Assert.IsTrue(gmp_lib.mpz_fits_sshort_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_sshort_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_sshort_p(op.ToIntPtr()); } /// /// Return non-zero iff the value of fits in an unsigned 32-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in an unsigned 32-bit integer. Otherwise, return zero. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in uint. /// Assert.IsTrue(gmp_lib.mpz_fits_uint_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in uint. /// Assert.IsTrue(gmp_lib.mpz_fits_uint_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_uint_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_uint_p(op.ToIntPtr()); } /// /// Return non-zero iff the value of fits in an unsigned 32-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in a signed 32-bit integer. Otherwise, return zero. /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op fits in ulong. /// Assert.IsTrue(gmp_lib.mpz_fits_ulong_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op fits in ulong. /// Assert.IsTrue(gmp_lib.mpz_fits_ulong_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_ulong_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_ulong_p(op.ToIntPtr()); } /// /// Return non-zero iff the value of fits in an unsigned 16-bit integer. Otherwise, return zero. /// /// The operand integer. /// Return non-zero iff the value of fits in an unsigned 16-bit integer. Otherwise, return zero. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in ushort. /// Assert.IsTrue(gmp_lib.mpz_fits_ushort_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in ushort. /// Assert.IsTrue(gmp_lib.mpz_fits_ushort_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_fits_ushort_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_fits_ushort_p(op.ToIntPtr()); } /// /// Set to the greatest common divisor of and . /// /// The result operand integer. /// The first operand integer. /// The second operand integer. /// /// /// The result is always positive even if one or both input operands are negative. /// Except if both inputs are zero; then this function defines gcd(0,0) = 0. /// /// /// mpz_gcd_ui /// mpz_gcdext /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the greatest common divisor of op1 and op2. /// gmp_lib.mpz_gcd(rop, op1, op2); /// /// // Assert that rop is 7. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 7); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the greatest common divisor of op1 and op2. /// gmp_lib.mpz_gcd(rop, op1, op2) /// /// ' Assert that rop is 7. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 7) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static void mpz_gcd(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_gcd(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compute the greatest common divisor of and . If is not null, store the result there. /// /// The result operand integer. /// The first operand integer. /// The second operand integer. /// If the result is small enough to fit in an unsigned int, it is returned. If the result does not fit, 0 is returned, and the result is equal to the argument . /// /// /// Note that the result will always fit if is non-zero. /// /// /// mpz_gcd /// mpz_gcdext /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Return the greatest common divisor of op1 and 70. /// Assert.IsTrue(gmp_lib.mpz_gcd_ui(null, op1, 70U) == 7); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Return the greatest common divisor of op1 and 70. /// Assert.IsTrue(gmp_lib.mpz_gcd_ui(Nothing, op1, 70UI) = 7) /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpz_clear(op1) /// /// public static uint mpz_gcd_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpz_gcd_ui(rop == null ? IntPtr.Zero : rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to the greatest common divisor of and , and in addition set and to coefficients satisfying * + * = . /// /// The greateast common divisor. /// The first result coefficient. /// The second result coefficient. /// The first operand integer. /// The second operand integer. /// /// /// The value in is always positive, even if one or both of and /// are negative (or zero if both inputs are zero). The values in and are chosen such /// that normally, | | < | | / (2 ) /// and | | < | | / (2 ), and these relations /// define and uniquely. There are a few exceptional cases: /// /// /// If | | = | |, then = 0, /// = sgn(). /// /// /// Otherwise, = sgn() if = 0 /// or | | = 2 , and = sgn() /// if = 0 or | | = 2 . /// /// /// In all cases, = 0 if and only if /// = | |, i.e., /// if divides /// or = = 0. /// /// /// If is null then that value is not computed. /// /// /// mpz_gcd /// mpz_gcd_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t b = new mpz_t(); /// gmp_lib.mpz_init_set_ui(b, 70U); /// /// // Create, initialize, and set the values of g, s, and t to 0. /// mpz_t g = new mpz_t(); /// mpz_t s = new mpz_t(); /// mpz_t t = new mpz_t(); /// gmp_lib.mpz_inits(g, s, t, null); /// /// // Set g to the the greatest common divisor of a and b, and set s and t such that a * s + b * t = g. /// gmp_lib.mpz_gcdext(g, s, t, a, b); /// /// // Assert that g is 7, and that s and t are respectively -1 and 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(g) == 7); /// Assert.IsTrue(gmp_lib.mpz_get_si(s) == -1); /// Assert.IsTrue(gmp_lib.mpz_get_si(t) == 1); /// /// // Release unmanaged memory allocated for g, s, t, a, and b. /// gmp_lib.mpz_clears(g, s, t, a, b, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim b As New mpz_t() /// gmp_lib.mpz_init_set_ui(b, 70UI) /// /// ' Create, initialize, and set the values of g, s, and t to 0. /// Dim g As New mpz_t() /// Dim s As New mpz_t() /// Dim t As New mpz_t() /// gmp_lib.mpz_inits(g, s, t, Nothing) /// /// ' Set g to the the greatest common divisor of a and b, and set s and t such that a * s + b * t = g. /// gmp_lib.mpz_gcdext(g, s, t, a, b) /// /// ' Assert that g is 7, and that s and t are respectively -1 and 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(g) = 7) /// Assert.IsTrue(gmp_lib.mpz_get_si(s) = -1) /// Assert.IsTrue(gmp_lib.mpz_get_si(t) = 1) /// /// ' Release unmanaged memory allocated for g, s, t, a, and b. /// gmp_lib.mpz_clears(g, s, t, a, b, Nothing) /// /// public static void mpz_gcdext(mpz_t g, mpz_t s, mpz_t t, /*const*/ mpz_t a, /*const*/ mpz_t b) { if (g == null) throw new ArgumentNullException("g"); if (s == null) throw new ArgumentNullException("s"); if (a == null) throw new ArgumentNullException("a"); if (b == null) throw new ArgumentNullException("b"); SafeNativeMethods.__gmpz_gcdext(g.ToIntPtr(), s.ToIntPtr(), t == null ? IntPtr.Zero : t.ToIntPtr(), a.ToIntPtr(), b.ToIntPtr()); } /// /// Convert to a double, truncating if necessary (i.e. rounding towards zero). /// /// The integer. /// as a double, truncating it if necessary (i.e. rounding towards zero). /// /// /// If the exponent from the conversion is too big, the result is system dependent. An infinity is returned /// where available. A hardware overflow trap may or may not occur. /// /// /// mpz_get_d_2exp /// mpz_get_si /// mpz_get_str /// mpz_get_ui /// Converting Integers /// GNU MP - Converting Integers /// /// /// // Create, initialize, and set the value of x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_d(x, 10.7D); /// /// // Assert that the value of x is 10.0. /// Assert.IsTrue(gmp_lib.mpz_get_d(x) == 10.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_d(x, 10.7) /// /// ' Assert that the value of x is 10.0. /// Assert.IsTrue(gmp_lib.mpz_get_d(x) = 10.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static double mpz_get_d(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_get_d(op.ToIntPtr()); } /// /// Convert to a double, truncating if necessary (i.e. rounding towards zero), and returning the exponent separately. /// /// The returned exponent. /// The integer. /// as a double, truncating if necessary (i.e. rounding towards zero). /// /// /// The return value is in the range 0.5 ≤ | d | < 1 and the exponent is stored to . /// d x 2^ is the (truncated) value. /// If is zero, the return value is 0.0 and 0 is stored to . /// /// /// This is similar to the standard C frexp function. /// /// /// mpz_get_d /// mpz_get_si /// mpz_get_str /// mpz_get_ui /// Converting Integers /// GNU MP - Converting Integers /// /// /// // Create, initialize, and set the value of x to 2^20. /// mpz_t x = new mpz_t(); /// char_ptr value = new char_ptr("100000000000000000000"); /// gmp_lib.mpz_init_set_str(x, value, 2); /// /// // Assert that x is equal to 0.5^21. /// int exp = 0; /// Assert.IsTrue(gmp_lib.mpz_get_d_2exp(ref exp, x) == 0.5D); /// Assert.IsTrue(exp == 21); /// /// // Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// /// /// ' Create, initialize, and set the value of x to 2^20. /// Dim x As New mpz_t() /// Dim value As New char_ptr("100000000000000000000") /// gmp_lib.mpz_init_set_str(x, value, 2) /// /// ' Assert that x is equal to 0.5^21. /// Dim exp As Integer = 0 /// Assert.IsTrue(gmp_lib.mpz_get_d_2exp(exp, x) = 0.5) /// Assert.IsTrue(exp = 21) /// /// ' Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// /// public static double mpz_get_d_2exp(ref int /*long int*/ exp, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_get_d_2exp(ref exp, op.ToIntPtr()); } /// /// Return the value of as an signed long. /// /// The integer. /// The value of as an signed long. /// /// /// If fits into a signed long int return the value of . /// Otherwise return the least significant part of , with the same sign as . /// /// /// If is too big to fit in a signed long int, the returned result is probably not very useful. /// To find out if the value will fit, use the function mpz_fits_slong_p. /// /// /// mpz_get_d /// mpz_get_d_2exp /// mpz_get_str /// mpz_get_ui /// Converting Integers /// GNU MP - Converting Integers /// /// /// // Create, initialize, and set the value of x to -10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10); /// /// // Retrieve the value of x, and assert that it is -10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == -10); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to -10. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10) /// /// ' Retrieve the value of x, and assert that it is -10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = -10) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static int mpz_get_si(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_get_si(op.ToIntPtr()); } /// /// Convert to a string of digits in base . /// /// The converted integer. /// The base. /// The integer. /// A pointer to the result string is returned, being either the allocated block, or the given . /// /// /// The base argument may vary from 2 to 62 or from −2 to −36. /// /// /// For base in the range 2..36, digits and lower-case letters are used; for −2..−36, digits and /// upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that /// significance order) are used. /// /// /// If is char_ptr.Zero, the result string is allocated using the current /// allocation function. The block will be strlen(str)+1 bytes, that being exactly enough for the string and /// null-terminator. /// /// /// If is not char_ptr.Zero, it should point to a block of storage large /// enough for the result, that being mpz_sizeinbase(op, base) + 2. /// The two extra bytes are for a possible minus sign, and the null-terminator. /// /// /// mpz_get_d /// mpz_get_d_2exp /// mpz_get_si /// mpz_get_ui /// Converting Integers /// GNU MP - Converting Integers /// /// /// // Create, initialize, and set the value of x to -210. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -210); /// /// // Retrieve the string value of x, and assert that it is "-210". /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == "-210"); /// /// // Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(s); /// /// /// ' Create, initialize, and set the value of x to -210. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -210) /// /// ' Retrieve the string value of x, and assert that it is "-210". /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x) /// Assert.IsTrue(s.ToString() = "-210") /// /// ' Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(s) /// /// public static char_ptr mpz_get_str(char_ptr str, int @base, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return new char_ptr(SafeNativeMethods.__gmpz_get_str(str.ToIntPtr(), @base, op.ToIntPtr())); } /// /// Return the value of as an unsigned long. /// /// The integer. /// The value of as an unsigned long. /// /// /// If is too big to fit an unsigned long then just the least significant /// bits that do fit are returned. The sign of is ignored, only the absolute /// value is used. /// /// /// mpz_get_d /// mpz_get_d_2exp /// mpz_get_si /// mpz_get_str /// Converting Integers /// GNU MP - Converting Integers /// /// /// // Create, initialize, and set the value of x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10U); /// /// // Retrieve the value of x, and assert that it is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) == 10U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10UI) /// /// ' Retrieve the value of x, and assert that it is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) = 10UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static uint /*unsigned long int*/ mpz_get_ui(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_get_ui(op.ToIntPtr()); } /// /// Return limb number from . /// /// The operand integer. /// The zero-based limb index. /// The limb number from . /// /// /// The sign of is ignored, just the absolute value is used. /// The least significant limb is number 0. /// /// /// mpz_size can be used to find how many limbs make up . /// mpz_getlimbn returns zero if is outside the range 0 /// to mpz_size() - 1. /// /// /// _mpz_realloc /// mpz_size /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_modify /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t op = new mpz_t(); /// char_ptr value = new char_ptr("1000 ABCD 1234 7AB8 24FD"); /// gmp_lib.mpz_init_set_str(op, value, 16); /// /// // Assert the value of the limbs of op. /// if (gmp_lib.mp_bytes_per_limb == 4) /// { /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 0) == 0x7AB824FD); /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 1) == 0xABCD1234); /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 2) == 0x00001000); /// } /// else // gmp_lib.mp_bytes_per_limb == 8 /// { /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 0) == 0xABCD12347AB824FD); /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 1) == 0x0000000000001000); /// } /// /// // Release unmanaged memory allocated for op and value. /// gmp_lib.mpz_clear(op); /// gmp_lib.free(value); /// /// /// ' Create and initialize new integer x. /// Dim op As New mpz_t() /// Dim value As New char_ptr("1000 ABCD 1234 7AB8 24FD") /// gmp_lib.mpz_init_set_str(op, value, 16) /// /// ' Assert the value of the limbs of op. /// If gmp_lib.mp_bytes_per_limb = 4 Then /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 0) = &H7ab824fd) /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 1) = &Habcd1234UI) /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 2) = &H1000) /// Else ' gmp_lib.mp_bytes_per_limb == 8 /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 0) = &Habcd12347ab824fdUL) /// Assert.IsTrue(gmp_lib.mpz_getlimbn(op, 1) = &H1000) /// End If /// /// ' Release unmanaged memory allocated for op and value. /// gmp_lib.mpz_clear(op) /// gmp_lib.free(value) /// /// public static mp_limb_t mpz_getlimbn(/*const*/ mpz_t op, mp_size_t n) { if (op == null) throw new ArgumentNullException("op"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpz_getlimbn_x86(op.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpz_getlimbn_x64(op.ToIntPtr(), n)); } /// /// Return the hamming distance between the two operands. /// /// The first operanf integer. /// The second operanf integer. /// The hamming distance between the two operands. /// /// /// If and are both ≥ 0 or both < 0, /// return the hamming distance between the two operands, which is the number of bit positions where /// and have different bit values. If one operand is /// ≥ 0 and the other < 0 then the number of bits different is infinite, and the /// return value is the largest possible mp_bitcnt_t. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Assert that the Hamming distance between op1 and op2 is 5. /// Assert.IsTrue(gmp_lib.mpz_hamdist(op1, op2) == 5U); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Assert that the Hamming distance between op1 and op2 is 5. /// Assert.IsTrue(gmp_lib.mpz_hamdist(op1, op2) = 5UI) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpz_clears(op1, op2, Nothing) /// /// public static mp_bitcnt_t mpz_hamdist(/*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return new mp_bitcnt_t(SafeNativeMethods.__gmpz_hamdist(op1.ToIntPtr(), op2.ToIntPtr())); } /// /// Set from an array of word data at . /// /// The result integer. /// The number of words to read. /// 1 for most significant word first or -1 for least significant first. /// The number of bytes in each word. /// 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. /// The number of most significant bits to skip. /// The operand integer. /// /// /// The parameters specify the format of the data. /// many words are read, each bytes. /// can be 1 for most significant word first or -1 for least significant first. /// Within each word endian can be 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. /// The most significant bits of each word are skipped, this can be 0 to use the full words. /// /// /// There is no sign taken from the data, will simply be a positive integer. /// An application can handle any sign itself, and apply it for instance with mpz_neg. /// /// /// There are no data alignment restrictions on , any address is allowed. /// /// /// Here’s an example converting an array of unsigned long data, most significant element first, and host byte order within each value. /// /// /// unsigned long a[20]; /// /* Initialize z and a */ /// mpz_import(z, 20, 1, sizeof(a[0]), 0, 0, a); /// /// /// This example assumes the full sizeof bytes are used for data in the given type, which is usually true, /// and certainly true for unsigned long everywhere we know of. However on Cray vector systems it may be noted that short /// and int are always stored in 8 bytes (and with sizeof indicating that) but use only 32 or 46 bits. /// The feature can account for this, by passing for instance 8 * sizeof(int) - INT_BIT. /// /// /// O:Math.Gmp.Native.gmp_lib.mpz_export /// Integer Import and Export /// GNU MP - Integer Import and Export /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Copy 0x800000000000000000000001, 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// void_ptr data = gmp_lib.allocate(12); /// Marshal.Copy(new byte[] { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, 0, data.ToIntPtr(), 12); /// /// // Import value into rop. /// gmp_lib.mpz_import(rop, 3, -1, 4, 1, 0, data); /// /// // Assert the value of rop. /// char_ptr value = gmp_lib.mpz_get_str(char_ptr.Zero, 16, rop); /// Assert.IsTrue(value.ToString() == "800000000000000000000001"); /// /// // Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(rop); /// gmp_lib.free(data); /// gmp_lib.free(value); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Copy 0x800000000000000000000001, 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. /// Dim data As void_ptr = gmp_lib.allocate(12) /// Marshal.Copy(New Byte() { &H0, &H0, &H0, &H1, &H0, &H0, &H0, &H0, &H80, &H0, &H0, &H0}, 0, data.ToIntPtr(), 12) /// /// ' Import value into rop. /// gmp_lib.mpz_import(rop, 3, -1, 4, 1, 0, data) /// /// ' Assert the value of rop. /// Dim value As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 16, rop) /// Assert.IsTrue(value.ToString() = "800000000000000000000001") /// /// ' Release unmanaged memory allocated for rop, data, and value. /// gmp_lib.mpz_clear(rop) /// gmp_lib.free(data) /// gmp_lib.free(value) /// /// public static void mpz_import(mpz_t rop, size_t count, int order, size_t size, int endian, size_t nails, void_ptr op) { if (rop == null) throw new ArgumentNullException("rop"); if (IntPtr.Size == 4) SafeNativeMethods.__gmpz_import_x86(rop.ToIntPtr(), (uint)count, order, (uint)size, endian, (uint)nails, op.ToIntPtr()); else SafeNativeMethods.__gmpz_import_x64(rop.ToIntPtr(), count, order, size, endian, nails, op.ToIntPtr()); } /// /// Initialize , and set its value to 0. /// /// The integer. /// mpz_clear /// mpz_clears /// mpz_inits /// mpz_init2 /// mpz_realloc2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Assert that the value of x is 0. /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == "0"); /// /// // Release unmanaged memory allocated for x and its string value. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(s); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Assert that the value of x is 0. /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x) /// Assert.IsTrue(s.ToString() = "0") /// /// ' Release unmanaged memory allocated for x and its string value. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(s) /// /// public static void mpz_init(mpz_t x) { if (x == null) throw new ArgumentNullException("x"); x.Initializing(); SafeNativeMethods.__gmpz_init(x.ToIntPtr()); } /// /// Initialize , with space for -bit numbers, and set its value to 0. /// /// The integer. /// The number of bits. /// /// /// Calling this function instead of mpz_init or mpz_inits /// is never necessary; reallocation is handled automatically by GMP when needed. /// /// /// While defines the initial space, will grow automatically in the normal way, /// if necessary, for subsequent values stored. /// mpz_init2 makes it possible to avoid such reallocations if a maximum size is known in advance. /// /// /// In preparation for an operation, GMP often allocates one limb more than ultimately needed. /// To make sure GMP will not perform reallocation for , you need to add the number of bits /// in mp_limb_t to . /// /// /// mpz_clear /// mpz_clears /// mpz_init /// mpz_inits /// mpz_realloc2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create a new integer x, and initialize its size to 300 bits. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init2(x, 300); /// /// // Assert that the value of x is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create a new integer x, and initialize its size to 300 bits. /// Dim x As New mpz_t() /// gmp_lib.mpz_init2(x, 300) /// /// ' Assert that the value of x is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_init2(mpz_t x, mp_bitcnt_t n) { if (x == null) throw new ArgumentNullException("x"); x.Initializing(); SafeNativeMethods.__gmpz_init2(x.ToIntPtr(), n); } /// /// Initialize a NULL-terminated list of mpz_t variables, and set their values to 0. /// /// A NULL-terminated list of mpz_t variables. /// mpz_clear /// mpz_clears /// mpz_init /// mpz_init2 /// mpz_realloc2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create new integers x1, x2 and x3. /// mpz_t x1 = new mpz_t(); /// mpz_t x2 = new mpz_t(); /// mpz_t x3 = new mpz_t(); /// /// // Initialize the integers. /// gmp_lib.mpz_inits(x1, x2, x3, null); /// /// // Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x1) == 0); /// Assert.IsTrue(gmp_lib.mpz_get_si(x2) == 0); /// Assert.IsTrue(gmp_lib.mpz_get_si(x3) == 0); /// /// // Release unmanaged memory allocated for the integers. /// gmp_lib.mpz_clears(x1, x2, x3, null); /// /// /// ' Create new integers x1, x2 and x3. /// Dim x1 As New mpz_t() /// Dim x2 As New mpz_t() /// Dim x3 As New mpz_t() /// /// ' Initialize the integers. /// gmp_lib.mpz_inits(x1, x2, x3, Nothing) /// /// ' Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(x1) = 0) /// Assert.IsTrue(gmp_lib.mpz_get_si(x2) = 0) /// Assert.IsTrue(gmp_lib.mpz_get_si(x3) = 0) /// /// ' Release unmanaged memory allocated for the integers. /// gmp_lib.mpz_clears(x1, x2, x3, Nothing) /// /// public static void mpz_inits(params mpz_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpz_t a in x) { if (a != null) mpz_init(a); } } /// /// Initialize with limb space and set the initial numeric value from . /// /// The destination integer. /// The source integer. /// mpz_init_set_ui /// mpz_init_set_si /// mpz_init_set_d /// mpz_init_set_str /// Simultaneous Integer Init & Assign /// GNU MP - Combined Integer Initialization and Assignment /// /// /// // Create, initialize, and set a new integer y to -210. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init(y); /// gmp_lib.mpz_set_si(y, -210); /// /// // Create, initialize, and set a new integer x to the value of y. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set(x, y); /// /// // Assert that x is equal to the value of y. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == -210); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, null); /// /// /// ' Create, initialize, and set a new integer y to -210. /// Dim y As New mpz_t() /// /// gmp_lib.mpz_init(y) /// gmp_lib.mpz_set_si(y, -210) /// /// ' Create, initialize, and set a new integer x to the value of y. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set(x, y) /// /// ' Assert that x is equal to the value of y. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = -210) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, Nothing) /// /// public static void mpz_init_set(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); rop.Initializing(); SafeNativeMethods.__gmpz_init_set(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Initialize with limb space and set the initial numeric value from . /// /// The destination integer. /// The source integer. /// /// /// mpz_init_set_d truncate to make it an integer. /// /// /// mpz_init_set /// mpz_init_set_ui /// mpz_init_set_si /// mpz_init_set_str /// Simultaneous Integer Init & Assign /// GNU MP - Combined Integer Initialization and Assignment /// /// /// // Create, initialize, and set the value of x to the truncation of 10.7. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_d(x, 10.7D); /// /// // Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 10); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to the truncation of 10.7. /// Dim x As New mpz_t() /// /// gmp_lib.mpz_init_set_d(x, 10.7) /// /// ' Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 10) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_init_set_d(mpz_t rop, double op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpz_init_set_d(rop.ToIntPtr(), op); } /// /// Initialize with limb space and set the initial numeric value from . /// /// The destination integer. /// The source integer. /// mpz_init_set /// mpz_init_set_ui /// mpz_init_set_d /// mpz_init_set_str /// Simultaneous Integer Init & Assign /// GNU MP - Combined Integer Initialization and Assignment /// /// /// // Create, initialize, and set the value of x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, 10); /// /// // Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 10); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10. /// Dim x As New mpz_t() /// /// gmp_lib.mpz_init_set_si(x, 10) /// /// ' Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 10) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_init_set_si(mpz_t rop, int /*long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpz_init_set_si(rop.ToIntPtr(), op); } /// /// Initialize and set its value like mpz_set_str. /// /// The destination integer. /// The source integer. /// The base. /// If the string is a correct base number, the function returns 0; if an error occurs it returns −1. is initialized even if an error occurs. /// /// /// See mpz_set_str for details. /// /// /// mpz_init_set /// mpz_init_set_ui /// mpz_init_set_si /// mpz_init_set_d /// Simultaneous Integer Init & Assign /// GNU MP - Combined Integer Initialization and Assignment /// /// /// // Create, initialize, and set the value of x. /// mpz_t x = new mpz_t(); /// char_ptr value = new char_ptr(" 1 234 567 890 876 543 211 234 567 890 987 654 321 "); /// gmp_lib.mpz_init_set_str(x, value, 10); /// /// // Assert the value of x. /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == value.ToString().Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// gmp_lib.free(s); /// /// /// ' Create, initialize, and set the value of x. /// Dim x As New mpz_t() /// Dim value As New char_ptr(" 1 234 567 890 876 543 211 234 567 890 987 654 321 ") /// gmp_lib.mpz_init_set_str(x, value, 10) /// /// ' Assert the value of x. /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x) /// /// Assert.IsTrue(s.ToString() = value.ToString().Replace(" ", "")) /// ' Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// gmp_lib.free(s) /// /// public static int mpz_init_set_str(mpz_t rop, /*const*/ char_ptr str, int @base) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); return SafeNativeMethods.__gmpz_init_set_str(rop.ToIntPtr(), str.ToIntPtr(), @base); } /// /// Initialize with limb space and set the initial numeric value from . /// /// The destination integer. /// The source integer. /// mpz_init_set /// mpz_init_set_si /// mpz_init_set_d /// mpz_init_set_str /// Simultaneous Integer Init & Assign /// GNU MP - Combined Integer Initialization and Assignment /// /// /// // Create, initialize, and set the value of x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10U); /// /// // Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) == 10U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10UI) /// /// ' Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) = 10UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_init_set_ui(mpz_t rop, uint /*unsigned long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpz_init_set_ui(rop.ToIntPtr(), op); } /// /// Input from stdio stream in the format written by mpz_out_raw, and put the result in . /// /// The result operand. /// Pointer to file stream. /// Return the number of bytes read, or if an error occurred, return 0. /// /// /// This routine can read the output from mpz_out_raw also from GMP 1, /// in spite of changes necessary for compatibility between 32-bit and 64-bit machines. /// /// /// mpz_out_str /// mpz_inp_str /// mpz_out_raw /// I/O of Integers /// GNU MP - I/O of Integers /// /// /// // Create, initialize, and set the value of op to 123456. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 123456U); /// /// // Write op to a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// Assert.IsTrue(gmp_lib.mpz_out_raw(stream, op) == 7); /// fclose(stream.Value.Value); /// /// // Read op from the temporary file, and assert that the number of bytes read is 6. /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.mpz_inp_raw(op, stream) == 7); /// fclose(stream.Value.Value); /// /// // Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpz_get_ui(op) == 123456U); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 123456. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 123456UI) /// /// ' Write op to a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "w") /// Assert.IsTrue(gmp_lib.mpz_out_raw(stream, op) = 7) /// fclose(stream.Value.Value) /// /// ' Read op from the temporary file, and assert that the number of bytes read is 6. /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.mpz_inp_raw(op, stream) = 7) /// fclose(stream.Value.Value) /// /// ' Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpz_get_ui(op) = 123456UI) /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static size_t mpz_inp_raw(mpz_t rop, ptr stream) { if (rop == null) throw new ArgumentNullException("rop"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpz_inp_raw_x86(rop.ToIntPtr(), stream.Value.Value)); else return new size_t(SafeNativeMethods.__gmpz_inp_raw_x64(rop.ToIntPtr(), stream.Value.Value)); } /// /// Input a possibly white-space preceded string in base from stdio stream , and put the read integer in . /// /// The result integer. /// Pointer to file stream. /// The base operand. /// Return the number of bytes read, or if an error occurred, return 0. /// /// /// The may vary from 2 to 62, or if base is 0, /// then the leading characters are used: 0x and 0X for hexadecimal, /// 0b and 0B for binary, 0 for octal, or decimal otherwise. /// /// /// For bases up to 36, case is ignored; upper-case and lower-case letters have the same value. /// For bases 37 to 62, upper-case letter represent the usual 10..35 while /// lower-case letter represent 36..61. /// /// /// mpz_out_str /// mpz_out_raw /// mpz_inp_raw /// I/O of Integers /// GNU MP - I/O of Integers /// /// /// // Create and initialize op. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init(op); /// /// // Write op to a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// System.IO.File.WriteAllText(pathname, "123456"); /// /// // Read op from the temporary file, and assert that the number of bytes read is 6. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.mpz_inp_str(op, stream, 10) == 6); /// fclose(stream.Value.Value); /// /// // Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpz_get_ui(op) == 123456U); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create and initialize op. /// Dim op As New mpz_t() /// gmp_lib.mpz_init(op) /// /// ' Write op to a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// System.IO.File.WriteAllText(pathname, "123456") /// /// ' Read op from the temporary file, and assert that the number of bytes read is 6. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.mpz_inp_str(op, stream, 10) = 6) /// fclose(stream.Value.Value) /// /// ' Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpz_get_ui(op) = 123456UI) /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static size_t mpz_inp_str(mpz_t rop, ptr stream, int @base) { if (rop == null) throw new ArgumentNullException("rop"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpz_inp_str_x86(rop.ToIntPtr(), stream.Value.Value, @base)); else return new size_t(SafeNativeMethods.__gmpz_inp_str_x64(rop.ToIntPtr(), stream.Value.Value, @base)); } /// /// Compute the inverse of modulo and put the result in . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// If the inverse exists, the return value is non-zero. If an inverse doesn’t exist the return value is zero. /// /// /// If the inverse exists, the return value is non-zero and will satisfy /// 0 ≤ < | | (with = 0 /// possible only when | | = 1, i.e., in the somewhat degenerate zero ring). /// If an inverse doesn’t exist the return value is zero and is undefined. /// The behaviour of this function is undefined when is zero. /// /// /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op1 to 3. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 3U); /// /// // Create, initialize, and set the value of op2 to 11. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 11U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the modular inverse of op1 mod op2, i.e. b, where op1 * b mod op1 = 1. /// gmp_lib.mpz_invert(rop, op1, op2); /// /// // Assert that rop is 4, /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 4); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 3. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 3UI) /// /// ' Create, initialize, and set the value of op2 to 11. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 11UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the modular inverse of op1 mod op2, i.e. b, where op1 * b mod op1 = 1. /// gmp_lib.mpz_invert(rop, op1, op2) /// /// ' Assert that rop is 4, /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 4) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static int mpz_invert(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpz_invert(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to bitwise inclusive-or . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the bitwise inclusive or of op1 and op2. /// gmp_lib.mpz_ior(rop, op1, op2); /// /// // Assert that rop is 127. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 127); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the bitwise inclusive or of op1 and op2. /// gmp_lib.mpz_ior(rop, op1, op2) /// /// ' Assert that rop is 127. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 127) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static void mpz_ior(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_ior(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Calculate the Jacobi symbol (/). /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/). /// /// /// This is defined only for odd. /// /// /// mpz_legendre /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of a to 11. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 11U); /// /// // Create, initialize, and set the value of b to 9. /// mpz_t b = new mpz_t(); /// gmp_lib.mpz_init_set_ui(b, 9U); /// /// // Assert that the Jacobi symbol of (a/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_jacobi(a, b) == 1); /// /// // Release unmanaged memory allocated for a and b. /// gmp_lib.mpz_clears(a, b, null); /// /// /// ' Create, initialize, and set the value of a to 11. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 11UI) /// /// ' Create, initialize, and set the value of b to 9. /// Dim b As New mpz_t() /// gmp_lib.mpz_init_set_ui(b, 9UI) /// /// ' Assert that the Jacobi symbol of (a/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_jacobi(a, b) = 1) /// /// ' Release unmanaged memory allocated for a and b. /// gmp_lib.mpz_clears(a, b, Nothing) /// /// public static int mpz_jacobi(/*const*/ mpz_t a, /*const*/ mpz_t b) { if (a == null) throw new ArgumentNullException("a"); if (b == null) throw new ArgumentNullException("b"); return SafeNativeMethods.__gmpz_jacobi(a.ToIntPtr(), b.ToIntPtr()); } /// /// Calculate the Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// /// When is odd the Jacobi symbol and Kronecker symbol are identical, /// so mpz_kronecker_ui, etc. can be used for mixed precision Jacobi symbols too. /// /// /// mpz_kronecker_si /// mpz_kronecker_ui /// mpz_legendre /// mpz_si_kronecker /// mpz_ui_kronecker /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of a to 15. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 15U); /// /// // Create, initialize, and set the value of b to 4. /// mpz_t b = new mpz_t(); /// gmp_lib.mpz_init_set_ui(b, 4U); /// /// // Assert that the Kronecker symbol of (a/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker(a, b) == 1); /// /// // Release unmanaged memory allocated for a and b. /// gmp_lib.mpz_clears(a, b, null); /// /// /// ' Create, initialize, and set the value of a to 15. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 15UI) /// /// ' Create, initialize, and set the value of b to 4. /// Dim b As New mpz_t() /// gmp_lib.mpz_init_set_ui(b, 4UI) /// /// ' Assert that the Kronecker symbol of (a/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker(a, b) = 1) /// /// ' Release unmanaged memory allocated for a and b. /// gmp_lib.mpz_clears(a, b, Nothing) /// /// public static int mpz_kronecker(/*const*/ mpz_t a, /*const*/ mpz_t b) { if (a == null) throw new ArgumentNullException("a"); if (b == null) throw new ArgumentNullException("b"); return SafeNativeMethods.__gmpz_jacobi(a.ToIntPtr(), b.ToIntPtr()); } /// /// Calculate the Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// /// When is odd the Jacobi symbol and Kronecker symbol are identical, /// so mpz_kronecker_ui, etc. can be used for mixed precision Jacobi symbols too. /// /// /// mpz_kronecker /// mpz_kronecker_ui /// mpz_legendre /// mpz_si_kronecker /// mpz_ui_kronecker /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of a to 15. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 15U); /// /// // Assert that the Kronecker symbol of (a/4) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker_si(a, 4) == 1); /// /// // Release unmanaged memory allocated for a. /// gmp_lib.mpz_clear(a); /// /// /// ' Create, initialize, and set the value of a to 15. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 15UI) /// /// ' Assert that the Kronecker symbol of (a/4) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker_si(a, 4) = 1) /// /// ' Release unmanaged memory allocated for a. /// gmp_lib.mpz_clear(a) /// /// public static int mpz_kronecker_si(/*const*/ mpz_t a, int /*long int*/ b) { if (a == null) throw new ArgumentNullException("a"); return SafeNativeMethods.__gmpz_kronecker_si(a.ToIntPtr(), b); } /// /// Calculate the Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// /// When is odd the Jacobi symbol and Kronecker symbol are identical, /// so mpz_kronecker_ui, etc. can be used for mixed precision Jacobi symbols too. /// /// /// mpz_kronecker /// mpz_kronecker_si /// mpz_legendre /// mpz_si_kronecker /// mpz_ui_kronecker /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of a to 15. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 15U); /// /// // Assert that the Kronecker symbol of (a/4) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker_ui(a, 4U) == 1); /// /// // Release unmanaged memory allocated for a. /// gmp_lib.mpz_clear(a); /// /// /// ' Create, initialize, and set the value of a to 15. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 15UI) /// /// ' Assert that the Kronecker symbol of (a/4) is 1. /// Assert.IsTrue(gmp_lib.mpz_kronecker_ui(a, 4UI) = 1) /// /// ' Release unmanaged memory allocated for a. /// gmp_lib.mpz_clear(a) /// /// public static int mpz_kronecker_ui(/*const*/ mpz_t a, uint /*unsigned long int*/ b) { if (a == null) throw new ArgumentNullException("a"); return SafeNativeMethods.__gmpz_kronecker_ui(a.ToIntPtr(), b); } /// /// Calculate the Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// /// When is odd the Jacobi symbol and Kronecker symbol are identical, /// so mpz_kronecker_ui, etc. can be used for mixed precision Jacobi symbols too. /// /// /// mpz_kronecker /// mpz_kronecker_si /// mpz_kronecker_ui /// mpz_legendre /// mpz_ui_kronecker /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of b to 4. /// mpz_t b = new mpz_t(); /// gmp_lib.mpz_init_set_ui(b, 4U); /// /// // Assert that the Kronecker symbol of (15/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_si_kronecker(15, b) == 1); /// /// // Release unmanaged memory allocated for b. /// gmp_lib.mpz_clear(b); /// /// /// ' Create, initialize, and set the value of b to 4. /// Dim b As New mpz_t() /// gmp_lib.mpz_init_set_ui(b, 4UI) /// /// ' Assert that the Kronecker symbol of (15/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_si_kronecker(15, b) = 1) /// /// ' Release unmanaged memory allocated for b. /// gmp_lib.mpz_clear(b) /// /// public static int mpz_si_kronecker(int /*long int*/ a, /*const*/ mpz_t b) { if (b == null) throw new ArgumentNullException("b"); return SafeNativeMethods.__gmpz_si_kronecker(a, b.ToIntPtr()); } /// /// Calculate the Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// The first operand integer. /// The second operand integer. /// The Jacobi symbol (/) with the Kronecker extension (/2) = (2/) when odd, or (/2) = 0 when even. /// /// /// When is odd the Jacobi symbol and Kronecker symbol are identical, /// so mpz_kronecker_ui, etc. can be used for mixed precision Jacobi symbols too. /// /// /// mpz_kronecker /// mpz_kronecker_si /// mpz_kronecker_ui /// mpz_legendre /// mpz_si_kronecker /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of b to 4. /// mpz_t b = new mpz_t(); /// gmp_lib.mpz_init_set_ui(b, 4U); /// /// // Assert that the Kronecker symbol of (15/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_ui_kronecker(15U, b) == 1); /// /// // Release unmanaged memory allocated for b. /// gmp_lib.mpz_clear(b); /// /// /// ' Create, initialize, and set the value of b to 4. /// Dim b As New mpz_t() /// gmp_lib.mpz_init_set_ui(b, 4UI) /// /// ' Assert that the Kronecker symbol of (15/b) is 1. /// Assert.IsTrue(gmp_lib.mpz_ui_kronecker(15UI, b) = 1) /// /// ' Release unmanaged memory allocated for b. /// gmp_lib.mpz_clear(b) /// /// public static int mpz_ui_kronecker(uint /*unsigned long int*/ a, /*const*/ mpz_t b) { if (b == null) throw new ArgumentNullException("b"); return SafeNativeMethods.__gmpz_ui_kronecker(a, b.ToIntPtr()); } /// /// Set to the least common multiple of and . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// is always positive, irrespective of the signs of and . /// will be zero if either or is zero. /// /// /// mpz_lcm_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op1 to 2. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 2U); /// /// // Create, initialize, and set the value of op2 to 3. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 3U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the least common multiple of op1 and op2. /// gmp_lib.mpz_lcm(rop, op1, op2); /// /// // Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 6); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 2. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 2UI) /// /// ' Create, initialize, and set the value of op2 to 3. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 3UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the least common multiple of op1 and op2. /// gmp_lib.mpz_lcm(rop, op1, op2) /// /// ' Assert that rop is 6. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 6) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static void mpz_lcm(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_lcm(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to the least common multiple of and . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// is always positive, irrespective of the signs of and . /// will be zero if either or is zero. /// /// /// mpz_lcm /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// /// /// /// public static void mpz_lcm_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_lcm_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Calculate the Legendre symbol (/). /// /// The first operand integer. /// The second operand integer. /// The Legendre symbol (/). /// /// /// This is defined only for an odd positive prime, /// and for such it’s identical to the Jacobi symbol. /// /// /// mpz_jacobi /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of a to 20. /// mpz_t a = new mpz_t(); /// gmp_lib.mpz_init_set_ui(a, 20U); /// /// // Create, initialize, and set the value of p to 11. /// mpz_t p = new mpz_t(); /// gmp_lib.mpz_init_set_ui(p, 11U); /// /// // Assert that the Legendre symbol of (a/p) is 1. /// Assert.IsTrue(gmp_lib.mpz_legendre(a, p) == 1); /// /// // Release unmanaged memory allocated for a and p. /// gmp_lib.mpz_clears(a, p, null); /// /// /// ' Create, initialize, and set the value of a to 20. /// Dim a As New mpz_t() /// gmp_lib.mpz_init_set_ui(a, 20UI) /// /// ' Create, initialize, and set the value of p to 11. /// Dim p As New mpz_t() /// gmp_lib.mpz_init_set_ui(p, 11UI) /// /// ' Assert that the Legendre symbol of (a/p) is 1. /// Assert.IsTrue(gmp_lib.mpz_legendre(a, p) = 1) /// /// ' Release unmanaged memory allocated for a and p. /// gmp_lib.mpz_clears(a, p, Nothing) /// /// public static int mpz_legendre(/*const*/ mpz_t a, /*const*/ mpz_t p) { if (a == null) throw new ArgumentNullException("a"); if (p == null) throw new ArgumentNullException("p"); return SafeNativeMethods.__gmpz_legendre(a.ToIntPtr(), p.ToIntPtr()); } /// /// Sets to to L[], the ’th Lucas number. /// /// The L[] result. /// The operand integer. /// /// /// The Fibonacci numbers and Lucas numbers are related sequences, so it’s never necessary to call both /// mpz_fib2_ui and mpz_lucnum2_ui. /// The formulas for going from Fibonacci to Lucas can be found in /// GNU MP - Lucas Numbers Algorithm, /// the reverse is straightforward too. /// /// /// mpz_lucnum2_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of ln to 0. /// mpz_t ln = new mpz_t(); /// gmp_lib.mpz_init(ln); /// /// // Set ln to the 9'th Lucas number. /// gmp_lib.mpz_lucnum_ui(ln, 9U); /// /// // Assert that ln is 76. /// Assert.IsTrue(gmp_lib.mpz_get_si(ln) == 76); /// /// // Release unmanaged memory allocated for ln. /// gmp_lib.mpz_clear(ln); /// /// /// ' Create, initialize, and set the value of ln to 0. /// Dim ln As New mpz_t() /// gmp_lib.mpz_init(ln) /// /// ' Set ln to the 9'th Lucas number. /// gmp_lib.mpz_lucnum_ui(ln, 9UI) /// /// ' Assert that ln is 76. /// Assert.IsTrue(gmp_lib.mpz_get_si(ln) = 76) /// /// ' Release unmanaged memory allocated for ln. /// gmp_lib.mpz_clear(ln) /// /// public static void mpz_lucnum_ui(mpz_t ln, uint /*unsigned long int*/ n) { if (ln == null) throw new ArgumentNullException("ln"); SafeNativeMethods.__gmpz_lucnum_ui(ln.ToIntPtr(), n); } /// /// Sets to L[], and to L[ - 1]. /// /// The L[] result. /// The L[ - 1] result. /// The operand integer. /// /// /// This function is designed for calculating isolated Lucas numbers. /// When a sequence of values is wanted it’s best to start with mpz_lucnum2_ui /// and iterate the defining L[n + 1] = L[n] + L[n - 1] or similar. /// /// /// The Fibonacci numbers and Lucas numbers are related sequences, so it’s never necessary to call both /// mpz_fib2_ui and mpz_lucnum2_ui. /// The formulas for going from Fibonacci to Lucas can be found in /// GNU MP - Lucas Numbers Algorithm, /// the reverse is straightforward too. /// /// /// mpz_lucnum_ui /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the values of lnsub1 and ln to 0. /// mpz_t ln = new mpz_t(); /// mpz_t lnsub1 = new mpz_t(); /// gmp_lib.mpz_inits(ln, lnsub1, null); /// /// // Set lnsub1 and ln to the 8'th and 9'th Lucas nunbers respectively. /// gmp_lib.mpz_lucnum2_ui(ln, lnsub1, 9U); /// /// // Assert that lnsub1 and ln are respectively 47 and 76. /// Assert.IsTrue(gmp_lib.mpz_get_si(lnsub1) == 47); /// Assert.IsTrue(gmp_lib.mpz_get_si(ln) == 76); /// /// // Release unmanaged memory allocated for ln and lnsub1. /// gmp_lib.mpz_clears(ln, lnsub1, null); /// /// /// ' Create, initialize, and set the values of lnsub1 and ln to 0. /// Dim ln As New mpz_t() /// Dim lnsub1 As New mpz_t() /// gmp_lib.mpz_inits(ln, lnsub1, Nothing) /// /// ' Set lnsub1 and ln to the 8'th and 9'th Lucas nunbers respectively. /// gmp_lib.mpz_lucnum2_ui(ln, lnsub1, 9UI) /// /// ' Assert that lnsub1 and ln are respectively 47 and 76. /// Assert.IsTrue(gmp_lib.mpz_get_si(lnsub1) = 47) /// Assert.IsTrue(gmp_lib.mpz_get_si(ln) = 76) /// /// ' Release unmanaged memory allocated for ln and lnsub1. /// gmp_lib.mpz_clears(ln, lnsub1, Nothing) /// /// public static void mpz_lucnum2_ui(mpz_t ln, mpz_t lnsub1, uint /*unsigned long int*/ n) { if (ln == null) throw new ArgumentNullException("ln"); if (lnsub1 == null) throw new ArgumentNullException("lnsub1"); SafeNativeMethods.__gmpz_lucnum2_ui(ln.ToIntPtr(), lnsub1.ToIntPtr(), n); } /// /// An implementation of the probabilistic primality test found in Knuth's Seminumerical Algorithms book. /// /// The operand integer. /// The number of internal passes of the probabilistic algorithm. /// If the function mpz_millerrabin returns 0 then is not prime. If it returns 1, then is 'probably' prime. /// /// /// The probability of a false positive is (1/4)^, where /// is the number of internal passes of the probabilistic algorithm. /// Knuth indicates that 25 passes are reasonable. /// /// /// mpz_probab_prime_p /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of n to 12. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_ui(n, 12U); /// /// // Assert that n is a composite number. /// Assert.IsTrue(gmp_lib.mpz_millerrabin(n, 25) == 0); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 12. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_ui(n, 12UI) /// /// ' Assert that n is a composite number. /// Assert.IsTrue(gmp_lib.mpz_millerrabin(n, 25) = 0) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static int mpz_millerrabin(/*const*/ mpz_t n, int reps) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_millerrabin(n.ToIntPtr(), reps); } /// /// Set to mod . /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// /// /// The sign of the divisor is ignored; the result is always non-negative. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod_ui /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 12222. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 12222U); /// /// // Create, initialize, and set the value of y to 10000. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 10000U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x mod y. /// gmp_lib.mpz_mod(z, x, y); /// /// // Assert that z is 12222 mod 10000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 12222 % 10000); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 12222. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 12222UI) /// /// ' Create, initialize, and set the value of y to 10000. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 10000UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x mod y. /// gmp_lib.mpz_mod(z, x, y) /// /// ' Assert that z is 12222 mod 10000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 12222 Mod 10000) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_mod(mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_mod(r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set to mod . /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// The remainder . /// /// /// The sign of the divisor is ignored; the result is always non-negative. /// /// /// mpz_mod_ui is identical to mpz_fdiv_r_ui, returning the remainder as well as setting . /// See mpz_fdiv_ui if only the return value is wanted. /// /// /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_qr /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of x to 12222. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 12222U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x mod y, and return z. /// Assert.IsTrue(gmp_lib.mpz_mod_ui(z, x, 10000U) == 12222 % 10000); /// /// // Assert that z is 12222 mod 10000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 12222 % 10000); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to 12222. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 12222UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x mod y, and return z. /// Assert.IsTrue(gmp_lib.mpz_mod_ui(z, x, 10000UI) = 12222 Mod 10000) /// /// ' Assert that z is 12222 mod 10000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 12222 Mod 10000) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static uint /*unsigned long int*/ mpz_mod_ui(mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_fdiv_r_ui(r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set to * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul_2exp /// mpz_mul_si /// mpz_mul_ui /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 12222. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 12222U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x * y. /// gmp_lib.mpz_mul(z, x, y); /// /// // Assert that z is the product of x and y. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 10000 * 12222); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 12222. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 12222UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x * y. /// gmp_lib.mpz_mul(z, x, y) /// /// ' Assert that z is the product of x and y. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 10000 * 12222) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_mul(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_mul(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to * 2^. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// This operation can also be defined as a left shift by bits. /// /// /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_mul_si /// mpz_mul_ui /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of x to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = -10000 * 2^2. /// gmp_lib.mpz_mul_2exp(z, x, 2U); /// /// // Assert that z is -40000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == -10000 * 4); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of x to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = -10000 * 2^2. /// gmp_lib.mpz_mul_2exp(z, x, 2UI) /// /// ' Assert that z is -40000. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = -10000 * 4) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_mul_2exp(mpz_t rop, /*const*/ mpz_t op1, mp_bitcnt_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_mul_2exp(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_mul_2exp /// mpz_mul_ui /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x * 12222. /// gmp_lib.mpz_mul_si(z, x, 12222); /// /// // Assert that z is the product of x and 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == -10000 * 12222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x * 12222. /// gmp_lib.mpz_mul_si(z, x, 12222) /// /// ' Assert that z is the product of x and 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = -10000 * 12222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_mul_si(mpz_t rop, /*const*/ mpz_t op1, int /*long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_mul_si(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_mul_2exp /// mpz_mul_si /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x * 12222. /// gmp_lib.mpz_mul_ui(z, x, 12222); /// /// // Assert that z is the product of x and 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == -10000 * 12222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x * 12222. /// gmp_lib.mpz_mul_ui(z, x, 12222) /// /// ' Assert that z is the product of x and 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = -10000 * 12222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_mul_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_mul_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to -. /// /// The result integer. /// The operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = -x. /// gmp_lib.mpz_neg(z, x); /// /// // Assert that z is -x. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 10000); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = -x. /// gmp_lib.mpz_neg(z, x) /// /// ' Assert that z is -x. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 10000) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_neg(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_neg(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to the next prime greater than . /// /// The result prime integer. /// The operand integer. /// /// /// This function uses a probabilistic algorithm to identify primes. /// For practical purposes it’s adequate, the chance of a composite passing will be extremely small. /// /// /// mpz_probab_prime_p /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of n to 12. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 12U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the next following op. /// gmp_lib.mpz_nextprime(rop, op); /// /// // Assert that rop is 13. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 13); /// /// // Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, null); /// /// /// ' Create, initialize, and set the value of n to 12. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 12UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the next following op. /// gmp_lib.mpz_nextprime(rop, op) /// /// ' Assert that rop is 13. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 13) /// /// ' Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, Nothing) /// /// public static void mpz_nextprime(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_nextprime(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Determine whether is odd. /// /// The operand integer. /// Return non-zero if odd, zero if even. /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_even_p /// mpz_sizeinbase /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op to 427294. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 427294); /// /// // Assert that op is not odd but even. /// Assert.IsTrue(gmp_lib.mpz_even_p(op) > 0); /// Assert.IsTrue(gmp_lib.mpz_odd_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 427294. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 427294) /// /// ' Assert that op is not odd but even. /// Assert.IsTrue(gmp_lib.mpz_even_p(op) > 0) /// Assert.IsTrue(gmp_lib.mpz_odd_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_odd_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_size != 0 && (op._mp_d[0] & 1) == 0 ? 0 : 1; } /// /// Output on stdio stream , in raw binary format. /// /// Pointer to file streama. /// The operand integer. /// Return the number of bytes written, or if an error occurred, return 0. /// /// /// The integer is written in a portable format, with 4 bytes of size information, and that many bytes of limbs. /// Both the size and the limbs are written in decreasing significance order (i.e., in big-endian). /// /// /// The output can be read with mpz_inp_raw. /// /// /// The output of this can not be read by mpz_inp_raw from GMP 1, /// because of changes necessary for compatibility between 32-bit and 64-bit machines. /// /// /// mpz_out_str /// mpz_inp_str /// mpz_inp_raw /// I/O of Integers /// GNU MP - I/O of Integers /// /// /// // Create, initialize, and set the value of op to 123456 (0x1E240). /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 0x1E240); /// /// // Get a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// /// // Open temporary file for writing. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// /// // Write op to temporary file, and assert that the number of bytes written is 7. /// Assert.IsTrue(gmp_lib.mpz_out_raw(stream, op) == 7); /// /// // Close temporary file. /// fclose(stream.Value.Value); /// /// // Assert that the content of the temporary file. /// byte[] r = System.IO.File.ReadAllBytes(pathname); /// Assert.IsTrue(r[0] == 0 && r[1] == 0 && r[2] == 0 && r[3] == 3 && r[4] == 0x01 && r[5] == 0xE2 && r[6] == 0x40); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 123456 (0x1E240). /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, &H1e240) /// /// ' Get a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// /// ' Open temporary file for writing. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "w") /// /// ' Write op to temporary file, and assert that the number of bytes written is 7. /// Assert.IsTrue(gmp_lib.mpz_out_raw(stream, op) = 7) /// /// ' Close temporary file. /// fclose(stream.Value.Value) /// /// ' Assert that the content of the temporary file. /// Dim r As Byte() = System.IO.File.ReadAllBytes(pathname) /// Assert.IsTrue(r(0) = 0 AndAlso r(1) = 0 AndAlso r(2) = 0 AndAlso r(3) = 3 AndAlso r(4) = &H1 AndAlso r(5) = &He2 AndAlso r(6) = &H40) /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static size_t mpz_out_raw(ptr stream, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpz_out_raw_x86(stream.Value.Value, op.ToIntPtr())); else return new size_t(SafeNativeMethods.__gmpz_out_raw_x64(stream.Value.Value, op.ToIntPtr())); } /// /// Output on stdio stream , as a string of digits in base . /// /// Pointer to file stream. /// The base operand. /// The operand integer. /// Return the number of bytes written, or if an error occurred, return 0. /// /// /// The argument may vary from 2 to 62 or from -2 to -36. /// /// /// For in the range 2..36, digits and lower-case letters are used; /// for -2..-36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, /// and lower-case letters (in that significance order) are used. /// /// /// mpz_inp_str /// mpz_out_raw /// mpz_inp_raw /// I/O of Integers /// GNU MP - I/O of Integers /// /// /// // Create, initialize, and set the value of op to 123456. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 123456U); /// /// // Get a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// /// // Open temporary file for writing. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// /// // Write op to temporary file, and assert that the number of bytes written is 6. /// Assert.IsTrue(gmp_lib.mpz_out_str(stream, 10, op) == 6); /// /// // Close temporary file. /// fclose(stream.Value.Value); /// /// // Assert that the content of the temporary file is "123456". /// string result = System.IO.File.ReadAllText(pathname); /// Assert.IsTrue(result == "123456"); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 123456. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 123456UI) /// /// ' Get a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// /// ' Open temporary file for writing. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "w") /// /// ' Write op to temporary file, and assert that the number of bytes written is 6. /// Assert.IsTrue(gmp_lib.mpz_out_str(stream, 10, op) = 6) /// /// ' Close temporary file. /// fclose(stream.Value.Value) /// /// ' Assert that the content of the temporary file is "123456". /// Dim result As String = System.IO.File.ReadAllText(pathname) /// Assert.IsTrue(result = "123456") /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static size_t mpz_out_str(ptr stream, int @base, /*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpz_out_str_x86(stream.Value.Value, @base, op.ToIntPtr())); else return new size_t(SafeNativeMethods.__gmpz_out_str_x64(stream.Value.Value, @base, op.ToIntPtr())); } /// /// Return non-zero if is a perfect power, i.e., if there exist integers a and b, with b > 1, such that = a^b. /// /// The operand integer. /// Non-zero if is a perfect power, i.e., if there exist integers a and b, with b > 1, such that = a^b. /// /// /// Under this definition both 0 and 1 are considered to be perfect powers. Negative values of are accepted, but of course can only be odd perfect powers. /// /// /// mpz_perfect_square_p /// mpz_root /// mpz_rootrem /// mpz_sqrt /// mpz_sqrtrem /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Assert that op is a perfect power. /// Assert.IsTrue(gmp_lib.mpz_perfect_power_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Assert that op is a perfect power. /// Assert.IsTrue(gmp_lib.mpz_perfect_power_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_perfect_power_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_perfect_power_p(op.ToIntPtr()); } /// /// Return non-zero if is a perfect square, i.e., if the square root of is an integer. /// /// The operand integer. /// Non-zero if is a perfect square, i.e., if the square root of is an integer. /// /// /// Under this definition both 0 and 1 are considered to be perfect squares. /// /// /// mpz_perfect_power_p /// mpz_root /// mpz_rootrem /// mpz_sqrt /// mpz_sqrtrem /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Assert that op is a perfect square. /// Assert.IsTrue(gmp_lib.mpz_perfect_square_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Assert that op is a perfect square. /// Assert.IsTrue(gmp_lib.mpz_perfect_square_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_perfect_square_p(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_perfect_square_p(op.ToIntPtr()); } /// /// Return the population count of . /// /// The operand integer. /// If ≥ 0, return the population count of , which is the number of 1 bits in the binary representation. If < 0, the number of 1s is infinite, and the return value is the largest possible mp_bitcnt_t. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op to 63. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 63U); /// /// // Assert that op has 6 one bits. /// Assert.IsTrue(gmp_lib.mpz_popcount(op) == 6U); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clears(op); /// /// /// ' Create, initialize, and set the value of op to 63. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 63UI) /// /// ' Assert that op has 6 one bits. /// Assert.IsTrue(gmp_lib.mpz_popcount(op) = 6UI) /// /// ' Release unmanaged memory allocated for op. /// /// public static mp_bitcnt_t mpz_popcount(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return new mp_bitcnt_t(SafeNativeMethods.__gmpz_popcount(op.ToIntPtr())); } /// /// Set to ^. The case 0^0 yields 1. /// /// The result integer. /// The base integer. /// The exponent integer. /// mpz_powm /// mpz_powm_ui /// mpz_powm_sec /// mpz_ui_pow_ui /// Integer Exponentiations /// GNU MP - Integer Exponentiation /// /// /// // Create, initialize, and set the value of base to 2. /// mpz_t @base = new mpz_t(); /// gmp_lib.mpz_init_set_ui(@base, 2U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = base^4. /// gmp_lib.mpz_pow_ui(rop, @base, 4U); /// /// // Assert that rop is 16. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 16); /// /// // Release unmanaged memory allocated for rop and base. /// gmp_lib.mpz_clears(rop, @base, null); /// /// /// ' Create, initialize, and set the value of base to 2. /// Dim base As New mpz_t() /// gmp_lib.mpz_init_set_ui(base, 2UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = base^4. /// gmp_lib.mpz_pow_ui(rop, base, 4UI) /// /// ' Assert that rop is 16. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 16) /// /// ' Release unmanaged memory allocated for rop and base. /// gmp_lib.mpz_clears(rop, base, Nothing) /// /// public static void mpz_pow_ui(mpz_t rop, /*const*/ mpz_t @base, uint /*unsigned long int*/ exp) { if (rop == null) throw new ArgumentNullException("rop"); if (@base == null) throw new ArgumentNullException("base"); SafeNativeMethods.__gmpz_pow_ui(rop.ToIntPtr(), @base.ToIntPtr(), exp); } /// /// Set to (^) modulo . /// /// The result integer. /// The base integer. /// The exponent integer. /// The modulo integer. /// /// /// Negative is supported if an inverse ^-1 modulo exists (see mpz_invert). /// If an inverse doesn’t exist then a divide by zero is raised. /// /// /// mpz_powm_ui /// mpz_powm_sec /// mpz_pow_ui /// mpz_ui_pow_ui /// Integer Exponentiations /// GNU MP - Integer Exponentiation /// /// /// // Create, initialize, and set the value of base to 2. /// mpz_t @base = new mpz_t(); /// gmp_lib.mpz_init_set_ui(@base, 2U); /// /// // Create, initialize, and set the value of exp to 4. /// mpz_t exp = new mpz_t(); /// gmp_lib.mpz_init_set_ui(exp, 4U); /// /// // Create, initialize, and set the value of mod to 3. /// mpz_t mod = new mpz_t(); /// gmp_lib.mpz_init_set_ui(mod, 3U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = base^exp mod mod. /// gmp_lib.mpz_powm(rop, @base, exp, mod); /// /// // Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 1); /// /// // Release unmanaged memory allocated for rop, base, exp, and mod. /// gmp_lib.mpz_clears(rop, @base, exp, mod, null); /// /// /// ' Create, initialize, and set the value of base to 2. /// Dim base As New mpz_t() /// gmp_lib.mpz_init_set_ui(base, 2UI) /// /// ' Create, initialize, and set the value of exp to 4. /// Dim exp As New mpz_t() /// gmp_lib.mpz_init_set_ui(exp, 4UI) /// /// ' Create, initialize, and set the value of mod to 3. /// Dim[mod] As New mpz_t() /// gmp_lib.mpz_init_set_ui([mod], 3UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = base^exp mod mod. /// gmp_lib.mpz_powm(rop, base, exp, [mod]) /// /// ' Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 1) /// /// ' Release unmanaged memory allocated for rop, base, exp, and mod. /// gmp_lib.mpz_clears(rop, base, exp, [mod], Nothing) /// /// public static void mpz_powm(mpz_t rop, /*const*/ mpz_t @base, /*const*/ mpz_t exp, /*const*/ mpz_t mod) { if (rop == null) throw new ArgumentNullException("rop"); if (@base == null) throw new ArgumentNullException("base"); if (exp == null) throw new ArgumentNullException("exp"); if (mod == null) throw new ArgumentNullException("mod"); SafeNativeMethods.__gmpz_powm(rop.ToIntPtr(), @base.ToIntPtr(), exp.ToIntPtr(), mod.ToIntPtr()); } /// /// Set to (^) modulo . /// /// The result integer. /// The base integer. /// The exponent integer. /// The modulo integer. /// /// /// It is required that > 0 and that is odd. /// /// /// This function is designed to take the same time and have the same cache access patterns for any two same-size arguments, /// assuming that function arguments are placed at the same position and that the machine state is identical upon function entry. /// This function is intended for cryptographic purposes, where resilience to side-channel attacks is desired. /// /// /// mpz_powm /// mpz_powm_ui /// mpz_pow_ui /// mpz_ui_pow_ui /// Integer Exponentiations /// GNU MP - Integer Exponentiation /// /// /// // Create, initialize, and set the value of base to 2. /// mpz_t @base = new mpz_t(); /// gmp_lib.mpz_init_set_ui(@base, 2U); /// /// // Create, initialize, and set the value of exp to 4. /// mpz_t exp = new mpz_t(); /// gmp_lib.mpz_init_set_ui(exp, 4U); /// /// // Create, initialize, and set the value of mod to 3. /// mpz_t mod = new mpz_t(); /// gmp_lib.mpz_init_set_ui(mod, 3U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = base^exp mod mod. /// gmp_lib.mpz_powm_sec(rop, @base, exp, mod); /// /// // Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 1); /// /// // Release unmanaged memory allocated for rop, base, exp, and mod. /// gmp_lib.mpz_clears(rop, @base, exp, mod, null); /// /// /// ' Create, initialize, and set the value of base to 2. /// Dim base As New mpz_t() /// gmp_lib.mpz_init_set_ui(base, 2UI) /// /// ' Create, initialize, and set the value of exp to 4. /// Dim exp As New mpz_t() /// gmp_lib.mpz_init_set_ui(exp, 4UI) /// /// ' Create, initialize, and set the value of mod to 3. /// Dim[mod] As New mpz_t() /// gmp_lib.mpz_init_set_ui([mod], 3UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = base^exp mod mod. /// gmp_lib.mpz_powm_sec(rop, base, exp, [mod]) /// /// ' Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 1) /// /// ' Release unmanaged memory allocated for rop, base, exp, and mod. /// gmp_lib.mpz_clears(rop, base, exp, [mod], Nothing) /// /// public static void mpz_powm_sec(mpz_t rop, /*const*/ mpz_t @base, /*const*/ mpz_t exp, /*const*/ mpz_t mod) { if (rop == null) throw new ArgumentNullException("rop"); if (@base == null) throw new ArgumentNullException("base"); if (exp == null) throw new ArgumentNullException("exp"); if (mod == null) throw new ArgumentNullException("mod"); SafeNativeMethods.__gmpz_powm_sec(rop.ToIntPtr(), @base.ToIntPtr(), exp.ToIntPtr(), mod.ToIntPtr()); } /// /// Set to (^) modulo . /// /// The result integer. /// The base integer. /// The exponent integer. /// The modulo integer. /// /// /// Negative is supported if an inverse ^-1 modulo exists (see mpz_invert). /// If an inverse doesn’t exist then a divide by zero is raised. /// /// /// mpz_powm /// mpz_powm_sec /// mpz_pow_ui /// mpz_ui_pow_ui /// Integer Exponentiations /// GNU MP - Integer Exponentiation /// /// /// // Create, initialize, and set the value of base to 2. /// mpz_t @base = new mpz_t(); /// gmp_lib.mpz_init_set_ui(@base, 2U); /// /// mpz_t mod = new mpz_t(); /// gmp_lib.mpz_init_set_ui(mod, 3U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = base^4 mod mod. /// gmp_lib.mpz_powm_ui(rop, @base, 4U, mod); /// /// // Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 1); /// /// // Release unmanaged memory allocated for rop, base, and mod. /// gmp_lib.mpz_clears(rop, @base, mod, null); /// /// /// ' Create, initialize, and set the value of base to 2. /// Dim base As New mpz_t() /// gmp_lib.mpz_init_set_ui(base, 2UI) /// Dim[mod] As New mpz_t() /// gmp_lib.mpz_init_set_ui([mod], 3UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = base^4 mod mod. /// gmp_lib.mpz_powm_ui(rop, base, 4UI, [mod]) /// /// ' Assert that rop is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 1) /// /// ' Release unmanaged memory allocated for rop, base, and mod. /// gmp_lib.mpz_clears(rop, base, [mod], Nothing) /// /// public static void mpz_powm_ui(mpz_t rop, /*const*/ mpz_t @base, uint /*unsigned long int*/ exp, /*const*/ mpz_t mod) { if (rop == null) throw new ArgumentNullException("rop"); if (@base == null) throw new ArgumentNullException("base"); if (mod == null) throw new ArgumentNullException("mod"); SafeNativeMethods.__gmpz_powm_ui(rop.ToIntPtr(), @base.ToIntPtr(), exp, mod.ToIntPtr()); } /// /// Determine whether is prime. /// /// The operand integer. /// The number of Miller-Rabin probabilistic primality tests to perform. /// Return 2 if is definitely prime, return 1 if is probably prime (without being certain), or return 0 if is definitely non-prime. /// /// /// This function performs some trial divisions, then Miller-Rabin probabilistic primality tests. /// A higher value will reduce the chances of a non-prime being identified as “probably prime”. /// A composite number will be identified as a prime with a probability of less than 4^(-reps). /// Reasonable values of are between 15 and 50. /// /// /// mpz_millerrabin /// mpz_nextprime /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of n to 12. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_ui(n, 12U); /// /// // Assert that n is a composite number. /// Assert.IsTrue(gmp_lib.mpz_probab_prime_p(n, 25) == 0); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 12. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_ui(n, 12UI) /// /// ' Assert that n is a composite number. /// Assert.IsTrue(gmp_lib.mpz_probab_prime_p(n, 25) = 0) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static int mpz_probab_prime_p(/*const*/ mpz_t n, int reps) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_probab_prime_p(n.ToIntPtr(), reps); } /// /// Generate a random integer of at most limbs. /// /// The result integer. /// The maximum number of limbs. /// /// /// The generated random number doesn’t satisfy any particular requirements of randomness. /// Negative random numbers are generated when is negative. /// /// /// This function is obsolete. Use mpz_urandomb or mpz_urandomm instead. /// /// /// The random number functions of GMP come in two groups; older function that rely on a global state, /// and newer functions that accept a state parameter that is read and modified. /// Please see the GNU MP - Random Number Functions /// for more information on how to use and not to use random number functions. /// /// /// mpz_urandomb /// mpz_urandomm /// mpz_rrandomb /// mpz_random2 /// Integer Random Numbers /// GNU MP - Integer Random Numbers /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Generate a random integer. /// gmp_lib.mpz_random(rop, 500); /// /// // Free all memory occupied by state and rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Generate a random integer. /// gmp_lib.mpz_random(rop, 500) /// /// ' Free all memory occupied by state and rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_random(mpz_t rop, mp_size_t max_size) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_random(rop.ToIntPtr(), max_size); } /// /// Generate a random integer of at most limbs, with long strings of zeros and ones in the binary representation. /// /// The result integer. /// The maximum number of limbs. /// /// /// Useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. /// Negative random numbers are generated when is negative. /// /// /// This function is obsolete. Use mpz_rrandomb instead. /// /// /// The random number functions of GMP come in two groups; older function that rely on a global state, /// and newer functions that accept a state parameter that is read and modified. /// Please see the GNU MP - Random Number Functions /// for more information on how to use and not to use random number functions. /// /// /// mpz_urandomb /// mpz_urandomm /// mpz_rrandomb /// mpz_random /// Integer Random Numbers /// GNU MP - Integer Random Numbers /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Generate a random integer. /// gmp_lib.mpz_random(rop, 100); /// /// // Free all memory occupied by rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Generate a random integer. /// gmp_lib.mpz_random(rop, 100) /// /// ' Free all memory occupied by rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_random2(mpz_t rop, mp_size_t max_size) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_random2(rop.ToIntPtr(), max_size); } /// /// Change the space allocated for to bits. /// /// The integer. /// The number of bits. /// /// /// The value in is preserved if it fits, or is set to 0 if not. /// /// /// Calling this function is never necessary; reallocation is handled automatically by GMP when /// needed. But this function can be used to increase the space for a variable in order to avoid /// repeated automatic reallocations, or to decrease it to give memory back to the heap. /// /// /// mpz_clear /// mpz_clears /// mpz_init /// mpz_inits /// mpz_init2 /// Initializing Integers /// GNU MP - Initializing Integers /// /// /// // Create and initialize new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x to a 77-bit integer. /// char_ptr value = new char_ptr("1000 0000 0000 0000 0000"); /// gmp_lib.mpz_set_str(x, value, 16); /// /// // Resize x to 512 bits, and assert that its value has not changed. /// gmp_lib.mpz_realloc2(x, 512U); /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 16, x); /// Assert.IsTrue(s.ToString() == "1000 0000 0000 0000 0000".Replace(" ", "")); /// /// // Resize x to 2 bits, and assert that its value has changed to 0. /// gmp_lib.mpz_realloc2(x, 2U); /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 0); /// /// // Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// gmp_lib.free(s); /// /// /// ' Create and initialize new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x to a 77-bit integer. /// Dim value As New char_ptr("1000 0000 0000 0000 0000") /// gmp_lib.mpz_set_str(x, value, 16) /// /// ' Resize x to 512 bits, and assert that its value has not changed. /// gmp_lib.mpz_realloc2(x, 512UI) /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 16, x) /// Assert.IsTrue(s.ToString() = "1000 0000 0000 0000 0000".Replace(" ", "")) /// /// ' Resize x to 2 bits, and assert that its value has changed to 0. /// gmp_lib.mpz_realloc2(x, 2UI) /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 0) /// /// ' Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// gmp_lib.free(s) /// /// public static void mpz_realloc2(mpz_t x, mp_bitcnt_t n) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_realloc2(x.ToIntPtr(), n); } /// /// Remove all occurrences of the factor from and store the result in . /// /// The result integer. /// The operand integer. /// The factor operand integer. /// The return value is how many such occurrences were removed. /// Number Theoretic Functions /// GNU MP - Number Theoretic Functions /// /// /// // Create, initialize, and set the value of op to 45. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 45U); /// /// // Create, initialize, and set the value of f to 3. /// mpz_t f = new mpz_t(); /// gmp_lib.mpz_init_set_ui(f, 3U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = op / f^n, and return n, the largest integer greater than or equal to 0, such that f^n divides op. /// Assert.IsTrue(gmp_lib.mpz_remove(rop, op, f) == 2); /// /// // Assert that rop is 5. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 5); /// /// // Release unmanaged memory allocated for rop, op, and f. /// gmp_lib.mpz_clears(rop, op, f, null); /// /// /// ' Create, initialize, and set the value of op to 45. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 45UI) /// /// ' Create, initialize, and set the value of f to 3. /// Dim f As New mpz_t() /// gmp_lib.mpz_init_set_ui(f, 3UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = op / f^n, and return n, the largest integer greater than or equal to 0, such that f^n divides op. /// Assert.IsTrue(gmp_lib.mpz_remove(rop, op, f) = 2) /// /// ' Assert that rop is 5. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 5) /// /// ' Release unmanaged memory allocated for rop, op, and f. /// gmp_lib.mpz_clears(rop, op, f, Nothing) /// /// public static mp_bitcnt_t mpz_remove(mpz_t rop, /*const*/ mpz_t op, /*const*/ mpz_t f) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); if (f == null) throw new ArgumentNullException("f"); return new mp_bitcnt_t(SafeNativeMethods.__gmpz_remove(rop.ToIntPtr(), op.ToIntPtr(), f.ToIntPtr())); } /// /// Set to the truncated integer part of the th root of . /// /// The result root integer. /// The first operand integer. /// The second operand integer. /// Return non-zero if the computation was exact, i.e., if is to the th power. /// mpz_perfect_power_p /// mpz_perfect_square_p /// mpz_rootrem /// mpz_sqrt /// mpz_sqrtrem /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of op to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = trunc(cbrt(10000)). /// gmp_lib.mpz_root(rop, op, 3U); /// /// // Assert that rop is 21. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 21); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clears(rop, op, null); /// /// /// ' Create, initialize, and set the value of op to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = trunc(cbrt(10000)). /// gmp_lib.mpz_root(rop, op, 3UI) /// /// ' Assert that rop is 21. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 21) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clears(rop, op, Nothing) /// /// public static int mpz_root(mpz_t rop, /*const*/ mpz_t op, uint /*unsigned long int*/ n) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_root(rop.ToIntPtr(), op.ToIntPtr(), n); } /// /// Set to the truncated integer part of the th root of . Set to the remainder, - ^. /// /// The result root integer. /// The result remainder integer. /// The first operand integer. /// The second operand integer. /// mpz_perfect_power_p /// mpz_perfect_square_p /// mpz_root /// mpz_sqrt /// mpz_sqrtrem /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of u to 10000. /// mpz_t u = new mpz_t(); /// gmp_lib.mpz_init_set_si(u, 10000); /// /// // Create, initialize, and set the values of root and rem to 0. /// mpz_t root = new mpz_t(); /// mpz_t rem = new mpz_t(); /// gmp_lib.mpz_inits(root, rem, null); /// /// // Set root = trunc(cbrt(10000)) and rem = u - root. /// gmp_lib.mpz_rootrem(root, rem, u, 3U); /// /// // Assert that root is 21, and rem is 739. /// Assert.IsTrue(gmp_lib.mpz_get_si(root) == 21); /// Assert.IsTrue(gmp_lib.mpz_get_si(rem) == 739); /// /// // Release unmanaged memory allocated for root, rem, and u. /// gmp_lib.mpz_clears(root, rem, u, null); /// /// /// ' Create, initialize, and set the value of u to 10000. /// Dim u As New mpz_t() /// gmp_lib.mpz_init_set_si(u, 10000) /// /// ' Create, initialize, and set the values of root and rem to 0. /// Dim root As New mpz_t() /// Dim[rem] As New mpz_t() /// gmp_lib.mpz_inits(root, [rem], Nothing) /// /// ' Set root = trunc(cbrt(10000)) and rem = u - root. /// gmp_lib.mpz_rootrem(root, [rem], u, 3UI) /// /// ' Assert that root is 21, and rem is 739. /// Assert.IsTrue(gmp_lib.mpz_get_si(root) = 21) /// Assert.IsTrue(gmp_lib.mpz_get_si([rem]) = 739) /// /// ' Release unmanaged memory allocated for root, rem, and u. /// gmp_lib.mpz_clears(root, [rem], u, Nothing) /// /// public static void mpz_rootrem(mpz_t root, mpz_t rem, /*const*/ mpz_t u, uint /*unsigned long int*/ n) { if (root == null) throw new ArgumentNullException("root"); if (rem == null) throw new ArgumentNullException("rem"); if (u == null) throw new ArgumentNullException("u"); SafeNativeMethods.__gmpz_rootrem(root.ToIntPtr(), rem.ToIntPtr(), u.ToIntPtr(), n); } /// /// Generate a random integer with long strings of zeros and ones in the binary representation. /// /// The result integer. /// The random number generator state. /// The operand integer. /// /// /// Useful for testing functions and algorithms, since this kind of random numbers have proven to be more /// likely to trigger corner-case bugs. The random number will be in the /// range 2^( - 1) to 2^ - 1, inclusive. /// /// /// The variable must be initialized by calling one of the gmp_randinit /// functions (GNU MP - Random State Initialization) before invoking this function. /// /// /// The random number functions of GMP come in two groups; older function that rely on a global state, /// and newer functions that accept a state parameter that is read and modified. /// Please see the GNU MP - Random Number Functions /// for more information on how to use and not to use random number functions. /// /// /// mpz_urandomb /// mpz_urandomm /// mpz_random /// mpz_random2 /// Integer Random Numbers /// GNU MP - Integer Random Numbers /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Generate a random integer in the range [2^(50-1), (2^50)-1]. /// gmp_lib.mpz_rrandomb(rop, state, 50); /// /// // Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Generate a random integer in the range [2^(50-1), (2^50)-1]. /// gmp_lib.mpz_rrandomb(rop, state, 50) /// /// ' Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_rrandomb(mpz_t rop, gmp_randstate_t state, mp_bitcnt_t n) { if (rop == null) throw new ArgumentNullException("rop"); if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmpz_rrandomb(rop.ToIntPtr(), state.ToIntPtr(), n); } /// /// Scan for 0 bit. /// /// The operand integer. /// The start bit index position. /// Return the index of the found bit. /// /// /// Scan , starting from bit , towards more significant bits, /// until the first 0 bit is found. Return the index of the found bit. /// /// /// If the bit at is already what’s sought, /// then is returned. /// /// /// If there’s no bit found, then the largest possible mp_bitcnt_t is returned. /// This will happen in mpz_scan0 past the end of a negative number, /// or mpz_scan1 past the end of a nonnegative number. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op to 70. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 70U); /// /// // Assert that the first 0 bit starting from bit 1 in op is bit 3. /// Assert.IsTrue(gmp_lib.mpz_scan0(op, 1U) == 3U); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 70. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 70UI) /// /// ' Assert that the first 0 bit starting from bit 1 in op is bit 3. /// Assert.IsTrue(gmp_lib.mpz_scan0(op, 1UI) = 3UI) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static mp_bitcnt_t mpz_scan0(/*const*/ mpz_t op, mp_bitcnt_t starting_bit) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_scan0(op.ToIntPtr(), starting_bit); } /// /// Scan for 1 bit. /// /// The operand integer. /// The start bit index position. /// Return the index of the found bit. /// /// /// Scan , starting from bit , towards more significant bits, /// until the first 1 bit is found. Return the index of the found bit. /// /// /// If the bit at is already what’s sought, /// then is returned. /// /// /// If there’s no bit found, then the largest possible mp_bitcnt_t is returned. /// This will happen in mpz_scan0 past the end of a negative number, /// or mpz_scan1 past the end of a nonnegative number. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op to 70. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op, 70U); /// /// // Assert that the first 1 bit starting from bit 3 in op is bit 6. /// Assert.IsTrue(gmp_lib.mpz_scan1(op, 3U) == 6U); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 70. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_ui(op, 70UI) /// /// ' Assert that the first 1 bit starting from bit 3 in op is bit 6. /// Assert.IsTrue(gmp_lib.mpz_scan1(op, 3UI) = 6UI) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static mp_bitcnt_t mpz_scan1(/*const*/ mpz_t op, mp_bitcnt_t starting_bit) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_scan1(op.ToIntPtr(), starting_bit); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// mpz_set_ui /// mpz_set_si /// mpz_set_d /// mpz_set_q /// mpz_set_f /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create, initialize, and set a new integer x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// gmp_lib.mpz_set_si(x, 10); /// /// // Create, initialize, and set a new integer y to -210. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init(y); /// gmp_lib.mpz_set_si(y, -210); /// /// // Assign the value of y to x. /// gmp_lib.mpz_set(x, y); /// /// // Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == -210); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, null); /// /// /// ' Create, initialize, and set a new integer x to 10. /// Dim x As New mpz_t() /// /// gmp_lib.mpz_init(x) /// gmp_lib.mpz_set_si(x, 10) /// /// ' Create, initialize, and set a new integer y to -210. /// Dim y As New mpz_t() /// gmp_lib.mpz_init(y) /// gmp_lib.mpz_set_si(y, -210) /// /// ' Assign the value of y to x. /// gmp_lib.mpz_set(x, y) /// /// ' Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = -210) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, Nothing) /// /// public static void mpz_set(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_set(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// /// /// mpz_set_d truncate to make it an integer. /// /// /// mpz_set /// mpz_set_ui /// mpz_set_si /// mpz_set_q /// mpz_set_f /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x to the truncation of 10.7. /// gmp_lib.mpz_set_d(x, 10.7D); /// /// // Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 10); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x to the truncation of 10.7. /// gmp_lib.mpz_set_d(x, 10.7) /// /// ' Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 10) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_set_d(mpz_t rop, double op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_set_d(rop.ToIntPtr(), op); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// /// /// mpz_set_f truncate to make it an integer. /// /// /// mpz_set /// mpz_set_ui /// mpz_set_si /// mpz_set_d /// mpz_set_q /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize new integer x, and float y. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// mpf_t y = "1.7007e3"; /// /// // Set the value of x to the truncation of 1700.7. /// gmp_lib.mpz_set_f(x, y); /// /// // Assert that the value of x is 1700. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 1700); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clear(x); /// gmp_lib.mpf_clear(y); /// /// /// ' Create and initialize new integer x, and float y. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// Dim y As mpf_t = "1.7007e3" /// /// ' Set the value of x to the truncation of 1700.7. /// gmp_lib.mpz_set_f(x, y) /// /// ' Assert that the value of x is 1700. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 1700) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clear(x) /// gmp_lib.mpf_clear(y) /// /// public static void mpz_set_f(mpz_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_set_f(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// /// /// mpz_set_q truncate to make it an integer. /// /// /// mpz_set /// mpz_set_ui /// mpz_set_si /// mpz_set_d /// mpz_set_f /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize new integer x, and rational y. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// mpq_t y = "100/3"; /// /// // Set the value of x to the truncation of 100/3. /// gmp_lib.mpz_set_q(x, y); /// /// // Assert that the value of x is 33. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == 33); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clear(x); /// gmp_lib.mpq_clear(y); /// /// /// ' Create and initialize new integer x, and rational y. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// Dim y As mpq_t = "100/3" /// /// ' Set the value of x to the truncation of 100/3. /// gmp_lib.mpz_set_q(x, y) /// /// ' Assert that the value of x is 33. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = 33) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clear(x) /// gmp_lib.mpq_clear(y) /// /// public static void mpz_set_q(mpz_t rop, /*const*/ mpq_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_set_q(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// mpz_set /// mpz_set_ui /// mpz_set_d /// mpz_set_q /// mpz_set_f /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x to -10. /// gmp_lib.mpz_set_si(x, -10); /// /// // Assert that the value of x is -10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == -10); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x to -10. /// gmp_lib.mpz_set_si(x, -10) /// /// ' Assert that the value of x is -10. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = -10) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_set_si(mpz_t rop, int /*long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_set_si(rop.ToIntPtr(), op); } /// /// Set the value of from , a null-terminated C string in base . /// /// The destination integer. /// The source integer. /// The base. /// This function returns 0 if the entire string is a valid number in base . Otherwise it returns −1. /// /// /// White space is allowed in the string, and is simply ignored. /// /// /// The base may vary from 2 to 62, or if base is 0, then the leading characters are /// used: 0x and 0X for hexadecimal, 0b and 0B for binary, 0 for octal, /// or decimal otherwise. /// /// /// For bases up to 36, case is ignored; upper-case and lower-case letters have the same value. /// For bases 37 to 62, upper-case letter represent the usual 10..35 while lower-case /// letter represent 36..61. /// /// /// mpz_set /// mpz_set_ui /// mpz_set_si /// mpz_set_d /// mpz_set_q /// mpz_set_f /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x. /// char_ptr value = new char_ptr("12 345 678 909 876 543 211 234 567 890 987 654 321"); /// gmp_lib.mpz_set_str(x, value, 10); /// /// // Assert the value of x. /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == value.ToString().Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// gmp_lib.free(s); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x. /// Dim value As New char_ptr("12 345 678 909 876 543 211 234 567 890 987 654 321") /// gmp_lib.mpz_set_str(x, value, 10) /// /// ' Assert the value of x. /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, 10, x) /// Assert.IsTrue(s.ToString() = value.ToString().Replace(" ", "")) /// /// ' Release unmanaged memory allocated for x and string values. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// gmp_lib.free(s) /// /// public static int mpz_set_str(mpz_t rop, /*const*/ char_ptr str, int @base) { if (rop == null) throw new ArgumentNullException("rop"); return SafeNativeMethods.__gmpz_set_str(rop.ToIntPtr(), str.ToIntPtr(), @base); } /// /// Set the value of from . /// /// The destination integer. /// The source integer. /// mpz_set /// mpz_set_si /// mpz_set_d /// mpz_set_q /// mpz_set_f /// mpz_set_str /// mpz_swap /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create and initialize a new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x to 10. /// gmp_lib.mpz_set_ui(x, 10U); /// /// // Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) == 10U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x); /// /// /// ' Create and initialize a new integer x. /// Dim x As New mpz_t() /// /// gmp_lib.mpz_init(x) /// /// ' Set the value of x to 10. /// gmp_lib.mpz_set_ui(x, 10UI) /// /// ' Assert that the value of x is 10. /// Assert.IsTrue(gmp_lib.mpz_get_ui(x) = 10UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpz_clear(x) /// /// public static void mpz_set_ui(mpz_t rop, uint /*unsigned long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_set_ui(rop.ToIntPtr(), op); } /// /// Set bit in . /// /// The result integer. /// The index of the bit to set. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of rop to 70. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init_set_si(rop, 70); /// /// // Set bit 3 of rop. /// gmp_lib.mpz_setbit(rop, 3U); /// /// // Assert that rop is 78. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 78); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 70. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init_set_si(rop, 70) /// /// ' Set bit 3 of rop. /// gmp_lib.mpz_setbit(rop, 3UI) /// /// ' Assert that rop is 78. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 78) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_setbit(mpz_t rop, mp_bitcnt_t bit_index) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_setbit(rop.ToIntPtr(), bit_index); } /// /// Return +1 if > 0, 0 if = 0, and -1 if < 0. /// /// The operand integer. /// Return +1 if > 0, 0 if = 0, and -1 if < 0. /// mpz_cmp /// mpz_cmp_d /// mpz_cmp_si /// mpz_cmp_ui /// mpz_cmpabs /// mpz_cmpabs_d /// mpz_cmpabs_ui /// Integer Comparisons /// GNU MP - Integer Comparisons /// /// /// // Create, initialize, and set the value of op to -10. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, -10); /// /// // Assert that the sign of op is -1. /// Assert.IsTrue(gmp_lib.mpz_sgn(op) == -1); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to -10. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, -10) /// /// ' Assert that the sign of op is -1. /// Assert.IsTrue(gmp_lib.mpz_sgn(op) = -1) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static int mpz_sgn(mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_size < 0 ? -1 : (op._mp_size > 0 ? 1 : 0); } /// /// Return the size of measured in number of limbs. /// /// The operand integer. /// The size of measured in number of limbs. /// /// /// If is zero, the returned value will be zero. /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_modify /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t op = new mpz_t(); /// char_ptr value = new char_ptr("1000 ABCD 1234 7AB8 24FD"); /// gmp_lib.mpz_init_set_str(op, value, 16); /// /// // Assert the value of the limbs of op. /// if (gmp_lib.mp_bytes_per_limb == 4) /// Assert.IsTrue(gmp_lib.mpz_size(op) == 3); /// else // gmp_lib.mp_bytes_per_limb == 8 /// Assert.IsTrue(gmp_lib.mpz_size(op) == 2); /// /// // Release unmanaged memory allocated for op and value. /// gmp_lib.mpz_clear(op); /// gmp_lib.free(value); /// /// /// ' Create and initialize new integer x. /// Dim op As New mpz_t() /// Dim value As New char_ptr("1000 ABCD 1234 7AB8 24FD") /// gmp_lib.mpz_init_set_str(op, value, 16) /// /// ' Assert the value of the limbs of op. /// If gmp_lib.mp_bytes_per_limb = 4 Then /// Assert.IsTrue(gmp_lib.mpz_size(op) = 3) /// Else ' gmp_lib.mp_bytes_per_limb == 8 /// Assert.IsTrue(gmp_lib.mpz_size(op) = 2) /// End If /// /// ' Release unmanaged memory allocated for op and value. /// gmp_lib.mpz_clear(op) /// gmp_lib.free(value) /// /// public static mp_size_t mpz_size(/*const*/ mpz_t op) { if (op == null) throw new ArgumentNullException("op"); return new mp_size_t(SafeNativeMethods.__gmpz_size(op.ToIntPtr())); } /// /// Return the size of measured in number of digits in the given . /// /// The operand integer /// The base. /// The size of measured in number of digits in the given . /// /// /// can vary from 2 to 62. /// The sign of is ignored, just the absolute value is used. /// The result will be either exact or 1 too big. /// If is a power of 2, the result is always exact. /// If is zero the return value is always 1. /// /// /// This function can be used to determine the space required when converting to a string. /// The right amount of allocation is normally two more than the value returned by mpz_sizeinbase, /// one extra for a minus sign and one for the null-terminator. /// /// /// It will be noted that mpz_sizeinbase(, 2) can be used to locate the most /// significant 1 bit in , counting from 1. /// (Unlike the bitwise functions which start from 0, /// see GNU MP - Logical and Bit Manipulation Functions.) /// /// /// mpz_fits_ulong_p /// mpz_fits_slong_p /// mpz_fits_uint_p /// mpz_fits_sint_p /// mpz_fits_ushort_p /// mpz_fits_sshort_p /// mpz_odd_p /// mpz_even_p /// Miscellaneous Integer Functions /// GNU MP - Miscellaneous Integer Functions /// /// /// // Create, initialize, and set the value of op to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Assert size in different bases. /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 2) == 14); /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 8) == 5); /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 10) == 5); /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 16) == 4); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op); /// /// /// ' Create, initialize, and set the value of op to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Assert size in different bases. /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 2) = 14) /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 8) = 5) /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 10) = 5) /// Assert.IsTrue(gmp_lib.mpz_sizeinbase(op, 16) = 4) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpz_clear(op) /// /// public static size_t mpz_sizeinbase(/*const*/ mpz_t op, int @base) { if (op == null) throw new ArgumentNullException("op"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpz_sizeinbase_x86(op.ToIntPtr(), @base)); else return new size_t(SafeNativeMethods.__gmpz_sizeinbase_x64(op.ToIntPtr(), @base)); } /// /// Set to the truncated integer part of the square root of . /// /// The result square root integer. /// The operand integer. /// mpz_perfect_power_p /// mpz_perfect_square_p /// mpz_root /// mpz_rootrem /// mpz_sqrtrem /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of op to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = trunc(sqrt(op)). /// gmp_lib.mpz_sqrt(rop, op); /// /// // Assert that rop is 100. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 100); /// /// // Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, null); /// /// /// ' Create, initialize, and set the value of op to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = trunc(sqrt(op)). /// gmp_lib.mpz_sqrt(rop, op) /// /// ' Assert that rop is 100. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 100) /// /// ' Release unmanaged memory allocated for rop and op. /// gmp_lib.mpz_clears(rop, op, Nothing) /// /// public static void mpz_sqrt(mpz_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_sqrt(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to the truncated integer part of the square root of , like mpz_sqrt. Set to the remainder - * , which will be zero if is a perfect square. /// /// The result square root integer. /// The result remainder integer. /// The operand integer. /// mpz_perfect_power_p /// mpz_perfect_square_p /// mpz_root /// mpz_rootrem /// mpz_sqrt /// Integer Roots /// GNU MP - Integer Roots /// /// /// // Create, initialize, and set the value of op to 10000. /// mpz_t op = new mpz_t(); /// gmp_lib.mpz_init_set_si(op, 10000); /// /// // Create, initialize, and set the values of root and rem to 0. /// mpz_t root = new mpz_t(); /// mpz_t rem = new mpz_t(); /// gmp_lib.mpz_inits(root, rem); /// /// // Set root = trunc(sqrt(op)), and rem = op - root. /// gmp_lib.mpz_sqrtrem(root, rem, op); /// /// // Assert that root is 100, and rem is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(root) == 100); /// Assert.IsTrue(gmp_lib.mpz_get_si(rem) == 0); /// /// // Release unmanaged memory allocated for root, rem, and op. /// gmp_lib.mpz_clears(root, rem, op, null); /// /// /// ' Create, initialize, and set the value of op to 10000. /// Dim op As New mpz_t() /// gmp_lib.mpz_init_set_si(op, 10000) /// /// ' Create, initialize, and set the values of root and rem to 0. /// Dim root As New mpz_t() /// Dim[rem] As New mpz_t() /// gmp_lib.mpz_inits(root, [rem]) /// /// ' Set root = trunc(sqrt(op)), and rem = op - root. /// gmp_lib.mpz_sqrtrem(root, [rem], op) /// /// ' Assert that root is 100, and rem is 0. /// Assert.IsTrue(gmp_lib.mpz_get_si(root) = 100) /// Assert.IsTrue(gmp_lib.mpz_get_si([rem]) = 0) /// /// ' Release unmanaged memory allocated for root, rem, and op. /// gmp_lib.mpz_clears(root, [rem], op, Nothing) /// /// public static void mpz_sqrtrem(mpz_t rop1, mpz_t rop2, /*const*/ mpz_t op) { if (rop1 == null) throw new ArgumentNullException("rop1"); if (rop2 == null) throw new ArgumentNullException("rop2"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpz_sqrtrem(rop1.ToIntPtr(), rop2.ToIntPtr(), op.ToIntPtr()); } /// /// Set to - . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub_ui /// mpz_submul /// mpz_ui_sub /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 12222. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 12222U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x - y. /// gmp_lib.mpz_sub(z, x, y); /// /// // Assert that z = x - y. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == -2222); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 12222. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 12222UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x - y. /// gmp_lib.mpz_sub(z, x, y) /// /// ' Assert that z = x - y. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = -2222) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_sub(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_sub(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to - . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// mpz_ui_sub /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = x - 12222. /// gmp_lib.mpz_sub_ui(z, x, 12222U); /// /// // Assert that z = x - 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == -2222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = x - 12222. /// gmp_lib.mpz_sub_ui(z, x, 12222UI) /// /// ' Assert that z = x - 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = -2222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_sub_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_sub_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to - . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_sub_ui /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of z to 0. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init(z); /// /// // Set z = 12222 - x. /// gmp_lib.mpz_ui_sub(z, 12222U, x); /// /// // Assert that z = 12222 - x. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 2222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of z to 0. /// Dim z As New mpz_t() /// gmp_lib.mpz_init(z) /// /// ' Set z = 12222 - x. /// gmp_lib.mpz_ui_sub(z, 12222UI, x) /// /// ' Assert that z = 12222 - x. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 2222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_ui_sub(mpz_t rop, uint /*unsigned long int*/ op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_ui_sub(rop.ToIntPtr(), op1, op2.ToIntPtr()); } /// /// Set to - * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul_ui /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to 10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 10000U); /// /// // Create, initialize, and set the value of y to 12222. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_ui(y, 12222U); /// /// // Create, initialize, and set the value of z to 20000. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init_set_si(z, 20000); /// /// // Set z -= x * y. /// gmp_lib.mpz_submul(z, x, y); /// /// // Assert that z has been decremented by 10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 20000 - 10000 * 12222); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, null); /// /// /// ' Create, initialize, and set the value of x to 10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 10000UI) /// /// ' Create, initialize, and set the value of y to 12222. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_ui(y, 12222UI) /// /// ' Create, initialize, and set the value of z to 20000. /// Dim z As New mpz_t() /// gmp_lib.mpz_init_set_si(z, 20000) /// /// ' Set z -= x * y. /// gmp_lib.mpz_submul(z, x, y) /// /// ' Assert that z has been decremented by 10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 20000 - 10000 * 12222) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpz_clears(x, y, z, Nothing) /// /// public static void mpz_submul(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_submul(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to - * . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// mpz_abs /// mpz_add /// mpz_addmul /// mpz_mul /// mpz_neg /// mpz_sub /// mpz_submul /// Integer Arithmetic /// GNU MP - Integer Arithmetic /// /// /// // Create, initialize, and set the value of x to -10000. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, -10000); /// /// // Create, initialize, and set the value of z to 20000. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init_set_si(z, 20000); /// /// // Set z -= x * 12222U. /// gmp_lib.mpz_submul_ui(z, x, 12222U); /// /// // Assert that z has been decremented by -10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) == 20000 - -10000 * 12222); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, null); /// /// /// ' Create, initialize, and set the value of x to -10000. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, -10000) /// /// ' Create, initialize, and set the value of z to 20000. /// Dim z As New mpz_t() /// gmp_lib.mpz_init_set_si(z, 20000) /// /// ' Set z -= x * 12222U. /// gmp_lib.mpz_submul_ui(z, x, 12222UI) /// /// ' Assert that z has been decremented by -10000 * 12222. /// Assert.IsTrue(gmp_lib.mpz_get_si(z) = 20000 - -10000 * 12222) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpz_clears(x, z, Nothing) /// /// public static void mpz_submul_ui(mpz_t rop, /*const*/ mpz_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpz_submul_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Swap the values and efficiently. /// /// The first integer. /// The second integer. /// mpz_set /// mpz_set_ui /// mpz_set_si /// mpz_set_d /// mpz_set_q /// mpz_set_f /// mpz_set_str /// Assigning Integers /// GNU MP - Assigning Integers /// /// /// // Create, initialize, and set a new integer x to 10. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_si(x, 10); /// /// // Create, initialize, and set a new integer x to -210. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init_set_si(y, -210); /// /// // Swap the values of x and y. /// gmp_lib.mpz_swap(x, y); /// /// // Assert that the values have been swapped. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) == -210); /// Assert.IsTrue(gmp_lib.mpz_get_si(y) == 10); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, null); /// /// /// ' Create, initialize, and set a new integer x to 10. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_si(x, 10) /// /// ' Create, initialize, and set a new integer x to -210. /// Dim y As New mpz_t() /// gmp_lib.mpz_init_set_si(y, -210) /// /// ' Swap the values of x and y. /// gmp_lib.mpz_swap(x, y) /// /// ' Assert that the values have been swapped. /// Assert.IsTrue(gmp_lib.mpz_get_si(x) = -210) /// Assert.IsTrue(gmp_lib.mpz_get_si(y) = 10) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpz_clears(x, y, Nothing) /// /// public static void mpz_swap(mpz_t rop1, mpz_t rop2) { if (rop1 == null) throw new ArgumentNullException("rop1"); if (rop2 == null) throw new ArgumentNullException("rop2"); SafeNativeMethods.__gmpz_swap(rop1.ToIntPtr(), rop2.ToIntPtr()); } /// /// Return the remainder | r | where r = - q * , and where q = trunc( / ). /// /// The numerator integer. /// The denominator integer. /// The remainder | r | where r = - q * , and where q = trunc( / ). /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Assert that returned value is |n - 3 * trunc(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_tdiv_ui(n, 3U) == 1U); /// /// // Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Assert that returned value is |n - 3 * trunc(n / 3)|. /// Assert.IsTrue(gmp_lib.mpz_tdiv_ui(n, 3UI) = 1UI) /// /// ' Release unmanaged memory allocated for n. /// gmp_lib.mpz_clear(n) /// /// public static uint /*unsigned long int*/ mpz_tdiv_ui(/*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_tdiv_ui(n.ToIntPtr(), d); } /// /// Set the quotient to trunc( / ). /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = trunc(n / d). /// gmp_lib.mpz_tdiv_q(q, n, d); /// /// // Assert that q is trunc(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// /// // Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = trunc(n / d). /// gmp_lib.mpz_tdiv_q(q, n, d) /// /// ' Assert that q is trunc(10000 / 3). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// /// ' Release unmanaged memory allocated for n, d, and q. /// gmp_lib.mpz_clears(n, d, q, Nothing) /// /// public static void mpz_tdiv_q(mpz_t q, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_tdiv_q(q.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the quotient to trunc( / 2^). /// /// The result quotient integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = trunc(n / 2^2). /// gmp_lib.mpz_tdiv_q_2exp(q, n, 2U); /// /// // Assert that q is trunc(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 2500); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = trunc(n / 2^2). /// gmp_lib.mpz_tdiv_q_2exp(q, n, 2UI) /// /// ' Assert that q is trunc(10001 / 4). /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 2500) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static void mpz_tdiv_q_2exp(mpz_t q, /*const*/ mpz_t n, mp_bitcnt_t b) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_tdiv_q_2exp(q.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the quotient to trunc( / ), and return the remainder r = | - * |. /// /// The result quotient integer. /// The numerator integer. /// The denominator integer. /// Return the remainder r = | - * |. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of q to 0. /// mpz_t q = new mpz_t(); /// gmp_lib.mpz_init(q); /// /// // Set q = trunc(n / 3) and return r = n - 3 * q. /// // Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_tdiv_q_ui(q, n, 3U) == 1U); /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// /// // Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of q to 0. /// Dim q As New mpz_t() /// gmp_lib.mpz_init(q) /// /// ' Set q = trunc(n / 3) and return r = n - 3 * q. /// ' Assert q and r values. /// Assert.IsTrue(gmp_lib.mpz_tdiv_q_ui(q, n, 3UI) = 1UI) /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// /// ' Release unmanaged memory allocated for n and q. /// gmp_lib.mpz_clears(n, q, Nothing) /// /// public static ulong mpz_tdiv_q_ui(mpz_t q, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_tdiv_q_ui(q.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the quotient to trunc( / ), and set the remainder to - * . /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = trunc(n / 3) and r = n - d * q. /// gmp_lib.mpz_tdiv_qr(q, r, n, d); /// /// // Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// ' Set q = trunc(n / 3) and r = n - d * q. /// gmp_lib.mpz_tdiv_qr(q, r, n, d) /// /// ' Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, d, q, and r. /// gmp_lib.mpz_clears(n, d, q, r, Nothing) /// /// public static void mpz_tdiv_qr(mpz_t q, mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_tdiv_qr(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set quotient to trunc( / ), set the remainder to - * , and return | |. /// /// The result quotient integer. /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the values of q and r to 0. /// mpz_t q = new mpz_t(); /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_inits(q, r, null); /// /// // Set q = trunc(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_tdiv_qr_ui(q, r, n, 3U) == 1U); /// /// // Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) == 3333); /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the values of q and r to 0. /// Dim q As New mpz_t() /// Dim r As New mpz_t() /// gmp_lib.mpz_inits(q, r, Nothing) /// /// ' Set q = trunc(n / 3), r = n - d * q, and return r. /// Assert.IsTrue(gmp_lib.mpz_tdiv_qr_ui(q, r, n, 3UI) = 1UI) /// /// ' Assert that q is 3333, and that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(q) = 3333) /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, q, and r. /// gmp_lib.mpz_clears(n, q, r, Nothing) /// /// public static uint /*unsigned long int*/ mpz_tdiv_qr_ui(mpz_t q, mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (q == null) throw new ArgumentNullException("q"); if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_tdiv_qr_ui(q.ToIntPtr(), r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Set the remainder to - q * where q = trunc( / ). /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of d to 3. /// mpz_t d = new mpz_t(); /// gmp_lib.mpz_init_set_si(d, 3); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - d * trunc(n / d). /// gmp_lib.mpz_tdiv_r(r, n, d); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of d to 3. /// Dim d As New mpz_t() /// gmp_lib.mpz_init_set_si(d, 3) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - d * trunc(n / d). /// gmp_lib.mpz_tdiv_r(r, n, d) /// /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n, d, and r. /// gmp_lib.mpz_clears(n, d, r, Nothing) /// /// public static void mpz_tdiv_r(mpz_t r, /*const*/ mpz_t n, /*const*/ mpz_t d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); if (d == null) throw new ArgumentNullException("d"); SafeNativeMethods.__gmpz_tdiv_r(r.ToIntPtr(), n.ToIntPtr(), d.ToIntPtr()); } /// /// Set the remainder to - q * 2^ where q = trunc( / 2^). /// /// The result remainder integer. /// The numerator integer. /// The exponent of the power of two denominator. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_r_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10001. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10001); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 2^2 * trunc(n / 2^2) /// gmp_lib.mpz_tdiv_r_2exp(r, n, 2U); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10001. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10001) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - 2^2 * trunc(n / 2^2) /// gmp_lib.mpz_tdiv_r_2exp(r, n, 2UI) /// /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static void mpz_tdiv_r_2exp(mpz_t r, /*const*/ mpz_t n, mp_bitcnt_t b) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_tdiv_r_2exp(r.ToIntPtr(), n.ToIntPtr(), b); } /// /// Set the remainder to - q * where q = trunc( / ), and return | |. /// /// The result remainder integer. /// The numerator integer. /// The denominator integer. /// Return | |. /// mpz_cdiv_qr /// mpz_congruent_p /// mpz_divexact /// mpz_divisible_p /// mpz_fdiv_qr /// mpz_mod /// mpz_tdiv_q /// mpz_tdiv_r /// mpz_tdiv_qr /// mpz_tdiv_q_ui /// mpz_tdiv_qr_ui /// mpz_tdiv_ui /// mpz_tdiv_q_2exp /// mpz_tdiv_r_2exp /// Integer Division /// GNU MP - Integer Division /// /// /// // Create, initialize, and set the value of n to 10000. /// mpz_t n = new mpz_t(); /// gmp_lib.mpz_init_set_si(n, 10000); /// /// // Create, initialize, and set the value of r to 0. /// mpz_t r = new mpz_t(); /// gmp_lib.mpz_init(r); /// /// // Set r = n - 3 * trunc(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_tdiv_r_ui(r, n, 3U) == 1U); /// /// // Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) == 1); /// /// // Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, null); /// /// /// ' Create, initialize, and set the value of n to 10000. /// Dim n As New mpz_t() /// gmp_lib.mpz_init_set_si(n, 10000) /// /// ' Create, initialize, and set the value of r to 0. /// Dim r As New mpz_t() /// gmp_lib.mpz_init(r) /// /// ' Set r = n - 3 * trunc(n / 3), and return |r|. /// Assert.IsTrue(gmp_lib.mpz_tdiv_r_ui(r, n, 3UI) = 1UI) /// /// ' Assert that r is 1. /// Assert.IsTrue(gmp_lib.mpz_get_si(r) = 1) /// /// ' Release unmanaged memory allocated for n and r. /// gmp_lib.mpz_clears(n, r, Nothing) /// /// public static uint mpz_tdiv_r_ui(mpz_t r, /*const*/ mpz_t n, uint /*unsigned long int*/ d) { if (r == null) throw new ArgumentNullException("r"); if (n == null) throw new ArgumentNullException("n"); return SafeNativeMethods.__gmpz_tdiv_r_ui(r.ToIntPtr(), n.ToIntPtr(), d); } /// /// Test bit in and return 0 or 1 accordingly. /// /// /// /// Test bit in and return 0 or 1 accordingly. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_xor /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of rop to 70. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init_set_si(rop, 70); /// /// // Assert that bit 3 of rop is 0. /// Assert.IsTrue(gmp_lib.mpz_tstbit(rop, 3U) == 0); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 70. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init_set_si(rop, 70) /// /// ' Assert that bit 3 of rop is 0. /// Assert.IsTrue(gmp_lib.mpz_tstbit(rop, 3UI) = 0) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static int mpz_tstbit(/*const*/ mpz_t op, mp_bitcnt_t bit_index) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpz_tstbit(op.ToIntPtr(), bit_index); } /// /// Set to ^. The case 0^0 yields 1. /// /// The result integer. /// The base integer. /// The exponent integer. /// mpz_powm /// mpz_powm_ui /// mpz_powm_sec /// mpz_pow_ui /// Integer Exponentiations /// GNU MP - Integer Exponentiation /// /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop = 2^4. /// gmp_lib.mpz_ui_pow_ui(rop, 2U, 4U); /// /// // Assert that rop is 16. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 16); /// /// // Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop = 2^4. /// gmp_lib.mpz_ui_pow_ui(rop, 2UI, 4UI) /// /// ' Assert that rop is 16. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 16) /// /// ' Release unmanaged memory allocated for rop. /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_ui_pow_ui(mpz_t rop, uint /*unsigned long int*/ @base, uint /*unsigned long int*/ exp) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpz_ui_pow_ui(rop.ToIntPtr(), @base, exp); } /// /// Generate a uniformly distributed random integer in the range 0 to 2^ - 1, inclusive. /// /// The result integer. /// The random number generator state. /// The operand integer. /// /// /// The variable must be initialized by calling one of the gmp_randinit /// functions (GNU MP - Random State Initialization) before invoking this function. /// /// /// The random number functions of GMP come in two groups; older function that rely on a global state, /// and newer functions that accept a state parameter that is read and modified. /// Please see the GNU MP - Random Number Functions /// for more information on how to use and not to use random number functions. /// /// /// mpz_urandomm /// mpz_rrandomb /// mpz_random /// mpz_random2 /// Integer Random Numbers /// GNU MP - Integer Random Numbers /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Generate a random integer in the range [0, (2^50)-1]. /// gmp_lib.mpz_urandomb(rop, state, 50); /// /// // Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpz_clear(rop); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Generate a random integer in the range [0, (2^50)-1]. /// gmp_lib.mpz_urandomb(rop, state, 50) /// /// ' Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpz_clear(rop) /// /// public static void mpz_urandomb(mpz_t rop, gmp_randstate_t state, mp_bitcnt_t n) { if (rop == null) throw new ArgumentNullException("rop"); if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmpz_urandomb(rop.ToIntPtr(), state.ToIntPtr(), n); } /// /// Generate a uniform random integer in the range 0 to - 1, inclusive. /// /// The result integer. /// The random number generator state. /// The operand integer. /// /// /// The variable must be initialized by calling one of the gmp_randinit /// functions (GNU MP - Random State Initialization) before invoking this function. /// /// /// The random number functions of GMP come in two groups; older function that rely on a global state, /// and newer functions that accept a state parameter that is read and modified. /// Please see the GNU MP - Random Number Functions /// for more information on how to use and not to use random number functions. /// /// /// mpz_urandomb /// mpz_rrandomb /// mpz_random /// mpz_random2 /// Integer Random Numbers /// GNU MP - Integer Random Numbers /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Create, initialize, and set a large integer. /// mpz_t n = new mpz_t(); /// char_ptr value = new char_ptr("123 456 789 012 345 678 901"); /// gmp_lib.mpz_init_set_str(n, value, 10); /// /// // Generate a random integer in the range [0, n-1]. /// gmp_lib.mpz_urandomm(rop, state, n); /// /// // Free all memory occupied by state, rop, and n. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpz_clears(rop, n, null); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Create, initialize, and set a large integer. /// Dim n As New mpz_t() /// Dim value As New char_ptr("123 456 789 012 345 678 901") /// gmp_lib.mpz_init_set_str(n, value, 10) /// /// ' Generate a random integer in the range [0, n-1]. /// gmp_lib.mpz_urandomm(rop, state, n) /// /// ' Free all memory occupied by state, rop, and n. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpz_clears(rop, n, Nothing) /// /// public static void mpz_urandomm(mpz_t rop, gmp_randstate_t state, /*const*/ mpz_t n) { if (rop == null) throw new ArgumentNullException("rop"); if (state == null) throw new ArgumentNullException("state"); if (n == null) throw new ArgumentNullException("n"); SafeNativeMethods.__gmpz_urandomm(rop.ToIntPtr(), state.ToIntPtr(), n.ToIntPtr()); } /// /// Set to bitwise exclusive-or . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// /// /// The function behaves as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). /// The least significant bit is number 0. /// /// /// mpz_and /// mpz_ior /// mpz_com /// mpz_popcount /// mpz_hamdist /// mpz_scan0 /// mpz_scan1 /// mpz_setbit /// mpz_clrbit /// mpz_combit /// mpz_tstbit /// Integer Logic and Bit Fiddling /// GNU MP - Integer Logic and Bit Fiddling /// /// /// // Create, initialize, and set the value of op1 to 63. /// mpz_t op1 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op1, 63U); /// /// // Create, initialize, and set the value of op2 to 70. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init_set_ui(op2, 70U); /// /// // Create, initialize, and set the value of rop to 0. /// mpz_t rop = new mpz_t(); /// gmp_lib.mpz_init(rop); /// /// // Set rop to the bitwise exclusive or of op1 and op2. /// gmp_lib.mpz_xor(rop, op1, op2); /// /// // Assert that rop is 121. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) == 121); /// /// // Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 63. /// Dim op1 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op1, 63UI) /// /// ' Create, initialize, and set the value of op2 to 70. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init_set_ui(op2, 70UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpz_t() /// gmp_lib.mpz_init(rop) /// /// ' Set rop to the bitwise exclusive or of op1 and op2. /// gmp_lib.mpz_xor(rop, op1, op2) /// /// ' Assert that rop is 121. /// Assert.IsTrue(gmp_lib.mpz_get_si(rop) = 121) /// /// ' Release unmanaged memory allocated for rop, op1, and op2. /// gmp_lib.mpz_clears(rop, op1, op2, Nothing) /// /// public static void mpz_xor(mpz_t rop, /*const*/ mpz_t op1, /*const*/ mpz_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpz_xor(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Return a pointer to the limb array representing the absolute value of . /// /// The integer. /// A pointer to the limb array representing the absolute value of . /// /// /// The size of the array is mpz_size(x). Intended for read access only. /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_size /// mpz_limbs_write /// mpz_limbs_modify /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Set the value of x. /// char_ptr value = new char_ptr("10000 00000000000000000000000000000000"); /// gmp_lib.mpz_set_str(x, value, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 4); /// /// // Get pointer to the limbs of x. /// mp_ptr limbs = gmp_lib.mpz_limbs_read(x); /// /// // Assert the values of the limbs based on current architecture (x86 or x64). /// Assert.IsTrue(limbs[0] == 0); /// Assert.IsTrue(limbs[1] == (gmp_lib.mp_bytes_per_limb == 4 ? 16U : 256U)); /// /// // Release unmanaged memory allocated for x and value. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(value); /// /// /// ' Create and initialize new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Set the value of x. /// Dim value As New char_ptr("10000 00000000000000000000000000000000") /// gmp_lib.mpz_set_str(x, value, If(gmp_lib.mp_bytes_per_limb = 4, 2, 4)) /// /// ' Get pointer to the limbs of x. /// Dim limbs As mp_ptr = gmp_lib.mpz_limbs_read(x) /// /// ' Assert the values of the limbs based on current architecture (x86 or x64). /// Assert.IsTrue(limbs(0) = 0) /// Assert.IsTrue(limbs(1) = (If(gmp_lib.mp_bytes_per_limb = 4, 16UI, 256UI))) /// /// ' Release unmanaged memory allocated for x and value. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(value) /// /// public static mp_ptr /*mp_limb_t**/ mpz_limbs_read(/*const*/ mpz_t x) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_limbs_read(x.ToIntPtr()); return x._mp_d; // new mp_ptr(SafeNativeMethods.__gmpz_limbs_read(x)); } /// /// Return a pointer to the limb array of , intended for write access. /// /// The operand integer. /// The number of limbs. /// A pointer to the limb array of , intended for write access. /// /// /// The array is reallocated as needed, to make room for limbs. /// Requires > 0. /// The mpz_limbs_write function may destroy the old value and /// return an array with unspecified contents. /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_size /// mpz_limbs_read /// mpz_limbs_modify /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Resize x to 3 limbs, and get pointer to the limbs. /// gmp_lib.mpz_set_ui(x, 2U); /// mp_ptr limbs = gmp_lib.mpz_limbs_write(x, 3); /// /// // Set the values of the limbs. /// limbs[0] = 0U; /// limbs[1] = 0U; /// limbs[2] = (gmp_lib.mp_bytes_per_limb == 4 ? 2U : 4U); /// gmp_lib.mpz_limbs_finish(x, -3); /// /// // Assert the value of x based on current architecture (x86 or x64). /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 4, x); /// Assert.IsTrue(s.ToString() == "-10 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(s); /// /// /// ' Create and initialize new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Resize x to 3 limbs, and get pointer to the limbs. /// gmp_lib.mpz_set_ui(x, 2UI) /// Dim limbs As mp_ptr = gmp_lib.mpz_limbs_write(x, 3) /// /// ' Set the values of the limbs. /// limbs(0) = 0UI /// limbs(1) = 0UI /// limbs(2) = (If(gmp_lib.mp_bytes_per_limb = 4, 2UI, 4UI)) /// gmp_lib.mpz_limbs_finish(x, -3) /// /// ' Assert the value of x based on current architecture (x86 or x64). /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, If(gmp_lib.mp_bytes_per_limb = 4, 2, 4), x) /// Assert.IsTrue(s.ToString() = "-10 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")) /// /// ' Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(s) /// /// public static mp_ptr /*mp_limb_t**/ mpz_limbs_write(/*const*/ mpz_t x, mp_size_t n) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_limbs_write(x.ToIntPtr(), n); return x._mp_d; // new mp_ptr(SafeNativeMethods.__gmpz_limbs_write(x, n)); } /// /// Return a pointer to the limb array of , intended for write access. /// /// The operand integer. /// The number of limbs. /// A pointer to the limb array of , intended for write access. /// /// /// The array is reallocated as needed, to make room for limbs. /// Requires > 0. /// The mpz_limbs_modify function returns an array that holds the old absolute /// value of /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_size /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_finish /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create, initialize, and set the value of x to 2. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init_set_ui(x, 2U); /// /// // Resize x to 3 limbs, and get pointer to the limbs. /// mp_ptr limbs = gmp_lib.mpz_limbs_modify(x, 3); /// /// // Set the value of x. /// limbs[0] = 0; /// limbs[1] = 0; /// limbs[2] = (IntPtr.Size == 4 ? 8U : 64U); /// gmp_lib.mpz_limbs_finish(x, -3); /// /// // Assert the value of x based on current architecture (x86 or x64). /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 4, x); /// Assert.IsTrue(s.ToString() == "-1000 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(s); /// /// /// ' Create, initialize, and set the value of x to 2. /// Dim x As New mpz_t() /// gmp_lib.mpz_init_set_ui(x, 2UI) /// /// ' Resize x to 3 limbs, and get pointer to the limbs. /// Dim limbs As mp_ptr = gmp_lib.mpz_limbs_modify(x, 3) /// /// ' Set the value of x. /// limbs(0) = 0 /// limbs(1) = 0 /// limbs(2) = (If(IntPtr.Size = 4, 8UI, 64UI)) /// gmp_lib.mpz_limbs_finish(x, -3) /// /// ' Assert the value of x based on current architecture (x86 or x64). /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, If(gmp_lib.mp_bytes_per_limb = 4, 2, 4), x) /// Assert.IsTrue(s.ToString() = "-1000 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")) /// /// ' Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(s) /// /// public static mp_ptr /*mp_limb_t**/ mpz_limbs_modify(/*const*/ mpz_t x, mp_size_t n) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_limbs_modify(x.ToIntPtr(), n); return x._mp_d; // new mp_ptr(SafeNativeMethods.__gmpz_limbs_modify(x, n)); } /// /// Updates the internal size field of . /// /// The operand integer. /// The number of limbs and the sign of . /// /// /// Used after writing to the limb array pointer returned by mpz_limbs_write /// or mpz_limbs_modify is completed. The array should contain | | /// valid limbs, representing the new absolute value for , and the sign of /// is taken from the sign of . /// This function never reallocates , so the limb pointer remains valid. /// /// /// void foo (mpz_t x) /// { /// mp_size_t n, i; /// mp_limb_t* xp; /// /// n = mpz_size(x); /// xp = mpz_limbs_modify(x, 2 * n); /// for (i = 0; i < n; i++) /// xp[n + i] = xp[n - 1 - i]; /// mpz_limbs_finish(x, mpz_sgn(x) < 0 ? - 2 * n : 2 * n); /// } /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_size /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_modify /// mpz_roinit_n /// Integer Special Functions /// GNU MP - Integer Special Functions public static void mpz_limbs_finish(/*const*/ mpz_t x, mp_size_t s) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpz_limbs_finish(x.ToIntPtr(), s); } /// /// Special initialization of , using the given limb array and size. /// /// The operand integer. /// The limbs array. /// The number of limbs and the sign. /// For convenience, the function returns , but cast to a const pointer type. /// /// /// should be treated as readonly: it can be passed safely as input to any mpz function, but not as an output. /// The array must point to at least a readable limb, its size is | |, and the /// sign of is the sign of . /// /// /// void foo (mpz_t x) /// { /// static const mp_limb_t y[3] = { 0x1, 0x2, 0x3 }; /// mpz_t tmp; /// mpz_add(x, x, mpz_roinit_n(tmp, y, 3)); /// } /// /// /// _mpz_realloc /// mpz_getlimbn /// mpz_size /// mpz_limbs_read /// mpz_limbs_write /// mpz_limbs_modify /// mpz_limbs_finish /// Integer Special Functions /// GNU MP - Integer Special Functions /// /// /// // Create and initialize new integer x. /// mpz_t x = new mpz_t(); /// gmp_lib.mpz_init(x); /// /// // Prepare new limbs for x. /// mp_ptr limbs; /// if (gmp_lib.mp_bytes_per_limb == 4) /// limbs = new mp_ptr(new uint[] { 0U, 0U, 2U }); /// else /// limbs = new mp_ptr(new ulong[] { 0UL, 0UL, 4UL }); /// /// // Assign new limbs to x, and make x negative. /// x = gmp_lib.mpz_roinit_n(x, limbs, -3); /// /// // Assert new value of x. /// char_ptr s = gmp_lib.mpz_get_str(char_ptr.Zero, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 4, x); /// Assert.IsTrue(s.ToString() == "-10 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x); /// gmp_lib.free(s); /// /// /// ' Create and initialize new integer x. /// Dim x As New mpz_t() /// gmp_lib.mpz_init(x) /// /// ' Prepare new limbs for x. /// Dim limbs As mp_ptr /// If gmp_lib.mp_bytes_per_limb = 4 Then /// limbs = New mp_ptr(New UInteger() { 0UI, 0UI, 2UI}) /// Else /// limbs = New mp_ptr(New ULong() { 0UL, 0UL, 4UL}) /// End If /// /// ' Assign new limbs to x, and make x negative. /// x = gmp_lib.mpz_roinit_n(x, limbs, -3) /// /// ' Assert new value of x. /// Dim s As char_ptr = gmp_lib.mpz_get_str(char_ptr.Zero, If(gmp_lib.mp_bytes_per_limb = 4, 2, 4), x) /// Assert.IsTrue(s.ToString() = "-10 00000000000000000000000000000000 00000000000000000000000000000000".Replace(" ", "")) /// /// ' Release unmanaged memory allocated for x and s. /// gmp_lib.mpz_clear(x) /// gmp_lib.free(s) /// /// public static /*const*/ mpz_t mpz_roinit_n(mpz_t x, /*const*/ mp_ptr /*mp_limb_t **/ xp, mp_size_t xs) { if (x == null) throw new ArgumentNullException("x"); if (xp == null) throw new ArgumentNullException("xp"); SafeNativeMethods.__gmpz_roinit_n(x.ToIntPtr(), xp.ToIntPtr(), xs); return x; } #endregion #region "Rational (i.e. Q) routines." /// /// Set to the absolute value of . /// /// The result rational. /// The operand rational. /// mpq_add /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_abs(mpq_t rop, /*const*/ mpq_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpq_abs(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to + . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_add(mpq_t sum, /*const*/ mpq_t addend1, /*const*/ mpq_t addend2) { if (sum == null) throw new ArgumentNullException("sum"); if (addend1 == null) throw new ArgumentNullException("addend1"); if (addend2 == null) throw new ArgumentNullException("addend2"); SafeNativeMethods.__gmpq_add(sum.ToIntPtr(), addend1.ToIntPtr(), addend2.ToIntPtr()); } /// /// Remove any factors that are common to the numerator and denominator of , and make the denominator positive. /// /// The operand rational. /// mpq_init /// mpq_inits /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// /// /// /// public static void mpq_canonicalize(mpq_t op) { if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpq_canonicalize(op.ToIntPtr()); } /// /// Free the space occupied by . /// /// The operand rational. /// /// /// Make sure to call this function for all mpq_t variables when you are done with them. /// /// /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create and initialize a new rational x. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) == 0.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x); /// /// /// ' Create and initialize a new rational x. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// /// ' Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) = 0.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x) /// /// public static void mpq_clear(mpq_t x) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpq_clear(x.ToIntPtr()); x.Clear(); } /// /// Free the space occupied by a NULL-terminated list of mpq_t variables. /// /// The operand rational. /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create new rationals x1, x2 and x3. /// mpq_t x1 = new mpq_t(); /// mpq_t x2 = new mpq_t(); /// mpq_t x3 = new mpq_t(); /// /// // Initialize the rationals. /// gmp_lib.mpq_inits(x1, x2, x3, null); /// /// // Assert that their value is 0.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x1) == 0.0); /// Assert.IsTrue(gmp_lib.mpq_get_d(x2) == 0.0); /// Assert.IsTrue(gmp_lib.mpq_get_d(x3) == 0.0); /// /// // Release unmanaged memory allocated for the rationals. /// gmp_lib.mpq_clears(x1, x2, x3, null); /// /// /// ' Create new rationals x1, x2 and x3. /// Dim x1 As New mpq_t() /// Dim x2 As New mpq_t() /// Dim x3 As New mpq_t() /// /// ' Initialize the rationals. /// gmp_lib.mpq_inits(x1, x2, x3, Nothing) /// /// ' Assert that their value is 0.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x1) = 0.0) /// Assert.IsTrue(gmp_lib.mpq_get_d(x2) = 0.0) /// Assert.IsTrue(gmp_lib.mpq_get_d(x3) = 0.0) /// /// ' Release unmanaged memory allocated for the rationals. /// gmp_lib.mpq_clears(x1, x2, x3, Nothing) /// /// public static void mpq_clears(params mpq_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpq_t a in x) { if (a != null) mpq_clear(a); } } /// /// Compare and . /// /// The first operand rational. /// The second operand rational. /// Return a positive value if > , zero if = , and a negative value if < . /// /// /// To determine if two rationals are equal, mpq_equal is faster than mpq_cmp. /// /// /// mpq_cmp_z /// mpq_cmp_ui /// mpq_cmp_si /// mpq_sgn /// mpq_equal /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set the value of op1 to 1 / 2. /// mpq_t op1 = new mpq_t(); /// gmp_lib.mpq_init(op1); /// gmp_lib.mpq_set_si(op1, 1, 2U); /// /// // Create, initialize, and set the value of op2 to 1 / 3. /// mpq_t op2 = new mpq_t(); /// gmp_lib.mpq_init(op2); /// gmp_lib.mpq_set_si(op2, 1, 3U); /// /// // Assert that op1 > op2. /// Assert.IsTrue(gmp_lib.mpq_cmp(op1, op2) > 0); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clears(op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 1 / 2. /// Dim op1 As New mpq_t() /// gmp_lib.mpq_init(op1) /// gmp_lib.mpq_set_si(op1, 1, 2UI) /// /// ' Create, initialize, and set the value of op2 to 1 / 3. /// Dim op2 As New mpq_t() /// gmp_lib.mpq_init(op2) /// gmp_lib.mpq_set_si(op2, 1, 3UI) /// /// ' Assert that op1 > op2. /// Assert.IsTrue(gmp_lib.mpq_cmp(op1, op2) > 0) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clears(op1, op2, Nothing) /// /// public static int mpq_cmp(/*const*/ mpq_t op1, /*const*/ mpq_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpq_cmp(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compare and / . /// /// The first operand rational. /// The second operand numerator integer. /// The second operand denominator integer. /// Return a positive value if > / , zero if = / , and a negative value if < / . /// /// /// and are allowed to have common factors. /// /// /// mpq_cmp /// mpq_cmp_z /// mpq_cmp_ui /// mpq_sgn /// mpq_equal /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set the value of op1 to 1 / 2. /// mpq_t op1 = new mpq_t(); /// gmp_lib.mpq_init(op1); /// gmp_lib.mpq_set_si(op1, 1, 2U); /// /// // Assert that op1 < 5/6. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op1, 5, 6U) < 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpq_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 1 / 2. /// Dim op1 As New mpq_t() /// gmp_lib.mpq_init(op1) /// gmp_lib.mpq_set_si(op1, 1, 2UI) /// /// ' Assert that op1 < 5/6. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op1, 5, 6UI) < 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpq_clear(op1) /// /// public static int mpq_cmp_si(/*const*/ mpq_t op1, int /*long int*/ num2, uint /*unsigned long int*/ den2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpq_cmp_si(op1.ToIntPtr(), num2, den2); } /// /// Compare and / . /// /// The first operand rational. /// The second operand numerator integer. /// The second operand denominator integer. /// Return a positive value if > / , zero if = / , and a negative value if < / . /// /// /// and are allowed to have common factors. /// /// /// mpq_cmp /// mpq_cmp_z /// mpq_cmp_si /// mpq_sgn /// mpq_equal /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set the value of op1 to 1 / 2. /// mpq_t op1 = new mpq_t(); /// gmp_lib.mpq_init(op1); /// gmp_lib.mpq_set_si(op1, 1, 2U); /// /// // Assert that op1 == 3/6. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(op1, 3, 6U) == 0); /// /// // Release unmanaged memory allocated for op1. /// gmp_lib.mpq_clear(op1); /// /// /// ' Create, initialize, and set the value of op1 to 1 / 2. /// Dim op1 As New mpq_t() /// gmp_lib.mpq_init(op1) /// gmp_lib.mpq_set_si(op1, 1, 2UI) /// /// ' Assert that op1 == 3/6. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(op1, 3, 6UI) = 0) /// /// ' Release unmanaged memory allocated for op1. /// gmp_lib.mpq_clear(op1) /// /// public static int mpq_cmp_ui(/*const*/ mpq_t op1, uint /*unsigned long int*/ num2, uint /*unsigned long int*/ den2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpq_cmp_ui(op1.ToIntPtr(), num2, den2); } /// /// Compare and . /// /// The first operand rational. /// The second operand rational. /// Return a positive value if > , zero if = , and a negative value if < . /// /// /// To determine if two rationals are equal, mpq_equal is faster than mpq_cmp. /// /// /// mpq_cmp /// mpq_cmp_ui /// mpq_cmp_si /// mpq_sgn /// mpq_equal /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set the value of op1 to 1 / 2. /// mpq_t op1 = new mpq_t(); /// gmp_lib.mpq_init(op1); /// gmp_lib.mpq_set_si(op1, 1, 2U); /// /// // Create, initialize, and set the value of op2 to 3. /// mpz_t op2 = new mpz_t(); /// gmp_lib.mpz_init(op2); /// gmp_lib.mpz_set_si(op2, 3); /// /// // Assert that op1 < op2. /// Assert.IsTrue(gmp_lib.mpq_cmp_z(op1, op2) < 0); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clear(op1); /// gmp_lib.mpz_clear(op2); /// /// /// ' Create, initialize, and set the value of op1 to 1 / 2. /// Dim op1 As New mpq_t() /// gmp_lib.mpq_init(op1) /// gmp_lib.mpq_set_si(op1, 1, 2UI) /// /// ' Create, initialize, and set the value of op2 to 3. /// Dim op2 As New mpz_t() /// gmp_lib.mpz_init(op2) /// gmp_lib.mpz_set_si(op2, 3) /// /// ' Assert that op1 < op2. /// Assert.IsTrue(gmp_lib.mpq_cmp_z(op1, op2) < 0) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clear(op1) /// gmp_lib.mpz_clear(op2) /// /// public static int mpq_cmp_z(/*const*/ mpq_t op1, /*const*/ mpz_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpq_cmp_z(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Return a reference to the denominator . /// /// The operand rational. /// Return a reference to the denominator . /// /// /// The mpz functions can be used on the returned reference. /// /// /// mpq_numref /// mpq_get_num /// mpq_get_den /// mpq_set_num /// mpq_set_den /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Get reference to denominator, and increment it by 2. /// mpz_t num = gmp_lib.mpq_denref(op); /// gmp_lib.mpz_add_ui(num, num, 2U); /// /// // Assert that op is -1 / 5. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 5U) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Get reference to denominator, and increment it by 2. /// Dim num As mpz_t = gmp_lib.mpq_denref(op) /// gmp_lib.mpz_add_ui(num, num, 2UI) /// /// ' Assert that op is -1 / 5. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 5UI) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op) /// /// public static mpz_t mpq_denref(/*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_den; } /// /// Set to / . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_add /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div_2exp /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_div(mpq_t quotient, /*const*/ mpq_t dividend, /*const*/ mpq_t divisor) { if (quotient == null) throw new ArgumentNullException("quotient"); if (dividend == null) throw new ArgumentNullException("dividend"); if (divisor == null) throw new ArgumentNullException("divisor"); SafeNativeMethods.__gmpq_div(quotient.ToIntPtr(), dividend.ToIntPtr(), divisor.ToIntPtr()); } /// /// Set to / 2^. /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_add /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_div_2exp(mpq_t rop, /*const*/ mpq_t op1, uint /*mp_bitcnt_t*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpq_div_2exp(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Return non-zero if and are equal, zero if they are non-equal. /// /// The first operand rational. /// The second operand rational. /// Return non-zero if and are equal, zero if they are non-equal. /// /// /// Although mpq_cmp can be used for the same purpose, this function is much faster. /// /// /// mpq_cmp /// mpq_cmp_z /// mpq_cmp_ui /// mpq_cmp_si /// mpq_sgn /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set the value of op1 to 1 / 2. /// mpq_t op1 = new mpq_t(); /// gmp_lib.mpq_init(op1); /// gmp_lib.mpq_set_si(op1, 1, 2U); /// /// // Create, initialize, and set the value of op2 to 1 / 3. /// mpq_t op2 = new mpq_t(); /// gmp_lib.mpq_init(op2); /// gmp_lib.mpq_set_si(op2, 1, 3U); /// /// // Assert that op1 != op2. /// Assert.IsTrue(gmp_lib.mpq_equal(op1, op2) == 0); /// /// // Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clears(op1, op2, null); /// /// /// ' Create, initialize, and set the value of op1 to 1 / 2. /// Dim op1 As New mpq_t() /// gmp_lib.mpq_init(op1) /// gmp_lib.mpq_set_si(op1, 1, 2UI) /// /// ' Create, initialize, and set the value of op2 to 1 / 3. /// Dim op2 As New mpq_t() /// gmp_lib.mpq_init(op2) /// gmp_lib.mpq_set_si(op2, 1, 3UI) /// /// ' Assert that op1 != op2. /// Assert.IsTrue(gmp_lib.mpq_equal(op1, op2) = 0) /// /// ' Release unmanaged memory allocated for op1 and op2. /// gmp_lib.mpq_clears(op1, op2, Nothing) /// /// public static int mpq_equal(/*const*/ mpq_t op1, /*const*/ mpq_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpq_equal(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to the numerator of . /// /// The result integer. /// The operand rational. /// /// /// The function is equivalent to calling mpz_set with mpq_numref. /// Direct use of mpq_numref is recommended instead of this functions. /// /// /// mpq_numref /// mpq_denref /// mpq_get_den /// mpq_set_num /// mpq_set_den /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Create and initialize a new integer. /// mpz_t num = new mpz_t(); /// gmp_lib.mpz_init(num); /// /// // Set integer to numerator of rational, and increment integer by 2. /// gmp_lib.mpq_get_num(num, op); /// gmp_lib.mpz_add_ui(num, num, 2U); /// /// // Assert that num is 1, and op is -1 / 3. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(num, 1) == 0); /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 3U) == 0); /// /// // Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op); /// gmp_lib.mpz_clear(num); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Create and initialize a new integer. /// Dim num As New mpz_t() /// gmp_lib.mpz_init(num) /// /// ' Set integer to numerator of rational, and increment integer by 2. /// gmp_lib.mpq_get_num(num, op) /// gmp_lib.mpz_add_ui(num, num, 2UI) /// /// ' Assert that num is 1, and op is -1 / 3. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(num, 1) = 0) /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 3UI) = 0) /// /// ' Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op) /// gmp_lib.mpz_clear(num) /// /// public static void mpq_get_num(mpz_t numerator, /*const*/ mpq_t rational) { if (numerator == null) throw new ArgumentNullException("numerator"); if (rational == null) throw new ArgumentNullException("rational"); SafeNativeMethods.__gmpq_get_num(numerator.ToIntPtr(), rational.ToIntPtr()); } /// /// Set to the denominator of . /// /// The result integer. /// The operand rational. /// /// /// The function is equivalent to calling mpz_set with mpq_denref. /// Direct use of mpq_denref is recommended instead of this functions. /// /// /// mpq_numref /// mpq_denref /// mpq_get_num /// mpq_get_den /// mpq_set_num /// mpq_set_den /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Create and initialize a new integer. /// mpz_t den = new mpz_t(); /// gmp_lib.mpz_init(den); /// /// // Set integer to numerator of rational, and increment integer by 2.. /// gmp_lib.mpq_get_den(den, op); /// gmp_lib.mpz_add_ui(den, den, 2U); /// /// // Assert that num is 1, and op is -1 / 3. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(den, 5) == 0); /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 3U) == 0); /// /// // Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op); /// gmp_lib.mpz_clear(den); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Create and initialize a new integer. /// Dim den As New mpz_t() /// gmp_lib.mpz_init(den) /// /// ' Set integer to numerator of rational, and increment integer by 2.. /// gmp_lib.mpq_get_den(den, op) /// gmp_lib.mpz_add_ui(den, den, 2UI) /// /// ' Assert that num is 1, and op is -1 / 3. /// Assert.IsTrue(gmp_lib.mpz_cmp_si(den, 5) = 0) /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 3UI) = 0) /// /// ' Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op) /// gmp_lib.mpz_clear(den) /// /// public static void mpq_get_den(mpz_t denominator, /*const*/ mpq_t rational) { if (denominator == null) throw new ArgumentNullException("denominator"); if (rational == null) throw new ArgumentNullException("rational"); SafeNativeMethods.__gmpq_get_den(denominator.ToIntPtr(), rational.ToIntPtr()); } /// /// Convert to a double, truncating if necessary (i.e. rounding towards zero). /// /// The operand rational. /// The converted double. /// /// /// If the exponent from the conversion is too big or too small to fit a double then the result is system dependent. /// For too big an infinity is returned when available. /// For too small 0.0 is normally returned. /// Hardware overflow, underflow and denorm traps may or may not occur. /// /// /// mpq_set_d /// mpq_set_f /// mpq_get_str /// Rational Conversions /// GNU MP - Rational Conversions /// /// /// // Create, initialize, and set the value of x to 10 / 11. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, 10, 11U); /// /// // Assert that the value of x is 10.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) == 10.0 / 11.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x); /// /// /// ' Create, initialize, and set the value of x to 10 / 11. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, 10, 11UI) /// /// ' Assert that the value of x is 10.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) = 10.0 / 11.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x) /// /// public static double mpq_get_d(/*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpq_get_d(op.ToIntPtr()); } /// /// Convert to a string of digits in base . /// /// The result string. /// The base. /// The operand rational. /// A pointer to the result string is returned, being either the allocated block, or the given . /// /// /// The base may vary from 2 to 36. The string will be of the form "num/den", or if the denominator is 1 then just "num". /// /// /// If is NULL, the result string is allocated using the current allocation function /// (see GNU MP - Custom Allocation). /// The block will be strlen() + 1 bytes, that being exactly enough for the string and null-terminator. /// /// /// If is not NULL, it should point to a block of storage large enough for the result, that being /// /// /// mpz_sizeinbase(mpq_numref(op), base) + mpz_sizeinbase(mpq_denref(op), base) + 3 /// /// /// The three extra bytes are for a possible minus sign, possible slash, and the null-terminator. /// /// /// mpq_get_d /// mpq_set_d /// mpq_set_f /// Rational Conversions /// GNU MP - Rational Conversions /// /// /// // Create, initialize, and set the value of x to -210 / 13. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, -210, 13U); /// /// // Retrieve the string value of x, and assert that it is "-210/13". /// char_ptr s = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == "-210/13"); /// /// // Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpq_clear(x); /// gmp_lib.free(s); /// /// /// ' Create, initialize, and set the value of x to -210 / 13. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, -210, 13UI) /// /// ' Retrieve the string value of x, and assert that it is "-210/13". /// Dim s As char_ptr = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x) /// Assert.IsTrue(s.ToString() = "-210/13") /// /// ' Release unmanaged memory allocated for x and the string value. /// gmp_lib.mpq_clear(x) /// gmp_lib.free(s) /// /// public static char_ptr mpq_get_str(char_ptr str, int @base, /*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); return new Native.char_ptr(SafeNativeMethods.__gmpq_get_str(str.ToIntPtr(), @base, op.ToIntPtr())); } /// /// Initialize and set it to 0/1. /// /// The operand rational. /// /// /// Each variable should normally only be initialized once, or at least cleared out /// (using the function mpq_clear) between each initialization. /// /// /// mpq_canonicalize /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create and initialize a new rational x. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Assert that the value of x is 0. /// char_ptr s = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == "0"); /// /// // Release unmanaged memory allocated for x and its string value. /// gmp_lib.mpq_clear(x); /// gmp_lib.free(s); /// /// /// ' Create and initialize a new rational x. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// /// ' Assert that the value of x is 0. /// Dim s As char_ptr = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x) /// Assert.IsTrue(s.ToString() = "0") /// /// ' Release unmanaged memory allocated for x and its string value. /// gmp_lib.mpq_clear(x) /// gmp_lib.free(s) /// /// public static void mpq_init(mpq_t x) { if (x == null) throw new ArgumentNullException("x"); x.Initializing(); SafeNativeMethods.__gmpq_init(x.ToIntPtr()); } /// /// Initialize a NULL-terminated list of mpq_t variables, and set their values to 0/1. /// /// The operand rational. /// mpq_canonicalize /// mpq_init /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create new rationals x1, x2 and x3. /// mpq_t x1 = new mpq_t(); /// mpq_t x2 = new mpq_t(); /// mpq_t x3 = new mpq_t(); /// /// // Initialize the rationals. /// gmp_lib.mpq_inits(x1, x2, x3); /// /// // Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x1) == 0.0); /// Assert.IsTrue(gmp_lib.mpq_get_d(x2) == 0.0); /// Assert.IsTrue(gmp_lib.mpq_get_d(x3) == 0.0); /// /// // Release unmanaged memory allocated for the rationals. /// gmp_lib.mpq_clears(x1, x2, x3, null); /// /// /// ' Create new rationals x1, x2 and x3. /// Dim x1 As New mpq_t() /// Dim x2 As New mpq_t() /// Dim x3 As New mpq_t() /// /// ' Initialize the rationals. /// gmp_lib.mpq_inits(x1, x2, x3) /// /// ' Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x1) = 0.0) /// Assert.IsTrue(gmp_lib.mpq_get_d(x2) = 0.0) /// Assert.IsTrue(gmp_lib.mpq_get_d(x3) = 0.0) /// /// ' Release unmanaged memory allocated for the rationals. /// gmp_lib.mpq_clears(x1, x2, x3, Nothing) /// /// public static void mpq_inits(params mpq_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpq_t a in x) { if (a != null) mpq_init(a); } } /// /// Read a string of digits from and convert them to a rational in . /// /// The result rational. /// Pointer to file stream. /// The base. /// Return the number of characters read (including white space), or 0 if a rational could not be read. /// /// /// Any initial white-space characters are read and discarded. /// /// /// The input can be a fraction like "17/63" or just an integer like "123". /// Reading stops at the first character not in this form, and white space is not permitted within the string. /// If the input might not be in canonical form, then mpq_canonicalize must be called /// (see GNU MP - Rational Number Functions). /// /// /// The base can be between 2 and 36, or can be 0 in which case the leading characters /// of the string determine the base, "0x" or "0X" for hexadecimal, "0" for octal, or decimal otherwise. /// The leading characters are examined separately for the numerator and denominator of a fraction, so for instance /// "0x10/11" is 16/11, whereas "0x10/0x11" is 16/17. /// /// /// mpq_out_str /// I/O of Rationals /// GNU MP - I/O of Rationals /// /// /// // Create, initialize, and set the value of op to 123/456. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// /// // Write rational to a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// System.IO.File.WriteAllText(pathname, "123/456"); /// /// // Read op from the temporary file, and assert that the number of bytes read is 7. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.mpq_inp_str(op, stream, 10) == 7); /// fclose(stream.Value.Value); /// /// // Assert that op is 123/456. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(op, 123, 456U) == 0); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op); /// /// /// ' Create, initialize, and set the value of op to 123/456. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// /// ' Write rational to a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// System.IO.File.WriteAllText(pathname, "123/456") /// /// ' Read op from the temporary file, and assert that the number of bytes read is 7. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.mpq_inp_str(op, stream, 10) = 7) /// fclose(stream.Value.Value) /// /// ' Assert that op is 123/456. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(op, 123, 456UI) = 0) /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op) /// /// public static size_t mpq_inp_str(mpq_t rop, ptr stream, int @base) { if (rop == null) throw new ArgumentNullException("rop"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpq_inp_str_x86(rop.ToIntPtr(), stream.Value.Value, @base)); else return new size_t(SafeNativeMethods.__gmpq_inp_str_x64(rop.ToIntPtr(), stream.Value.Value, @base)); } /// /// Set to 1 / . /// /// The result rational. /// The operand rational. /// /// /// If the new denominator is zero, this routine will divide by zero. /// /// /// mpq_add /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_abs /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_inv(mpq_t inverted_number, /*const*/ mpq_t number) { if (inverted_number == null) throw new ArgumentNullException("inverted_number"); if (number == null) throw new ArgumentNullException("number"); SafeNativeMethods.__gmpq_inv(inverted_number.ToIntPtr(), number.ToIntPtr()); } /// /// Set to * . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_add /// mpq_sub /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_mul(mpq_t product, /*const*/ mpq_t multiplier, /*const*/ mpq_t multiplicand) { if (product == null) throw new ArgumentNullException("product"); if (multiplier == null) throw new ArgumentNullException("multiplier"); if (multiplicand == null) throw new ArgumentNullException("multiplicand"); SafeNativeMethods.__gmpq_mul(product.ToIntPtr(), multiplier.ToIntPtr(), multiplicand.ToIntPtr()); } /// /// Set to * 2*. /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_add /// mpq_sub /// mpq_mul /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_mul_2exp(mpq_t rop, /*const*/ mpq_t op1, uint /*mp_bitcnt_t*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpq_mul_2exp(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to -. /// /// The result rational. /// The operand rational. /// mpq_add /// mpq_sub /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_neg(mpq_t negated_operand, /*const*/ mpq_t operand) { if (negated_operand == null) throw new ArgumentNullException("negated_operand"); if (operand == null) throw new ArgumentNullException("operand"); SafeNativeMethods.__gmpq_neg(negated_operand.ToIntPtr(), operand.ToIntPtr()); } /// /// Return a reference to the numerator . /// /// The operand rational. /// Return a reference to the numerator . /// /// /// The mpz functions can be used on the returned reference. /// /// /// mpq_denref /// mpq_get_num /// mpq_get_den /// mpq_set_num /// mpq_set_den /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Get reference to numerator, and increment it by 2. /// mpz_t num = gmp_lib.mpq_numref(op); /// gmp_lib.mpz_add_ui(num, num, 2U); /// /// // Assert that op is 1 / 3. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, 1, 3U) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Get reference to numerator, and increment it by 2. /// Dim num As mpz_t = gmp_lib.mpq_numref(op) /// gmp_lib.mpz_add_ui(num, num, 2UI) /// /// ' Assert that op is 1 / 3. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, 1, 3UI) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op) /// /// public static mpz_t mpq_numref(/*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_num; } /// /// Output on stdio stream , as a string of digits in base . /// /// Pointer to file stream. /// The base. /// The operand rational. /// Return the number of bytes written, or if an error occurred, return 0. /// /// /// The may vary from 2 to 36. /// Output is in the form "num/den" or if the denominator is 1 then just "num". /// /// /// mpq_inp_str /// I/O of Rationals /// GNU MP - I/O of Rationals /// /// /// // Create, initialize, and set the value of op to 123/456. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_ui(op, 123, 456U); /// /// // Get a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// /// // Open temporary file for writing. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// /// // Write op to temporary file, and assert that the number of bytes written is 7. /// Assert.IsTrue(gmp_lib.mpq_out_str(stream, 10, op) == 7); /// /// // Close temporary file. /// fclose(stream.Value.Value); /// /// // Assert that the content of the temporary file is "123/456". /// string result = System.IO.File.ReadAllText(pathname); /// Assert.IsTrue(result == "123/456"); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op); /// /// /// ' Create, initialize, and set the value of op to 123/456. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_ui(op, 123, 456UI) /// /// ' Get a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// /// ' Open temporary file for writing. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "w") /// /// ' Write op to temporary file, and assert that the number of bytes written is 7. /// Assert.IsTrue(gmp_lib.mpq_out_str(stream, 10, op) = 7) /// /// ' Close temporary file. /// fclose(stream.Value.Value) /// /// ' Assert that the content of the temporary file is "123/456". /// Dim result As String = System.IO.File.ReadAllText(pathname) /// /// Assert.IsTrue(result = "123/456") /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpq_clear(op) /// /// public static size_t mpq_out_str(ptr stream, int @base, /*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpq_out_str_x86(stream.Value.Value, @base, op.ToIntPtr())); else return new size_t(SafeNativeMethods.__gmpq_out_str_x64(stream.Value.Value, @base, op.ToIntPtr())); } /// /// Assign from . /// /// The result rational. /// The operand rational. /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create, initialize, and set a new rational x to 10 / 11. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, 10, 11); /// /// // Create, initialize, and set a new rational y to -210 / 13. /// mpq_t y = new mpq_t(); /// gmp_lib.mpq_init(y); /// gmp_lib.mpq_set_si(y, -210, 13); /// /// // Assign the value of y to x. /// gmp_lib.mpq_set(x, y); /// /// // Assert that the value of x is -210 / 13. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 13) == 0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clears(x, y, null); /// /// /// ' Create, initialize, and set a new rational x to 10 / 11. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, 10, 11) /// /// ' Create, initialize, and set a new rational y to -210 / 13. /// Dim y As New mpq_t() /// gmp_lib.mpq_init(y) /// gmp_lib.mpq_set_si(y, -210, 13) /// /// ' Assign the value of y to x. /// gmp_lib.mpq_set(x, y) /// /// ' Assert that the value of x is -210 / 13. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 13) = 0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clears(x, y, Nothing) /// /// public static void mpq_set(mpq_t rop, /*const*/ mpq_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpq_set(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to the value of . There is no rounding, this conversion is exact. /// /// The result rational. /// The operand double. /// mpq_canonicalize /// mpq_get_d /// mpq_set_f /// mpq_get_str /// Rational Conversions /// GNU MP - Rational Conversions /// /// /// // Create and initialize a new rational. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Set the value of x to 10.0 / 11.0. /// gmp_lib.mpq_set_d(x, 10.0D / 11.0); /// /// // Assert that the value of x is 10.0 / 11.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) == 10.0D / 11.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x); /// /// /// ' Create and initialize a new rational. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// /// ' Set the value of x to 10.0 / 11.0. /// gmp_lib.mpq_set_d(x, 10.0 / 11.0) /// /// ' Assert that the value of x is 10.0 / 11.0. /// Assert.IsTrue(gmp_lib.mpq_get_d(x) = 10.0 / 11.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x) /// /// public static void mpq_set_d(mpq_t rop, double op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpq_set_d(rop.ToIntPtr(), op); } /// /// Set the denominator of to . /// /// The result rational. /// The operand integer. /// /// The function is equivalent to calling mpz_set with mpq_denref. /// Direct use of mpq_denref is recommended instead of this functions. /// /// mpq_numref /// mpq_denref /// mpq_get_num /// mpq_get_den /// mpq_set_num /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Create, initialize, and set the value of a new integer to 5. /// mpz_t den = new mpz_t(); /// gmp_lib.mpz_init_set_ui(den, 5U); /// /// // Set the denominator of op. /// gmp_lib.mpq_set_den(op, den); /// /// // Assert that op is -1 / 5. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 5U) == 0); /// /// // Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op); /// gmp_lib.mpz_clear(den); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Create, initialize, and set the value of a new integer to 5. /// Dim den As New mpz_t() /// gmp_lib.mpz_init_set_ui(den, 5UI) /// /// ' Set the denominator of op. /// gmp_lib.mpq_set_den(op, den) /// /// ' Assert that op is -1 / 5. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, -1, 5UI) = 0) /// /// ' Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op) /// gmp_lib.mpz_clear(den) /// /// public static void mpq_set_den(mpq_t rational, /*const*/ mpz_t denominator) { if (rational == null) throw new ArgumentNullException("rational"); if (denominator == null) throw new ArgumentNullException("denominator"); SafeNativeMethods.__gmpq_set_den(rational.ToIntPtr(), denominator.ToIntPtr()); } /// /// Set to the value of . There is no rounding, this conversion is exact. /// /// The result rational. /// The operand float. /// mpq_get_d /// mpq_set_d /// mpq_get_str /// Rational Conversions /// GNU MP - Rational Conversions /// /// /// // Create, initialize, and set a new rational x to 10 / 11. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, 10, 11); /// /// // Create, initialize, and set a new float y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init(y); /// gmp_lib.mpf_set_si(y, -210); /// /// // Assign the value of y to x. /// gmp_lib.mpq_set_f(x, y); /// /// // Assert that the value of x is -210 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 1) == 0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(x); /// gmp_lib.mpf_clear(y); /// /// /// ' Create, initialize, and set a new rational x to 10 / 11. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, 10, 11) /// /// ' Create, initialize, and set a new float y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init(y) /// gmp_lib.mpf_set_si(y, -210) /// /// ' Assign the value of y to x. /// gmp_lib.mpq_set_f(x, y) /// /// ' Assert that the value of x is -210 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 1) = 0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(x) /// gmp_lib.mpf_clear(y) /// /// public static void mpq_set_f(mpq_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpq_set_f(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the numerator of to . /// /// The result rational. /// The operand integer. /// /// /// The function is equivalent to calling mpz_set with mpq_numref. /// Direct use of mpq_numref is recommended instead of this functions. /// /// /// mpq_numref /// mpq_denref /// mpq_get_num /// mpq_get_den /// mpq_set_den /// Applying Integer Functions /// GNU MP - Applying Integer Functions /// /// /// // Create, initialize, and set the value of op to -1 / 3. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -1, 3U); /// /// // Create, initialize, and set the value of a new integer to 5. /// mpz_t num = new mpz_t(); /// gmp_lib.mpz_init_set_ui(num, 5U); /// /// // Set the numerator of op. /// gmp_lib.mpq_set_num(op, num); /// /// // Assert that op is 5 / 3. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, 5, 3U) == 0); /// /// // Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op); /// gmp_lib.mpz_clear(num); /// /// /// ' Create, initialize, and set the value of op to -1 / 3. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -1, 3UI) /// /// ' Create, initialize, and set the value of a new integer to 5. /// Dim num As New mpz_t() /// gmp_lib.mpz_init_set_ui(num, 5UI) /// /// ' Set the numerator of op. /// gmp_lib.mpq_set_num(op, num) /// /// ' Assert that op is 5 / 3. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(op, 5, 3UI) = 0) /// /// ' Release unmanaged memory allocated for op and num. /// gmp_lib.mpq_clear(op) /// gmp_lib.mpz_clear(num) /// /// public static void mpq_set_num(mpq_t rational, /*const*/ mpz_t numerator) { if (rational == null) throw new ArgumentNullException("rational"); if (numerator == null) throw new ArgumentNullException("numerator"); SafeNativeMethods.__gmpq_set_num(rational.ToIntPtr(), numerator.ToIntPtr()); } /// /// Set the value of to / . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// /// /// Note that if and have common factors, /// has to be passed to mpq_canonicalize before any /// operations are performed on . /// /// /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create and initialize a new rational x. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Set the value of x to -10 / 11. /// gmp_lib.mpq_set_si(x, -10, 11); /// /// // Assert that the value of x is -10 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -10, 11U) == 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x); /// /// /// ' Create and initialize a new rational x. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// /// ' Set the value of x to -10 / 11. /// gmp_lib.mpq_set_si(x, -10, 11) /// /// ' Assert that the value of x is -10 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -10, 11UI) = 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x) /// /// public static void mpq_set_si(mpq_t rop, int /*long int*/ op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpq_set_si(rop.ToIntPtr(), op1, op2); } /// /// Set from a null-terminated string in the given . /// /// The result rational. /// The source string. /// The base, /// The return value is 0 if the entire string is a valid number, or -1 if not. /// /// /// The string can be an integer like "41" or a fraction like "41/152". /// The fraction must be in canonical form /// (see GNU MP - Rational Number Functions), /// or if not then mpq_canonicalize must be called. /// /// /// The numerator and optional denominator are parsed the same as in mpz_set_str /// (see GNU MP - Assigning Integers). /// White space is allowed in the string, and is simply ignored. /// The base can vary from 2 to 62, or if is 0 then the leading characters are used: /// 0x or 0X for hex, 0b or 0B for binary, 0 for octal, or decimal otherwise. /// Note that this is done separately for the numerator and denominator, so for instance 0xEF/100 is 239/100, /// whereas 0xEF/0x100 is 239/256. /// /// /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create and initialize a new rational x. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Set the value of x. /// char_ptr value = new char_ptr("12 345 678 909 876 543 211 234 567 890 987 654 321 / 234 567 890"); /// gmp_lib.mpq_set_str(x, value, 10); /// /// // Assert the value of x. /// char_ptr s = gmp_lib.mpq_get_str(char_ptr.Zero, 10, x); /// Assert.IsTrue(s.ToString() == value.ToString().Replace(" ", "")); /// /// // Release unmanaged memory allocated for x and string values. /// gmp_lib.mpq_clear(x); /// gmp_lib.free(value); /// gmp_lib.free(s); /// /// /// /// public static int mpq_set_str(mpq_t rop, /*const*/ char_ptr str, int @base) { if (rop == null) throw new ArgumentNullException("rop"); return SafeNativeMethods.__gmpq_set_str(rop.ToIntPtr(), str.ToIntPtr(), @base); } /// /// Set the value of to / . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// /// /// Note that if and have common factors, /// has to be passed to mpq_canonicalize before any /// operations are performed on . /// /// /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create and initialize a new rational x. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// /// // Set the value of x to 10 / 11. /// gmp_lib.mpq_set_ui(x, 10U, 11U); /// /// // Assert that the value of x is 10 / 11. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(x, 10U, 11U) == 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x); /// /// /// ' Create and initialize a new rational x. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// /// ' Set the value of x to 10 / 11. /// gmp_lib.mpq_set_ui(x, 10UI, 11UI) /// /// ' Assert that the value of x is 10 / 11. /// Assert.IsTrue(gmp_lib.mpq_cmp_ui(x, 10UI, 11UI) = 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpq_clear(x) /// /// public static void mpq_set_ui(mpq_t rop, uint /*unsigned long int*/ op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpq_set_ui(rop.ToIntPtr(), op1, op2); } /// /// Assign from . /// /// The result rational. /// The operand integer. /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// mpq_swap /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create, initialize, and set a new rational x to 10 / 11. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, 10, 11); /// /// // Create, initialize, and set a new integer y to -210. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init(y); /// gmp_lib.mpz_set_si(y, -210); /// /// // Assign the value of y to x. /// gmp_lib.mpq_set_z(x, y); /// /// // Assert that the value of x is -210 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 1) == 0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(x); /// gmp_lib.mpz_clear(y); /// /// /// ' Create, initialize, and set a new rational x to 10 / 11. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, 10, 11) /// /// ' Create, initialize, and set a new integer y to -210. /// Dim y As New mpz_t() /// gmp_lib.mpz_init(y) /// gmp_lib.mpz_set_si(y, -210) /// /// ' Assign the value of y to x. /// gmp_lib.mpq_set_z(x, y) /// /// ' Assert that the value of x is -210 / 1. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 1) = 0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(x) /// gmp_lib.mpz_clear(y) /// /// public static void mpq_set_z(mpq_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpq_set_z(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Return +1 if > 0, 0 if = 0, and -1 if < 0. /// /// The operand rational. /// Return +1 if > 0, 0 if = 0, and -1 if < 0. /// mpq_cmp /// mpq_cmp_z /// mpq_cmp_ui /// mpq_cmp_si /// mpq_equal /// Comparing Rationals /// GNU MP - Comparing Rationals /// /// /// // Create, initialize, and set a new rational x to -10 / 11. /// mpq_t op = new mpq_t(); /// gmp_lib.mpq_init(op); /// gmp_lib.mpq_set_si(op, -10, 11); /// /// // Assert that op is negative. /// Assert.IsTrue(gmp_lib.mpq_sgn(op) == -1); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(op); /// /// /// ' Create, initialize, and set a new rational x to -10 / 11. /// Dim op As New mpq_t() /// gmp_lib.mpq_init(op) /// gmp_lib.mpq_set_si(op, -10, 11) /// /// ' Assert that op is negative. /// Assert.IsTrue(gmp_lib.mpq_sgn(op) = -1) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clear(op) /// /// public static int mpq_sgn(/*const*/ mpq_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_num._mp_size < 0 ? -1 : (op._mp_num._mp_size > 0 ? 1 : 0); } /// /// Set to - . /// /// The result rational. /// The first operand rational. /// The second operand rational. /// mpq_add /// mpq_mul /// mpq_mul_2exp /// mpq_div /// mpq_div_2exp /// mpq_neg /// mpq_abs /// mpq_inv /// Rational Arithmetic /// GNU MP - Rational Arithmetic /// /// /// /// /// /// public static void mpq_sub(mpq_t difference, /*const*/ mpq_t minuend, /*const*/ mpq_t subtrahend) { if (difference == null) throw new ArgumentNullException("difference"); if (minuend == null) throw new ArgumentNullException("minuend"); if (subtrahend == null) throw new ArgumentNullException("subtrahend"); SafeNativeMethods.__gmpq_sub(difference.ToIntPtr(), minuend.ToIntPtr(), subtrahend.ToIntPtr()); } /// /// Swap the values and efficiently. /// /// The first rational. /// The second rational. /// mpq_canonicalize /// mpq_init /// mpq_inits /// mpq_clear /// mpq_clears /// mpq_set /// mpq_set_z /// mpq_set_ui /// mpq_set_si /// mpq_set_str /// Initializing Rationals /// GNU MP - Initializing Rationals /// /// /// // Create, initialize, and set a new rational x to 10 / 11. /// mpq_t x = new mpq_t(); /// gmp_lib.mpq_init(x); /// gmp_lib.mpq_set_si(x, 10, 11U); /// /// // Create, initialize, and set a new rational x to -210 / 13. /// mpq_t y = new mpq_t(); /// gmp_lib.mpq_init(y); /// gmp_lib.mpq_set_si(y, -210, 13U); /// /// // Swap the values of x and y. /// gmp_lib.mpq_swap(x, y); /// /// // Assert that the values have been swapped. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 13U) == 0); /// Assert.IsTrue(gmp_lib.mpq_cmp_si(y, 10, 11U) == 0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clears(x, y, null); /// /// /// ' Create, initialize, and set a new rational x to 10 / 11. /// Dim x As New mpq_t() /// gmp_lib.mpq_init(x) /// gmp_lib.mpq_set_si(x, 10, 11UI) /// /// ' Create, initialize, and set a new rational x to -210 / 13. /// Dim y As New mpq_t() /// gmp_lib.mpq_init(y) /// gmp_lib.mpq_set_si(y, -210, 13UI) /// /// ' Swap the values of x and y. /// gmp_lib.mpq_swap(x, y) /// /// ' Assert that the values have been swapped. /// Assert.IsTrue(gmp_lib.mpq_cmp_si(x, -210, 13UI) = 0) /// Assert.IsTrue(gmp_lib.mpq_cmp_si(y, 10, 11UI) = 0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpq_clears(x, y, Nothing) /// /// public static void mpq_swap(mpq_t rop1, mpq_t rop2) { if (rop1 == null) throw new ArgumentNullException("rop1"); if (rop2 == null) throw new ArgumentNullException("rop2"); SafeNativeMethods.__gmpq_swap(rop1.ToIntPtr(), rop2.ToIntPtr()); } #endregion #region "Float (i.e. F) routines." /// /// Set to | |. /// /// The result float. /// The operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to -10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, -10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = |x|. /// gmp_lib.mpf_abs(z, x); /// /// // Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to -10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, -10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = |x|. /// gmp_lib.mpf_absg(z, x) /// /// ' Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_abs(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_abs(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to + . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add_ui /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x + y. /// gmp_lib.mpf_add(z, x, y); /// /// // Assert that the value of z is -200. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -200.0); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x + y. /// gmp_lib.mpf_add(z, x, y) /// /// ' Assert that the value of z is -200. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = -200.0) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, Nothing) /// /// public static void mpf_add(mpf_t rop, /*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_add(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to + . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x + 210. /// gmp_lib.mpf_add_ui(z, x, 210U); /// /// // Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 220.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x + 210. /// gmp_lib.mpf_add_ui(z, x, 210UI) /// /// ' Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 220.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_add_ui(mpf_t rop, /*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_add_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to rounded to the next higher integer. /// /// The result float. /// The operand float. /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10.4. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, 10.4); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = ceil(x). /// gmp_lib.mpf_ceil(z, x); /// /// // Assert that the value of z is 11. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 11.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10.4. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, 10.4) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = ceil(x). /// gmp_lib.mpf_ceil(z, x) /// /// ' Assert that the value of z is 11. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 11.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_ceil(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_ceil(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Free the space occupied by . /// /// The operand float. /// /// /// Make sure to call this function for all mpf_t variables when you are done with them. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create and initialize a new floating-point number x. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// /// // Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 0.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create and initialize a new floating-point number x. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// /// ' Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 0.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_clear(mpf_t x) { if (x == null) throw new ArgumentNullException("x"); SafeNativeMethods.__gmpf_clear(x.ToIntPtr()); x.Clear(); } /// /// Free the space occupied by a NULL-terminated list of mpf_t variables. /// /// The operand float. /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Create new floating-point numbers x1, x2 and x3. /// mpf_t x1 = new mpf_t(); /// mpf_t x2 = new mpf_t(); /// mpf_t x3 = new mpf_t(); /// /// // Initialize the floating-point numbers. /// gmp_lib.mpf_inits(x1, x2, x3, null); /// /// // Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x1) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_d(x2) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_d(x3) == 0.0); /// /// // Release unmanaged memory allocated for the floating-point numbers. /// gmp_lib.mpf_clears(x1, x2, x3, null); /// /// /// ' Create new floating-point numbers x1, x2 and x3. /// Dim x1 As New mpf_t() /// Dim x2 As New mpf_t() /// Dim x3 As New mpf_t() /// /// ' Initialize the floating-point numbers. /// gmp_lib.mpf_inits(x1, x2, x3, Nothing) /// /// ' Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x1) = 0.0) /// Assert.IsTrue(gmp_lib.mpf_get_d(x2) = 0.0) /// Assert.IsTrue(gmp_lib.mpf_get_d(x3) = 0.0) /// /// ' Release unmanaged memory allocated for the floating-point numbers. /// gmp_lib.mpf_clears(x1, x2, x3, Nothing) /// /// public static void mpf_clears(params mpf_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpf_t a in x) { if (a != null) mpf_clear(a); } } /// /// Compare and . /// /// The first operand float. /// The second operand float. /// Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. /// mpf_cmp_z /// mpf_cmp_d /// mpf_cmp_ui /// mpf_cmp_si /// mpf_reldiff /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init_set_si(z, 128); /// /// // Assert that x > z. /// Assert.IsTrue(gmp_lib.mpf_cmp(x, z) > 0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init_set_si(z, 128) /// /// ' Assert that x > z. /// Assert.IsTrue(gmp_lib.mpf_cmp(x, z) > 0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static int mpf_cmp(/*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpf_cmp(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compare and . /// /// The first operand float. /// The second operand float. /// Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. /// mpf_cmp /// mpf_cmp_d /// mpf_cmp_ui /// mpf_cmp_si /// mpf_reldiff /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Create and initialize a new integer z. /// mpz_t z = new mpz_t(); /// gmp_lib.mpz_init_set_si(z, 128); /// /// // Assert that x > z. /// Assert.IsTrue(gmp_lib.mpf_cmp_z(x, z) > 0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clear(x); /// gmp_lib.mpz_clear(z); /// /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Create and initialize a new integer z. /// Dim z As New mpz_t() /// gmp_lib.mpz_init_set_si(z, 128) /// /// ' Assert that x > z. /// Assert.IsTrue(gmp_lib.mpf_cmp_z(x, z) > 0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clear(x) /// gmp_lib.mpz_clear(z) /// /// public static int mpf_cmp_z(/*const*/ mpf_t op1, /*const*/ mpz_t op2) { if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); return SafeNativeMethods.__gmpf_cmp_z(op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Compare and . /// /// The first operand float. /// The second operand float. /// Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. /// /// /// mpf_cmp_d can be called with an infinity, but results are undefined for a NaN. /// /// /// mpf_cmp /// mpf_cmp_z /// mpf_cmp_ui /// mpf_cmp_si /// mpf_reldiff /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Assert that x > 128.0. /// Assert.IsTrue(gmp_lib.mpf_cmp_d(x, 128.0) > 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Assert that x > 128.0. /// Assert.IsTrue(gmp_lib.mpf_cmp_d(x, 128.0) > 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static int mpf_cmp_d(/*const*/ mpf_t op1, double op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpf_cmp_d(op1.ToIntPtr(), op2); } /// /// Compare and . /// /// The first operand float. /// The second operand float. /// Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. /// mpf_cmp /// mpf_cmp_z /// mpf_cmp_d /// mpf_cmp_ui /// mpf_reldiff /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Assert that x > 128. /// Assert.IsTrue(gmp_lib.mpf_cmp_si(x, 128) > 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Assert that x > 128. /// Assert.IsTrue(gmp_lib.mpf_cmp_si(x, 128) > 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static int mpf_cmp_si(/*const*/ mpf_t op1, int /*long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpf_cmp_si(op1.ToIntPtr(), op2); } /// /// Compare and . /// /// The first operand float. /// The second operand float. /// Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. /// mpf_cmp /// mpf_cmp_z /// mpf_cmp_d /// mpf_cmp_si /// mpf_reldiff /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Assert that x > 128. /// Assert.IsTrue(gmp_lib.mpf_cmp_ui(x, 128) > 0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Assert that x > 128. /// Assert.IsTrue(gmp_lib.mpf_cmp_ui(x, 128) > 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static int mpf_cmp_ui(/*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (op1 == null) throw new ArgumentNullException("op1"); return SafeNativeMethods.__gmpf_cmp_ui(op1.ToIntPtr(), op2); } /// /// Set to / . /// /// The result float. /// The first operand. /// The second operand. /// /// /// Division is undefined if the divisor is zero, and passing a zero divisor to the divide /// functions will make it intentionally divide by zero. /// This lets the user handle arithmetic exceptions in division functions in the same manner /// as other arithmetic exceptions. /// /// /// mpf_add /// mpf_sub /// mpf_mul /// mpf_ui_div /// mpf_div_ui /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// mpf_div_2exp /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = y / x. /// gmp_lib.mpf_div(z, y, x); /// /// // Assert that the value of z is -21. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -21.0); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = y / x. /// gmp_lib.mpf_div(z, y, x) /// /// ' Assert that the value of z is -21. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = -21.0) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, Nothing) /// /// public static void mpf_div(mpf_t rop, /*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_div(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to / 2^. /// /// The result float. /// The fisrt operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_ui_div /// mpf_div_ui /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 512. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 512); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x / 2^8. /// gmp_lib.mpf_div_2exp(z, x, 8U); /// /// // Assert that the value of z is 2. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 2.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 512. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 512) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x / 2^8. /// gmp_lib.mpf_div_2exp(z, x, 8UI) /// /// ' Assert that the value of z is 2. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 2.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_div_2exp(mpf_t rop, /*const*/ mpf_t op1, uint /*mp_bitcnt_t*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_div_2exp(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to / . /// /// The result float. /// The first operand. /// The second operand. /// /// /// Division is undefined if the divisor is zero, and passing a zero divisor to the divide /// functions will make it intentionally divide by zero. /// This lets the user handle arithmetic exceptions in division functions in the same manner /// as other arithmetic exceptions. /// /// /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_ui_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// mpf_div_2exp /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = y / 10. /// gmp_lib.mpf_div_ui(z, y, 10U); /// /// // Assert that the value of z is -21. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -21.0); /// /// // Release unmanaged memory allocated for y and z. /// gmp_lib.mpf_clears(y, z, null); /// /// /// /// public static void mpf_div_ui(mpf_t rop, /*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_div_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Return non-zero if fits in a 32-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in a 32-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpf_fits_sint_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpf_fits_sint_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_sint_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_sint_p(op.ToIntPtr()); } /// /// Return non-zero if fits in a 32-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in a 32-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in long. /// Assert.IsTrue(gmp_lib.mpf_fits_slong_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in long. /// Assert.IsTrue(gmp_lib.mpf_fits_slong_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_slong_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_slong_p(op.ToIntPtr()); } /// /// Return non-zero if fits in a 16-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in a 16-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in short. /// Assert.IsTrue(gmp_lib.mpf_fits_sshort_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in short. /// Assert.IsTrue(gmp_lib.mpf_fits_sshort_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_sshort_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_sshort_p(op.ToIntPtr()); } /// /// Return non-zero if fits in an unsigned 32-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in an unsigned 32-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in uint. /// Assert.IsTrue(gmp_lib.mpf_fits_uint_p(op) > 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in uint. /// Assert.IsTrue(gmp_lib.mpf_fits_uint_p(op) > 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_uint_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_uint_p(op.ToIntPtr()); } /// /// Return non-zero if fits in an unsigned 32-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in an unsigned 32-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpf_fits_sint_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in int. /// Assert.IsTrue(gmp_lib.mpf_fits_sint_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_ulong_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_ulong_p(op.ToIntPtr()); } /// /// Return non-zero if fits in an unsigned 16-bit integer, when truncated to an integer. /// /// The operand float. /// Return non-zero if fits in an unsigned 16-bit integer, when truncated to an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// /// /// // Create, initialize, and set the value of op 4294967295. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, uint.MaxValue); /// /// // Assert that op does not fit in ushort. /// Assert.IsTrue(gmp_lib.mpf_fits_ushort_p(op) == 0); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// ' Create, initialize, and set the value of op 4294967295. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_ui(op, UInteger.MaxValue) /// /// ' Assert that op does not fit in ushort. /// Assert.IsTrue(gmp_lib.mpf_fits_ushort_p(op) = 0) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_fits_ushort_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_fits_ushort_p(op.ToIntPtr()); } /// /// Set to rounded to the next lower integer. /// /// The result float. /// The operand float. /// mpf_ceil /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// / Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10.4. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, 10.4); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = floor(x). /// gmp_lib.mpf_floor(z, x); /// /// // Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10.4. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, 10.4) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = floor(x). /// gmp_lib.mpf_floor(z, x) /// /// ' Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_floor(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_floor(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Convert to a double, truncating if necessary (i.e. rounding towards zero). /// /// The operand float. /// The cpnverted double. /// /// /// If the exponent in is too big or too small to fit a double then the result is system dependent. /// For too big an infinity is returned when available. /// For too small 0.0 is normally returned. /// Hardware overflow, underflow and denorm traps may or may not occur. /// /// /// mpf_get_d_2exp /// mpf_get_si /// mpf_get_ui /// O:Math.Gmp.Native.gmp_lib.mpf_get_str /// Converting Floats /// GNU MP - Converting Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -123.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -123.0); /// /// // Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -123.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -123.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -123.0) /// /// ' Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -123.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static double mpf_get_d(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_get_d(op.ToIntPtr()); } /// /// Convert op to a double, truncating if necessary (i.e. rounding towards zero), and with an exponent returned separately. /// /// Pointer to 32-bit signed integer. /// The operand float. /// The return value is in the range 0.5 ≤ | d | < 1 and the exponent is stored at . d * 2^ is the (truncated) value. If is zero, the return is 0.0 and 0 is stored at . /// /// /// This is similar to the standard C frexp function /// (see GNU C - Normalization Functions in The GNU C Library Reference Manual). /// /// /// mpf_get_d /// mpf_get_si /// mpf_get_ui /// O:Math.Gmp.Native.gmp_lib.mpf_get_str /// Converting Floats /// GNU MP - Converting Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -8.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -8.0); /// /// // Assert that the absolute value of x is 0.5 x 2^4. /// ptr<int> exp = new ptr<int>(0); /// Assert.IsTrue(gmp_lib.mpf_get_d_2exp(exp, x) == 0.5); /// Assert.IsTrue(exp.Value == 4); /// /// // Release unmanaged memory allocated for x and exp. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -8.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -8.0) /// /// ' Assert that the absolute value of x is 0.5 x 2^4. /// Dim exp As New ptr(Of Integer)(0) /// Assert.IsTrue(gmp_lib.mpf_get_d_2exp(exp, x) = 0.5) /// Assert.IsTrue(exp.Value = 4) /// /// ' Release unmanaged memory allocated for x and exp. /// gmp_lib.mpf_clear(x) /// /// public static double mpf_get_d_2exp(ptr /*long int **/ exp, /*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); if (exp == null) throw new ArgumentNullException("exp"); return SafeNativeMethods.__gmpf_get_d_2exp(ref exp.Value, op.ToIntPtr()); } /// /// Return the default precision actually used. /// /// The default precision actually used. /// /// /// An mpf_t object must be initialized before storing the first value in it. /// The functions mpf_init and mpf_init2 are used for that purpose. /// /// /// mpf_set_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128U); /// /// // Assert that the value of x is 128 bits. /// Assert.IsTrue(gmp_lib.mpf_get_default_prec() == 128U); /// /// /// ' Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128UI) /// /// ' Assert that the value of x is 128 bits. /// Assert.IsTrue(gmp_lib.mpf_get_default_prec() = 128UI) /// /// public static mp_bitcnt_t mpf_get_default_prec() { return new mp_bitcnt_t(SafeNativeMethods.__gmpf_get_default_prec()); } /// /// Return the current precision of , in bits. /// /// The operand float. /// The current precision of , in bits. /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Create and initialize a new floating-point number x with 64-bit precision. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 64U); /// /// // Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) == 64U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number x with 64-bit precision. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 64UI) /// /// ' Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 0.0) /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) = 64UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static mp_bitcnt_t mpf_get_prec(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return new mp_bitcnt_t(SafeNativeMethods.__gmpf_get_prec(op.ToIntPtr())); } /// /// Convert to a 32-bit integer, truncating any fraction part. /// /// The operand float. /// The converted integer. /// /// /// If is too big for the return type, the result is undefined. /// /// /// See also mpf_fits_slong_p and mpf_fits_ulong_p /// (see GNU MP - Miscellaneous Float Functions). /// /// /// mpf_get_d /// mpf_get_d_2exp /// mpf_get_ui /// O:Math.Gmp.Native.gmp_lib.mpf_get_str /// Converting Floats /// GNU MP - Converting Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -8.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -8.0); /// /// // Assert that the value of x is -8. /// Assert.IsTrue(gmp_lib.mpf_get_si(x) == -8); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -8.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -8.0) /// /// ' Assert that the value of x is -8. /// Assert.IsTrue(gmp_lib.mpf_get_si(x) = -8) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static int /*long int*/ mpf_get_si(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_get_si(op.ToIntPtr()); } /// /// Convert to a string of digits in base . /// /// The output string. /// The exponent. /// The base. /// Maximum number of output digits. /// The operand floating-point number. /// A pointer to the result string is returned, being either the allocated block or the given . /// /// /// The argument may vary from 2 to 62 or from -2 to -36. /// Up to digits will be generated. /// Trailing zeros are not returned. /// No more digits than can be accurately represented by are ever generated. /// If is 0 then that accurate maximum number of digits are generated. /// /// /// For in the range 2..36, digits and lower-case letters are used; for -2..-36, /// digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters /// (in that significance order) are used. /// /// /// If is NULL, the result string is allocated using the current allocation function /// (see GNU MP - Custom Allocation). /// The block will be strlen(str) + 1 bytes, that being exactly enough for the string and null-terminator. /// /// /// If is not NULL, it should point to a block of + 2 bytes, /// that being enough for the mantissa, a possible minus sign, and a null-terminator. /// When is 0 to get all significant digits, an application won’t be able to know /// the space required, and should be NULL in that case. /// /// /// The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. /// The applicable exponent is written through the pointer. /// For example, the number 3.1416 would be returned as string "31416" and exponent 1. /// /// /// When is zero, an empty string is produced and the exponent returned is 0. /// /// /// mpf_get_d /// mpf_get_d_2exp /// mpf_get_si /// mpf_get_ui /// Converting Floats /// GNU MP - Converting Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -8.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -8.0); /// /// // Assert that the value of x is -8. /// mp_exp_t exp = 0; /// char_ptr value = gmp_lib.mpf_get_str(char_ptr.Zero, ref exp, 10, 0, x); /// Assert.IsTrue(value.ToString() == "-8"); /// Assert.IsTrue(exp == 1); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// gmp_lib.free(value); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -8.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -8.0) /// /// ' Assert that the value of x is -8. /// Dim exp As mp_exp_t = 0 /// Dim value As char_ptr = gmp_lib.mpf_get_str(char_ptr.Zero, exp, 10, 0, x) /// Assert.IsTrue(value.ToString() = "-8") /// Assert.IsTrue(exp = 1) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// gmp_lib.free(value) /// /// public static char_ptr mpf_get_str(char_ptr str, ref mp_exp_t expptr, int @base, size_t n_digits, /*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); if (IntPtr.Size == 4) return new char_ptr(SafeNativeMethods.__gmpf_get_str_x86(str.ToIntPtr(), ref expptr.Value, @base, (uint)n_digits, op.ToIntPtr())); else return new char_ptr(SafeNativeMethods.__gmpf_get_str_x64(str.ToIntPtr(), ref expptr.Value, @base, n_digits, op.ToIntPtr())); } /// /// Convert to a string of digits in base . /// /// The output string. /// The exponent. /// The base. /// Maximum number of output digits. /// The operand floating-point number. /// A pointer to the result string is returned, being either the allocated block or the given . /// /// /// The argument may vary from 2 to 62 or from -2 to -36. /// Up to digits will be generated. /// Trailing zeros are not returned. /// No more digits than can be accurately represented by are ever generated. /// If is 0 then that accurate maximum number of digits are generated. /// /// /// For in the range 2..36, digits and lower-case letters are used; for -2..-36, /// digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters /// (in that significance order) are used. /// /// /// If is NULL, the result string is allocated using the current allocation function /// (see GNU MP - Custom Allocation). /// The block will be strlen(str) + 1 bytes, that being exactly enough for the string and null-terminator. /// /// /// If is not NULL, it should point to a block of + 2 bytes, /// that being enough for the mantissa, a possible minus sign, and a null-terminator. /// When is 0 to get all significant digits, an application won’t be able to know /// the space required, and should be NULL in that case. /// /// /// The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. /// The applicable exponent is written through the pointer. /// For example, the number 3.1416 would be returned as string "31416" and exponent 1. /// /// /// When is zero, an empty string is produced and the exponent returned is 0. /// /// /// mpf_get_d /// mpf_get_d_2exp /// mpf_get_si /// mpf_get_ui /// Converting Floats /// GNU MP - Converting Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -8.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -8.0); /// /// // Assert that the value of x is -8. /// ptr<mp_exp_t> exp = new ptr<mp_exp_t>(0); /// char_ptr value = gmp_lib.mpf_get_str(char_ptr.Zero, exp, 10, 0, x); /// Assert.IsTrue(value.ToString() == "-8"); /// Assert.IsTrue(exp.Value == 1); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// gmp_lib.free(value); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -8.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -8.0) /// /// ' Assert that the value of x is -8. /// Dim exp As New ptr(Of mp_exp_t)(0) /// Dim value As char_ptr = gmp_lib.mpf_get_str(char_ptr.Zero, exp, 10, 0, x) /// Assert.IsTrue(value.ToString() = "-8") /// Assert.IsTrue(exp.Value = 1) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// gmp_lib.free(value) /// /// public static char_ptr mpf_get_str(char_ptr str, ptr expptr, int @base, size_t n_digits, /*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); if (expptr == null) throw new ArgumentNullException("expptr"); if (IntPtr.Size == 4) return new char_ptr(SafeNativeMethods.__gmpf_get_str_x86(str.ToIntPtr(), ref expptr.Value.Value, @base, (uint)n_digits, op.ToIntPtr())); else return new char_ptr(SafeNativeMethods.__gmpf_get_str_x64(str.ToIntPtr(), ref expptr.Value.Value, @base, n_digits, op.ToIntPtr())); } /// /// Convert to an unsigned 32-bit integer, truncating any fraction part. /// /// The operand float. /// The converted integer. /// /// /// If is too big for the return type, the result is undefined. /// /// /// See also mpf_fits_slong_p and mpf_fits_ulong_p /// (see GNU MP - Miscellaneous Float Functions). /// /// /// mpf_get_d /// mpf_get_d_2exp /// mpf_get_si /// O:Math.Gmp.Native.gmp_lib.mpf_get_str /// Converting Floats /// GNU MP - Converting Floats /// /// /// /// /// /// public static uint /*unsigned long int*/ mpf_get_ui(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_get_ui(op.ToIntPtr()); } /// /// Initialize to 0. /// /// The operand float. /// /// /// Normally, a variable should be initialized once only or at least be cleared, using mpf_clear, /// between initializations. The precision of is undefined unless a default precision has /// already been established by a call to mpf_set_default_prec. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create and initialize a new floating-point number x. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// /// // Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 0.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create and initialize a new floating-point number x. /// Dim x As New mpf_t() /// /// gmp_lib.mpf_init(x) /// /// ' Assert that the value of x is 0.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 0.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_init(mpf_t x) { if (x == null) throw new ArgumentNullException("x"); x.Initializing(); SafeNativeMethods.__gmpf_init(x.ToIntPtr()); x.Initialized(); } /// /// Initialize to 0 and set its precision to be at least bits. /// /// The operand float. /// The minimum precision in bits. /// /// /// Normally, a variable should be initialized once only or at least be cleared, using mpf_clear, between initializations. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Create and initialize a new floating-point number x with 64-bit precision. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 64U); /// /// // Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 0.0); /// uint p = gmp_lib.mpf_get_prec(x); /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) == 64U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number x with 64-bit precision. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 64UI) /// /// ' Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 0.0) /// Dim p As UInteger = gmp_lib.mpf_get_prec(x) /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) = 64UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_init2(mpf_t x, mp_bitcnt_t prec) { if (x == null) throw new ArgumentNullException("x"); x.Initializing(); SafeNativeMethods.__gmpf_init2(x.ToIntPtr(), prec); x.Initialized(); } /// /// Initialize a NULL-terminated list of mpf_t variables, and set their values to 0. /// /// The operand float. /// /// /// The precision of the initialized variables is undefined unless a default precision has already /// been established by a call to mpf_set_default_prec. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Create new floating-point numbers x1, x2 and x3. /// mpf_t x1 = new mpf_t(); /// mpf_t x2 = new mpf_t(); /// mpf_t x3 = new mpf_t(); /// /// // Initialize the floating-point numbers. /// gmp_lib.mpf_inits(x1, x2, x3, null); /// /// // Assert that their value is 0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x1) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_d(x2) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_d(x3) == 0.0); /// /// // Release unmanaged memory allocated for the floating-point numbers. /// gmp_lib.mpf_clears(x1, x2, x3, null); /// /// /// /// public static void mpf_inits(params mpf_t[] x) { if (x == null) throw new ArgumentNullException("x"); foreach (mpf_t a in x) { if (a != null) mpf_init(a); } } /// /// Initialize and set its value from . /// /// The result float. /// The operand. /// /// /// The precision of will be taken from the active default precision, as set by mpf_set_default_prec. /// /// /// mpf_init_set_ui /// mpf_init_set_si /// mpf_init_set_d /// mpf_init_set_str /// Simultaneous Float Init & Assign /// GNU MP - Combined Float Initialization and Assignment /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to x. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set(y, x); /// /// // Assert that the value of y is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(y) == 10.0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to x. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set(y, x) /// /// ' Assert that the value of y is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(y) = 10.0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, Nothing) /// /// public static void mpf_init_set(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); rop.Initializing(); SafeNativeMethods.__gmpf_init_set(rop.ToIntPtr(), op.ToIntPtr()); rop.Initialized(); } /// /// Initialize and set its value from . /// /// The result float. /// The operand. /// /// /// The precision of will be taken from the active default precision, as set by mpf_set_default_prec. /// /// /// mpf_init_set /// mpf_init_set_ui /// mpf_init_set_si /// mpf_init_set_str /// Simultaneous Float Init & Assign /// GNU MP - Combined Float Initialization and Assignment /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to -123.0 /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, -123.0); /// /// // Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -123.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to -123.0 /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, -123.0) /// /// ' Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -123.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_init_set_d(mpf_t rop, double op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpf_init_set_d(rop.ToIntPtr(), op); rop.Initialized(); } /// /// Initialize and set its value from . /// /// The result float. /// The operand. /// /// /// The precision of will be taken from the active default precision, as set by mpf_set_default_prec. /// /// /// mpf_init_set /// mpf_init_set_ui /// mpf_init_set_d /// mpf_init_set_str /// Simultaneous Float Init & Assign /// GNU MP - Combined Float Initialization and Assignment /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize and set a new floating-point number to -123. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, -123); /// /// // Assert that the value of x is -123. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -123.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize and set a new floating-point number to -123. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, -123) /// /// ' Assert that the value of x is -123. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -123.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_init_set_si(mpf_t rop, int /*long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpf_init_set_si(rop.ToIntPtr(), op); rop.Initialized(); } /// /// Initialize and set its value from the string in . /// /// The result float. /// The operand string. /// The base. /// This function returns 0 if the entire string is a valid number in base . Otherwise it returns -1. /// /// /// See mpf_set_str for details on the assignment operation. /// /// /// Note that is initialized even if an error occurs. (I.e., you have to call mpf_clear for it.) /// /// /// The precision of will be taken from the active default precision, as set by mpf_set_default_prec. /// /// /// mpf_init_set /// mpf_init_set_ui /// mpf_init_set_si /// mpf_init_set_d /// Simultaneous Float Init & Assign /// GNU MP - Combined Float Initialization and Assignment /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 0.0234. /// char_ptr value = new char_ptr("234e-4"); /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_str(x, value, 10); /// /// // Assert that x is 40. /// Assert.IsTrue(x.ToString() == "0.234e-1"); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x); /// gmp_lib.free(value); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 0.0234. /// Dim value As New char_ptr("234e-4") /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_str(x, value, 10) /// /// ' Assert that x is 40. /// Assert.IsTrue(x.ToString() = "0.234e-1") /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x) /// gmp_lib.free(value) /// /// public static int mpf_init_set_str(mpf_t rop, /*const*/ char_ptr str, int @base) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); int result = SafeNativeMethods.__gmpf_init_set_str(rop.ToIntPtr(), str.ToIntPtr(), @base); rop.Initialized(); return result; } /// /// Initialize and set its value from . /// /// The result float. /// The operand. /// /// /// The precision of will be taken from the active default precision, as set by mpf_set_default_prec. /// /// /// mpf_init_set /// mpf_init_set_si /// mpf_init_set_d /// mpf_init_set_str /// Simultaneous Float Init & Assign /// GNU MP - Combined Float Initialization and Assignment /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number to 100. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_ui(x, 100U); /// /// // Assert that the value of x is 100. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 100.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number to 100. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_ui(x, 100UI) /// /// ' Assert that the value of x is 100. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 100.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_init_set_ui(mpf_t rop, uint /*unsigned long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); rop.Initializing(); SafeNativeMethods.__gmpf_init_set_ui(rop.ToIntPtr(), op); rop.Initialized(); } /// /// Read a string in base from , and put the read float in . /// /// The result float. /// Pointer to file stream. /// The base. /// Return the number of bytes read, or if an error occurred, return 0. /// /// /// The string is of the form "M@N" or, if the base is 10 or less, alternatively "MeN". /// "M" is the mantissa and "N’" is the exponent. /// The mantissa is always in the specified . /// The exponent is either in the specified or, if is negative, in decimal. /// The decimal point expected is taken from the current locale, on systems providing localeconv. /// /// /// The argument may be in the ranges 2 to 36, or -36 to -2. /// Negative values are used to specify that the exponent is in decimal. /// /// /// Unlike the corresponding mpz function, the will not be determined from the leading /// characters of the string if base is 0. /// This is so that numbers like "0.23" are not interpreted as octal. /// /// /// mpf_out_str /// I/O of Floats /// GNU MP - I/O of Floats /// /// /// // Create and initialize op. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init(op); /// /// // Write op to a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// System.IO.File.WriteAllText(pathname, "0.123456e6"); /// /// // Read op from the temporary file, and assert that the number of bytes read is 6. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "r"); /// Assert.IsTrue(gmp_lib.mpf_inp_str(op, stream, 10) == 10); /// fclose(stream.Value.Value); /// /// // Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpf_get_ui(op) == 123456U); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create and initialize op. /// Dim op As New mpf_t() /// gmp_lib.mpf_init(op) /// /// ' Write op to a temporary file. /// Dim pathname As String = System.IO.Path.GetTempFileName() /// System.IO.File.WriteAllText(pathname, "0.123456e6") /// /// ' Read op from the temporary file, and assert that the number of bytes read is 6. /// Dim stream As New ptr(Of FILE)() /// _wfopen_s(stream.Value.Value, pathname, "r") /// Assert.IsTrue(gmp_lib.mpf_inp_str(op, stream, 10) = 10) /// fclose(stream.Value.Value) /// /// ' Assert that op is 123456. /// Assert.IsTrue(gmp_lib.mpf_get_ui(op) = 123456UI) /// /// ' Delete temporary file. /// System.IO.File.Delete(pathname) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static size_t mpf_inp_str(mpf_t rop, ptr stream, int @base) { if (rop == null) throw new ArgumentNullException("rop"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpf_inp_str_x86(rop.ToIntPtr(), stream.Value.Value, @base)); else return new size_t(SafeNativeMethods.__gmpf_inp_str_x64(rop.ToIntPtr(), stream.Value.Value, @base)); } /// /// Return non-zero if is an integer. /// /// The operand float. /// Return non-zero if is an integer. /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, 10); /// /// // Assert that s is an integer. /// Assert.IsTrue(gmp_lib.mpf_integer_p(x) != 0); /// /// // Release unmanaged memory allocated for x. /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, 10) /// /// ' Assert that s is an integer. /// Assert.IsTrue(gmp_lib.mpf_integer_p(x) <> 0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static int mpf_integer_p(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return SafeNativeMethods.__gmpf_integer_p(op.ToIntPtr()); } /// /// Set to * . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul_ui /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// mpf_mul_2exp /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x * y. /// gmp_lib.mpf_mul(z, x, y); /// /// // Assert that the value of z is -2100. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -2100.0); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x * y. /// gmp_lib.mpf_mul(z, x, y) /// /// ' Assert that the value of z is -2100. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = -2100.0) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, Nothing) /// /// public static void mpf_mul(mpf_t rop, /*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_mul(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to * 2^. /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_mul_ui /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 100. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 100); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x * 2^8. /// gmp_lib.mpf_mul_2exp(z, x, 8U); /// /// // Assert that the value of z is 25600. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 25600.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 100. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 100) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x * 2^8. /// gmp_lib.mpf_mul_2exp(z, x, 8UI) /// /// ' Assert that the value of z is 25600. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 25600.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_mul_2exp(mpf_t rop, /*const*/ mpf_t op1, mp_bitcnt_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_mul_2exp(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to * . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// mpf_mul_2exp /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x * 210. /// gmp_lib.mpf_mul_ui(z, x, 210U); /// /// // Assert that the value of z is 2100. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 2100.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x * 210. /// gmp_lib.mpf_mul_ui(z, x, 210UI) /// /// ' Assert that the value of z is 2100. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 2100.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_mul_ui(mpf_t rop, /*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_mul_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Set to -. /// /// The result float. /// The operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = -x. /// gmp_lib.mpf_neg(z, x); /// /// // Assert that the value of z is -10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = -x. /// gmp_lib.mpf_neg(z, x) /// /// ' Assert that the value of z is -10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = -10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_neg(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_neg(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Print to , as a string of digits. /// /// Pointer to file stream. /// The base. /// Maximum number fo digits to write. /// The operand float. /// Return the number of bytes written, or if an error occurred, return 0. /// /// /// The mantissa is prefixed with an "0." and is in the given , /// which may vary from 2 to 62 or from -2 to -36. /// An exponent is then printed, separated by an "e", or if the /// is greater than 10 then by an "@". /// The exponent is always in decimal. /// The decimal point follows the current locale, on systems providing localeconv. /// /// /// For in the range 2..36, digits and lower-case letters are used; /// for -2..-36, digits and upper-case letters are used; for 37..62, digits, /// upper-case letters, and lower-case letters (in that significance order) are used. /// /// /// Up to will be printed from the mantissa, except that no more /// digits than are accurately representable by op will be printed. /// can be 0 to select that accurate maximum. /// /// /// mpf_inp_str /// I/O of Floats /// GNU MP - I/O of Floats /// /// /// // Create, initialize, and set the value of op to 123456. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_ui(op, 123456U); /// /// // Get a temporary file. /// string pathname = System.IO.Path.GetTempFileName(); /// /// // Open temporary file for writing. /// ptr<FILE> stream = new ptr<FILE>(); /// _wfopen_s(out stream.Value.Value, pathname, "w"); /// /// // Write op to temporary file, and assert that the number of bytes written is 10. /// Assert.IsTrue(gmp_lib.mpf_out_str(stream, 10, 0, op) == 10); /// /// // Close temporary file. /// fclose(stream.Value.Value); /// /// // Assert that the content of the temporary file is "123456". /// string result = System.IO.File.ReadAllText(pathname); /// Assert.IsTrue(result == "0.123456e6"); /// /// // Delete temporary file. /// System.IO.File.Delete(pathname); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// /// public static size_t mpf_out_str(ptr stream, int @base, size_t n_digits, /*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); if (stream == null) throw new ArgumentNullException("stream"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpf_out_str_x86(stream.Value.Value, @base, (uint)n_digits, op.ToIntPtr())); else return new size_t(SafeNativeMethods.__gmpf_out_str_x64(stream.Value.Value, @base, n_digits, op.ToIntPtr())); } /// /// Set to ^. /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = sqrt(x). /// gmp_lib.mpf_pow_ui(z, x, 3U); /// /// // Assert that the value of z is 1000. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 1000.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = sqrt(x). /// gmp_lib.mpf_pow_ui(z, x, 3UI) /// /// ' Assert that the value of z is 1000. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 1000.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_pow_ui(mpf_t rop, /*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_pow_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Generate a random float of at most limbs, with long strings of zeros and ones in the binary representation. /// /// The result float. /// The maximum number of limbs. /// The range of the random exponent. /// /// /// The exponent of the number is in the interval - to (in limbs). /// This function is useful for testing functions and algorithms, since these kind of random numbers have proven to be more /// likely to trigger corner-case bugs. /// Negative random numbers are generated when is negative. /// /// /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and set the value of rop to 0. /// mpf_t rop = new mpf_t(); /// gmp_lib.mpf_init(rop); /// /// // Generate a random floating-point number with at most 10 limbs and its exponent in [-5 5]. /// gmp_lib.mpf_random2(rop, 10, 5); /// /// // Free all memory occupied by rop. /// gmp_lib.mpf_clear(rop); /// /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpf_t() /// gmp_lib.mpf_init(rop) /// /// ' Generate a random floating-point number with at most 10 limbs and its exponent in [-5 5]. /// gmp_lib.mpf_random2(rop, 10, 5) /// /// ' Free all memory occupied by rop. /// gmp_lib.mpf_clear(rop) /// /// public static void mpf_random2(mpf_t rop, mp_size_t max_size, mp_exp_t exp) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_random2(rop.ToIntPtr(), max_size, exp); } /// /// Compute the relative difference between and and store the result in . This is | - | / . /// /// The result float. /// The first operand float. /// The second operand float. /// mpf_cmp /// mpf_cmp_z /// mpf_cmp_d /// mpf_cmp_ui /// mpf_cmp_si /// mpf_sgn /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = |x - y| / x. /// gmp_lib.mpf_reldiff(z, x, y); /// /// // Assert that the value of z is 22. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 22.0); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = |x - y| / x. /// gmp_lib.mpf_reldiff(z, x, y) /// /// ' Assert that the value of z is 22. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 22.0) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, Nothing) /// /// public static void mpf_reldiff(mpf_t rop, /*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_reldiff(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set_ui /// mpf_set_si /// mpf_set_d /// mpf_set_z /// mpf_set_q /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 128U); /// gmp_lib.mpf_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init2(y, 128U); /// gmp_lib.mpf_set_si(y, -210); /// /// // Assign the value of y to x. /// gmp_lib.mpf_set(x, y); /// /// // Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -210.0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, null); /// /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 128UI) /// gmp_lib.mpf_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init2(y, 128UI) /// gmp_lib.mpf_set_si(y, -210) /// /// ' Assign the value of y to x. /// gmp_lib.mpf_set(x, y) /// /// ' Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -210.0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, Nothing) /// /// public static void mpf_set(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_set(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set /// mpf_set_ui /// mpf_set_si /// mpf_set_z /// mpf_set_q /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create and initialize a new floating-point number. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 128U); /// /// // Set x to -123.0. /// gmp_lib.mpf_set_d(x, -123.0); /// /// // Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -123.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 128UI) /// /// ' Set x to -123.0. /// gmp_lib.mpf_set_d(x, -123.0) /// /// ' Assert that the value of x is -123.0. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -123.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_set_d(mpf_t rop, double op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_set_d(rop.ToIntPtr(), op); } /// /// Set the default precision to be at least bits. /// /// The minimum precision in bits. /// /// /// All subsequent calls to mpf_init will use this precision, but previously initialized variables are unaffected. /// /// /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128U); /// /// // Assert that the value of x is 128 bits. /// Assert.IsTrue(gmp_lib.mpf_get_default_prec() == 128U); /// /// /// ' Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128UI) /// /// ' Assert that the value of x is 128 bits. /// Assert.IsTrue(gmp_lib.mpf_get_default_prec() = 128UI) /// /// public static void mpf_set_default_prec(mp_bitcnt_t prec) { SafeNativeMethods.__gmpf_set_default_prec(prec); } /// /// Set the precision of to be at least bits. /// /// The result float. /// The minimum precision in bits. /// /// /// The value in rop will be truncated to the new precision. /// /// /// This function requires a call to realloc, and so should not be used in a tight loop. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec_raw /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Create and initialize a new floating-point number x. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// /// // Set its precision to 64 bits. /// gmp_lib.mpf_set_prec(x, 64U); /// /// // Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 0.0); /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) == 64U); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number x. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// /// ' Set its precision to 64 bits. /// gmp_lib.mpf_set_prec(x, 64UI) /// /// ' Assert that the value of x is 0.0, and that its precision is 64 bits. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 0.0) /// Assert.IsTrue(gmp_lib.mpf_get_prec(x) = 64UI) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_set_prec(mpf_t rop, mp_bitcnt_t prec) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_set_prec(rop.ToIntPtr(), prec); } /// /// Set the precision of to be at least bits, without changing the memory allocated. /// /// The result float. /// The minimum precision in bits. /// /// /// must be no more than the allocated precision for , /// that being the precision when was initialized, or in the most recent mpf_set_prec. /// /// /// The value in is unchanged, and in particular if it had a higher precision than /// it will retain that higher precision. New values written to will use the new . /// /// /// Before calling mpf_clear or the full mpf_set_prec, another mpf_set_prec_raw call /// must be made to restore to its original allocated precision. Failing to do so will have unpredictable results. /// /// /// mpf_get_prec can be used before mpf_set_prec_raw to get the original allocated precision. /// After mpf_set_prec_raw it reflects the prec value set. /// /// /// mpf_set_prec_raw is an efficient way to use an mpf_t variable at different precisions during a calculation, /// perhaps to gradually increase precision in an iteration, or just to use various different precisions for different purposes during a calculation. /// /// /// mpf_set_default_prec /// mpf_get_default_prec /// mpf_init /// mpf_init2 /// mpf_inits /// mpf_clear /// mpf_clears /// mpf_get_prec /// mpf_set_prec /// Initializing Floats /// GNU MP - Initializing Floats /// /// /// // Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128U); /// /// // Create, initialize, and set a new rational y to 200 / 3. /// mpq_t y = new mpq_t(); /// gmp_lib.mpq_init(y); /// gmp_lib.mpq_set_ui(y, 200, 3U); /// /// // Create, initialize, and set a new floating-point number x to y. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// gmp_lib.mpf_set_q(x, y); /// /// Assert.IsTrue(x.ToString() == "0.6666666666666666666666666666666666666667e2"); /// /// // Change precision of x, and set its value to 10000 / 3. /// gmp_lib.mpf_set_prec_raw(x, 8U); /// gmp_lib.mpq_set_ui(y, 10000, 3U); /// gmp_lib.mpf_set_q(x, y); /// /// Assert.IsTrue(x.ToString() == "0.333333333333333333333e4"); /// /// // Restore precision of x. /// gmp_lib.mpf_set_prec_raw(x, 128U); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x); /// gmp_lib.mpq_clear(y); /// /// /// ' Set default precision to 128 bits. /// gmp_lib.mpf_set_default_prec(128UI) /// /// ' Create, initialize, and set a new rational y to 200 / 3. /// Dim y As New mpq_t() /// gmp_lib.mpq_init(y) /// gmp_lib.mpq_set_ui(y, 200, 3UI) /// /// ' Create, initialize, and set a new floating-point number x to y. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// gmp_lib.mpf_set_q(x, y) /// Assert.IsTrue(x.ToString() = "0.6666666666666666666666666666666666666667e2") /// /// ' Change precision of x, and set its value to 10000 / 3. /// gmp_lib.mpf_set_prec_raw(x, 8UI) /// gmp_lib.mpq_set_ui(y, 10000, 3UI) /// gmp_lib.mpf_set_q(x, y) /// Assert.IsTrue(x.ToString() = "0.333333333333333333333e4") /// /// ' Restore precision of x. /// gmp_lib.mpf_set_prec_raw(x, 128UI) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x) /// gmp_lib.mpq_clear(y) /// /// public static void mpf_set_prec_raw(mpf_t rop, mp_bitcnt_t prec) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_set_prec_raw(rop.ToIntPtr(), prec); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set /// mpf_set_ui /// mpf_set_si /// mpf_set_d /// mpf_set_z /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create, initialize, and set a new rational y to 200 / 5. /// mpq_t y = new mpq_t(); /// gmp_lib.mpq_init(y); /// gmp_lib.mpq_set_ui(y, 200, 5U); /// /// // Create, initialize, and set a new floating-point number x to y. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// gmp_lib.mpf_set_q(x, y); /// /// // Assert that x is 40. /// Assert.IsTrue(x.ToString() == "0.4e2"); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x); /// gmp_lib.mpq_clear(y); /// /// /// ' Create, initialize, and set a new rational y to 200 / 5. /// Dim y As New mpq_t() /// gmp_lib.mpq_init(y) /// gmp_lib.mpq_set_ui(y, 200, 5UI) /// /// ' Create, initialize, and set a new floating-point number x to y. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// gmp_lib.mpf_set_q(x, y) /// /// ' Assert that x is 40. /// Assert.IsTrue(x.ToString() = "0.4e2") /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x) /// gmp_lib.mpq_clear(y) /// /// public static void mpf_set_q(mpf_t rop, /*const*/ mpq_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_set_q(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set /// mpf_set_ui /// mpf_set_d /// mpf_set_z /// mpf_set_q /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create and initialize a new floating-point number. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 128U); /// /// // Set x to -123. /// gmp_lib.mpf_set_si(x, -123); /// /// // Assert that the value of x is -123. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -123.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 128UI) /// /// ' Set x to -123. /// gmp_lib.mpf_set_si(x, -123) /// /// ' Assert that the value of x is -123. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -123.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_set_si(mpf_t rop, int /*long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_set_si(rop.ToIntPtr(), op); } /// /// Set the value of from the string in . /// /// The result float. /// The input string. /// The base. /// This function returns 0 if the entire string is a valid number in base . Otherwise it returns -1. /// /// /// The string is of the form "M@N" or, if the is 10 or less, /// alternatively "MeN". /// "M" is the mantissa and "N" is the exponent. /// The mantissa is always in the specified . /// The exponent is either in the specified or, if is negative, in decimal. /// The decimal point expected is taken from the current locale, on systems providing localeconv. /// /// /// The argument may be in the ranges 2 to 62, or -62 to -2. /// Negative values are used to specify that the exponent is in decimal. /// /// /// For bases up to 36, case is ignored; upper-case and lower-case letters have the same value; /// for bases 37 to 62, upper-case letter represent the usual 10..35 while lower-case /// letter represent 36..61. /// /// /// Unlike the corresponding mpz function, the will not be determined from the leading characters /// of the string if base is 0. This is so that numbers like "0.23" are not interpreted as octal. /// /// /// White space is allowed in the string, and is simply ignored. /// [This is not really true; white-space is ignored in the beginning of the string and within the mantissa, /// but not in other places, such as after a minus sign or in the exponent. /// We are considering changing the definition of this function, making it fail when there is any white-space /// in the input, since that makes a lot of sense. /// Please tell us your opinion about this change. /// Do you really want it to accept "3 14" as meaning 314 as it does now?] /// /// /// mpf_set /// mpf_set_ui /// mpf_set_si /// mpf_set_d /// mpf_set_z /// mpf_set_q /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create, initialize, and set a new floating-point number x to 0.0234. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// char_ptr value = new char_ptr("234e-4"); /// gmp_lib.mpf_set_str(x, value, 10); /// /// // Assert that x is 40. /// Assert.IsTrue(x.ToString() == "0.234e-1"); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x); /// gmp_lib.free(value); /// /// /// ' Create, initialize, and set a new floating-point number x to 0.0234. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// Dim value As New char_ptr("234e-4") /// gmp_lib.mpf_set_str(x, value, 10) /// /// ' Assert that x is 40. /// Assert.IsTrue(x.ToString() = "0.234e-1") /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x) /// gmp_lib.free(value) /// /// public static int mpf_set_str(mpf_t rop, /*const*/ char_ptr str, int @base) { if (rop == null) throw new ArgumentNullException("rop"); return SafeNativeMethods.__gmpf_set_str(rop.ToIntPtr(), str.ToIntPtr(), @base); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set /// mpf_set_si /// mpf_set_d /// mpf_set_z /// mpf_set_q /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create and initialize a new floating-point number. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 128U); /// /// // Set x to 100. /// gmp_lib.mpf_set_ui(x, 100U); /// /// // Assert that the value of x is 100. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == 100.0); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Create and initialize a new floating-point number. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 128UI) /// /// ' Set x to 100. /// gmp_lib.mpf_set_ui(x, 100UI) /// /// ' Assert that the value of x is 100. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = 100.0) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static void mpf_set_ui(mpf_t rop, uint /*unsigned long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_set_ui(rop.ToIntPtr(), op); } /// /// Set the value of from . /// /// The result float. /// The operand. /// mpf_set /// mpf_set_ui /// mpf_set_si /// mpf_set_d /// mpf_set_q /// mpf_set_str /// mpf_swap /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create, initialize, and set a new integer y to 200. /// mpz_t y = new mpz_t(); /// gmp_lib.mpz_init(y); /// gmp_lib.mpz_set_ui(y, 200U); /// /// // Create, initialize, and set a new floating-point number x to y. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init(x); /// gmp_lib.mpf_set_z(x, y); /// /// // Assert that x is 200. /// Assert.IsTrue(x.ToString() == "0.2e3"); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x); /// gmp_lib.mpz_clear(y); /// /// /// ' Create, initialize, and set a new integer y to 200. /// Dim y As New mpz_t() /// gmp_lib.mpz_init(y) /// gmp_lib.mpz_set_ui(y, 200UI) /// /// ' Create, initialize, and set a new floating-point number x to y. /// Dim x As New mpf_t() /// gmp_lib.mpf_init(x) /// gmp_lib.mpf_set_z(x, y) /// /// ' Assert that x is 200. /// Assert.IsTrue(x.ToString() = "0.2e3") /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clear(x) /// gmp_lib.mpz_clear(y) /// /// public static void mpf_set_z(mpf_t rop, /*const*/ mpz_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_set_z(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Return +1 if op > 0, 0 if op = 0, and -1 if op < 0. /// /// The operand float. /// Return +1 if op > 0, 0 if op = 0, and -1 if op < 0. /// mpf_cmp /// mpf_cmp_z /// mpf_cmp_d /// mpf_cmp_ui /// mpf_cmp_si /// mpf_reldiff /// Float Comparison /// GNU MP - Float Comparison /// /// /// // Create, initialize, and set the value of op to -10. /// mpf_t op = new mpf_t(); /// gmp_lib.mpf_init_set_si(op, -10); /// /// // Assert that the sign of op is -1. /// Assert.IsTrue(gmp_lib.mpf_sgn(op) == -1); /// /// // Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op); /// /// /// ' Create, initialize, and set the value of op to -10. /// Dim op As New mpf_t() /// gmp_lib.mpf_init_set_si(op, -10) /// /// ' Assert that the sign of op is -1. /// Assert.IsTrue(gmp_lib.mpf_sgn(op) = -1) /// /// ' Release unmanaged memory allocated for op. /// gmp_lib.mpf_clear(op) /// /// public static int mpf_sgn(mpf_t op) { if (op == null) throw new ArgumentNullException("op"); return op._mp_size < 0 ? -1 : (op._mp_size > 0 ? 1 : 0); } /// /// Return the number of limbs currently in use. /// /// The operand float. /// The number of limbs currently in use. /// mpf_t /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x. /// mpf_t x = "1.00000000000000000000001"; /// /// // Assert that the size of x is 1. /// Assert.IsTrue(gmp_lib.mpf_size(x) == 4); /// /// // Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x. /// Dim x As mpf_t = "1.00000000000000000000001" /// /// ' Assert that the size of x is 1. /// Assert.IsTrue(gmp_lib.mpf_size(x) = 4) /// /// ' Release unmanaged memory allocated for x. /// gmp_lib.mpf_clear(x) /// /// public static size_t mpf_size(/*const*/ mpf_t op) { if (op == null) throw new ArgumentNullException("op"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpf_size_x86(op.ToIntPtr())); else return new size_t(SafeNativeMethods.__gmpf_size_x64(op.ToIntPtr())); } /// /// Set to the square root of . /// /// The result float. /// The operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt_ui /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 100. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 100); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = sqrt(x). /// gmp_lib.mpf_sqrt(z, x); /// /// // Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 100. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 100) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = sqrt(x). /// gmp_lib.mpf_sqrt(z, x) /// /// ' Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_sqrt(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_sqrt(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to the square root of . /// /// The result float. /// The operand. /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = sqrt(100). /// gmp_lib.mpf_sqrt_ui(z, 100U); /// /// // Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clear(z); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = sqrt(100). /// gmp_lib.mpf_sqrt_ui(z, 100UI) /// /// ' Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clear(z) /// /// public static void mpf_sqrt_ui(mpf_t rop, uint /*unsigned long int*/ op) { if (rop == null) throw new ArgumentNullException("rop"); SafeNativeMethods.__gmpf_sqrt_ui(rop.ToIntPtr(), op); } /// /// Set to - . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_ui_sub /// mpf_sub_ui /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x - y. /// gmp_lib.mpf_sub(z, x, y); /// /// // Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 220.0); /// /// // Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x - y. /// gmp_lib.mpf_sub(z, x, y) /// /// /// ' Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 220.0) /// /// ' Release unmanaged memory allocated for x, y, and z. /// gmp_lib.mpf_clears(x, y, z, Nothing) /// /// public static void mpf_sub(mpf_t rop, /*const*/ mpf_t op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_sub(rop.ToIntPtr(), op1.ToIntPtr(), op2.ToIntPtr()); } /// /// Set to - . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_ui_sub /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = x - 200. /// gmp_lib.mpf_sub_ui(z, x, 200U); /// /// // Assert that the value of z is -190. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == -190.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = x - 200. /// gmp_lib.mpf_sub_ui(z, x, 200UI) /// /// ' Assert that the value of z is -190. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = -190.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_sub_ui(mpf_t rop, /*const*/ mpf_t op1, uint /*unsigned long int*/ op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op1 == null) throw new ArgumentNullException("op1"); SafeNativeMethods.__gmpf_sub_ui(rop.ToIntPtr(), op1.ToIntPtr(), op2); } /// /// Swap and efficiently. /// /// The first result float. /// The second result float. /// /// /// Both the values and the precisions of the two variables are swapped. /// /// /// mpf_set /// mpf_set_ui /// mpf_set_si /// mpf_set_d /// mpf_set_z /// mpf_set_q /// mpf_set_str /// Assigning Floats /// GNU MP - Assigning Floats /// /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init2(x, 128U); /// gmp_lib.mpf_set_si(x, 10); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init2(y, 128U); /// gmp_lib.mpf_set_si(y, -210); /// /// // Swap the values of x and y. /// gmp_lib.mpf_swap(x, y); /// /// // Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) == -210.0); /// /// // Assert that the value of y is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(y) == 10.0); /// /// // Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, null); /// /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init2(x, 128UI) /// gmp_lib.mpf_set_si(x, 10) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init2(y, 128UI) /// gmp_lib.mpf_set_si(y, -210) /// /// ' Swap the values of x and y. /// gmp_lib.mpf_swap(x, y) /// /// ' Assert that the value of x is -210. /// Assert.IsTrue(gmp_lib.mpf_get_d(x) = -210.0) /// /// ' Assert that the value of y is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(y) = 10.0) /// /// ' Release unmanaged memory allocated for x and y. /// gmp_lib.mpf_clears(x, y, Nothing) /// /// public static void mpf_swap(mpf_t rop1, mpf_t rop2) { if (rop1 == null) throw new ArgumentNullException("rop1"); if (rop2 == null) throw new ArgumentNullException("rop2"); SafeNativeMethods.__gmpf_swap(rop1.ToIntPtr(), rop2.ToIntPtr()); } /// /// Set to rounded to the integer towards zero. /// /// The result float. /// The operand float. /// mpf_ceil /// mpf_floor /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_urandomb /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10.4. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_d(x, 10.4); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = trunc(x). /// gmp_lib.mpf_trunc(z, x); /// /// // Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 10.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10.4. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_d(x, 10.4) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = trunc(x). /// gmp_lib.mpf_trunc(z, x) /// /// ' Assert that the value of z is 10. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 10.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_trunc(mpf_t rop, /*const*/ mpf_t op) { if (rop == null) throw new ArgumentNullException("rop"); if (op == null) throw new ArgumentNullException("op"); SafeNativeMethods.__gmpf_trunc(rop.ToIntPtr(), op.ToIntPtr()); } /// /// Set to / . /// /// The result float. /// The first operand. /// The second operand. /// /// /// Division is undefined if the divisor is zero, and passing a zero divisor to the divide /// functions will make it intentionally divide by zero. /// This lets the user handle arithmetic exceptions in division functions in the same manner /// as other arithmetic exceptions. /// /// /// mpf_add /// mpf_sub /// mpf_mul /// mpf_div /// mpf_div_ui /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// mpf_div_2exp /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number x to 10. /// mpf_t x = new mpf_t(); /// gmp_lib.mpf_init_set_si(x, 10); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = 210 / x. /// gmp_lib.mpf_ui_div(z, 210U, x); /// /// // Assert that the value of z is 21. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 21.0); /// /// // Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number x to 10. /// Dim x As New mpf_t() /// gmp_lib.mpf_init_set_si(x, 10) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = 210 / x. /// gmp_lib.mpf_ui_div(z, 210UI, x) /// /// ' Assert that the value of z is 21. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 21.0) /// /// ' Release unmanaged memory allocated for x and z. /// gmp_lib.mpf_clears(x, z, Nothing) /// /// public static void mpf_ui_div(mpf_t rop, uint /*unsigned long int*/ op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_ui_div(rop.ToIntPtr(), op1, op2.ToIntPtr()); } /// /// Set to - . /// /// The result float. /// The first operand. /// The second operand. /// mpf_add /// mpf_sub /// mpf_sub_ui /// mpf_mul /// mpf_div /// mpf_sqrt /// mpf_pow_ui /// mpf_neg /// mpf_abs /// Float Arithmetic /// GNU MP - Float Arithmetic /// /// /// // Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64U); /// /// // Create, initialize, and set a new floating-point number y to -210. /// mpf_t y = new mpf_t(); /// gmp_lib.mpf_init_set_si(y, -210); /// /// // Create and initialize a new floating-point number z. /// mpf_t z = new mpf_t(); /// gmp_lib.mpf_init(z); /// /// // Set z = 10 - y. /// gmp_lib.mpf_ui_sub(z, 10U, y); /// /// // Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) == 220.0); /// /// // Release unmanaged memory allocated for y, and z. /// gmp_lib.mpf_clears(y, z, null); /// /// /// ' Set default precision to 64 bits. /// gmp_lib.mpf_set_default_prec(64UI) /// /// ' Create, initialize, and set a new floating-point number y to -210. /// Dim y As New mpf_t() /// gmp_lib.mpf_init_set_si(y, -210) /// /// ' Create and initialize a new floating-point number z. /// Dim z As New mpf_t() /// gmp_lib.mpf_init(z) /// /// ' Set z = 10 - y. /// gmp_lib.mpf_ui_sub(z, 10UI, y) /// /// ' Assert that the value of z is 220. /// Assert.IsTrue(gmp_lib.mpf_get_d(z) = 220.0) /// /// ' Release unmanaged memory allocated for y, and z. /// gmp_lib.mpf_clears(y, z, Nothing) /// /// public static void mpf_ui_sub(mpf_t rop, uint /*unsigned long int*/ op1, /*const*/ mpf_t op2) { if (rop == null) throw new ArgumentNullException("rop"); if (op2 == null) throw new ArgumentNullException("op2"); SafeNativeMethods.__gmpf_ui_sub(rop.ToIntPtr(), op1, op2.ToIntPtr()); } /// /// Generate a uniformly distributed random float in , such that 0 ≤ rop < 1, with significant bits in the mantissa or less if the precision of is smaller. /// /// The result float. /// The random number generator state. /// Number of significant bits. /// /// /// The variable state must be initialized by calling one of the gmp_randinit functions /// Random State Initialization /// (GNU MP - Random State Initialization) before invoking this function. /// /// /// mpf_ceil /// mpf_floor /// mpf_trunc /// mpf_integer_p /// mpf_fits_ulong_p /// mpf_fits_slong_p /// mpf_fits_uint_p /// mpf_fits_sint_p /// mpf_fits_ushort_p /// mpf_fits_sshort_p /// mpf_random2 /// Miscellaneous Float Functions /// GNU MP - Miscellaneous Float Functions /// /// /// // Create, initialize, and seed a new random number generator. /// gmp_randstate_t state = new gmp_randstate_t(); /// gmp_lib.gmp_randinit_mt(state); /// gmp_lib.gmp_randseed_ui(state, 100000U); /// /// // Create, initialize, and set the value of rop to 0. /// mpf_t rop = new mpf_t(); /// gmp_lib.mpf_init(rop); /// /// // Generate a random integer in the range [0, 1) with 50 bits precision. /// gmp_lib.mpf_urandomb(rop, state, 50); /// /// // Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state); /// gmp_lib.mpf_clear(rop); /// /// /// ' Create, initialize, and seed a new random number generator. /// Dim state As New gmp_randstate_t() /// gmp_lib.gmp_randinit_mt(state) /// gmp_lib.gmp_randseed_ui(state, 100000UI) /// /// ' Create, initialize, and set the value of rop to 0. /// Dim rop As New mpf_t() /// gmp_lib.mpf_init(rop) /// /// ' Generate a random integer in the range [0, 1) with 50 bits precision. /// gmp_lib.mpf_urandomb(rop, state, 50) /// /// ' Free all memory occupied by state and rop. /// gmp_lib.gmp_randclear(state) /// gmp_lib.mpf_clear(rop) /// /// public static void mpf_urandomb(mpf_t rop, gmp_randstate_t state, mp_bitcnt_t nbits) { if (rop == null) throw new ArgumentNullException("rop"); if (state == null) throw new ArgumentNullException("state"); SafeNativeMethods.__gmpf_urandomb(rop.ToIntPtr(), state.ToIntPtr(), nbits); } #endregion #region "Low level positive-integer (i.e. N) routines." /// /// Add {, } and {, }, and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The number of limbs in . /// The second operand integer. /// The number of limbs in . /// Return carry, either 0 or 1. /// /// /// This function requires that is greater than or equal to . /// /// /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = s1 + s2. /// mp_limb_t carry = gmp_lib.mpn_add(rp, s1p, s1p.Size, s2p, s2p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = s1 + s2. /// Dim carry As mp_limb_t = gmp_lib.mpn_add(rp, s1p, s1p.Size, s2p, s2p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_add(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t s1n, /*const*/ mp_ptr s2p, mp_size_t s2n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_add_x86(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_add_x64(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); } /// /// Add {, } and , and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The number of limbs in . /// The second operand integer. /// Return carry, either 0 or 1. /// mpn_add /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = s1 + 1. /// mp_limb_t carry = gmp_lib.mpn_add_1(rp, s1p, s1p.Size, 1); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = s1 + 1. /// Dim carry As mp_limb_t = gmp_lib.mpn_add_1(rp, s1p, s1p.Size, 1) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static mp_limb_t mpn_add_1(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n, mp_limb_t s2limb) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_add_1_x86(rp.ToIntPtr(), s1p.ToIntPtr(), n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_add_1_x64(rp.ToIntPtr(), s1p.ToIntPtr(), n, s2limb)); } /// /// Add {, } and {, }, and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs in and . /// 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 CPUs. /// For addition of a variable to itself (i.e., equals ) /// use mpn_lshift with a count of 1 for optimal speed. /// /// /// mpn_add /// mpn_add_1 /// mpn_addmul_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = s1 + s2. /// mp_limb_t carry = gmp_lib.mpn_add_n(rp, s1p, s2p, rp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = s1 + s2. /// Dim carry As mp_limb_t = gmp_lib.mpn_add_n(rp, s1p, s2p, rp.Size) /// /// ' Assert result of operation. /// /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_add_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_add_n_x86(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_add_n_x64(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); } /// /// Multiply {, } and , and add the least significant limbs of the product to {, } and write the result to . /// /// The result integer. /// The first operand integer. /// The number of limbs in . /// The second operand integer. /// Return the most significant limb of the product, plus carry-out from the addition. /// /// /// {, } and {, } are allowed /// to overlap provided . /// /// /// 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 CPUs. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[] { 0x00000002, 0x00000000 }); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp += s1 * 2. /// mp_limb_t carry = gmp_lib.mpn_addmul_1(rp, s1p, s1p.Size, 2); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 0x02); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger() { &H2, &H0}) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp += s1 * 2. /// Dim carry As mp_limb_t = gmp_lib.mpn_addmul_1(rp, s1p, s1p.Size, 2) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = &H2) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static mp_limb_t mpn_addmul_1(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n, mp_limb_t s2limb) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_addmul_1_x86(rp.ToIntPtr(), s1p.ToIntPtr(), n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_addmul_1_x64(rp.ToIntPtr(), s1p.ToIntPtr(), n, s2limb)); } /// /// Compare {, } and {, }. /// /// The first operand integer. /// The second operand integer. /// The number of limbs in and . /// Return a positive value if s1 > s2, 0 if they are equal, or a negative value if s1 < s2. /// mpn_perfect_power_p /// mpn_perfect_square_p /// mpn_zero_p /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// /// // Assert s1p > s2p. /// Assert.IsTrue(gmp_lib.mpn_cmp(s1p, s2p, s1p.Size) > 0); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p, s2p); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H0}) /// /// ' Assert s1p > s2p. /// Assert.IsTrue(gmp_lib.mpn_cmp(s1p, s2p, s1p.Size) > 0) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p, s2p) /// /// public static int mpn_cmp(/*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); return SafeNativeMethods.__gmpn_cmp(s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Test {, } and return 1 if the operand is zero, 0 otherwise. /// /// The operand integer. /// The number of limbs in . /// Return 1 if the operand is zero, 0 otherwise. /// mpn_cmp /// mpn_perfect_power_p /// mpn_perfect_square_p /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr sp = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Assert sp == 0. /// Assert.IsTrue(gmp_lib.mpn_zero_p(sp, sp.Size) == 1); /// /// // Release unmanaged memory. /// gmp_lib.free(sp); /// /// /// ' Create multi-precision operand. /// Dim sp As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Assert sp == 0. /// Assert.IsTrue(gmp_lib.mpn_zero_p(sp, sp.Size) = 1) /// /// ' Release unmanaged memory. /// gmp_lib.free(sp) /// /// public static int mpn_zero_p(/*const*/ mp_ptr sp, mp_size_t n) { if (sp == null) throw new ArgumentNullException("sp"); return SafeNativeMethods.__gmpn_zero_p(sp.ToIntPtr(), n); } /// /// Divide {, } by , expecting it to divide exactly, and writing the result to {r, }. /// /// The result integer. /// The first operand integer. /// The number of limbs in and . /// The second operand integer. /// /// /// If doesn’t divide exactly, the value written to {, } is undefined. /// The areas at and have to be identical or completely separate, not partially overlapping. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x55555555, 0x00005555 }); /// /// // Set rp = sp / 3. /// gmp_lib.mpn_divexact_1(rp, sp, sp.Size, 0x3); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H55555555, &H5555}) /// /// ' Set rp = sp / 3. /// gmp_lib.mpn_divexact_1(rp, sp, sp.Size, &H3) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static void mpn_divexact_1(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n, mp_limb_t d) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) SafeNativeMethods.__gmpn_divexact_1_x86(rp.ToIntPtr(), sp.ToIntPtr(), n, (uint)d); else SafeNativeMethods.__gmpn_divexact_1_x64(rp.ToIntPtr(), sp.ToIntPtr(), n, d); } /// /// Divide {, } by 3, expecting it to divide exactly, and writing the result to {, }. /// /// The result integer. /// The operand integer. /// The number of limbs in . /// 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 from low to high. /// 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 /// c * b^n + a - i = 3 * q, where b = 2^mp_bits_per_limb. /// The return c is always 0, 1 or 2, and the initial carry i must also be 0, /// 1 or 2 (these are both borrows really). When c = 0 clearly q = (a - i) / 3. /// When c != 0, the remainder (a - i) mod 3 is given by 3 - c, because /// b ≡ 1 mod 3 (when mp_bits_per_limb is even, which is always so currently). /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x55555555, 0x00005555 }); /// /// // Set rp = sp / 3. /// mp_limb_t remainder = gmp_lib.mpn_divexact_by3(rp, sp, sp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(remainder == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H55555555, &H5555}) /// /// ' Set rp = sp / 3. /// Dim remainder As mp_limb_t = gmp_lib.mpn_divexact_by3(rp, sp, sp.Size) /// /// ' Assert result of operation. /// /// Assert.IsTrue(remainder = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static mp_limb_t mpn_divexact_by3(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_divexact_by3_x86(rp.ToIntPtr(), sp.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_divexact_by3_x64(rp.ToIntPtr(), sp.ToIntPtr(), n)); } /// /// Divide {, } by 3, expecting it to divide exactly, and writing the result to {, }. /// /// The result integer. /// The operand integer. /// The number of limbs in . /// The initial carry. /// 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 from low to high. /// 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 /// c * b^n + a - i = 3 * q, where b = 2^mp_bits_per_limb. /// The return c is always 0, 1 or 2, and the initial carry i must also be 0, /// 1 or 2 (these are both borrows really). When c = 0 clearly q = (a - i) / 3. /// When c != 0, the remainder (a - i) mod 3 is given by 3 - c, because /// b ≡ 1 mod 3 (when mp_bits_per_limb is even, which is always so currently). /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xaaaaaaaa, 0x5555aaaa }); /// /// // Set rp = sp / 3. /// mp_limb_t remainder = gmp_lib.mpn_divexact_by3c(rp, sp, sp.Size, 1); /// /// // Assert result of operation. /// Assert.IsTrue(remainder == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HaaaaaaaaUI, &H5555aaaa}) /// /// ' Set rp = sp / 3. /// Dim remainder As mp_limb_t = gmp_lib.mpn_divexact_by3c(rp, sp, sp.Size, 1) /// /// ' Assert result of operation. /// Assert.IsTrue(remainder = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static mp_limb_t mpn_divexact_by3c(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n, mp_limb_t carry) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_divexact_by3c_x86(rp.ToIntPtr(), sp.ToIntPtr(), n, (uint)carry)); else return new mp_limb_t(SafeNativeMethods.__gmpn_divexact_by3c_x64(rp.ToIntPtr(), sp.ToIntPtr(), n, carry)); } /// /// Divide {, } by , and write the quotient at . /// /// /// /// /// /// /// Return the remainder. /// /// /// The integer quotient is written to { + , } /// and in addition fraction limbs are developed and written to {, }. /// Either or both and can be zero. /// For most usages, will be zero. /// /// /// mpn_divmod_1 exists for upward source compatibility and is simply a macro /// calling mpn_divrem_1 with a of 0. /// /// /// The areas at and have to be identical or /// completely separate, not partially overlapping. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s2p = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr r1p = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x435e50d7, 0x00000d79 }); /// /// // Set r1p = s2p / 19. /// mp_limb_t remainder = gmp_lib.mpn_divrem_1(r1p, 0, s2p, s2p.Size, 0x13); /// /// // Assert result of operation. /// Assert.IsTrue(remainder == 10); /// Assert.IsTrue(r1p.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(r1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s2p As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim r1p As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H435e50d7, &Hd79}) /// /// ' Set r1p = s2p / 19. /// Dim remainder As mp_limb_t = gmp_lib.mpn_divrem_1(r1p, 0, s2p, s2p.Size, &H13) /// /// ' Assert result of operation. /// Assert.IsTrue(remainder = 10) /// Assert.IsTrue(r1p.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(r1p, s2p, result) /// /// public static mp_limb_t mpn_divrem_1(mp_ptr r1p, mp_size_t qxn, /*const*/ mp_ptr s2p, mp_size_t s2n, mp_limb_t s3limb) { if (r1p == null) throw new ArgumentNullException("r1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_divrem_1_x86(r1p.ToIntPtr(), qxn, s2p.ToIntPtr(), s2n, (uint)s3limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_divrem_1_x64(r1p.ToIntPtr(), qxn, s2p.ToIntPtr(), s2n, s3limb)); } /// /// Divide {, } by , and write the quotient at . /// /// /// /// /// /// Return the remainder. /// /// /// The integer quotient is written to {, }. /// can be zero. /// /// /// mpn_divmod_1 exists for upward source compatibility and is simply a macro /// calling mpn_divrem_1 with a qxn of 0. /// /// /// The areas at and have to be identical or /// completely separate, not partially overlapping. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s2p = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr r1p = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x435e50d7, 0x00000d79 }); /// /// // Set r1p = s2p / 19. /// mp_limb_t remainder = gmp_lib.mpn_divmod_1(r1p, s2p, s2p.Size, 0x13); /// /// // Assert result of operation. /// Assert.IsTrue(remainder == 10); /// Assert.IsTrue(r1p.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(r1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s2p As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim r1p As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H435e50d7, &Hd79}) /// /// ' Set r1p = s2p / 19. /// Dim remainder As mp_limb_t = gmp_lib.mpn_divmod_1(r1p, s2p, s2p.Size, &H13) /// /// ' Assert result of operation. /// Assert.IsTrue(remainder = 10) /// Assert.IsTrue(r1p.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(r1p, s2p, result) /// /// public static mp_limb_t mpn_divmod_1(mp_ptr r1p, /*const*/ mp_ptr s2p, mp_size_t s2n, mp_limb_t s3limb) { if (r1p == null) throw new ArgumentNullException("r1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_divrem_1_x86(r1p.ToIntPtr(), 0, s2p.ToIntPtr(), s2n, (uint)s3limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_divrem_1_x64(r1p.ToIntPtr(), 0, s2p.ToIntPtr(), s2n, s3limb)); } ////obsolete ////#define mpn_divrem __MPN(divrem) ////public static mp_limb_t mpn_divrem(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/) //{ // return SafeNativeMethods.__gmpn_divrem(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/); //} ////obsolete ////#define mpn_divrem_1 __MPN(divrem_1) ////public static mp_limb_t mpn_divrem_1(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ qxn, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n, mp_limb_t s3limb) //{ // return SafeNativeMethods.__gmpn_divrem_1(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ qxn, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n, mp_limb_t s3limb); //} ////obsolete ////#define mpn_divrem_2 __MPN(divrem_2) ////public static mp_limb_t mpn_divrem_2(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/) //{ // return SafeNativeMethods.__gmpn_divrem_2(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/); //} ////#define mpn_div_qr_1 __MPN(div_qr_1) ////public static mp_limb_t mpn_div_qr_1(IntPtr /*mp_ptr*/, mp_limb_t*, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t) //{ // return SafeNativeMethods.__gmpn_div_qr_1(IntPtr /*mp_ptr*/, mp_limb_t*, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t); //} ////#define mpn_div_qr_2 __MPN(div_qr_2) ////public static mp_limb_t mpn_div_qr_2(IntPtr /*mp_ptr*/, IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/) //{ // return SafeNativeMethods.__gmpn_div_qr_2(IntPtr /*mp_ptr*/, IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/); //} /// /// Set {, retval} to the greatest common divisor of {, } and {, }. /// /// The result integer. /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The result can be up to limbs, the return value is the actual number produced; i.e. the number of limbs of . /// /// /// Both source operands are destroyed. /// /// /// It is required that > 0, and the most significant limb /// of {, } must be non-zero. /// No overlap is permitted between {, } and {, }. /// /// /// mpn_gcd_1 /// O:Math.Gmp.Native.gmp_lib.mpn_gcdext /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr xp = new mp_ptr(new uint[] { 0x964619c7, 0x00000002 }); /// mp_ptr yp = new mp_ptr(new uint[] { 0xc2d24d55, 0x00000007 }); /// mp_ptr rp = new mp_ptr(yp.Size); /// mp_ptr result = new mp_ptr(new uint[] { 0x964619c7, 0x00000002 }); /// /// // Set rp = gcd(xp, yp). /// mp_size_t size = gmp_lib.mpn_gcd(rp, xp, xp.Size, yp, yp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(size == result.Size); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, xp, yp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim xp As New mp_ptr(New UInteger() { &H964619c7UI, &H2}) /// Dim yp As New mp_ptr(New UInteger() { &Hc2d24d55UI, &H7}) /// Dim rp As New mp_ptr(yp.Size) /// Dim result As New mp_ptr(New UInteger() { &H964619c7UI, &H2}) /// /// ' Set rp = gcd(xp, yp). /// Dim size As mp_size_t = gmp_lib.mpn_gcd(rp, xp, xp.Size, yp, yp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(size = result.Size) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, xp, yp, result) /// /// public static mp_size_t mpn_gcd(mp_ptr rp, mp_ptr xp, mp_size_t xn, mp_ptr yp, mp_size_t yn) { if (rp == null) throw new ArgumentNullException("rp"); if (xp == null) throw new ArgumentNullException("xp"); if (yp == null) throw new ArgumentNullException("yp"); return new mp_size_t(SafeNativeMethods.__gmpn_gcd(rp.ToIntPtr(), xp.ToIntPtr(), xn, yp.ToIntPtr(), yn)); } /// /// Return the greatest common divisor of {, } and . /// /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The greatest common divisor of {, } and . /// /// /// Both operands must be non-zero. /// /// /// mpn_gcd /// O:Math.Gmp.Native.gmp_lib.mpn_gcdext /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr xp = new mp_ptr(new uint[] { 0x00000000, 0x00000001 }); /// /// // Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_gcd_1(xp, xp.Size, 1073741824) == 1073741824); /// /// // Release unmanaged memory. /// gmp_lib.free(xp); /// /// /// ' Create multi-precision operand. /// Dim xp As New mp_ptr(New UInteger() { &H0, &H1}) /// /// ' Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_gcd_1(xp, xp.Size, 1073741824) = 1073741824) /// /// ' Release unmanaged memory. /// gmp_lib.free(xp) /// /// public static mp_limb_t mpn_gcd_1(/*const*/ mp_ptr xp, mp_size_t xn, mp_limb_t ylimb) { if (xp == null) throw new ArgumentNullException("xp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_gcd_1_x86(xp.ToIntPtr(), xn, (uint)ylimb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_gcd_1_x64(xp.ToIntPtr(), xn, ylimb)); } ////#define mpn_gcdext_1 __MPN(gcdext_1) ////public static mp_limb_t mpn_gcdext_1(mp_limb_signed_t*, mp_limb_signed_t*, mp_limb_t, mp_limb_t) //{ // return SafeNativeMethods.__gmpn_gcdext_1(mp_limb_signed_t*, mp_limb_signed_t*, mp_limb_t, mp_limb_t); //} /// /// Compute the greatest common divisor G of U and V. Compute a cofactor S such that G = US + VT. /// /// The fisrt result operand. /// The second result operand. /// Pointer to the number of limbs of . /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The number of limbs of . /// /// /// Let U be defined by {, } /// and let V be defined by {, }. /// /// /// The second cofactor T is not computed but can easily be obtained from (G - U * S) / V (the division will be exact). /// It is required that > 0, /// and the most significant limb of {, } must be non-zero. /// /// /// Store G at and let the return value define its limb count. /// Store S at and let | .Value | define its limb count. /// S can be negative; when this happens .Value will be negative. /// The area at should have room for limbs /// and the area at should have room for + 1 limbs. /// /// /// Both source operands are destroyed. /// /// /// Compatibility notes: GMP 4.3.0 and 4.3.1 defined S less strictly. /// Earlier as well as later GMP releases define S as described here. /// GMP releases before GMP 4.3.0 required additional space for both input and output areas. /// More precisely, the areas {, + 1} and /// {, + 1} were destroyed (i.e. the operands /// plus an extra limb past the end of each), and the areas pointed to by /// and should each have room for + 1 limbs. /// /// /// mpn_gcd /// mpn_gcd_1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr up = new mp_ptr(new uint[] { 0x40000000, 0x00000000 }); /// mp_ptr vp = new mp_ptr(new uint[] { 0x00000000, 0x00000001 }); /// mp_ptr gp = new mp_ptr(new uint[vp.Size * (IntPtr.Size / 4)]); /// mp_ptr sp = new mp_ptr(new uint[(vp.Size + 1) * (IntPtr.Size / 4)]); /// mp_ptr result = new mp_ptr(new uint[] { 0x40000000, 0x00000000 }); /// mp_ptr cofactor = new mp_ptr(new uint[] { 0x00000001, 0x00000000, 0x00000000 }); /// /// // Set gp = gcd(up, vp). /// mp_size_t sn = 0; /// mp_size_t size = gmp_lib.mpn_gcdext(gp, sp, ref sn, up, up.Size, vp, vp.Size); /// /// // Assert result. /// Assert.IsTrue(size == 1); /// Assert.IsTrue(gp.SequenceEqual(result)); /// Assert.IsTrue(sn == 1); /// Assert.IsTrue(sp.SequenceEqual(cofactor)); /// /// // Release unmanaged memory. /// gmp_lib.free(gp, up, vp, sp, result, cofactor); /// /// /// ' Create multi-precision operands, and expected result. /// Dim up As New mp_ptr(New UInteger() { &H40000000, &H0}) /// Dim vp As New mp_ptr(New UInteger() { &H0, &H1}) /// Dim gp As New mp_ptr(New UInteger(vp.Size* (IntPtr.Size / 4) - 1) {}) /// Dim sp As New mp_ptr(New UInteger((vp.Size + 1) * (IntPtr.Size / 4) - 1) {}) /// Dim result As New mp_ptr(New UInteger() { &H40000000, &H0}) /// Dim cofactor As New mp_ptr(New UInteger() { &H1, &H0, &H0}) /// /// ' Set gp = gcd(up, vp). /// Dim sn As mp_size_t = 0 /// Dim size As mp_size_t = gmp_lib.mpn_gcdext(gp, sp, sn, up, up.Size, vp, vp.Size) /// /// ' Assert result. /// Assert.IsTrue(size = 1) /// Assert.IsTrue(gp.SequenceEqual(result)) /// Assert.IsTrue(sn = 1) /// Assert.IsTrue(sp.SequenceEqual(cofactor)) /// /// ' Release unmanaged memory. /// gmp_lib.free(gp, up, vp, sp, result, cofactor) /// /// public static mp_size_t mpn_gcdext(mp_ptr gp, mp_ptr sp, ref mp_size_t sn, mp_ptr up, mp_size_t un, mp_ptr vp, mp_size_t vn) { if (gp == null) throw new ArgumentNullException("gp"); if (sp == null) throw new ArgumentNullException("sp"); if (up == null) throw new ArgumentNullException("up"); if (vp == null) throw new ArgumentNullException("vp"); return new mp_size_t(SafeNativeMethods.__gmpn_gcdext(gp.ToIntPtr(), sp.ToIntPtr(), ref sn.Value, up.ToIntPtr(), un, vp.ToIntPtr(), vn)); } /// /// Compute the greatest common divisor G of U and V. Compute a cofactor S such that G = US + VT. /// /// The fisrt result operand. /// The second result operand. /// Pointer to the number of limbs of . /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The number of limbs of . /// /// /// Let U be defined by {, } /// and let V be defined by {, }. /// /// /// The second cofactor T is not computed but can easily be obtained from (G - U * S) / V (the division will be exact). /// It is required that > 0, /// and the most significant limb of {, } must be non-zero. /// /// /// Store G at and let the return value define its limb count. /// Store S at and let | .Value | define its limb count. /// S can be negative; when this happens .Value will be negative. /// The area at should have room for limbs /// and the area at should have room for + 1 limbs. /// /// /// Both source operands are destroyed. /// /// /// Compatibility notes: GMP 4.3.0 and 4.3.1 defined S less strictly. /// Earlier as well as later GMP releases define S as described here. /// GMP releases before GMP 4.3.0 required additional space for both input and output areas. /// More precisely, the areas {, + 1} and /// {, + 1} were destroyed (i.e. the operands /// plus an extra limb past the end of each), and the areas pointed to by /// and should each have room for + 1 limbs. /// /// /// mpn_gcd /// mpn_gcd_1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr up = new mp_ptr(new uint[] { 0x40000000, 0x00000000 }); /// mp_ptr vp = new mp_ptr(new uint[] { 0x00000000, 0x00000001 }); /// mp_ptr gp = new mp_ptr(new uint[vp.Size * (IntPtr.Size / 4)]); /// mp_ptr sp = new mp_ptr(new uint[(vp.Size + 1) * (IntPtr.Size / 4)]); /// mp_ptr result = new mp_ptr(new uint[] { 0x40000000, 0x00000000 }); /// mp_ptr cofactor = new mp_ptr(new uint[] { 0x00000001, 0x00000000, 0x00000000 }); /// /// // Set gp = gcd(up, vp). /// ptr<mp_size_t> sn = new ptr<mp_size_t>(0); /// mp_size_t size = gmp_lib.mpn_gcdext(gp, sp, sn, up, up.Size, vp, vp.Size); /// /// // Assert result. /// Assert.IsTrue(size == 1); /// Assert.IsTrue(gp.SequenceEqual(result)); /// Assert.IsTrue(sn.Value == 1); /// Assert.IsTrue(sp.SequenceEqual(cofactor)); /// /// // Release unmanaged memory. /// gmp_lib.free(gp, up, vp, sp, result, cofactor); /// /// /// ' Create multi-precision operands, and expected result. /// Dim up As New mp_ptr(New UInteger() { &H40000000, &H0}) /// Dim vp As New mp_ptr(New UInteger() { &H0, &H1}) /// Dim gp As New mp_ptr(New UInteger(vp.Size* (IntPtr.Size / 4) - 1) {}) /// Dim sp As New mp_ptr(New UInteger((vp.Size + 1) * (IntPtr.Size / 4) - 1) {}) /// Dim result As New mp_ptr(New UInteger() { &H40000000, &H0}) /// Dim cofactor As New mp_ptr(New UInteger() { &H1, &H0, &H0}) /// /// ' Set gp = gcd(up, vp). /// Dim sn As New ptr(Of mp_size_t)(0) /// Dim size As mp_size_t = gmp_lib.mpn_gcdext(gp, sp, sn, up, up.Size, vp, vp.Size) /// /// ' Assert result. /// Assert.IsTrue(size = 1) /// Assert.IsTrue(gp.SequenceEqual(result)) /// Assert.IsTrue(sn.Value = 1) /// Assert.IsTrue(sp.SequenceEqual(cofactor)) /// /// ' Release unmanaged memory. /// gmp_lib.free(gp, up, vp, sp, result, cofactor) /// /// public static mp_size_t mpn_gcdext(mp_ptr gp, mp_ptr sp, ptr sn, mp_ptr up, mp_size_t un, mp_ptr vp, mp_size_t vn) { if (gp == null) throw new ArgumentNullException("gp"); if (sp == null) throw new ArgumentNullException("sp"); if (sn == null) throw new ArgumentNullException("sn"); if (up == null) throw new ArgumentNullException("up"); if (vp == null) throw new ArgumentNullException("vp"); return new mp_size_t(SafeNativeMethods.__gmpn_gcdext(gp.ToIntPtr(), sp.ToIntPtr(), ref sn.Value.Value, up.ToIntPtr(), un, vp.ToIntPtr(), vn)); } /// /// Convert {, } to a raw unsigned char array at in base , and return the number of characters produced. /// /// The result string. /// The base. /// The operand integer. /// The number of limbs of . /// The number of characters produced at . /// /// /// There may be leading zeros in the string. /// 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. can vary from 2 to 256. /// /// /// The most significant limb of the input {, } must be non-zero. /// The input {, } is clobbered, except when base is a power of 2, /// in which case it’s unchanged. /// /// /// The area at has to have space for the largest possible number /// represented by a long limb array, plus one extra character. /// /// /// mpn_set_str /// mpn_sizeinbase /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands. /// mp_ptr s1p = new mp_ptr(new uint[] { 0x00000001, 0x00000001 }); /// char_ptr str = new char_ptr("xxxxxxxxxxxxxxxxx"); /// /// // Convert s1p to hex string. /// size_t count = gmp_lib.mpn_get_str(str, 16, s1p, s1p.Size); /// /// // Copy out str to bytes. /// byte[] s = new byte[count]; /// Marshal.Copy(str.ToIntPtr(), s, 0, (int)count); /// /// // Assert the non-ASCII, hex representation of s1p. /// Assert.IsTrue(s.SequenceEqual(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 })); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// gmp_lib.free(str); /// /// /// ' Create multi-precision operands. /// Dim s1p As New mp_ptr(New UInteger() { &H1, &H1}) /// Dim str As New char_ptr("xxxxxxxxxxxxxxxxx") /// /// ' Convert s1p to hex string. /// Dim count As size_t = gmp_lib.mpn_get_str(str, 16, s1p, s1p.Size) /// /// ' Copy out str to bytes. /// Dim s As Byte() = New Byte(count - 1) { } /// Marshal.Copy(str.ToIntPtr(), s, 0, CInt(count)) /// /// ' Assert the non-ASCII, hex representation of s1p. /// Assert.IsTrue(s.SequenceEqual(New Byte() { 1, 0, 0, 0, 0, 0, 0, 0, 1})) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// gmp_lib.free(str) /// /// public static size_t mpn_get_str(/*unsigned*/ char_ptr str, int @base, mp_ptr s1p, mp_size_t s1n) { if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpn_get_str_x86(str.ToIntPtr(), @base, s1p.ToIntPtr(), s1n)); else return new size_t(SafeNativeMethods.__gmpn_get_str_x64(str.ToIntPtr(), @base, s1p.ToIntPtr(), s1n)); } /// /// Compute the hamming distance between {, } and {, }, which is the number of bit positions where the two operands have different bit values. /// /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// The hamming distance between {, } and {. /// mpn_lshift /// mpn_popcount /// mpn_rshift /// mpn_scan0 /// mpn_scan1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0xffffffff }); /// /// // Assert hamming distance. /// Assert.IsTrue(gmp_lib.mpn_hamdist(s1p, s2p, s1p.Size) == 31); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p, s2p); /// /// /// ' Create multi-precision operands. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &HffffffffUI}) /// /// ' Assert hamming distance. /// Assert.IsTrue(gmp_lib.mpn_hamdist(s1p, s2p, s1p.Size) = 31) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p, s2p) /// /// public static mp_bitcnt_t mpn_hamdist(/*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); return new mp_bitcnt_t(SafeNativeMethods.__gmpn_hamdist(s1p.ToIntPtr(), s2p.ToIntPtr(), n)); } /// /// Shift {, } left by bits, and write the result to {, }. /// /// The result integer. /// The operand integer. /// The number of limbs of . /// The number of bits ot shift. /// The bits shifted out at the left are returned in the least significant count bits of the return value (the rest of the return value is zero). /// /// /// must be in the range 1 to mp_bits_per_limb - 1. /// The regions {, } and {, } may overlap, /// provided . /// /// /// This function is written in assembly for most CPUs. /// /// /// mpn_hamdist /// mpn_popcount /// mpn_rshift /// mpn_scan0 /// mpn_scan1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffc, 0xffffffff }); /// /// // Set rp = sp << 1. /// mp_limb_t bits = gmp_lib.mpn_lshift(rp, sp, sp.Size, 1); /// /// // Assert result of operation. /// Assert.IsTrue(bits == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffcUI, &HffffffffUI}) /// /// ' Set rp = sp << 1. /// Dim bits As mp_limb_t = gmp_lib.mpn_lshift(rp, sp, sp.Size, 1) /// /// ' Assert result of operation. /// Assert.IsTrue(bits = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static mp_limb_t mpn_lshift(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n, uint count) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_lshift_x86(rp.ToIntPtr(), sp.ToIntPtr(), n, count)); else return new mp_limb_t(SafeNativeMethods.__gmpn_lshift_x64(rp.ToIntPtr(), sp.ToIntPtr(), n, count)); } /// /// Divide {, } by , and return the remainder. /// /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The remainder. /// /// /// can be zero. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xfffffffe, 0x0000ffff }); /// /// // Assert s1p mod 3 is 2. /// Assert.IsTrue(gmp_lib.mpn_mod_1(s1p, s1p.Size, 3) == 2); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &HfffffffeUI, &Hffff}) /// /// ' Assert s1p mod 3 is 2. /// Assert.IsTrue(gmp_lib.mpn_mod_1(s1p, s1p.Size, 3) = 2) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static mp_limb_t mpn_mod_1(/*const*/ mp_ptr s1p, mp_size_t s1n, mp_limb_t s2limb) { if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_mod_1_x86(s1p.ToIntPtr(), s1n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_mod_1_x64(s1p.ToIntPtr(), s1n, s2limb)); } /// /// Multiply {, } and {, }, and write the ( + )-limb result to . /// /// The result integer. /// The first operand integer. /// The number of limbs of . /// The first operand integer. /// The number of limbs of . /// Return the most significant limb of the result. /// /// /// The destination has to have space for + limbs, /// even if the product’s most significant limb is zero. /// No overlap is permitted between the destination and either source. /// /// /// This function requires that is greater than or equal to . /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000002 }); /// mp_ptr rp = new mp_ptr(new uint[3]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff, 0x00000001 }); /// /// // Set rp = s1 * s2. /// gmp_lib.mpn_mul(rp, s1p, s1p.Size, s2p, s2p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H2}) /// Dim rp As New mp_ptr(New UInteger(2) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI, &H1}) /// /// ' Set rp = s1 * s2. /// gmp_lib.mpn_mul(rp, s1p, s1p.Size, s2p, s2p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_mul(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t s1n, /*const*/ mp_ptr s2p, mp_size_t s2n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_mul_x86(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_mul_x64(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); } /// /// Multiply {, } by , and write the least significant limbs of the product to . /// /// The result integer. /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// Return the most significant limb of the product. /// /// /// {, } and {, } /// are allowed to overlap provided . /// /// /// 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 CPUs. /// /// /// Don’t call this function if is a power of 2; /// use mpn_lshift with a count equal to the logarithm of /// instead, for optimal speed. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1 * 2. /// mp_limb_t carry = gmp_lib.mpn_mul_1(rp, s1p, s1p.Size, 2); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1 * 2. /// Dim carry As mp_limb_t = gmp_lib.mpn_mul_1(rp, s1p, s1p.Size, 2) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static mp_limb_t mpn_mul_1(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n, mp_limb_t s2limb) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_mul_1_x86(rp.ToIntPtr(), s1p.ToIntPtr(), n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_mul_1_x64(rp.ToIntPtr(), s1p.ToIntPtr(), n, s2limb)); } /// /// Multiply {, } and {, }, and write the (2 * )-limb result to . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// /// /// The destination has to have space for 2 * limbs, even if the product’s /// most significant limb is zero. No overlap is permitted between the destination and either source. /// /// /// If the two input operands are the same, use mpn_sqr. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000002, 0x00000000 }); /// mp_ptr rp = new mp_ptr(new uint[4]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff, 0x00000001, 0x00000000 }); /// /// // Set rp = s1 * s2. /// gmp_lib.mpn_mul_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H2, &H0}) /// Dim rp As New mp_ptr(New UInteger(3) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI, &H1, &H0}) /// /// ' Set rp = s1 * s2. /// gmp_lib.mpn_mul_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_mul_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_mul_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Compute the square of {, } and write the (2 * )-limb result to . /// /// The result integer. /// The operand integer. /// The number of limbs of . /// /// /// The destination has to have space for 2 * limbs, even if the result’s /// most significant limb is zero. No overlap is permitted between the destination and the source. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[4]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x00000000, 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1^2. /// gmp_lib.mpn_sqr(rp, s1p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(3) { }) /// Dim result As New mp_ptr(New UInteger() { &H1, &H0, &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1^2. /// gmp_lib.mpn_sqr(rp, s1p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static void mpn_sqr(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); SafeNativeMethods.__gmpn_sqr(rp.ToIntPtr(), s1p.ToIntPtr(), n); } /// /// Perform the negation of {, }, and write the result to {, }. /// /// The result integer. /// The operand integer. /// The number of limbs of and . /// Return borrow, either 0 or 1. /// /// /// This is equivalent to calling mpn_sub_n with a -limb /// zero minuend and passing {, } as subtrahend. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x0000001, 0x00000000 }); /// /// // Set rp = -sp. /// mp_limb_t borrow = gmp_lib.mpn_neg(rp, sp, sp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H1, &H0}) /// /// ' Set rp = -sp. /// Dim borrow As mp_limb_t = gmp_lib.mpn_neg(rp, sp, sp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static mp_limb_t mpn_neg(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_neg_x86(rp.ToIntPtr(), sp.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_neg_x64(rp.ToIntPtr(), sp.ToIntPtr(), n)); } /// /// Perform the bitwise complement of {, }, and write the result to {, }. /// /// The result integer. /// The operand integer. /// The numbe rof limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xf0f0f0f0, 0xf0f0f0f0 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x0f0f0f0f, 0x0f0f0f0f }); /// /// // Set rp = not(sp). /// gmp_lib.mpn_com(rp, sp, sp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &Hf0f0f0f0UI, &Hf0f0f0f0UI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &Hf0f0f0f, &Hf0f0f0f}) /// /// ' Set rp = not(sp). /// gmp_lib.mpn_com(rp, sp, sp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static void mpn_com(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); SafeNativeMethods.__gmpn_com(rp.ToIntPtr(), sp.ToIntPtr(), n); } /// /// Return non-zero iff {, } is a perfect square. /// /// The operand integer. /// The numbe rof limbs of . /// Non-zero iff {, } is a perfect square. /// /// /// The most significant limb of the input {, } must be non-zero. /// /// /// mpn_cmp /// mpn_perfect_power_p /// mpn_zero_p /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// /// // Assert s1p is not a perfect square. /// Assert.IsTrue(gmp_lib.mpn_perfect_square_p(s1p, s1p.Size) == 0); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// /// ' Assert s1p is not a perfect square. /// Assert.IsTrue(gmp_lib.mpn_perfect_square_p(s1p, s1p.Size) = 0) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static int mpn_perfect_square_p(/*const*/ mp_ptr s1p, mp_size_t n) { if (s1p == null) throw new ArgumentNullException("s1p"); return SafeNativeMethods.__gmpn_perfect_square_p(s1p.ToIntPtr(), n); } /// /// Return non-zero iff {, } is a perfect power. /// /// The operand integer. /// The numbe rof limbs of . /// Non-zero iff {, } is a perfect power. /// mpn_cmp /// mpn_perfect_square_p /// mpn_zero_p /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xd4a51000, 0x000000e8 }); /// /// // Assert s1p is a perfect power. /// Assert.IsTrue(gmp_lib.mpn_perfect_power_p(s1p, s1p.Size) != 0); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &Hd4a51000UI, &He8}) /// /// ' Assert s1p is a perfect power. /// Assert.IsTrue(gmp_lib.mpn_perfect_power_p(s1p, s1p.Size) <> 0) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static int mpn_perfect_power_p(/*const*/ mp_ptr sp, mp_size_t n) { if (sp == null) throw new ArgumentNullException("sp"); return SafeNativeMethods.__gmpn_perfect_power_p(sp.ToIntPtr(), n); } /// /// Count the number of set bits in {, }. /// /// The operand integer. /// The number of limbs of . /// The number of set bits in {, }. /// mpn_hamdist /// mpn_lshift /// mpn_rshift /// mpn_scan0 /// mpn_scan1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0x0000001, 0x00000001 }); /// /// // Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_popcount(s1p, s1p.Size) == 2); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &H1, &H1}) /// /// ' Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_popcount(s1p, s1p.Size) = 2) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static mp_bitcnt_t mpn_popcount(/*const*/ mp_ptr s1p, mp_size_t n) { if (s1p == null) throw new ArgumentNullException("s1p"); return new mp_bitcnt_t(SafeNativeMethods.__gmpn_popcount(s1p.ToIntPtr(), n)); } ////#define mpn_pow_1 __MPN(pow_1) ////public static int /*mp_size_t*/ mpn_pow_1(IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, IntPtr /*mp_ptr*/) //{ // return SafeNativeMethods.__gmpn_pow_1(IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, IntPtr /*mp_ptr*/); //} /////* undocumented now, but retained here for upward compatibility */ ////#define mpn_preinv_mod_1 __MPN(preinv_mod_1) ////public static mp_limb_t mpn_preinv_mod_1(/*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, mp_limb_t) //{ // return SafeNativeMethods.__gmpn_preinv_mod_1(/*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, mp_limb_t); //} /// /// Generate a random number of length and store it at . /// /// The result integer. /// The number of limbs of . /// /// /// 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. /// /// /// mpn_random2 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr r1p = new mp_ptr(new uint[2]); /// /// // Generate random number. /// gmp_lib.mpn_random(r1p, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 1); /// /// // Release unmanaged memory. /// gmp_lib.free(r1p); /// /// /// ' Create multi-precision operand. /// Dim r1p As New mp_ptr(New UInteger(1) { }) /// /// ' Generate random number. /// gmp_lib.mpn_random(r1p, If(gmp_lib.mp_bytes_per_limb = 4, 2, 1)) /// /// ' Release unmanaged memory. /// gmp_lib.free(r1p) /// /// public static void mpn_random(mp_ptr r1p, mp_size_t r1n) { if (r1p == null) throw new ArgumentNullException("r1p"); SafeNativeMethods.__gmpn_random(r1p.ToIntPtr(), r1n); } /// /// Generate a random number of length and store it at . /// /// The result integer. /// The number of limbs of . /// /// /// 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. /// /// /// mpn_random /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr r1p = new mp_ptr(new uint[2]); /// /// // Generate random number. /// gmp_lib.mpn_random2(r1p, gmp_lib.mp_bytes_per_limb == 4 ? 2 : 1); /// /// // Release unmanaged memory. /// gmp_lib.free(r1p); /// /// /// ' Create multi-precision operand. /// Dim r1p As New mp_ptr(New UInteger(1) { }) /// /// ' Generate random number. /// gmp_lib.mpn_random2(r1p, If(gmp_lib.mp_bytes_per_limb = 4, 2, 1)) /// /// ' Release unmanaged memory. /// gmp_lib.free(r1p) /// /// public static void mpn_random2(mp_ptr r1p, mp_size_t r1n) { if (r1p == null) throw new ArgumentNullException("r1p"); SafeNativeMethods.__gmpn_random2(r1p.ToIntPtr(), r1n); } /// /// Shift {, } right by bits, and write the result to {, }. /// /// The result integer. /// The operand integer. /// The number of limbs of and . /// /// The bits shifted out at the right are returned in the most significant bits of the return value (the rest of the return value is zero). /// /// /// must be in the range 1 to mp_bits_per_limb - 1. /// The regions {, } and {, } /// may overlap, provided . /// /// /// This function is written in assembly for most CPUs. /// /// /// mpn_hamdist /// mpn_lshift /// mpn_popcount /// mpn_scan0 /// mpn_scan1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xffffffff, 0x7fffffff }); /// /// // Set rp = sp >> 1. /// mp_limb_t bits = gmp_lib.mpn_rshift(rp, sp, sp.Size, 1); /// /// // Assert result of operation. /// Assert.IsTrue(bits == (gmp_lib.mp_bytes_per_limb == 4 ? 0x80000000 : 0x8000000000000000)); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HffffffffUI, &H7fffffff}) /// /// ' Set rp = sp >> 1. /// Dim bits As mp_limb_t = gmp_lib.mpn_rshift(rp, sp, sp.Size, 1) /// /// ' Assert result of operation. /// Assert.IsTrue(bits = (If(gmp_lib.mp_bytes_per_limb = 4, &H80000000UI, &H8000000000000000UL))) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static mp_limb_t mpn_rshift(mp_ptr rp, /*const*/ mp_ptr sp, mp_size_t n, uint count) { if (rp == null) throw new ArgumentNullException("rp"); if (sp == null) throw new ArgumentNullException("sp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_rshift_x86(rp.ToIntPtr(), sp.ToIntPtr(), n, count)); else return new mp_limb_t(SafeNativeMethods.__gmpn_rshift_x64(rp.ToIntPtr(), sp.ToIntPtr(), n, count)); } /// /// Scan from bit position for the next clear bit. /// /// The operand integer. /// The index of the starting bit. /// The index of the next clear bit. /// /// /// It is required that there be a clear bit within the area at /// at or beyond bit position , so that the function has something to return. /// /// /// mpn_hamdist /// mpn_lshift /// mpn_popcount /// mpn_rshift /// mpn_scan1 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0x0000001, 0x00000001 }); /// /// // Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_scan0(s1p, 0) == 1); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &H1, &H1}) /// /// ' Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_scan0(s1p, 0) = 1) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static mp_bitcnt_t mpn_scan0(/*const*/ mp_ptr s1p, mp_bitcnt_t bit) { if (s1p == null) throw new ArgumentNullException("s1p"); return new mp_bitcnt_t(SafeNativeMethods.__gmpn_scan0(s1p.ToIntPtr(), bit)); } /// /// Scan from bit position for the next set bit. /// /// The operand integer. /// The index of the starting bit. /// The index of the next set bit. /// /// /// It is required that there be a set bit within the area at /// at or beyond bit position , so that the function has something to return. /// /// /// mpn_hamdist /// mpn_lshift /// mpn_popcount /// mpn_rshift /// mpn_scan0 /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand. /// mp_ptr s1p = new mp_ptr(new uint[] { 0x0000001, 0x00000001 }); /// /// // Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_scan1(s1p, 1) == 32); /// /// // Release unmanaged memory. /// gmp_lib.free(s1p); /// /// /// ' Create multi-precision operand. /// Dim s1p As New mp_ptr(New UInteger() { &H1, &H1}) /// /// ' Assert result of operation. /// Assert.IsTrue(gmp_lib.mpn_scan1(s1p, 1) = 32) /// /// ' Release unmanaged memory. /// gmp_lib.free(s1p) /// /// public static mp_bitcnt_t mpn_scan1(/*const*/ mp_ptr s1p, mp_bitcnt_t bit) { if (s1p == null) throw new ArgumentNullException("s1p"); return new mp_bitcnt_t(SafeNativeMethods.__gmpn_scan1(s1p.ToIntPtr(), bit)); } /// /// Convert bytes {, } in the given to limbs at . /// /// The result integer. /// The operand string. /// The length of . /// /// The number of limbs of . /// /// /// [0] is the most significant input byte and /// [ - 1] is the least significant input byte. /// Each byte should be a value in the range 0 to - 1, /// not an ASCII character. base can vary from 2 to 256. /// /// /// The converted value is {, rn} where rn is the return value. /// If the most significant input byte [0] is non-zero, /// then [rn - 1] will be non-zero, /// else [rn - 1] and some number of subsequent limbs may be zero. /// /// /// The area at has to have space for the largest possible number with /// digits in the chosen , plus one extra limb. /// /// /// The input must have at least one byte, and no overlap is permitted /// between {, } and the result at . /// /// /// mpn_get_str /// mpn_sizeinbase /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands. /// mp_ptr rp = new mp_ptr(new uint[2]); /// byte[] s = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }; /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x00000001 }); /// char_ptr str = new char_ptr("xxxxxxxxxxxxxxxxx"); /// Marshal.Copy(s, 0, str.ToIntPtr(), 9); /// /// // Convert rp from str in hex base. /// mp_size_t count = gmp_lib.mpn_set_str(rp, str, 9, 16); /// /// // Assert the non-ASCII, hex representation of s1p. /// Assert.IsTrue(count == rp.Size); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp); /// gmp_lib.free(str); /// /// /// ' Create multi-precision operands. /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim s As Byte() = New Byte() { 1, 0, 0, 0, 0, 0, 0, 0, 1} /// Dim result As New mp_ptr(New UInteger() { &H1, &H1}) /// Dim str As New char_ptr("xxxxxxxxxxxxxxxxx") /// Marshal.Copy(s, 0, str.ToIntPtr(), 9) /// /// ' Convert rp from str in hex base. /// Dim count As mp_size_t = gmp_lib.mpn_set_str(rp, str, 9, 16) /// /// ' Assert the non-ASCII, hex representation of s1p. /// Assert.IsTrue(count = rp.Size) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp) /// gmp_lib.free(str) /// /// public static mp_size_t mpn_set_str(mp_ptr rp, /*const unsigned*/ char_ptr str, size_t strsize, int @base) { if (rp == null) throw new ArgumentNullException("rp"); if (IntPtr.Size == 4) return new mp_size_t(SafeNativeMethods.__gmpn_set_str_x86(rp.ToIntPtr(), str.ToIntPtr(), (uint)strsize, @base)); else return new mp_size_t(SafeNativeMethods.__gmpn_set_str_x64(rp.ToIntPtr(), str.ToIntPtr(), strsize, @base)); } /// /// Return the size of {, } measured in number of digits in the given . /// /// The operand integer. /// The number of limbs of . /// The base. /// The size of {, } measured in number of digits in the given . /// /// /// can vary from 2 to 62. /// Requires > 0 and [ - 1] > 0. /// The result will be either exact or 1 too big. /// If base is a power of 2, the result is always exact. /// /// /// mpn_get_str /// mpn_set_str /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr xp = new mp_ptr(new uint[] { 0x00000001, 0x00000001 }); /// /// // Assert that the number of bits required is 33. /// Assert.IsTrue(gmp_lib.mpn_sizeinbase(xp, xp.Size, 2) == 33); /// /// // Release unmanaged memory. /// gmp_lib.free(xp); /// /// /// ' Create multi-precision operands, and expected result. /// Dim xp As New mp_ptr(New UInteger() { &H1, &H1}) /// /// ' Assert that the number of bits required is 33. /// Assert.IsTrue(gmp_lib.mpn_sizeinbase(xp, xp.Size, 2) = 33) /// /// ' Release unmanaged memory. /// gmp_lib.free(xp) /// /// public static size_t mpn_sizeinbase(/*const*/ mp_ptr xp, mp_size_t n, int @base) { if (xp == null) throw new ArgumentNullException("xp"); if (IntPtr.Size == 4) return new size_t(SafeNativeMethods.__gmpn_sizeinbase_x86(xp.ToIntPtr(), n, @base)); else return new size_t(SafeNativeMethods.__gmpn_sizeinbase_x64(xp.ToIntPtr(), n, @base)); } /// /// Compute the square root of {, } and put the result at {, ceil( / 2)} and the remainder at {, retval}. /// /// The first result integer. /// The second result integer. /// The operand integwer. /// The number of limbs of . /// The number of limbs of . /// /// /// needs space for limbs, /// but the return value indicates how many are produced. /// /// /// The most significant limb of {, } must be non-zero. /// The areas {, ceil( / 2)} and /// {, } must be completely separate. /// The areas {, } and {, } /// must be either identical or completely separate. /// /// /// If the remainder is not wanted then can be NULL, and in this case the return value /// is zero or non-zero according to whether the remainder would have been zero or non-zero. /// /// /// A return value of zero indicates a perfect square. See also mpn_perfect_square_p. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0x00000001, 0x00000001 }); /// mp_ptr r1p = new mp_ptr(new uint[sp.Size * (gmp_lib.mp_bytes_per_limb / 4)]); /// mp_ptr r2p = new mp_ptr(new uint[sp.Size * (gmp_lib.mp_bytes_per_limb / 4)]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00010000, 0x00000000 }); /// mp_ptr remainder = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// /// // Set r1p = trunc(sqrt(sp)), r2p = sp - r1p^2 /// mp_size_t r2n = gmp_lib.mpn_sqrtrem(r1p, r2p, sp, sp.Size); /// /// // Assert result. /// Assert.IsTrue(r2n == 1); /// Assert.IsTrue(r1p.SequenceEqual(result)); /// Assert.IsTrue(r2p.SequenceEqual(remainder)); /// /// // Release unmanaged memory. /// gmp_lib.free(sp, r1p, r2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &H1, &H1}) /// Dim r1p As New mp_ptr(New UInteger(sp.Size * (gmp_lib.mp_bytes_per_limb / 4) - 1) {}) /// Dim r2p As New mp_ptr(New UInteger(sp.Size * (gmp_lib.mp_bytes_per_limb / 4) - 1) {}) /// Dim result As New mp_ptr(New UInteger() { &H10000, &H0}) /// Dim remainder As New mp_ptr(New UInteger() { &H1, &H0}) /// /// ' Set r1p = trunc(sqrt(sp)), r2p = sp - r1p^2 /// Dim r2n As mp_size_t = gmp_lib.mpn_sqrtrem(r1p, r2p, sp, sp.Size) /// /// ' Assert result. /// Assert.IsTrue(r2n = 1) /// Assert.IsTrue(r1p.SequenceEqual(result)) /// Assert.IsTrue(r2p.SequenceEqual(remainder)) /// /// ' Release unmanaged memory. /// gmp_lib.free(sp, r1p, r2p, result) /// /// public static mp_size_t mpn_sqrtrem(mp_ptr r1p, mp_ptr r2p, /*const*/ mp_ptr sp, mp_size_t n) { if (r1p == null) throw new ArgumentNullException("r1p"); if (r2p == null) throw new ArgumentNullException("r2p"); if (sp == null) throw new ArgumentNullException("sp"); return new mp_size_t(SafeNativeMethods.__gmpn_sqrtrem(r1p.ToIntPtr(), r2p.ToIntPtr(), sp.ToIntPtr(), n)); } /// /// Subtract {, } from {, }, and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// 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 CPUs. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1 - s2. /// mp_limb_t borrow = gmp_lib.mpn_sub(rp, s1p, s1p.Size, s2p, s2p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1 - s2. /// Dim borrow As mp_limb_t = gmp_lib.mpn_sub(rp, s1p, s1p.Size, s2p, s2p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_sub(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t s1n, /*const*/ mp_ptr s2p, mp_size_t s2n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sub_x86(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_sub_x64(rp.ToIntPtr(), s1p.ToIntPtr(), s1n, s2p.ToIntPtr(), s2n)); } /// /// Subtract from {, }, and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The numbe rof limbs of . /// The second operand integer. /// Return borrow, either 0 or 1. /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1 - 1. /// mp_limb_t borrow = gmp_lib.mpn_sub_1(rp, s1p, s1p.Size, 1); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1 - 1. /// Dim borrow As mp_limb_t = gmp_lib.mpn_sub_1(rp, s1p, s1p.Size, 1) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static mp_limb_t mpn_sub_1(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n, mp_limb_t s2limb) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sub_1_x86(rp.ToIntPtr(), s1p.ToIntPtr(), n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_sub_1_x64(rp.ToIntPtr(), s1p.ToIntPtr(), n, s2limb)); } /// /// Subtract {, } from {, }, and write the least significant limbs of the result to . /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The numbe rof limbs of and . /// Return borrow, either 0 or 1. /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1 - s2. /// mp_limb_t borrow = gmp_lib.mpn_sub_n(rp, s1p, s2p, rp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1 - s2. /// Dim borrow As mp_limb_t = gmp_lib.mpn_sub_n(rp, s1p, s2p, rp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_sub_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sub_n_x86(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_sub_n_x64(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); } /// /// Multiply {, } and , and subtract the least significant limbs of the product from {, } and write the result to . /// /// The result integer. /// The first operand integer. /// The number of limbs of /// The second operand integer. /// Return the most significant limb of the product, plus borrow-out from the subtraction. /// /// /// {, } and {, } /// are allowed to overlap provided . /// /// /// 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 CPUs. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_sqr /// mpn_sqrtrem /// mpn_tdiv_qr /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr rp = new mp_ptr(new uint[] { 0x00000002, 0x00000000 }); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000004, 0x00000000 }); /// /// // Set rp -= s1 * 2. /// mp_limb_t borrow = gmp_lib.mpn_submul_1(rp, s1p, s1p.Size, 2); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0x02); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim rp As New mp_ptr(New UInteger() { &H2, &H0}) /// Dim result As New mp_ptr(New UInteger() { &H4, &H0}) /// /// ' Set rp -= s1 * 2. /// Dim borrow As mp_limb_t = gmp_lib.mpn_submul_1(rp, s1p, s1p.Size, 2) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = &H2) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, result) /// /// public static mp_limb_t mpn_submul_1(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n, mp_limb_t s2limb) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_submul_1_x86(rp.ToIntPtr(), s1p.ToIntPtr(), n, (uint)s2limb)); else return new mp_limb_t(SafeNativeMethods.__gmpn_submul_1_x64(rp.ToIntPtr(), s1p.ToIntPtr(), n, s2limb)); } /// /// Divide {, } by {, } and put the quotient at {, - + 1} and the remainder at {, }. /// /// The result quotient integer. /// The result remainder integer. /// Must be 0. /// The numerator operand integer. /// The number of limbs of . /// The denominator operand integer. /// The number of limbs of . /// /// /// The quotient is rounded towards 0. /// /// /// No overlap is permitted between arguments, except that might /// equal . /// The dividend size must be greater than or equal to divisor /// size . /// The most significant limb of the divisor must be non-zero. /// The operand must be zero. /// /// /// mpn_add /// mpn_add_1 /// mpn_add_n /// mpn_addmul_1 /// mpn_divexact_1 /// mpn_divexact_by3 /// mpn_divexact_by3c /// mpn_divmod_1 /// mpn_divrem_1 /// mpn_mod_1 /// mpn_mul /// mpn_mul_1 /// mpn_mul_n /// mpn_neg /// mpn_sub /// mpn_sub_1 /// mpn_sub_n /// mpn_submul_1 /// mpn_sqr /// mpn_sqrtrem /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr np = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr dp = new mp_ptr(new uint[] { 0x00000013 }); /// mp_ptr qp = new mp_ptr(new uint[np.Size - dp.Size + 1]); /// mp_ptr rp = new mp_ptr(new uint[dp.Size]); /// mp_ptr quotient = new mp_ptr(new uint[] { 0x435e50d7, 0x00000d79 }); /// mp_ptr remainder = new mp_ptr(new uint[] { 0x0000000a }); /// /// // Set rp = np / dp. /// gmp_lib.mpn_tdiv_qr(qp, rp, 0, np, np.Size, dp, dp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(qp.SequenceEqual(quotient)); /// Assert.IsTrue(rp.SequenceEqual(remainder)); /// /// // Release unmanaged memory. /// gmp_lib.free(qp, rp, np, dp, quotient, remainder); /// /// /// ' Create multi-precision operands, and expected result. /// Dim np As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim dp As New mp_ptr(New UInteger() { &H13}) /// Dim qp As New mp_ptr(New UInteger(np.Size - dp.Size) { }) /// Dim rp As New mp_ptr(New UInteger(dp.Size - 1) { }) /// Dim quotient As New mp_ptr(New UInteger() { &H435e50d7, &Hd79}) /// Dim remainder As New mp_ptr(New UInteger() { &Ha}) /// /// ' Set rp = np / dp. /// gmp_lib.mpn_tdiv_qr(qp, rp, 0, np, np.Size, dp, dp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(qp.SequenceEqual(quotient)) /// Assert.IsTrue(rp.SequenceEqual(remainder)) /// /// ' Release unmanaged memory. /// gmp_lib.free(qp, rp, np, dp, quotient, remainder) /// /// public static void mpn_tdiv_qr(mp_ptr qp, mp_ptr rp, mp_size_t qxn, /*const*/ mp_ptr np, mp_size_t nn, /*const*/ mp_ptr dp, mp_size_t dn) { if (qp == null) throw new ArgumentNullException("qp"); if (rp == null) throw new ArgumentNullException("rp"); if (np == null) throw new ArgumentNullException("np"); if (dp == null) throw new ArgumentNullException("dp"); SafeNativeMethods.__gmpn_tdiv_qr(qp.ToIntPtr(), rp.ToIntPtr(), qxn, np.ToIntPtr(), nn, dp.ToIntPtr(), dn); } /// /// Perform the bitwise logical and of {, } and {, }, and write the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// /// // Set rp = s1 and s2. /// gmp_lib.mpn_and_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H1, &H12345678}) /// /// ' Set rp = s1 and s2. /// gmp_lib.mpn_and_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_and_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_and_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical and of {, } and the bitwise complement of {, }, and write the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xedcba987 }); /// /// // Set rp = s1 and not s2. /// gmp_lib.mpn_andn_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &Hedcba987UI}) /// /// ' Set rp = s1 and not s2. /// gmp_lib.mpn_andn_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_andn_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_andn_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical and of {, } and {, }, and write the bitwise complement of the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// /// // Set rp = not(s1 and s2). /// gmp_lib.mpn_and_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H1, &H12345678}) /// /// ' Set rp = not(s1 and s2). /// gmp_lib.mpn_and_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_nand_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_nand_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical inclusive or of {, } and {, }, and write the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// /// // Set rp = s1 or s2. /// gmp_lib.mpn_ior_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// /// ' Set rp = s1 or s2. /// gmp_lib.mpn_ior_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_ior_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_ior_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical inclusive or of {, } and the bitwise complement of {, }, and write the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// /// // Set rp = s1 or not s2. /// gmp_lib.mpn_iorn_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// /// ' Set rp = s1 or not s2. /// gmp_lib.mpn_iorn_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_iorn_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_iorn_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical inclusive or of {, } and {, }, and write the bitwise complement of the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_xor_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = not (s1 or s2). /// gmp_lib.mpn_nior_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = not (s1 or s2). /// gmp_lib.mpn_nior_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_nior_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_nior_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical exclusive or of {, } and {, }, and write the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xnor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xedcba987 }); /// /// // Set rp = s1 xor s2. /// gmp_lib.mpn_xor_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &Hedcba987UI}) /// /// ' Set rp = s1 xor s2. /// gmp_lib.mpn_xor_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_xor_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_xor_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Perform the bitwise logical exclusive or of {, } and {, }, and write the bitwise complement of the result to {, }. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// mpn_and_n /// mpn_andn_n /// mpn_com /// mpn_ior_n /// mpn_iorn_n /// mpn_nand_n /// mpn_nior_n /// mpn_xor_n /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x12345678 }); /// /// // Set rp = not(s1 xor s2). /// gmp_lib.mpn_xnor_n(rp, s1p, s2p, s1p.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H12345678}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H1, &H12345678}) /// /// ' Set rp = not(s1 xor s2). /// gmp_lib.mpn_xnor_n(rp, s1p, s2p, s1p.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static void mpn_xnor_n(mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); SafeNativeMethods.__gmpn_xnor_n(rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n); } /// /// Copy from {, } to {, }, increasingly. /// /// The result integer. /// The operand integer. /// The number of limbs of . /// mpn_copyd /// mpn_zero /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xf0f0f0f0, 0xf0f0f0f0 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xf0f0f0f0, 0xf0f0f0f0 }); /// /// // Set rp = sp. /// gmp_lib.mpn_copyi(rp, sp, sp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &Hf0f0f0f0UI, &Hf0f0f0f0UI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &Hf0f0f0f0UI, &Hf0f0f0f0UI}) /// /// ' Set rp = sp. /// gmp_lib.mpn_copyi(rp, sp, sp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static void mpn_copyi(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); SafeNativeMethods.__gmpn_copyi(rp.ToIntPtr(), s1p.ToIntPtr(), n); } /// /// Copy from {, } to {, }, decreasingly. /// /// The result integer. /// The operand integer. /// The number of limbs of . /// mpn_copyi /// mpn_zero /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr sp = new mp_ptr(new uint[] { 0xf0f0f0f0, 0xf0f0f0f0 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xf0f0f0f0, 0xf0f0f0f0 }); /// /// // Set rp = sp. /// gmp_lib.mpn_copyd(rp, sp, sp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, sp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim sp As New mp_ptr(New UInteger() { &Hf0f0f0f0UI, &Hf0f0f0f0UI}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &Hf0f0f0f0UI, &Hf0f0f0f0UI}) /// /// ' Set rp = sp. /// gmp_lib.mpn_copyd(rp, sp, sp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, sp, result) /// /// public static void mpn_copyd(mp_ptr rp, /*const*/ mp_ptr s1p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); SafeNativeMethods.__gmpn_copyd(rp.ToIntPtr(), s1p.ToIntPtr(), n); } /// /// Zero {, }. /// /// The result integer. /// The number of limbs of . /// mpn_copyd /// mpn_copyi /// Low-level Functions /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operand, and expected result. /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = sp. /// gmp_lib.mpn_zero(rp, rp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, result); /// /// /// ' Create multi-precision operand, and expected result. /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = sp. /// gmp_lib.mpn_zero(rp, rp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, result) /// /// public static void mpn_zero(mp_ptr rp, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); SafeNativeMethods.__gmpn_zero(rp.ToIntPtr(), n); } /// /// If is non-zero, it produces the same result as a regular mpn_add_n, and if is zero, it copies {, } to the result area and returns zero. /// /// Conditonal value: non-zero for true, zero for false. /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// If is non-zero, return carry, either 0 or 1, and if is zero, return 0. /// /// /// This function does conditional addition. /// If is non-zero, it produces the same result as a regular mpn_add_n, /// and if is zero, it copies {, } to the result area and returns zero. /// The functions is designed to have timing and memory access patterns depending only /// on size and location of the data areas, but independent of the condition . /// Like for mpn_add_n, on most machines, the timing will also be independent /// of the actual limb values. /// /// /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// /// // Set rp = s1 + s2. /// mp_limb_t carry = gmp_lib.mpn_cnd_add_n(1, rp, s1p, s2p, rp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// /// ' Set rp = s1 + s2. /// Dim carry As mp_limb_t = gmp_lib.mpn_cnd_add_n(1, rp, s1p, s2p, rp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_cnd_add_n(mp_limb_t cnd, mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_cnd_add_n_x86((uint)cnd, rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_cnd_add_n_x64(cnd, rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); } /// /// If is non-zero, it produces the same result as a regular mpn_sub_n, and if is zero, it copies {, } to the result area and returns zero. /// /// Conditonal value: non-zero for true, zero for false. /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// If is non-zero, return borrow, either 0 or 1, and if is zero, return 0. /// /// /// This function does conditional addition. /// If is non-zero, it produces the same result as a regular mpn_sub_n, /// and if is zero, it copies {, } to the result area and returns zero. /// The functions is designed to have timing and memory access patterns depending only /// on size and location of the data areas, but independent of the condition . /// Like for mpn_sub_n, on most machines, the timing will also be independent /// of the actual limb values. /// /// /// mpn_cnd_add_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr s1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr s2p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr rp = new mp_ptr(new uint[2]); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// /// // Set rp = s1 - s2. /// mp_limb_t borrow = gmp_lib.mpn_cnd_sub_n(1, rp, s1p, s2p, rp.Size); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim s1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim s2p As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim rp As New mp_ptr(New UInteger(1) { }) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// /// ' Set rp = s1 - s2. /// Dim borrow As mp_limb_t = gmp_lib.mpn_cnd_sub_n(1, rp, s1p, s2p, rp.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, s1p, s2p, result) /// /// public static mp_limb_t mpn_cnd_sub_n(mp_limb_t cnd, mp_ptr rp, /*const*/ mp_ptr s1p, /*const*/ mp_ptr s2p, mp_size_t n) { if (rp == null) throw new ArgumentNullException("rp"); if (s1p == null) throw new ArgumentNullException("s1p"); if (s2p == null) throw new ArgumentNullException("s2p"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_cnd_sub_n_x86((uint)cnd, rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); else return new mp_limb_t(SafeNativeMethods.__gmpn_cnd_sub_n_x64(cnd, rp.ToIntPtr(), s1p.ToIntPtr(), s2p.ToIntPtr(), n)); } /// /// Set R to A + b, where R = {, }, A = {, }, and is a single limb. /// /// The result integer. /// The first operand integer. /// The number of limbs of and . /// The second operand integer. /// The scratch operand integer. /// Returns carry, either 0 or 1. /// /// /// This function takes O(N) time, unlike the leaky functions mpn_add_1 which is O(1) on average. /// It requires scratch space of mpn_sec_add_1_itch(n) limbs, to be passed in the parameter. /// The scratch space requirements are guaranteed to be at most limbs, and increase monotonously in the operand size. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1_itch /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); /// mp_ptr rp = new mp_ptr(result.Size); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_add_1_itch(ap.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = ap + 1. /// mp_limb_t carry = gmp_lib.mpn_sec_add_1(rp, ap, ap.Size, 1, tp); /// /// // Assert result of operation. /// Assert.IsTrue(carry == 1); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim ap As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim result As New mp_ptr(New UInteger() { &H0, &H0}) /// Dim rp As New mp_ptr(result.Size) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_add_1_itch(ap.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set rp = ap + 1. /// Dim carry As mp_limb_t = gmp_lib.mpn_sec_add_1(rp, ap, ap.Size, 1, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(carry = 1) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result) /// /// public static mp_limb_t mpn_sec_add_1(mp_ptr rp, /*const*/ mp_ptr ap, mp_size_t n, mp_limb_t b, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (ap == null) throw new ArgumentNullException("ap"); if (tp == null) throw new ArgumentNullException("tp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sec_add_1_x86(rp.ToIntPtr(), ap.ToIntPtr(), n, (uint)b, tp.ToIntPtr())); else return new mp_limb_t(SafeNativeMethods.__gmpn_sec_add_1_x64(rp.ToIntPtr(), ap.ToIntPtr(), n, b, tp.ToIntPtr())); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_add_1. /// /// The number of limbs of the mpn_sec_add_1 operand. /// The scratch space in number of limbs required by the function mpn_sec_add_1. /// mpn_sec_add_1 /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_add_1_itch(mp_size_t n) { return new mp_size_t(SafeNativeMethods.__gmpn_sec_add_1_itch(n)); } /// /// Set R to A - b, where R = {, }, A = {, }, and is a single limb. /// /// The result integer. /// The first operand integer. /// The number of limbs of and . /// The second operand integer. /// The scratch operand integer. /// Returns borrow, either 0 or 1. /// /// /// This function takes O(N) time, unlike the leaky functions mpn_sub_1 which is O(1) on average. /// It requires scratch space of mpn_sec_sub_1_itch(n) limbs, to be passed in the parameter. /// The scratch space requirements are guaranteed to be at most limbs, and increase monotonously in the operand size. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1_itch /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff }); /// mp_ptr rp = new mp_ptr(result.Size); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_sub_1_itch(ap.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = ap - 1. /// mp_limb_t borrow = gmp_lib.mpn_sec_sub_1(rp, ap, ap.Size, 1, tp); /// /// // Assert result of operation. /// Assert.IsTrue(borrow == 0); /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim ap As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI}) /// Dim rp As New mp_ptr(result.Size) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_sub_1_itch(ap.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set rp = ap - 1. /// Dim borrow As mp_limb_t = gmp_lib.mpn_sec_sub_1(rp, ap, ap.Size, 1, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(borrow = 0) /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result) /// /// public static mp_limb_t mpn_sec_sub_1(mp_ptr rp, /*const*/ mp_ptr ap, mp_size_t n, mp_limb_t b, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (ap == null) throw new ArgumentNullException("ap"); if (tp == null) throw new ArgumentNullException("tp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sec_sub_1_x86(rp.ToIntPtr(), ap.ToIntPtr(), n, (uint)b, tp.ToIntPtr())); else return new mp_limb_t(SafeNativeMethods.__gmpn_sec_sub_1_x64(rp.ToIntPtr(), ap.ToIntPtr(), n, b, tp.ToIntPtr())); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_sub_1. /// /// The number of limbs of the mpn_sec_sub_1 operand. /// The scratch space in number of limbs required by the function mpn_sec_sub_1. /// mpn_sec_sub_1 /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_sub_1_itch(mp_size_t n) { return SafeNativeMethods.__gmpn_sec_sub_1_itch(n); } /// /// If is non-zero, swaps the contents of the areas {, } and {, }. Otherwise, the areas are left unmodified. /// /// Conditonal value: non-zero for true, zero for false. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// /// /// Implemented using logical operations on the limbs, with the same memory accesses independent of the value of . /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr bp = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr a1p = new mp_ptr(new uint[] { 0x00000001, 0x00000000 }); /// mp_ptr b1p = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// /// // Exchange ab and bp. /// gmp_lib.mpn_cnd_swap(1, ap, bp, ap.Size); /// /// // Assert result of operation. /// Assert.IsTrue(ap.SequenceEqual(a1p)); /// Assert.IsTrue(bp.SequenceEqual(b1p)); /// /// // Release unmanaged memory. /// gmp_lib.free(ap, bp, a1p, b1p); /// /// /// ' Create multi-precision operands, and expected result. /// Dim ap As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim bp As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim a1p As New mp_ptr(New UInteger() { &H1, &H0}) /// Dim b1p As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// /// ' Exchange ab and bp. /// gmp_lib.mpn_cnd_swap(1, ap, bp, ap.Size) /// /// ' Assert result of operation. /// Assert.IsTrue(ap.SequenceEqual(a1p)) /// Assert.IsTrue(bp.SequenceEqual(b1p)) /// /// ' Release unmanaged memory. /// gmp_lib.free(ap, bp, a1p, b1p) /// /// public static void mpn_cnd_swap(mp_limb_t cnd, /*volatile*/ mp_ptr /*mp_limb_t**/ ap, /*volatile*/ mp_ptr /*mp_limb_t**/ bp, mp_size_t n) { if (ap == null) throw new ArgumentNullException("ap"); if (bp == null) throw new ArgumentNullException("bp"); if (IntPtr.Size == 4) SafeNativeMethods.__gmpn_cnd_swap_x86((uint)cnd, ap.ToIntPtr(), bp.ToIntPtr(), n); else SafeNativeMethods.__gmpn_cnd_swap_x64(cnd, ap.ToIntPtr(), bp.ToIntPtr(), n); } /// /// Set R to A * B, where A = {, }, B = {, }, and R = {, + }. /// /// The result integer. /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The scratch operand integer. /// /// /// It is required that ≥ bn > 0. /// /// /// No overlapping between R and the input operands is allowed. /// For A = B, use mpn_sec_sqr for optimal performance. /// /// /// This function requires scratch space of mpn_sec_mul_itch(, ) /// limbs to be passed in the tp parameter. The scratch space requirements are guaranteed to increase monotonously in the operand sizes. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul_itch /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr bp = new mp_ptr(new uint[] { 0x00000002 }); /// mp_ptr result = new mp_ptr(new uint[] { 0xfffffffe, 0xffffffff, 0x00000001 }); /// mp_ptr rp = new mp_ptr(ap.Size + bp.Size); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_mul_itch(ap.Size, bp.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = ap * bp. /// gmp_lib.mpn_sec_mul(rp, ap, ap.Size, bp, bp.Size, tp); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, ap, bp, tp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim ap As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim bp As New mp_ptr(New UInteger() { &H2}) /// Dim result As New mp_ptr(New UInteger() { &HfffffffeUI, &HffffffffUI, &H1}) /// Dim rp As New mp_ptr(ap.Size + bp.Size) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_mul_itch(ap.Size, bp.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set rp = ap * bp. /// gmp_lib.mpn_sec_mul(rp, ap, ap.Size, bp, bp.Size, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, ap, bp, tp, result) /// /// public static void mpn_sec_mul(mp_ptr rp, /*const*/ mp_ptr ap, mp_size_t an, /*const*/ mp_ptr bp, mp_size_t bn, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (ap == null) throw new ArgumentNullException("ap"); if (bp == null) throw new ArgumentNullException("bp"); if (tp == null) throw new ArgumentNullException("tp"); SafeNativeMethods.__gmpn_sec_mul(rp.ToIntPtr(), ap.ToIntPtr(), an, bp.ToIntPtr(), bn, tp.ToIntPtr()); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_mul. /// /// The number of limbs of the mpn_sec_mul first operand. /// The number of limbs of the mpn_sec_mul second operand. /// The scratch space in number of limbs required by the function mpn_sec_mul. /// mpn_sec_mul /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_mul_itch(mp_size_t an, mp_size_t bn) { return SafeNativeMethods.__gmpn_sec_mul_itch(an, bn); } /// /// Set R to A^2, where A = {, }, and R = {, 2 * }. /// /// The result operand. /// The operand integer. /// The number of limbs of . /// The scratch operand integer. /// /// /// It is required that > 0. /// /// /// No overlapping between R and the input operands is allowed. /// /// /// This function requires scratch space of mpn_sec_sqr_itch() /// limbs to be passed in the parameter. /// The scratch space requirements are guaranteed to increase monotonously in the operand size. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr_itch /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001, 0x00000000, 0xfffffffe, 0xffffffff }); /// mp_ptr rp = new mp_ptr(2 * ap.Size); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_sqr_itch(ap.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = s1^2. /// gmp_lib.mpn_sec_sqr(rp, ap, ap.Size, tp); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim ap As New mp_ptr(New UInteger() { &HffffffffUI, &HffffffffUI}) /// Dim result As New mp_ptr(New UInteger() { &H1, &H0, &HfffffffeUI, &HffffffffUI}) /// Dim rp As New mp_ptr(2 * ap.Size) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_sqr_itch(ap.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set rp = s1^2. /// gmp_lib.mpn_sec_sqr(rp, ap, ap.Size, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, ap, tp, result) /// /// public static void mpn_sec_sqr(mp_ptr rp, /*const*/ mp_ptr ap, mp_size_t an, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (ap == null) throw new ArgumentNullException("ap"); if (tp == null) throw new ArgumentNullException("tp"); SafeNativeMethods.__gmpn_sec_sqr(rp.ToIntPtr(), ap.ToIntPtr(), an, tp.ToIntPtr()); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_sqr. /// /// The number of limbs of the mpn_sec_sqr operand. /// The scratch space in number of limbs required by the function mpn_sec_sqr. /// mpn_sec_sqr /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_sqr_itch(mp_size_t an) { return SafeNativeMethods.__gmpn_sec_sqr_itch(an); } /// /// Set R to (B^E) modulo M, where R = {, }, M = {, }, and E = {, ceil( / mp_bits_per_limb)}. /// /// The result operand. /// The first operand integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The third operand integer. /// The number of limbs of . /// The scratch operand integer. /// /// /// It is required that B > 0, that M > 0 is odd, and that E < 2^. /// /// /// No overlapping between R and the input operands is allowed. /// /// /// This function requires scratch space of mpn_sec_powm_itch(, , ) /// limbs to be passed in the parameter. /// The scratch space requirements are guaranteed to increase monotonously in the operand sizes. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm_itch /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr bp = new mp_ptr(new uint[] { 0x00000002 }); /// mp_ptr ep = new mp_ptr(new uint[] { 0x00000004 }); /// mp_ptr mp = new mp_ptr(new uint[] { 0x00000003 }); /// mp_ptr result = new mp_ptr(new uint[] { 0x00000001 }); /// mp_ptr rp = new mp_ptr(bp.Size); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_powm_itch(bp.Size, 3, mp.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = bp^ep mod mp. /// gmp_lib.mpn_sec_powm(rp, bp, bp.Size, ep, 3, mp, mp.Size, tp); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(rp, bp, ep, mp, tp, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim bp As New mp_ptr(New UInteger() { &H2}) /// Dim ep As New mp_ptr(New UInteger() { &H4}) /// Dim mp As New mp_ptr(New UInteger() { &H3}) /// Dim result As New mp_ptr(New UInteger() { &H1}) /// Dim rp As New mp_ptr(bp.Size) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_powm_itch(bp.Size, 3, mp.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set rp = bp^ep mod mp. /// gmp_lib.mpn_sec_powm(rp, bp, bp.Size, ep, 3, mp, mp.Size, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(rp, bp, ep, mp, tp, result) /// /// public static void mpn_sec_powm(mp_ptr rp, /*const*/ mp_ptr bp, mp_size_t bn, /*const*/ mp_ptr ep, mp_bitcnt_t enb, /*const*/ mp_ptr mp, mp_size_t n, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (bp == null) throw new ArgumentNullException("bp"); if (ep == null) throw new ArgumentNullException("ep"); if (mp == null) throw new ArgumentNullException("mp"); if (tp == null) throw new ArgumentNullException("tp"); SafeNativeMethods.__gmpn_sec_powm(rp.ToIntPtr(), bp.ToIntPtr(), bn, ep.ToIntPtr(), enb, mp.ToIntPtr(), n, tp.ToIntPtr()); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_powm. /// /// The number of limbs of the mpn_sec_powm first operand. /// The number of limbs of the mpn_sec_powm second operand. /// The number of limbs of the mpn_sec_powm third operand. /// The scratch space in number of limbs required by the function mpn_sec_powm. /// mpn_sec_powm /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_powm_itch(mp_size_t bn, mp_bitcnt_t enb, mp_size_t n) { return SafeNativeMethods.__gmpn_sec_powm_itch(bn, enb, n); } /// /// Select entry from table , which has entries, each limbs. Store the selected entry at . /// /// The result integer. /// The table of operand integers. /// The number of limbs in each entry of the table. /// The number of entries in the table. /// The zero-based index of the entry to select. /// /// /// This function reads the entire table to avoid side-channel information leaks. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_powm_itch /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr tab = new mp_ptr(new uint[] { 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x33333333, 0x00000000 }); /// mp_ptr result = new mp_ptr(new uint[] { 0x33333333 }); /// mp_ptr rp = new mp_ptr(result.Size); /// /// // Set rp to third entry in tab. /// gmp_lib.mpn_sec_tabselect(rp, tab, 1, tab.Size, 2); /// /// // Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)); /// /// // Release unmanaged memory. /// gmp_lib.free(tab, result); /// /// /// ' Create multi-precision operands, and expected result. /// Dim tab As New mp_ptr(New UInteger() { &H11111111, &H22222222, &H33333333, &H44444444, &H33333333, &H0}) /// Dim result As New mp_ptr(New UInteger() { &H33333333}) /// Dim rp As New mp_ptr(result.Size) /// /// ' Set rp to third entry in tab. /// gmp_lib.mpn_sec_tabselect(rp, tab, 1, tab.Size, 2) /// /// ' Assert result of operation. /// Assert.IsTrue(rp.SequenceEqual(result)) /// /// ' Release unmanaged memory. /// gmp_lib.free(tab, result) /// /// public static void mpn_sec_tabselect(/*volatile*/ mp_ptr /*mp_limb_t**/ rp, /*volatile const*/ mp_ptr /*mp_limb_t**/ tab, mp_size_t n, mp_size_t nents, mp_size_t which) { if (rp == null) throw new ArgumentNullException("rp"); if (tab == null) throw new ArgumentNullException("tab"); SafeNativeMethods.__gmpn_sec_tabselect(rp.ToIntPtr(), tab.ToIntPtr(), n, nents, which); } /// /// Set Q to the truncated quotient N / D and R to N modulo D, where N = {, }, D = {, }, Q’s most significant limb is the function return value and the remaining limbs are {, - }, and R = {, }. /// /// The quotient result operand. /// The first operand and remainder result integer. /// The number of limbs of . /// The second operand integer. /// The number of limbs of . /// The scratch operand integer. /// Q’s most significant limb. /// /// /// It is required that ≥ 1, and that [ - 1] ≠ 0. /// This does not imply that N ≥ D since N might be zero-padded. /// /// /// Note the overlapping between N and R. /// No other operand overlapping is allowed. /// The entire space occupied by N is overwritten. /// /// /// This function requires scratch space of mpn_sec_div_qr_itch(, ) /// limbs to be passed in the parameter. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr_itch /// mpn_sec_div_r /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr np = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr dp = new mp_ptr(new uint[] { 0x00000003 }); /// mp_ptr remainder = new mp_ptr(new uint[] { 0x00000000 }); /// mp_ptr qp = new mp_ptr(new uint[np.Size]); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_div_qr_itch(np.Size, dp.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set qp = floor(np / dp) and rp = np mod dp. /// mp_limb_t mslimb = gmp_lib.mpn_sec_div_qr(qp, np, np.Size, dp, dp.Size, tp); /// /// // Assert result of operation. /// Assert.IsTrue(mslimb == (ulong)(gmp_lib.mp_bytes_per_limb == 4 ? 0x00005555 : 0x0000555555555555)); /// Assert.IsTrue(qp[0] == (ulong)(gmp_lib.mp_bytes_per_limb == 4 ? 0x55555555 : 0x0000000000000000)); /// Assert.IsTrue(np[0] == remainder[0]); /// /// // Release unmanaged memory. /// gmp_lib.free(qp, np, dp, remainder, tp); /// /// /// ' Create multi-precision operands, and expected result. /// Dim np As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim dp As New mp_ptr(New UInteger() { &H3}) /// Dim remainder As New mp_ptr(New UInteger() { &H0}) /// Dim qp As New mp_ptr(New UInteger(np.Size - 1) { }) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_div_qr_itch(np.Size, dp.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set qp = floor(np / dp) and rp = np mod dp. /// Dim mslimb As mp_limb_t = gmp_lib.mpn_sec_div_qr(qp, np, np.Size, dp, dp.Size, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(mslimb = CULng(If(gmp_lib.mp_bytes_per_limb = 4, &H5555, &H555555555555L))) /// Assert.IsTrue(qp(0) = CULng(If(gmp_lib.mp_bytes_per_limb = 4, &H55555555, &H0))) /// Assert.IsTrue(np(0) = remainder(0)) /// /// ' Release unmanaged memory. /// gmp_lib.free(qp, np, dp, remainder, tp) /// /// public static mp_limb_t mpn_sec_div_qr(mp_ptr qp, mp_ptr np, mp_size_t nn, /*const*/ mp_ptr dp, mp_size_t dn, mp_ptr tp) { if (qp == null) throw new ArgumentNullException("qp"); if (np == null) throw new ArgumentNullException("np"); if (dp == null) throw new ArgumentNullException("dp"); if (tp == null) throw new ArgumentNullException("tp"); if (IntPtr.Size == 4) return new mp_limb_t(SafeNativeMethods.__gmpn_sec_div_qr_x86(qp.ToIntPtr(), np.ToIntPtr(), nn, dp.ToIntPtr(), dn, tp.ToIntPtr())); else return new mp_limb_t(SafeNativeMethods.__gmpn_sec_div_qr_x64(qp.ToIntPtr(), np.ToIntPtr(), nn, dp.ToIntPtr(), dn, tp.ToIntPtr())); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_div_qr. /// /// The number of limbs of the mpn_sec_div_qr first operand. /// The number of limbs of the mpn_sec_div_qr second operand. /// The scratch space in number of limbs required by the function mpn_sec_div_qr. /// mpn_sec_div_qr /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_div_qr_itch(mp_size_t nn, mp_size_t dn) { return new mp_size_t(SafeNativeMethods.__gmpn_sec_div_qr_itch(nn, dn)); } /// /// Set R to N modulo D, where N = {, }, D = {, }, and R = {, }. /// /// The first operand and result integer. /// The number of limbs of . /// The second operand integer /// The number of limbs of . /// The scratch operand integer. /// /// /// It is required that ≥ 1, /// and that [ - 1] ≠ 0. /// This does not imply that N ≥ D since N might be zero-padded. /// /// /// Note the overlapping between N and R. /// No other operand overlapping is allowed. /// The entire space occupied by N is overwritten. /// /// /// This function requires scratch space of mpn_sec_div_r_itch(, ) /// limbs to be passed in the parameter. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r_itch /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr np = new mp_ptr(new uint[] { 0xffffffff, 0x0000ffff }); /// mp_ptr dp = new mp_ptr(new uint[] { 0x00000004 }); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_div_r_itch(np.Size, dp.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set np = np mod dp. /// gmp_lib.mpn_sec_div_r(np, np.Size, dp, dp.Size, tp); /// /// // Assert result of operation. /// Assert.IsTrue(np[0] == 3); /// /// // Release unmanaged memory. /// gmp_lib.free(np, dp, tp); /// /// /// ' Create multi-precision operands, and expected result. /// Dim np As New mp_ptr(New UInteger() { &HffffffffUI, &Hffff}) /// Dim dp As New mp_ptr(New UInteger() { &H4}) /// /// ' Create scratch space. /// Dim size As mp_size_t = gmp_lib.mpn_sec_div_r_itch(np.Size, dp.Size) /// Dim tp As New mp_ptr(size) /// /// ' Set np = np mod dp. /// gmp_lib.mpn_sec_div_r(np, np.Size, dp, dp.Size, tp) /// /// ' Assert result of operation. /// Assert.IsTrue(np(0) = 3) /// /// ' Release unmanaged memory. /// gmp_lib.free(np, dp, tp) /// /// public static void mpn_sec_div_r(mp_ptr np, mp_size_t nn, /*const*/ mp_ptr dp, mp_size_t dn, mp_ptr tp) { if (np == null) throw new ArgumentNullException("np"); if (dp == null) throw new ArgumentNullException("dp"); if (tp == null) throw new ArgumentNullException("tp"); SafeNativeMethods.__gmpn_sec_div_r(np.ToIntPtr(), nn, dp.ToIntPtr(), dn, tp.ToIntPtr()); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_div_r. /// /// The number of limbs of the mpn_sec_div_r first operand. /// The number of limbs of the mpn_sec_div_r second operand. /// The scratch space in number of limbs required by the function mpn_sec_div_r. /// mpn_sec_div_r /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_div_r_itch(mp_size_t nn, mp_size_t dn) { return new mp_size_t(SafeNativeMethods.__gmpn_sec_div_r_itch(nn, dn)); } /// /// Set R to the inverse of A modulo M, where R = {, }, A = {, }, and M = {, }. This function’s interface is preliminary. /// /// The result integer. /// The first operand integer. /// The second operand integer. /// The number of limbs of and . /// The third operand integer. /// The scratch operand integer. /// If an inverse exists, return 1, otherwise return 0 and leave R undefined. /// /// /// If an inverse exists, return 1, otherwise return 0 and leave R undefined. /// In either case, the input A is destroyed. /// /// /// It is required that M is odd, and that ≥ ceil(log(A + 1)) + ceil(log(M + 1)). /// A safe choice is = 2 * * mp_bits_per_limb, /// but a smaller value might improve performance if M or A are known to have leading zero bits. /// /// /// This function requires scratch space of mpn_sec_invert_itch() /// limbs to be passed in the parameter. /// /// /// mpn_cnd_add_n /// mpn_cnd_sub_n /// mpn_sec_add_1 /// mpn_sec_sub_1 /// mpn_cnd_swap /// mpn_sec_mul /// mpn_sec_sqr /// mpn_sec_powm /// mpn_sec_tabselect /// mpn_sec_div_qr /// mpn_sec_div_r /// mpn_sec_invert_itch /// Low-level functions for cryptography /// GNU MP - Low-level Functions /// /// /// // Create multi-precision operands, and expected result. /// mp_ptr ap = new mp_ptr(new uint[] { 3 }); /// mp_ptr mp = new mp_ptr(new uint[] { 11 }); /// mp_ptr rp = new mp_ptr(ap.Size); /// mp_ptr result = new mp_ptr(new uint[] { 4 }); /// /// // Create scratch space. /// mp_size_t size = gmp_lib.mpn_sec_invert_itch(ap.Size); /// mp_ptr tp = new mp_ptr(size); /// /// // Set rp = ap^-1 mod mp. /// gmp_lib.mpn_sec_invert(rp, ap, mp, ap.Size, (uint)(2 * ap.Size* gmp_lib.mp_bits_per_limb), tp); /// /// // Assert result of operation. /// Assert.IsTrue(rp[0] == result[0]); /// /// // Release unmanaged memory. /// gmp_lib.free(ap, mp, rp, result, tp); /// /// /// /// public static int mpn_sec_invert(mp_ptr rp, mp_ptr ap, /*const*/ mp_ptr mp, mp_size_t n, mp_bitcnt_t nbcnt, mp_ptr tp) { if (rp == null) throw new ArgumentNullException("rp"); if (ap == null) throw new ArgumentNullException("ap"); if (mp == null) throw new ArgumentNullException("mp"); if (tp == null) throw new ArgumentNullException("tp"); return SafeNativeMethods.__gmpn_sec_invert(rp.ToIntPtr(), ap.ToIntPtr(), mp.ToIntPtr(), n, nbcnt, tp.ToIntPtr()); } /// /// Return the scratch space in number of limbs required by the function mpn_sec_invert. /// /// The number of limbs of the mpn_sec_invert first operand. /// The scratch space in number of limbs required by the function mpn_sec_invert. /// mpn_sec_invert /// Low-level functions for cryptography /// GNU MP - Low-level Functions public static mp_size_t mpn_sec_invert_itch(mp_size_t n) { return new mp_size_t(SafeNativeMethods.__gmpn_sec_invert_itch(n)); } #endregion [SuppressUnmanagedCodeSecurity] private static class SafeNativeMethods { #region "Win32 functions." [DllImport("kernel32", CharSet = CharSet.Unicode)] public static extern IntPtr LoadLibrary(string lpFileName); [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetDllDirectory(string directory); [DllImport("kernel32.dll")] public static extern void RtlZeroMemory(IntPtr dst, int length); #endregion #region "Memory allocation functions." [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_get_memory_functions(ref IntPtr /*void*(**) (size_t)*/ alloc_func_ptr, ref IntPtr /*void*(**) (void*, size_t, size_t)*/ realloc_func_ptr, ref IntPtr /*void (**) (void*, size_t)*/ free_func_ptr); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_set_memory_functions(IntPtr /*void*(*) (size_t)*/ alloc_func_ptr, IntPtr /*void*(*) (void*, size_t, size_t)*/ realloc_func_ptr, IntPtr /*void (*) (void*, size_t)*/ free_func_ptr); #endregion #region "Random number routines." ///* obsolete */ //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmp_randinit(IntPtr /*gmp_randstate_t*/, gmp_randalg_t, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randinit_default(IntPtr /*gmp_randstate_t*/ state); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randinit_lc_2exp(IntPtr /*gmp_randstate_t*/ state, /*const*/ IntPtr /*mpz_t*/ a, uint /*unsigned long int*/ c, uint /*mp_bitcnt_t*/ m2exp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_randinit_lc_2exp_size(IntPtr /*gmp_randstate_t*/ state, uint /*mp_bitcnt_t*/ size); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randinit_mt(IntPtr /*gmp_randstate_t*/ state); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randinit_set(IntPtr /*gmp_randstate_t*/ state, /*const*/ IntPtr /*gmp_randstate_t*/ /*__gmp_randstate_struct **/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randseed(IntPtr /*gmp_randstate_t*/ state, /*const*/ IntPtr /*mpz_t*/ seed); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randseed_ui(IntPtr /*gmp_randstate_t*/ state, uint /*unsigned long int*/ seed); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmp_randclear(IntPtr /*gmp_randstate_t*/ state); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmp_urandomb_ui(IntPtr /*gmp_randstate_t*/ state, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmp_urandomm_ui(IntPtr /*gmp_randstate_t*/ state, uint /*unsigned long int*/ n); #endregion #region "Formatted output routines." //#define gmp_asprintf __gmp_asprintf //__GMP_DECLSPEC int gmp_asprintf (char **, const char *, ...); //#define gmp_fprintf __gmp_fprintf //# ifdef _GMP_H_HAVE_FILE //__GMP_DECLSPEC int gmp_fprintf (FILE *, const char *, ...); //#endif //#define gmp_obstack_printf __gmp_obstack_printf //#if defined (_GMP_H_HAVE_OBSTACK) //__GMP_DECLSPEC int gmp_obstack_printf (struct obstack *, const char *, ...); //#endif //#define gmp_obstack_vprintf __gmp_obstack_vprintf //#if defined (_GMP_H_HAVE_OBSTACK) && defined (_GMP_H_HAVE_VA_LIST) //__GMP_DECLSPEC int gmp_obstack_vprintf (struct obstack *, const char *, va_list); //#endif //#define gmp_printf __gmp_printf // __GMP_DECLSPEC int gmp_printf(const char*, ...); //#define gmp_snprintf __gmp_snprintf // __GMP_DECLSPEC int gmp_snprintf(char*, size_t, const char*, ...); //#define gmp_sprintf __gmp_sprintf // __GMP_DECLSPEC int gmp_sprintf(char*, const char*, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vasprintf(ref IntPtr /*char ***/ pp, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vfprintf(IntPtr /*FILE **/ pp, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vprintf(/*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmp_vsnprintf")] public static extern int __gmp_vsnprintf_x86(IntPtr /*char **/ buf, uint /*size_t*/ size, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmp_vsnprintf")] public static extern int __gmp_vsnprintf_x64(IntPtr /*char **/ buf, ulong /*size_t*/ size, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vsprintf(IntPtr /*char **/ buf, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ args); #endregion #region "Formatted input routines." //#define gmp_fscanf __gmp_fscanf //# ifdef _GMP_H_HAVE_FILE // __GMP_DECLSPEC int gmp_fscanf(FILE*, const char*, ...); //#endif //#define gmp_scanf __gmp_scanf // __GMP_DECLSPEC int gmp_scanf(const char*, ...); //#define gmp_sscanf __gmp_sscanf // __GMP_DECLSPEC int gmp_sscanf(const char*, const char*, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vfscanf(IntPtr /*FILE **/ fp, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ ap); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vscanf(/*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ ap); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmp_vsscanf(/*const*/ IntPtr /*char **/ s, /*const*/ IntPtr /*char **/ fmt, IntPtr /*va_list*/ ap); #endregion #region "Integer (i.e. Z) routines." [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void_ptr /*void **/ __gmpz_realloc(IntPtr /*mpz_t*/ integer, int /*mp_size_t*/ new_alloc); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_abs(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_add(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_add_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_addmul(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_addmul_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_and(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); //[DllImport(@"libgmp-10.dll")] /* OBSOLETE */ //public static extern void __gmpz_array_init(IntPtr /*mpz_t*/ integer_array, int /*mp_size_t*/ array_size, int /*mp_size_t*/ fixed_num_bits); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_bin_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ k); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_bin_uiui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ n, uint /*unsigned long int*/ k); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_cdiv_q(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_cdiv_q_2exp(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_cdiv_q_ui(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_cdiv_qr(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_cdiv_qr_ui(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_cdiv_r(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_cdiv_r_2exp(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_cdiv_r_ui(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_cdiv_ui(/*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_clear(IntPtr /*mpz_t*/ x); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpz_clears(mpz_t[] x); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_clrbit(IntPtr /*mpz_t*/ rop, uint /*mp_bitcnt_t*/ bit_index); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmp(/*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmp_d(/*const*/ IntPtr /*mpz_t*/ op1, double op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmp_si(/*const*/ IntPtr /*mpz_t*/ op1, int /*long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmp_ui(/*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmpabs(/*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmpabs_d(/*const*/ IntPtr /*mpz_t*/ op1, double op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_cmpabs_ui(/*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_com(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_combit(IntPtr /*mpz_t*/ rop, uint /*mp_bitcnt_t*/ bit_index); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_congruent_p(/*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ c, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_congruent_2exp_p(/*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ c, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_congruent_ui_p(/*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ c, uint /*unsigned long int*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_divexact(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_divexact_ui(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_divisible_p(/*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_divisible_ui_p(/*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_divisible_2exp_p(/*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpz_dump(/*const*/ IntPtr /*mpz_t*/ x); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_export")] public static extern IntPtr /*void **/ __gmpz_export_x86(IntPtr /*void **/ rop, ref uint /*size_t **/ countp, int order, uint /*size_t*/ size, int endian, uint /*size_t*/ nails, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_export")] public static extern IntPtr /*void **/ __gmpz_export_x64(IntPtr /*void **/ rop, ref ulong /*size_t **/ countp, int order, ulong /*size_t **/ size, int endian, ulong /*size_t **/ nails, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fac_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_2fac_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mfac_uiui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ n, uint /*unsigned long int*/ m); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_primorial_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fdiv_q(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fdiv_q_2exp(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_fdiv_q_ui(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fdiv_qr(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_fdiv_qr_ui(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fdiv_r(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fdiv_r_2exp(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_fdiv_r_ui(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_fdiv_ui(/*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fib_ui(IntPtr /*mpz_t*/ fn, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_fib2_ui(IntPtr /*mpz_t*/ fn, IntPtr /*mpz_t*/ fnsub1, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_sint_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_slong_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_sshort_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_uint_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_ulong_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_fits_ushort_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_gcd(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_gcd_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_gcdext(IntPtr /*mpz_t*/ g, IntPtr /*mpz_t*/ s, IntPtr /*mpz_t*/ t, /*const*/ IntPtr /*mpz_t*/ a, /*const*/ IntPtr /*mpz_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern double __gmpz_get_d(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern double __gmpz_get_d_2exp(ref int /*long int*/ exp, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*long int*/ __gmpz_get_si(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr /*char **/ __gmpz_get_str(IntPtr /*char **/ str, int @base, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_get_ui(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_getlimbn")] public static extern uint /*mp_limb_t*/ __gmpz_getlimbn_x86(/*const*/ IntPtr /*mpz_t*/ op, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_getlimbn")] public static extern ulong /*mp_limb_t*/ __gmpz_getlimbn_x64(/*const*/ IntPtr /*mpz_t*/ op, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpz_hamdist(/*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_import")] public static extern void __gmpz_import_x86(IntPtr /*mpz_t*/ rop, uint /*size_t*/ count, int order, uint /*size_t*/ size, int endian, uint /*size_t*/ nails, IntPtr /*void **/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_import")] public static extern void __gmpz_import_x64(IntPtr /*mpz_t*/ rop, ulong /*size_t*/ count, int order, ulong /*size_t*/ size, int endian, ulong /*size_t*/ nails, IntPtr /*void **/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init(IntPtr /*mpz_t*/ x); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init2(IntPtr /*mpz_t*/ x, uint /*mp_bitcnt_t*/ n); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpz_inits(IntPtr /*mpz_t*/ x, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init_set(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init_set_d(IntPtr /*mpz_t*/ rop, double op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init_set_si(IntPtr /*mpz_t*/ rop, int /*long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_init_set_str(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr str, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_init_set_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_inp_raw")] public static extern uint /*size_t*/ __gmpz_inp_raw_x86(IntPtr /*mpz_t*/ rop, IntPtr /*FILE **/ stream); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_inp_raw")] public static extern ulong /*size_t*/ __gmpz_inp_raw_x64(IntPtr /*mpz_t*/ rop, IntPtr /*FILE **/ stream); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_inp_str")] public static extern uint /*size_t*/ __gmpz_inp_str_x86(IntPtr /*mpz_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_inp_str")] public static extern ulong /*size_t*/ __gmpz_inp_str_x64(IntPtr /*mpz_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_invert(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_ior(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_jacobi(/*const*/ IntPtr /*mpz_t*/ a, /*const*/ IntPtr /*mpz_t*/ b); //#define mpz_kronecker mpz_jacobi /* alias */ [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_kronecker_si(/*const*/ IntPtr /*mpz_t*/ a, int /*long int*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_kronecker_ui(/*const*/ IntPtr /*mpz_t*/ a, uint /*unsigned long int*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_si_kronecker(int /*long int*/ a, /*const*/ IntPtr /*mpz_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_ui_kronecker(uint /*unsigned long int*/ a, /*const*/ IntPtr /*mpz_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_lcm(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_lcm_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_legendre(/*const*/ IntPtr /*mpz_t*/ a, /*const*/ IntPtr /*mpz_t*/ p); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_lucnum_ui(IntPtr /*mpz_t*/ ln, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_lucnum2_ui(IntPtr /*mpz_t*/ ln, IntPtr /*mpz_t*/ lnsub1, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_millerrabin(/*const*/ IntPtr /*mpz_t*/ n, int reps); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mod(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); //#define mpz_mod_ui mpz_fdiv_r_ui /* same as fdiv_r because divisor unsigned */ [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mul(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mul_2exp(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*mp_bitcnt_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mul_si(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, int /*long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_mul_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_neg(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_nextprime(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_out_raw")] public static extern uint /*size_t*/ __gmpz_out_raw_x86(IntPtr /*FILE **/stream, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_out_raw")] public static extern ulong /*size_t*/ __gmpz_out_raw_x64(IntPtr /*FILE **/stream, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_out_str")] public static extern uint /*size_t*/ __gmpz_out_str_x86(IntPtr /*FILE **/stream, int @base, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_out_str")] public static extern ulong /*size_t*/ __gmpz_out_str_x64(IntPtr /*FILE **/stream, int @base, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_perfect_power_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_perfect_square_p(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpz_popcount(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_pow_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ @base, uint /*unsigned long int*/ exp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_powm(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ @base, /*const*/ IntPtr /*mpz_t*/ exp, /*const*/ IntPtr /*mpz_t*/ mod); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_powm_sec(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ @base, /*const*/ IntPtr /*mpz_t*/ exp, /*const*/ IntPtr /*mpz_t*/ mod); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_powm_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ @base, uint /*unsigned long int*/ exp, /*const*/ IntPtr /*mpz_t*/ mod); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_probab_prime_p(/*const*/ IntPtr /*mpz_t*/ n, int reps); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] // OBSOLETE public static extern void __gmpz_random(IntPtr /*mpz_t*/ rop, int /*mp_size_t*/ max_size); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] // OBSOLETE public static extern void __gmpz_random2(IntPtr /*mpz_t*/ rop, int /*mp_size_t*/ max_size); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_realloc2(IntPtr /*mpz_t*/ x, uint /*mp_bitcnt_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpz_remove(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op, /*const*/ IntPtr /*mpz_t*/ f); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_root(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_rootrem(IntPtr /*mpz_t*/ root, IntPtr /*mpz_t*/ rem, /*const*/ IntPtr /*mpz_t*/ u, uint /*unsigned long int*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_rrandomb(IntPtr /*mpz_t*/ rop, IntPtr /*gmp_randstate_t*/ state, uint /*mp_bitcnt_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpz_scan0(/*const*/ IntPtr /*mpz_t*/ op, uint /*mp_bitcnt_t*/ starting_bit); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpz_scan1(/*const*/ IntPtr /*mpz_t*/ op, uint /*mp_bitcnt_t*/ starting_bit); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set_d(IntPtr /*mpz_t*/ rop, double op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set_f(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set_q(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set_si(IntPtr /*mpz_t*/ rop, int /*long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_set_str(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*char **/ str, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_set_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_setbit(IntPtr /*mpz_t*/ rop, uint /*mp_bitcnt_t*/ bit_index); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpz_size(/*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_sizeinbase")] public static extern uint /*size_t*/ __gmpz_sizeinbase_x86(/*const*/ IntPtr /*mpz_t*/ op, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpz_sizeinbase")] public static extern ulong /*size_t*/ __gmpz_sizeinbase_x64(/*const*/ IntPtr /*mpz_t*/ op, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_sqrt(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_sqrtrem(IntPtr /*mpz_t*/ rop1, IntPtr /*mpz_t*/ rop2, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_sub(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_sub_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_ui_sub(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_submul(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_submul_ui(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_swap(IntPtr /*mpz_t*/ rop1, IntPtr /*mpz_t*/ rop2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_tdiv_ui(/*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_tdiv_q(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_tdiv_q_2exp(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_tdiv_q_ui(IntPtr /*mpz_t*/ q, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_tdiv_qr(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_tdiv_qr_ui(IntPtr /*mpz_t*/ q, IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_tdiv_r(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, /*const*/ IntPtr /*mpz_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_tdiv_r_2exp(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*mp_bitcnt_t*/ b); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpz_tdiv_r_ui(IntPtr /*mpz_t*/ r, /*const*/ IntPtr /*mpz_t*/ n, uint /*unsigned long int*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpz_tstbit(/*const*/ IntPtr /*mpz_t*/ op, uint /*mp_bitcnt_t*/ bit_index); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_ui_pow_ui(IntPtr /*mpz_t*/ rop, uint /*unsigned long int*/ @base, uint /*unsigned long int*/ exp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_urandomb(IntPtr /*mpz_t*/ rop, IntPtr /*gmp_randstate_t*/ state, uint /*mp_bitcnt_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_urandomm(IntPtr /*mpz_t*/ rop, IntPtr /*gmp_randstate_t*/ state, /*const*/ IntPtr /*mpz_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_xor(IntPtr /*mpz_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr /*mp_limb_t**/ __gmpz_limbs_read(/*const*/ IntPtr /*mpz_t*/ x); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr /*mp_limb_t**/ __gmpz_limbs_write(/*const*/ IntPtr /*mpz_t*/ x, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr /*mp_limb_t**/ __gmpz_limbs_modify(/*const*/ IntPtr /*mpz_t*/ x, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpz_limbs_finish(/*const*/ IntPtr /*mpz_t*/ x, int /*mp_size_t*/ s); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern /*const*/ IntPtr /*mpz_t*/ __gmpz_roinit_n(IntPtr /*mpz_t*/ x, /*const*/ IntPtr /*mp_limb_t*/ xp, int /*mp_size_t*/ xs); #endregion #region "Rational (i.e. Q) routines." [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_abs(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_add(IntPtr /*mpq_t*/ sum, /*const*/ IntPtr /*mpq_t*/ addend1, /*const*/ IntPtr /*mpq_t*/ addend2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_canonicalize(IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_clear(IntPtr /*mpq_t*/ x); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpq_clears(IntPtr /*mpq_t*/, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_cmp(/*const*/ IntPtr /*mpq_t*/ op1, /*const*/ IntPtr /*mpq_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_cmp_si(/*const*/ IntPtr /*mpq_t*/ op1, int /*long int*/ num2, uint /*unsigned long int*/ den2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_cmp_ui(/*const*/ IntPtr /*mpq_t*/ op1, uint /*unsigned long int*/ num2, uint /*unsigned long int*/ den2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_cmp_z(/*const*/ IntPtr /*mpq_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_div(IntPtr /*mpq_t*/ quotient, /*const*/ IntPtr /*mpq_t*/ dividend, /*const*/ IntPtr /*mpq_t*/ divisor); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_div_2exp(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op1, uint /*mp_bitcnt_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_equal(/*const*/ IntPtr /*mpq_t*/ op1, /*const*/ IntPtr /*mpq_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_get_num(IntPtr /*mpz_t*/ numerator, /*const*/ IntPtr /*mpq_t*/ rational); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_get_den(IntPtr /*mpz_t*/ denominator, /*const*/ IntPtr /*mpq_t*/ rational); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern double __gmpq_get_d(/*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr /*char **/ __gmpq_get_str(IntPtr /*char **/ str, int @base, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_init(IntPtr /*mpq_t*/ x); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpq_inits(IntPtr /*mpq_t*/, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpq_inp_str")] public static extern uint /*size_t*/ __gmpq_inp_str_x86(IntPtr /*mpq_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpq_inp_str")] public static extern ulong /*size_t*/ __gmpq_inp_str_x64(IntPtr /*mpq_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_inv(IntPtr /*mpq_t*/ inverted_number, /*const*/ IntPtr /*mpq_t*/ number); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_mul(IntPtr /*mpq_t*/ product, /*const*/ IntPtr /*mpq_t*/ multiplier, /*const*/ IntPtr /*mpq_t*/ multiplicand); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_mul_2exp(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op1, uint /*mp_bitcnt_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_neg(IntPtr /*mpq_t*/ negated_operand, /*const*/ IntPtr /*mpq_t*/ operand); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpq_out_str")] public static extern uint /*size_t*/ __gmpq_out_str_x86(IntPtr /*FILE **/ stream, int @base, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpq_out_str")] public static extern ulong /*size_t*/ __gmpq_out_str_x64(IntPtr /*FILE **/ stream, int @base, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_d(IntPtr /*mpq_t*/ rop, double op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_den(IntPtr /*mpq_t*/ rational, /*const*/ IntPtr /*mpz_t*/ denominator); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_f(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_num(IntPtr /*mpq_t*/ rational, /*const*/ IntPtr /*mpz_t*/ numerator); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_si(IntPtr /*mpq_t*/ rop, int /*long int*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpq_set_str(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*char **/ str, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_ui(IntPtr /*mpq_t*/ rop, uint /*unsigned long int*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_set_z(IntPtr /*mpq_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_sub(IntPtr /*mpq_t*/ difference, /*const*/ IntPtr /*mpq_t*/ minuend, /*const*/ IntPtr /*mpq_t*/ subtrahend); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpq_swap(IntPtr /*mpq_t*/ rop1, IntPtr /*mpq_t*/ rop2); #endregion #region "Float (i.e. F) routines." [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_abs(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_add(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_add_ui(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_ceil(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_clear(IntPtr /*mpf_t*/ x); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpf_clears(IntPtr /*mpf_t*/, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_cmp(/*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_cmp_z(/*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpz_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_cmp_d(/*const*/ IntPtr /*mpf_t*/ op1, double op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_cmp_si(/*const*/ IntPtr /*mpf_t*/ op1, int /*long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_cmp_ui(/*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_div(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_div_2exp(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*mp_bitcnt_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_div_ui(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpf_dump(/*const*/ IntPtr /*mpf_t*/ op); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern int __gmpf_eq(/*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2, uint /*mp_bitcnt_t*/ op3); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_sint_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_slong_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_sshort_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_uint_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_ulong_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_fits_ushort_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_floor(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern double __gmpf_get_d(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern double __gmpf_get_d_2exp(ref int /*long int **/ exp, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpf_get_default_prec(/*void*/); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpf_get_prec(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*long int*/ __gmpf_get_si(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_get_str")] public static extern IntPtr /*char **/ __gmpf_get_str_x86(IntPtr /*char **/ str, ref int /*mp_exp_t **/ expptr, int @base, uint /*size_t*/ n_digits, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_get_str")] public static extern IntPtr /*char **/ __gmpf_get_str_x64(IntPtr /*char **/ str, ref int /*mp_exp_t **/ expptr, int @base, ulong /*size_t*/ n_digits, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*unsigned long int*/ __gmpf_get_ui(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init(IntPtr /*mpf_t*/ x); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init2(IntPtr /*mpf_t*/ x, uint /*mp_bitcnt_t*/ prec); //[DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] //public static extern void __gmpf_inits(IntPtr /*mpf_t*/, ...); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init_set(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init_set_d(IntPtr /*mpf_t*/ rop, double op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init_set_si(IntPtr /*mpf_t*/ rop, int /*long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_init_set_str(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*char **/ str, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_init_set_ui(IntPtr /*mpf_t*/ rop, uint /*unsigned long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_inp_str")] public static extern uint /*size_t*/ __gmpf_inp_str_x86(IntPtr /*mpf_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_inp_str")] public static extern ulong /*size_t*/ __gmpf_inp_str_x64(IntPtr /*mpf_t*/ rop, IntPtr /*FILE **/ stream, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_integer_p(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_mul(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_mul_2exp(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*mp_bitcnt_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_mul_ui(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_neg(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_out_str")] public static extern uint /*size_t*/ __gmpf_out_str_x86(IntPtr /*FILE **/ stream, int @base, uint /*size_t*/ n_digits, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_out_str")] public static extern ulong /*size_t*/ __gmpf_out_str_x64(IntPtr /*FILE **/ stream, int @base, ulong /*size_t*/ n_digits, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_pow_ui(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_random2(IntPtr /*mpf_t*/ rop, int /*mp_size_t*/ max_size, int /*mp_exp_t*/ exp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_reldiff(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_d(IntPtr /*mpf_t*/ rop, double op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_default_prec(uint /*mp_bitcnt_t*/ prec); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_prec(IntPtr /*mpf_t*/ rop, uint /*mp_bitcnt_t*/ prec); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_prec_raw(IntPtr /*mpf_t*/ rop, uint /*mp_bitcnt_t*/ prec); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_q(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpq_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_si(IntPtr /*mpf_t*/ rop, int /*long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpf_set_str(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*char **/ str, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_ui(IntPtr /*mpf_t*/ rop, uint /*unsigned long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_set_z(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpz_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_size")] public static extern uint /*size_t*/ __gmpf_size_x86(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpf_size")] public static extern ulong /*size_t*/ __gmpf_size_x64(/*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_sqrt(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_sqrt_ui(IntPtr /*mpf_t*/ rop, uint /*unsigned long int*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_sub(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_sub_ui(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op1, uint /*unsigned long int*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_swap(IntPtr /*mpf_t*/ rop1, IntPtr /*mpf_t*/ rop2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_trunc(IntPtr /*mpf_t*/ rop, /*const*/ IntPtr /*mpf_t*/ op); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_ui_div(IntPtr /*mpf_t*/ rop, uint /*unsigned long int*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_ui_sub(IntPtr /*mpf_t*/ rop, uint /*unsigned long int*/ op1, /*const*/ IntPtr /*mpf_t*/ op2); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpf_urandomb(IntPtr /*mpf_t*/ rop, IntPtr /*gmp_randstate_t*/ state, uint /*mp_bitcnt_t*/ nbits); #endregion #region "Low level positive-integer (i.e. N) routines." [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add")] public static extern uint /*mp_limb_t*/ __gmpn_add_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add")] public static extern ulong /*mp_limb_t*/ __gmpn_add_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add_1")] public static extern uint /*mp_limb_t*/ __gmpn_add_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add_1")] public static extern ulong /*mp_limb_t*/ __gmpn_add_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add_n")] public static extern uint /*mp_limb_t*/ __gmpn_add_n_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_add_n")] public static extern ulong /*mp_limb_t*/ __gmpn_add_n_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_addmul_1")] public static extern uint /*mp_limb_t*/ __gmpn_addmul_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_addmul_1")] public static extern ulong /*mp_limb_t*/ __gmpn_addmul_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpn_cmp(/*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpn_zero_p(/*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_1")] public static extern void __gmpn_divexact_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint /*mp_limb_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_1")] public static extern void __gmpn_divexact_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ d); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_by3")] public static extern uint /*mp_limb_t*/ __gmpn_divexact_by3_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_by3")] public static extern ulong /*mp_limb_t*/ __gmpn_divexact_by3_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_by3c")] public static extern uint /*mp_limb_t*/ __gmpn_divexact_by3c_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint /*mp_limb_t*/ carry); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divexact_by3c")] public static extern ulong /*mp_limb_t*/ __gmpn_divexact_by3c_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ carry); //obsolete //#define mpn_divrem __MPN(divrem) //public static extern mp_limb_t __gmpn_divrem(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divrem_1")] public static extern uint /*mp_limb_t*/ __gmpn_divrem_1_x86(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ qxn, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n, uint /*mp_limb_t*/ s3limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_divrem_1")] public static extern ulong /*mp_limb_t*/ __gmpn_divrem_1_x64(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ qxn, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n, ulong /*mp_limb_t*/ s3limb); //obsolete //#define mpn_divrem_2 __MPN(divrem_2) //public static extern mp_limb_t __gmpn_divrem_2(IntPtr /*mp_ptr*/, int /*mp_size_t*/, IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/); //#define mpn_div_qr_1 __MPN(div_qr_1) //public static extern mp_limb_t __gmpn_div_qr_1(IntPtr /*mp_ptr*/, mp_limb_t*, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t); //#define mpn_div_qr_2 __MPN(div_qr_2) //public static extern mp_limb_t __gmpn_div_qr_2(IntPtr /*mp_ptr*/, IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, /*const*/ IntPtr /*mp_ptr*/); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_gcd(IntPtr /*mp_ptr*/ rp, IntPtr /*mp_ptr*/ xp, int /*mp_size_t*/ xn, IntPtr /*mp_ptr*/ yp, int /*mp_size_t*/ yn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_gcd_1")] public static extern uint /*mp_limb_t*/ __gmpn_gcd_1_x86(/*const*/ IntPtr /*mp_ptr*/ xp, int /*mp_size_t*/ xn, uint /*mp_limb_t*/ ylimb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_gcd_1")] public static extern ulong /*mp_limb_t*/ __gmpn_gcd_1_x64(/*const*/ IntPtr /*mp_ptr*/ xp, int /*mp_size_t*/ xn, ulong /*mp_limb_t*/ ylimb); //#define mpn_gcdext_1 __MPN(gcdext_1) //public static extern mp_limb_t __gmpn_gcdext_1(mp_limb_signed_t*, mp_limb_signed_t*, mp_limb_t, mp_limb_t); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_gcdext(IntPtr /*mp_ptr*/ gp, IntPtr /*mp_ptr*/ sp, ref int /*mp_size_t**/ sn, IntPtr /*mp_ptr*/ up, int /*mp_size_t*/ un, IntPtr /*mp_ptr*/ vp, int /*mp_size_t*/ vn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_get_str")] public static extern uint /*size_t*/ __gmpn_get_str_x86(IntPtr /*unsigned char **/ str, int @base, IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_get_str")] public static extern ulong /*size_t*/ __gmpn_get_str_x64(IntPtr /*unsigned char **/ str, int @base, IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpn_hamdist(/*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_lshift")] public static extern uint /*mp_limb_t*/ __gmpn_lshift_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint count); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_lshift")] public static extern ulong /*mp_limb_t*/ __gmpn_lshift_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint count); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mod_1")] public static extern uint /*mp_limb_t*/ __gmpn_mod_1_x86(/*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mod_1")] public static extern ulong /*mp_limb_t*/ __gmpn_mod_1_x64(/*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mul")] public static extern uint /*mp_limb_t*/ __gmpn_mul_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mul")] public static extern ulong /*mp_limb_t*/ __gmpn_mul_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mul_1")] public static extern uint /*mp_limb_t*/ __gmpn_mul_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_mul_1")] public static extern ulong /*mp_limb_t*/ __gmpn_mul_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_mul_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sqr(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_neg")] public static extern uint /*mp_limb_t*/ __gmpn_neg_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_neg")] public static extern ulong /*mp_limb_t*/ __gmpn_neg_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_com(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpn_perfect_square_p(/*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpn_perfect_power_p(/*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpn_popcount(/*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n); //#define mpn_pow_1 __MPN(pow_1) //public static extern int /*mp_size_t*/ __gmpn_pow_1(IntPtr /*mp_ptr*/, /*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, IntPtr /*mp_ptr*/); ///* undocumented now, but retained here for upward compatibility */ //#define mpn_preinv_mod_1 __MPN(preinv_mod_1) //public static extern mp_limb_t __gmpn_preinv_mod_1(/*const*/ IntPtr /*mp_ptr*/, int /*mp_size_t*/, mp_limb_t, mp_limb_t); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_random(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ r1n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_random2(IntPtr /*mp_ptr*/ r1p, int /*mp_size_t*/ r1n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_rshift")] public static extern uint /*mp_limb_t*/ __gmpn_rshift_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint count); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_rshift")] public static extern ulong /*mp_limb_t*/ __gmpn_rshift_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n, uint count); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpn_scan0(/*const*/ IntPtr /*mp_ptr*/ s1p, uint /*mp_bitcnt_t*/ bit); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern uint /*mp_bitcnt_t*/ __gmpn_scan1(/*const*/ IntPtr /*mp_ptr*/ s1p, uint /*mp_bitcnt_t*/ bit); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_set_str")] public static extern int /*mp_size_t*/ __gmpn_set_str_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*unsigned char **/ str, uint /*size_t*/ strsize, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_set_str")] public static extern int /*mp_size_t*/ __gmpn_set_str_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*unsigned char **/ str, ulong /*size_t*/ strsize, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sizeinbase")] public static extern uint /*size_t*/ __gmpn_sizeinbase_x86(/*const*/ IntPtr /*mp_ptr*/ xp, int /*mp_size_t*/ n, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sizeinbase")] public static extern ulong /*size_t*/ __gmpn_sizeinbase_x64(/*const*/ IntPtr /*mp_ptr*/ xp, int /*mp_size_t*/ n, int @base); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sqrtrem(IntPtr /*mp_ptr*/ r1p, IntPtr /*mp_ptr*/ r2p, /*const*/ IntPtr /*mp_ptr*/ sp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub")] public static extern uint /*mp_limb_t*/ __gmpn_sub_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub")] public static extern ulong /*mp_limb_t*/ __gmpn_sub_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ s1n, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ s2n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub_1")] public static extern uint /*mp_limb_t*/ __gmpn_sub_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub_1")] public static extern ulong /*mp_limb_t*/ __gmpn_sub_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub_n")] public static extern uint /*mp_limb_t*/ __gmpn_sub_n_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sub_n")] public static extern ulong /*mp_limb_t*/ __gmpn_sub_n_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_submul_1")] public static extern uint /*mp_limb_t*/ __gmpn_submul_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, uint /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_submul_1")] public static extern ulong /*mp_limb_t*/ __gmpn_submul_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ s2limb); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_tdiv_qr(IntPtr /*mp_ptr*/ qp, IntPtr /*mp_ptr*/ rp, int /*mp_size_t*/ qxn, /*const*/ IntPtr /*mp_ptr*/ np, int /*mp_size_t*/ nn, /*const*/ IntPtr /*mp_ptr*/ dp, int /*mp_size_t*/ dn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_and_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_andn_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_nand_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_ior_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_iorn_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_nior_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_xor_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_xnor_n(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_copyi(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_copyd(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_zero(IntPtr /*mp_ptr*/ rp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_add_n")] public static extern uint /*mp_limb_t*/ __gmpn_cnd_add_n_x86(uint /*mp_limb_t*/ cnd, IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_add_n")] public static extern ulong /*mp_limb_t*/ __gmpn_cnd_add_n_x64(ulong /*mp_limb_t*/ cnd, IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_sub_n")] public static extern uint /*mp_limb_t*/ __gmpn_cnd_sub_n_x86(uint /*mp_limb_t*/ cnd, IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_sub_n")] public static extern ulong /*mp_limb_t*/ __gmpn_cnd_sub_n_x64(ulong /*mp_limb_t*/ cnd, IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ s1p, /*const*/ IntPtr /*mp_ptr*/ s2p, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_add_1")] public static extern uint /*mp_limb_t*/ __gmpn_sec_add_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ n, uint /*mp_limb_t*/ b, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_add_1")] public static extern ulong /*mp_limb_t*/ __gmpn_sec_add_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ b, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_add_1_itch(int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_sub_1")] public static extern uint /*mp_limb_t*/ __gmpn_sec_sub_1_x86(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ n, uint /*mp_limb_t*/ b, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_sub_1")] public static extern ulong /*mp_limb_t*/ __gmpn_sec_sub_1_x64(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ n, ulong /*mp_limb_t*/ b, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_sub_1_itch(int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_swap")] public static extern void __gmpn_cnd_swap_x86(uint /*mp_limb_t*/ cnd, /*volatile*/ IntPtr /*mp_limb_t**/ ap, /*volatile*/ IntPtr /*mp_limb_t**/ bp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_cnd_swap")] public static extern void __gmpn_cnd_swap_x64(ulong /*mp_limb_t*/ cnd, /*volatile*/ IntPtr /*mp_limb_t**/ ap, /*volatile*/ IntPtr /*mp_limb_t**/ bp, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sec_mul(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ an, /*const*/ IntPtr /*mp_ptr*/ b, int /*mp_size_t*/ bn, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_mul_itch(int /*mp_size_t*/ an, int /*mp_size_t*/ bn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sec_sqr(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ ap, int /*mp_size_t*/ an, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_sqr_itch(int /*mp_size_t*/ an); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sec_powm(IntPtr /*mp_ptr*/ rp, /*const*/ IntPtr /*mp_ptr*/ bp, int /*mp_size_t*/ bn, /*const*/ IntPtr /*mp_ptr*/ ep, uint /*mp_bitcnt_t*/ enb, /*const*/ IntPtr /*mp_ptr*/ mp, int /*mp_size_t*/ n, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_powm_itch(int /*mp_size_t*/ bn, uint /*mp_bitcnt_t*/ enb, int /*mp_size_t*/ n); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sec_tabselect(/*volatile*/ IntPtr /*mp_limb_t**/ rp, /*volatile const*/ IntPtr /*mp_limb_t**/ tab, int /*mp_size_t*/ n, int /*mp_size_t*/ nents, int /*mp_size_t*/ which); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_div_qr")] public static extern uint /*mp_limb_t*/ __gmpn_sec_div_qr_x86(IntPtr /*mp_ptr*/ qp, IntPtr /*mp_ptr*/ np, int /*mp_size_t*/ nn, /*const*/ IntPtr /*mp_ptr*/ dp, int /*mp_size_t*/ dn, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "__gmpn_sec_div_qr")] public static extern ulong /*mp_limb_t*/ __gmpn_sec_div_qr_x64(IntPtr /*mp_ptr*/ qp, IntPtr /*mp_ptr*/ np, int /*mp_size_t*/ nn, /*const*/ IntPtr /*mp_ptr*/ dp, int /*mp_size_t*/ dn, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_div_qr_itch(int /*mp_size_t*/ nn, int /*mp_size_t*/ dn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void __gmpn_sec_div_r(IntPtr /*mp_ptr*/ np, int /*mp_size_t*/ nn, /*const*/ IntPtr /*mp_ptr*/ dp, int /*mp_size_t*/ dn, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_div_r_itch(int /*mp_size_t*/ nn, int /*mp_size_t*/ dn); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int __gmpn_sec_invert(IntPtr /*mp_ptr*/ rp, IntPtr /*mp_ptr*/ ap, /*const*/ IntPtr /*mp_ptr*/ mp, int /*mp_size_t*/ n, uint /*mp_bitcnt_t*/ nbcnt, IntPtr /*mp_ptr*/ tp); [DllImport(@"libgmp-10.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int /*mp_size_t*/ __gmpn_sec_invert_itch(int /*mp_size_t*/ n); #endregion } private class SafeHandle : SafeHandleZeroOrMinusOneIsInvalid { public SafeHandle(IntPtr handle) : base(true) { SetHandle(handle); } public IntPtr Handle { get { return handle; } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] protected override bool ReleaseHandle() { gmp_lib.SafeNativeMethods.FreeLibrary(handle); return true; } } } }