fmpz.h
#include <flint/fmpz.h>This module implements the integers . An fmpz_t holds an integer of any size and is the coefficient type of most of FLINT: polynomials, matrices, rationals and number fields over are arrays of fmpz. A value whose absolute value is at most COEFF_MAX is stored directly in one machine word; larger values are stored in a GMP mpz structure that the word points to. The switch between the two forms is automatic, so fmpz_t behaves like GMP's mpz_t, but small values cost no memory allocation and most operations on them run without a function call into GMP. Large values use FLINT's own multiplication, division and square root code where it is faster than GMP's.
Start with Getting started with fmpz, and read The fmpz representation before writing code that accesses the representation or creates large arrays of integers. The naming, memory and aliasing conventions common to all FLINT modules are described in Types, naming and aliasing.
OverviewMultiplying integersHow FLINT multiplies integers of each size — its own assembly basecases, Karatsuba, GMP's Toom–Cook algorithms, and FFT multiplication with fft_small or Schönhage–Strassen — with the algorithms, their costs and the dispatch in flint_mpn_mul.Guides#
Types and conventions#
An fmpz is an slong. If its two most significant bits are equal ( or ), it is an integer with COEFF_MAX, stored directly. If they are , the remaining bits hold a pointer to an mpz_struct shifted right by two bits, and the value is that of the mpz. An fmpz_t is an array of one fmpz, so that it is passed by reference like an mpz_t. A pointer fmpz * to an entry of an array can be passed wherever an fmpz_t is expected.
The largest and the smallest value stored inline: and , that is, on 64-bit and on 32-bit machines. The macro SMALL_FMPZ_BITCOUNT_MAX is .
COEFF_IS_MPZ(f)mpz_ptr COEFF_TO_PTR(fmpz f)fmpz PTR_TO_COEFF(mpz_ptr ptr)Macros converting between an fmpz that holds a pointer and the mpz_ptr itself. PTR_TO_COEFF shifts the pointer right by two bits and sets the tag bits ; this relies on mpz_structs being aligned to at least four bytes. COEFF_TO_PTR must only be applied to values for which COEFF_IS_MPZ is true.
A structure with the same layout as GMP's mpz_struct, declared in fmpz_types.h: the fields are alloc (the number of allocated limbs), size (the number of limbs in use, negated for a negative value) and ptr (the limbs, least significant first). It lets FLINT's headers access the limbs of a large fmpz without including gmp.h.
FMPZ_TO_ZZ(x)Converts an fmpz value x that holds a pointer (COEFF_IS_MPZ(x) true) to a zz_ptr, like COEFF_TO_PTR.
The minimum number of limbs allocated for an mpz owned by an fmpz, currently 2. Every mpz handed out by _fmpz_new_mpz has at least this many limbs, and every mpz given back to _fmpz_clear_mpz must still have at least this many.
The following rules apply to all functions of the module.
- Aliasing. Every output may be the same object as any input, unless stated otherwise. When a function has several outputs, such as a quotient and a remainder, the outputs must be distinct objects.
- Canonical form. A value that fits inline is always stored inline. Every function expects its inputs in this form and produces its outputs in it; only the internal functions documented at the end may leave a small value in an
mpz. See Canonical form. - Errors. Division by zero and other invalid arguments raise an exception through
flint_throw(by default, a message andabort), unless stated otherwise. A few functions document preconditions that are not checked. - Functions with a leading underscore are internal or have additional preconditions, stated in each case.
Memory management#
void fmpz_init2(fmpz_t f, ulong limbs)Initialises to zero with space for limbs limbs. If limbs is zero, this is fmpz_init; otherwise an mpz is attached and grown to limbs limbs, so that is temporarily not in canonical form (its value is 0 in an mpz) until the first assignment. Only useful to avoid reallocations when the final size is known.
void fmpz_clear(fmpz_t f)Releases the memory used by . A large value gives its mpz back to the memory manager (see Memory management); a small value needs no work.
void fmpz_init_set(fmpz_t f, const fmpz_t g)void fmpz_init_set_ui(fmpz_t f, ulong g)void fmpz_init_set_si(fmpz_t f, slong g)Initialise and set it to .
Assignment and conversion#
void fmpz_set_si(fmpz_t f, slong val)void fmpz_set_ui(fmpz_t f, ulong val)void fmpz_neg_ui(fmpz_t f, ulong val)Set to val, and fmpz_neg_ui to val.
void fmpz_set_uiui(fmpz_t f, ulong hi, ulong lo)void fmpz_neg_uiui(fmpz_t f, ulong hi, ulong lo)Set to , respectively to its negative, where and are the unsigned words hi and lo.
void fmpz_set_signed_uiui(fmpz_t r, ulong hi, ulong lo)void fmpz_set_signed_uiuiui(fmpz_t r, ulong hi, ulong mid, ulong lo)Set to the integer whose two's complement representation with (respectively ) bits is hi, lo (respectively hi, mid, lo), the most significant word first. These convert the results of double-word signed arithmetic such as smul_ppmm.
void fmpz_set_ui_array(fmpz_t out, const ulong * in, slong n)void fmpz_set_signed_ui_array(fmpz_t out, const ulong * in, slong n)Set out to the integer with the limbs in[0], ..., in[n-1], least significant first: interpreted as an unsigned integer, or as a two's complement integer of bits.
void fmpz_get_ui_array(ulong * out, slong n, const fmpz_t in)void fmpz_get_signed_ui_array(ulong * out, slong n, const fmpz_t in)Write the limbs of in to out, least significant first. The unsigned version requires in ; the signed version writes in modulo , which is its two's complement representation if it fits in bits.
void fmpz_set_mpn_large(fmpz_t z, nn_srcptr src, slong n, int negative)Sets to the integer with the limbs src, negated if negative is
- Requires and
src[n-1]nonzero, so that the value does not fit inline; usefmpz_set_uiandfmpz_neg_uifor one limb, orfmpz_set_ui_arrayin general.
slong fmpz_get_si(const fmpz_t f)ulong fmpz_get_ui(const fmpz_t f)Return as an slong, respectively an ulong. The result is unspecified if does not fit (use fmpz_fits_si and fmpz_abs_fits_ui to check); currently fmpz_get_ui returns the low limb of , so a negative is not reduced modulo .
void fmpz_get_uiui(ulong * hi, ulong * low, const fmpz_t f)Sets *hi and *low to the high and low limbs of , for .
void fmpz_get_signed_uiui(ulong * hi, ulong * lo, const fmpz_t x)Sets *hi and *lo to the two limbs of modulo , its two's complement representation if fits in bits.
ulong fmpz_get_nmod(const fmpz_t f, nmod_t mod)Returns in for the modulus of mod, for any .
double fmpz_get_d(const fmpz_t f)Returns as a double, rounded towards zero if it is not exactly representable. The result is undefined if exceeds the range of double; use fmpz_get_d_2exp for large values.
void fmpz_set_d(fmpz_t f, double c)Sets to rounded towards zero. The result is undefined if is infinite or a NaN.
double fmpz_get_d_2exp(slong * exp, const fmpz_t f)Returns a double with and sets *exp to such that approximates ; is truncated to 53 bits. For it returns 0 and sets *exp to 0.
void fmpz_set_d_2exp(fmpz_t f, double d, slong exp)Sets to rounded towards zero, so that gives 0.
void fmpz_get_mpz(mpz_t x, const fmpz_t f)void fmpz_set_mpz(fmpz_t f, const mpz_t x)Convert between fmpz_t and an initialised GMP mpz_t, copying the value. Declared only if gmp.h is included before the FLINT headers.
void fmpz_get_mpf(mpf_t x, const fmpz_t f)void fmpz_set_mpf(fmpz_t f, const mpf_t x)Convert between fmpz_t and GMP's mpf_t; fmpz_set_mpf truncates a fractional value towards zero. Declared only if gmp.h is included first.
void fmpz_get_mpfr(mpfr_t x, const fmpz_t f, mpfr_rnd_t rnd)Sets the MPFR number to , rounded in the direction rnd to the precision of . Declared only if mpfr.h is included before the FLINT headers.
int fmpz_get_mpn(nn_ptr * n, fmpz_t n_in)Allocates an array with flint_malloc, stores the limbs of in it, sets *n to it and returns the number of limbs (at least 1). The caller frees the array with flint_free. For the array has one limb, which is 0.
Converting to and from GMP without copying#
These functions give GMP read access to an fmpz_t and FLINT read access to an mpz_t, without copying limbs. The guide Interfacing with GMP shows how to use them. All require gmp.h to be included before the FLINT headers.
void flint_mpz_init_set_readonly(mpz_t z, const fmpz_t f)void flint_mpz_clear_readonly(mpz_t z)flint_mpz_init_set_readonly sets the uninitialised mpz_t to the value of , sharing the limbs of if it is large; must not be modified, must not change while is in use, and is released with flint_mpz_clear_readonly.
void fmpz_init_set_readonly(fmpz_t f, const mpz_t z)void fmpz_clear_readonly(fmpz_t f)fmpz_init_set_readonly sets the uninitialised fmpz_t to the value of , sharing the limbs of if the value is large; must not be modified, must not change while is in use, and is released with fmpz_clear_readonly.
Input and output#
The function page Strings, printing and binary input and output gives the details and an example.
char * fmpz_get_str(char * str, int b, const fmpz_t f)Returns the representation of in base , , with a leading minus sign if . If str is NULL, the string is allocated with flint_malloc and must be freed with flint_free; otherwise it is written to str, which must have room for fmpz_sizeinbase(f, b) + 2 characters.
int fmpz_set_str(fmpz_t f, const char * str, int b)Sets to the integer written in base , , in the null-terminated string str, and returns 0. White space before and after the number and a minus sign are accepted. Returns if the string is not a valid integer, in which case is undefined.
int fmpz_fprint(FILE * file, const fmpz_t x)int fmpz_print(const fmpz_t x)Print in decimal to file or to stdout, with a minus sign if negative and no newline. Return the number of characters written, or a nonpositive value on error.
int fmpz_fread(FILE * file, fmpz_t f)Read a decimal integer (optional white space and minus sign, then digits) from file or from stdin into . Return a positive value on success and 0 on failure, following mpz_inp_str.
size_t fmpz_out_raw(FILE * fout, const fmpz_t x)size_t fmpz_inp_raw(fmpz_t x, FILE * fin)Write to, respectively read it from, a stream in GMP's portable binary format: four bytes holding the signed number of bytes, then the bytes of , most significant first. Files are interchangeable with mpz_out_raw and mpz_inp_raw. Return the number of bytes written or read, and 0 on error.
Random integers#
All functions take a flint_rand_t; see flint.h. They are meant for testing and are not suitable for cryptography.
void fmpz_randbits_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)void fmpz_randbits(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)Set to a uniformly random integer with exactly bits bits (its top bit set), nonnegative for the unsigned version and with a random sign otherwise. For bits the result is 0.
void fmpz_randtest_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)Set to a random integer whose absolute value has a random number of bits between 0 and bits, nonnegative for the unsigned version. The values are biased towards sparse binary representations, 0, 1 and COEFF_MAX, which exercise special cases in tests.
void fmpz_randtest_not_zero(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)As fmpz_randtest, but never 0. Requires bits .
void fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m)Sets to a uniformly random integer in for , and in for .
void fmpz_randm_not_zero(fmpz_t f, flint_rand_t state, const fmpz_t m)Sets to a uniformly random integer in . Requires .
void fmpz_randtest_mod(fmpz_t f, flint_rand_t state, const fmpz_t m)void fmpz_randtest_mod_signed(fmpz_t f, flint_rand_t state, const fmpz_t m)Set to a random integer in , respectively , for , with an increased probability of values near the ends of the range (and near zero for the signed version).
void fmpz_randprime(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits, int proved)Sets to a random prime with exactly bits bits: the next prime after a random integer of that size, retried until it has the right size, so the distribution is not exactly uniform over primes. If proved is nonzero the result is proved prime, otherwise it is a probable prime as in fmpz_nextprime.
Properties and comparison#
int fmpz_is_zero(const fmpz_t f)int fmpz_is_one(const fmpz_t f)int fmpz_is_pm1(const fmpz_t f)Return whether , and .
int fmpz_is_even(const fmpz_t f)int fmpz_is_odd(const fmpz_t f)Return whether is even, respectively odd.
int fmpz_equal(const fmpz_t f, const fmpz_t g)int fmpz_equal_si(const fmpz_t f, slong g)int fmpz_equal_ui(const fmpz_t f, ulong g)Return 1 if and 0 otherwise.
int fmpz_cmp_ui(const fmpz_t f, ulong g)int fmpz_cmp_si(const fmpz_t f, slong g)Return a negative value, zero or a positive value according to whether , or .
int fmpz_cmpabs(const fmpz_t f, const fmpz_t g)int fmpz_cmp2abs(const fmpz_t f, const fmpz_t g)Compare with , respectively with , returning a negative value, zero or a positive value.
flint_bitcnt_t fmpz_bits(const fmpz_t f)Return the number of bits, respectively limbs, of ; both are 0 for .
size_t fmpz_sizeinbase(const fmpz_t f, int b)Returns the number of digits of in base , , and 1 for . Unlike GMP's mpz_sizeinbase, the result is exact for all bases.
flint_bitcnt_t fmpz_val2(const fmpz_t x)Returns the exponent of the largest power of 2 dividing (the number of trailing zero bits), and 0 for .
int fmpz_abs_fits_ui(const fmpz_t f)int fmpz_fits_si(const fmpz_t f)ulong fmpz_abs_lbound_ui_2exp(slong * exp, const fmpz_t x, int bits)ulong fmpz_abs_ubound_ui_2exp(slong * exp, const fmpz_t x, int bits)For and bits , return a mantissa with exactly bits bits and set *exp to such that (lower bound, is truncated), respectively (upper bound, is rounded up, which may increase by one if the rounding carries).
Returns an approximation of for , with a relative error of a few units in the last place of a double.
slong fmpz_flog_ui(const fmpz_t x, ulong b)slong fmpz_clog_ui(const fmpz_t x, ulong b)Return and , computed exactly. Require and .
Addition and multiplication#
Set to , respectively .
void fmpz_add_ui(fmpz_t f, const fmpz_t g, ulong x)void fmpz_add_si(fmpz_t f, const fmpz_t g, slong x)void fmpz_sub_ui(fmpz_t f, const fmpz_t g, ulong x)void fmpz_sub_si(fmpz_t f, const fmpz_t g, slong x)Set to , respectively (with in place of ).
void fmpz_mul_ui(fmpz_t f, const fmpz_t g, ulong x)void fmpz_mul_si(fmpz_t f, const fmpz_t g, slong x)Set to (with in place of ). Two small operands are multiplied inline; large ones go to flint_mpn_mul or flint_mpn_sqr, which switch from basecase code through Karatsuba and GMP's Toom–Cook algorithms to FFT multiplication (Multiplying integers).
void fmpz_mul2_uiui(fmpz_t f, const fmpz_t g, ulong h1, ulong h2)Sets to .
void fmpz_addmul(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_addmul_ui(fmpz_t f, const fmpz_t g, ulong x)void fmpz_addmul_si(fmpz_t f, const fmpz_t g, slong x)void fmpz_submul(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_submul_ui(fmpz_t f, const fmpz_t g, ulong x)void fmpz_submul_si(fmpz_t f, const fmpz_t g, slong x)Set to , respectively (with in place of ), without a temporary for the product when the operands are small.
Full documentationSet to , respectively .
Full documentationvoid fmpz_mul_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_one_2exp(fmpz_t f, ulong exp)Set to , respectively . fmpz_mul_2exp assumes that does not overflow an ulong.
void fmpz_mul_tdiv_q_2exp(fmpz_t f, const fmpz_t g, const fmpz_t h, ulong exp)void fmpz_mul_si_tdiv_q_2exp(fmpz_t f, const fmpz_t g, slong x, ulong exp)Set to (with in place of ), rounded towards zero.
Powers and roots#
The function page Square roots, n-th roots and perfect powers describes the root functions and their algorithms.
void fmpz_pow_ui(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_ui_pow_ui(fmpz_t x, ulong b, ulong e)Set to , respectively to , with .
int fmpz_pow_fmpz(fmpz_t a, const fmpz_t b, const fmpz_t e)Sets to with and returns 1. For and an exponent that does not fit in an slong (the result would not fit in memory), returns 0 and leaves unchanged. Throws an exception if .
void fmpz_sqrtrem(fmpz_t f, fmpz_t r, const fmpz_t g)Set to and to , for . Throw an exception if . In fmpz_sqrtrem, and must be distinct objects.
int fmpz_perfect_sqrt(fmpz_t s, const fmpz_t a)If is a perfect square, sets to and returns 1; otherwise returns 0 and leaves undefined. Faster than fmpz_is_square followed by fmpz_sqrt.
int fmpz_is_square(const fmpz_t f)Returns whether is a perfect square ().
Full documentationSets to the -th root of rounded towards zero and returns 1 if it is exact and 0 otherwise. Requires , and if is even; otherwise an exception is raised.
Full documentationint fmpz_is_perfect_power(fmpz_t root, const fmpz_t f)If for some , sets root to and returns ; otherwise returns 0. Negative are allowed (then is odd). The exponent is not necessarily the largest one: currently gives , and gives . By convention 0 and 1 give and gives .
Division#
Division comes in four rounding conventions: tdiv rounds the quotient towards zero, fdiv towards , cdiv towards and ndiv to the nearest integer. The function page Division and rounding conventions explains them, and Integer division and Newton iteration the algorithms.
void fmpz_tdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)void fmpz_fdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)void fmpz_cdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)Set to the quotient of by , rounded towards zero, and respectively, and to the remainder . The remainder has the sign of (tdiv), of (fdiv) or of (cdiv), and . Throw an exception if .
void fmpz_ndiv_qr(fmpz_t q, fmpz_t r, const fmpz_t a, const fmpz_t b)Sets to rounded to the nearest integer, ties to even, and to , so that . Throws an exception if .
Full documentationvoid fmpz_tdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_fdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_cdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_tdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)void fmpz_fdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)void fmpz_cdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)void fmpz_tdiv_q_si(fmpz_t f, const fmpz_t g, slong h)void fmpz_fdiv_q_si(fmpz_t f, const fmpz_t g, slong h)void fmpz_cdiv_q_si(fmpz_t f, const fmpz_t g, slong h)Set to the quotient of by , rounded towards zero, and respectively. Throw an exception if .
Full documentationvoid fmpz_fdiv_r(fmpz_t f, const fmpz_t g, const fmpz_t h)Sets to the remainder of fmpz_fdiv_qr, which has the sign of . Throws an exception if .
void fmpz_tdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_fdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_cdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_tdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_fdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)void fmpz_cdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)Set to the quotient, respectively the remainder, of by with the rounding of the prefix. fmpz_fdiv_q_2exp is an arithmetic right shift and fmpz_fdiv_r_2exp the reduction to .
ulong fmpz_tdiv_ui(const fmpz_t g, ulong h)ulong fmpz_fdiv_ui(const fmpz_t g, ulong h)ulong fmpz_cdiv_ui(const fmpz_t g, ulong h)Return the absolute value of the remainder of by with the rounding of the prefix. fmpz_fdiv_ui thus returns in . Throw an exception if .
void fmpz_divexact(fmpz_t f, const fmpz_t g, const fmpz_t h)void fmpz_divexact_ui(fmpz_t f, const fmpz_t g, ulong h)void fmpz_divexact_si(fmpz_t f, const fmpz_t g, slong h)Set to , assuming that divides . This is not checked; if the division is not exact the result is meaningless. Faster than the other divisions, since it uses 2-adic (Hensel) division. Throw an exception if .
Full documentationvoid fmpz_divexact2_uiui(fmpz_t f, const fmpz_t g, ulong h1, ulong h2)Sets to , assuming that divides . Throws an exception if or is 0.
Full documentationint fmpz_div_ui(fmpz_t q, const fmpz_t g, ulong h)int fmpz_div_si(fmpz_t q, const fmpz_t g, slong h)Checked exact division: if divides , set and return 1; otherwise set and return 0. Division by zero counts as exact only for (with ).
Full documentationint fmpz_divides(fmpz_t q, const fmpz_t g, const fmpz_t h)Older name for fmpz_div.
int fmpz_divisible(const fmpz_t f, const fmpz_t g)Returns whether divides , that is, whether for an integer . Every divides 0, and 0 divides only 0.
int fmpz_divisible_ui(const fmpz_t f, ulong g)int fmpz_divisible_si(const fmpz_t f, slong g)Return whether divides . As for fmpz_divisible, every divides 0, and 0 divides only 0.
A precomputed inverse of a divisor for fmpz_fdiv_qr_preinvn: the fields are dinv (an -limb approximation of the reciprocal of , computed by flint_mpn_preinvn, where is shifted left by norm bits to have its top bit set) and n (the number of limbs of ).
void fmpz_preinvn_init(fmpz_preinvn_t inv, const fmpz_t f)void fmpz_preinvn_clear(fmpz_preinvn_t inv)Compute a precomputed inverse of , respectively release it.
void fmpz_fdiv_qr_preinvn(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h, const fmpz_preinvn_t inv)void fmpz_fdiv_r_preinvn(fmpz_t f, const fmpz_t g, const fmpz_t h, const fmpz_preinvn_t inv)As fmpz_fdiv_qr and fmpz_fdiv_r, using the precomputed inverse inv of , for many divisions by the same multi-limb .
Modular arithmetic#
The function page Modular arithmetic with integers describes inversion and exponentiation.
Sets to in . Requires , which is not checked (a zero divisor crashes with a division by zero).
Full documentationulong fmpz_mod_ui(fmpz_t f, const fmpz_t g, ulong h)Sets to in and also returns it. Throws an exception if .
Sets to the symmetric remainder with . Requires (not checked).
Full documentationvoid fmpz_set_ui_smod(fmpz_t f, ulong x, ulong m)Sets to the symmetric remainder with , for (not checked).
void fmpz_negmod(fmpz_t r, const fmpz_t a, const fmpz_t mod)Sets to modulo mod for mod (not checked).
int fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)If , sets to the inverse of modulo in and returns 1; otherwise returns 0 and is undefined. Every is invertible modulo , with inverse 0. Throws an exception if .
Full documentationvoid fmpz_powm_ui(fmpz_t f, const fmpz_t g, ulong exp, const fmpz_t m)Set to in , for (an exception is raised otherwise). For the result is 0, and otherwise . fmpz_powm accepts if is invertible modulo and raises an exception if it is not.
int fmpz_sqrtmod(fmpz_t b, const fmpz_t a, const fmpz_t p)For a prime : if is a square modulo , sets to a square root and returns 1, otherwise sets to 0 and returns 0. Primality of is not checked; for composite a return value of 1 means nothing.
Full documentationslong fmpz_remove(fmpz_t rop, const fmpz_t op, const fmpz_t f)Sets rop to op with all factors removed and returns their number, the largest with op. Requires (an exception is raised otherwise). For op , rop is set to 0 and 0 is returned.
int fmpz_jacobi(const fmpz_t a, const fmpz_t p)Returns the Jacobi symbol for any and odd . The conditions on are not checked.
int fmpz_kronecker(const fmpz_t a, const fmpz_t n)Returns the Kronecker symbol for any and .
void fmpz_divides_mod_list(fmpz_t xstart, fmpz_t xstride, fmpz_t xlength, const fmpz_t a, const fmpz_t b, const fmpz_t n)Sets the outputs so that the solutions of are exactly for ; there are none if xlength is 0. Requires ; the outputs must be distinct objects.
Arithmetic modulo powers of two#
These functions use 2-adic Newton iteration on the limbs (Exact division). Results are reduced to .
void fmpz_invmod_2exp(fmpz_t rop, const fmpz_t op, flint_bitcnt_t N)Sets rop to the inverse of op modulo . Throws an exception if op is even.
void fmpz_divmod_2exp(fmpz_t rop, const fmpz_t a, const fmpz_t b, flint_bitcnt_t N)Hensel division: sets rop to the unique with . Throws an exception if is even.
int fmpz_sqrtmod_2exp(fmpz_t rop, const fmpz_t op, flint_bitcnt_t N)int fmpz_rsqrtmod_2exp(fmpz_t rop, const fmpz_t op, flint_bitcnt_t N)If op is an odd square modulo (for : op ), set rop to a square root , respectively an inverse square root (), modulo , and return 1; otherwise return 0. The root returned is the one congruent to 1 modulo 4, reduced modulo . Even op is not supported (0 is returned).
Greatest common divisor#
The function page GCD, LCM and Bézout coefficients explains the extended GCD variants and their normalisations; Integer GCD describes the algorithms.
void fmpz_gcd_ui(fmpz_t res, const fmpz_t a, ulong b)Set to , with .
Full documentationSets to . Faster than two calls to fmpz_gcd when one operand is small or the GCD of the two shortest operands is 1.
Sets to the least common multiple of and , which is nonnegative, and 0 if or is 0.
Full documentationSets and cofactors , with . The outputs must be distinct objects.
Full documentationAs fmpz_xgcd, but the cofactors are the canonical Bézout coefficients defined on GCD, LCM and Bézout coefficients: the minimal ones, with fixed choices in the degenerate cases. Usually faster than fmpz_xgcd.
void fmpz_gcdinv(fmpz_t d, fmpz_t a, const fmpz_t f, const fmpz_t g)For , sets and to a cofactor in with , so that if . For it sets and . The outputs must be distinct objects.
Full documentationRuns the extended Euclidean algorithm on until the remainder drops to at most , with Lehmer's acceleration. On return and are the last two remainders and co2, co1 the corresponding cofactors, with co2 co1. No aliasing is allowed.
Chinese remaindering#
The function page Chinese remaindering compares the CRT interfaces and has examples; Remainder trees and the Chinese remainder theorem describes the product and remainder trees used for many moduli.
void fmpz_CRT(fmpz_t out, const fmpz_t r1, const fmpz_t m1, const fmpz_t r2, const fmpz_t m2, int sign)Set out to the unique , in if sign is 0, and in otherwise. Require coprime , and ; only the coprimality is checked (by fmpz_CRT, which raises an exception).
void _fmpz_CRT_ui_precomp(fmpz_t out, const fmpz_t r1, const fmpz_t m1, ulong r2, ulong m2, ulong m2inv, const fmpz_t m1m2, ulong c, int sign)As fmpz_CRT_ui with precomputed data: m2inv n_preinvert_limb(m2), m1m2 and . Used when the same pair of moduli recurs.
Precomputed data for reduction modulo, and reconstruction from, a fixed list of word-size moduli (fmpz_comb_t), and the scratch space for one call (fmpz_comb_temp_t). An fmpz_comb_t is a thin wrapper around the flint_mpn_crt_t of mpn_extras.
Flags for fmpz_comb_init2 selecting the precomputation for fmpz_multi_mod_ui and fmpz_multi_CRT_ui respectively.
void fmpz_comb_init(fmpz_comb_t C, nn_srcptr primes, slong num_primes)void fmpz_comb_init2(fmpz_comb_t C, nn_srcptr primes, slong num_primes, int flags)void fmpz_comb_clear(fmpz_comb_t C)Initialise C for the pairwise coprime nonzero word-size moduli in primes (they need not be prime; the array is copied), respectively release it. fmpz_comb_init2 precomputes only what the operations in flags (FMPZ_COMB_MOD, FMPZ_COMB_CRT or both) need.
void fmpz_comb_temp_init(fmpz_comb_temp_t CT, const fmpz_comb_t C)void fmpz_comb_temp_clear(fmpz_comb_temp_t CT)Allocate, respectively free, scratch space for calls with C. A scratch space is not thread-safe; each thread needs its own.
void fmpz_multi_mod_ui(ulong * out, const fmpz_t in, const fmpz_comb_t C, fmpz_comb_temp_t CT)Sets out[i] to in modulo the -th modulus of C, in , for any in.
void fmpz_multi_CRT_ui(fmpz_t output, nn_srcptr residues, const fmpz_comb_t comb, fmpz_comb_temp_t temp, int sign)Sets output to the integer congruent to residues[i] modulo each modulus of comb, in if sign is 0 and in otherwise, where is the product of the moduli.
void fmpz_multi_mod_ui_once(ulong * out, const fmpz_t in, nn_srcptr primes, slong num_primes)void fmpz_multi_CRT_ui_once(fmpz_t output, fmpz_t prod, nn_srcptr residues, nn_srcptr primes, slong num_primes, int sign)One-shot versions of fmpz_multi_mod_ui and fmpz_multi_CRT_ui that do not need an fmpz_comb_t. If prod is not NULL, the CRT version also sets it to the product of the moduli.
Precomputed data for Chinese remaindering with a fixed list of moduli of any size.
void fmpz_multi_CRT_init(fmpz_multi_CRT_t CRT)void fmpz_multi_CRT_clear(fmpz_multi_CRT_t P)Initialise CRT, respectively release P.
int fmpz_multi_CRT_precompute(fmpz_multi_CRT_t CRT, const fmpz * moduli, slong len)Prepares CRT for the moduli (signs are ignored) and returns 1 if they are usable: one nonzero modulus, or several that are pairwise coprime and none of which is or . Otherwise returns 0, and later reconstructions give undefined results.
void fmpz_multi_CRT_precomp(fmpz_t output, const fmpz_multi_CRT_t P, const fmpz * inputs, int sign)Sets output to the integer congruent to inputs[i] modulo the -th modulus of P, for any values inputs[i]: in if sign is 0 and in otherwise, where is the product of the absolute values of the moduli.
void _fmpz_multi_CRT_precomp(fmpz * outputs, const fmpz_multi_CRT_t P, const fmpz * inputs, int sign)As fmpz_multi_CRT_precomp, with the result in outputs[0] and the other entries of the array, which must have P->localsize initialised entries, used as scratch space.
int fmpz_multi_CRT(fmpz_t output, const fmpz * moduli, const fmpz * values, slong len, int sign)Combines fmpz_multi_CRT_precompute and fmpz_multi_CRT_precomp and returns the value of the former.
Precomputed data for reducing an integer modulo a fixed list of moduli of any size.
void fmpz_multi_mod_init(fmpz_multi_mod_t P)void fmpz_multi_mod_clear(fmpz_multi_mod_t P)Initialise, respectively release, P.
int fmpz_multi_mod_precompute(fmpz_multi_mod_t P, const fmpz * f, slong r)Prepares P for reduction modulo the moduli f (signs are ignored) and returns 1 if none of them is zero, 0 otherwise.
void fmpz_multi_mod_precomp(fmpz * outputs, const fmpz_multi_mod_t P, const fmpz_t input, int sign)void _fmpz_multi_mod_precomp(fmpz * outputs, const fmpz_multi_mod_t P, const fmpz_t input, int sign, fmpz * tmp)Set outputs[i] to input modulo the -th modulus of P: in if sign is 0 and in otherwise. The underscore version takes scratch space tmp of P->localsize initialised entries.
Bit operations#
Bit operations act on the infinite two's complement representation, as in GMP: a negative integer has infinitely many leading one bits. The function page Bit operations has examples.
int fmpz_tstbit(const fmpz_t f, ulong i)Returns bit of (0 or 1).
Full documentationvoid fmpz_setbit(fmpz_t f, ulong i)void fmpz_clrbit(fmpz_t f, ulong i)void fmpz_combit(fmpz_t f, ulong i)Set, clear and complement bit of .
Full documentationvoid fmpz_complement(fmpz_t r, const fmpz_t f)Sets to the one's complement of .
Full documentationSet to the bitwise and, inclusive or and exclusive or of and .
Full documentationflint_bitcnt_t fmpz_popcnt(const fmpz_t c)Returns the number of one bits of . For negative (infinitely many one bits) it returns 0.
Full documentationint fmpz_bit_pack(nn_ptr arr, flint_bitcnt_t shift, flint_bitcnt_t bits, const fmpz_t coeff, int negate, int borrow)int fmpz_bit_unpack(fmpz_t coeff, nn_srcptr arr, flint_bitcnt_t shift, flint_bitcnt_t bits, int negate, int borrow)void fmpz_bit_unpack_unsigned(fmpz_t coeff, nn_srcptr arr, flint_bitcnt_t shift, flint_bitcnt_t bits)Pack an integer into, respectively extract it from, a field of bits bits starting at bit shift of the limb array arr. Signed fields are stored in two's complement with a borrow that propagates to the next field. These are the building blocks of Kronecker substitution in fmpz_poly.
Combinatorial functions#
Binary splitting and product trees explains the product trees behind these functions.
void fmpz_fac_ui(fmpz_t f, ulong n)Sets to , from a table for (on 64-bit machines) and with GMP's mpz_fac_ui otherwise.
void fmpz_bin_uiui(fmpz_t res, ulong n, ulong k)Sets res to the binomial coefficient , which is 0 for .
void fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong n)void fmpz_rfac_uiui(fmpz_t r, ulong x, ulong n)Set to the rising factorial , with value 1 for . Any sign of is allowed.
void _fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong a, ulong b)Sets to for and , by binary splitting with several factors packed per word at the leaves.
void fmpz_fib_ui(fmpz_t f, ulong n)Sets to the Fibonacci number .
void fmpz_primorial(fmpz_t res, ulong n)Sets res to the primorial , the product of the primes , by a balanced product tree over word-size factors.
void fmpz_euler_phi(fmpz_t res, const fmpz_t n)Sets res to Euler's totient , the number of with , and to 0 for . Word-size use n_euler_phi; larger are factored. The function fmpz_factor_euler_phi of fmpz_factor takes a factorisation.
int fmpz_moebius_mu(const fmpz_t n)Returns the Möbius function : 0 if has a square factor , otherwise for prime factors; . Factors unless it fits in a word.
void fmpz_divisor_sigma(fmpz_t res, ulong k, const fmpz_t n)Sets res to , the sum of the -th powers of the positive divisors of , and to 0 for . Factors .
Primality testing#
Primality proving: APRCL and ECPP describes the proofs.
int fmpz_is_probabprime(const fmpz_t p)Returns 0 if is composite and 1 if it is a probable prime. For up to about 81 bits on 64-bit machines (where n_is_prime and n_ll_is_prime decide primality), the answer is exact; otherwise the test is trial division, a perfect square check and the Baillie–PSW test (a base-2 strong probable prime test and a Lucas test), for which no composite that passes is known.
int fmpz_is_prime(const fmpz_t n)Returns 1 if is prime and 0 otherwise, with a proof.
Moreint fmpz_is_strong_probabprime(const fmpz_t n, const fmpz_t a)Returns 1 if is a strong probable prime to base , and 0 otherwise.
int fmpz_is_probabprime_lucas(const fmpz_t n)Returns 1 if is a Lucas probable prime with parameters chosen by Selfridge's method A [PSW1980], and 0 otherwise. No prime is declared composite.
int fmpz_is_probabprime_BPSW(const fmpz_t n)Returns 1 if passes the Baillie–PSW test [PSW1980] (a strong probable prime test to base 2 and a Lucas test), and 0 otherwise. No composite passing it is known, and no prime fails it.
int fmpz_is_prime_pseudosquare(const fmpz_t n)Returns 0 if is composite and 1 if it is proved prime by the pseudosquare test (Theorem 2.7 of [LukPatWil1996]), using a table of pseudosquares up to . Returns if is too large (above about 94 bits).
The test of Pocklington and the test of Morrison for odd (non-square for Morrison), given the num_pm1 (num_pp1) primes pm1 (pp1) dividing (). Return 0 if is composite. Otherwise they set to the factored part of and to and return 1, which proves that every prime factor of is (respectively ). This proves prime if (respectively ).
void _fmpz_nm1_trial_factors(const fmpz_t n, nn_ptr pm1, slong * num_pm1, ulong limit)void _fmpz_np1_trial_factors(const fmpz_t n, nn_ptr pp1, slong * num_pp1, ulong limit)Trial factor (respectively ) by the primes up to about limit and store the primes found in the array, setting its length. The array needs room for entries. A limit of a small multiple of is a reasonable choice for the tests above.
void fmpz_lucas_chain_full(fmpz_t Vm, fmpz_t Vm1, const fmpz_t A, const fmpz_t B, const fmpz_t m, const fmpz_t n)Set and modulo for the Lucas sequence , , (respectively ), by the doubling formulas. No aliasing is allowed.
void fmpz_lucas_chain_double(fmpz_t U2m, fmpz_t U2m1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t A, const fmpz_t B, const fmpz_t n)void fmpz_lucas_chain_add(fmpz_t Umn, fmpz_t Umn1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t Un, const fmpz_t Un1, const fmpz_t A, const fmpz_t B, const fmpz_t n)void fmpz_lucas_chain_mul(fmpz_t Ukm, fmpz_t Ukm1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t A, const fmpz_t B, const fmpz_t k, const fmpz_t n)For the Lucas sequence , , modulo : given (and ), compute , respectively , respectively . Each output may alias the corresponding input ( with , and so on); no other aliasing is allowed.
void fmpz_lucas_chain_VtoU(fmpz_t Um, fmpz_t Um1, const fmpz_t Vm, const fmpz_t Vm1, const fmpz_t A, const fmpz_t B, const fmpz_t Dinv, const fmpz_t n)Given and modulo , sets and , where Dinv is the inverse of modulo . may alias and may alias .
void fmpz_nextprime(fmpz_t res, const fmpz_t n, int proved)Sets res to the smallest prime greater than (2 for ). If proved is nonzero, the result is proved prime with fmpz_is_prime; otherwise it is a probable prime (n_nextprime for small , GMP's mpz_nextprime, a Baillie–PSW test, otherwise).
int fmpz_divisor_in_residue_class_lenstra(fmpz_t fac, const fmpz_t n, const fmpz_t r, const fmpz_t s)Lenstra's algorithm: for with , finds a divisor of congruent to modulo if there is one, sets fac to it and returns 1; otherwise returns 0. Efficient when , as in the final step of the primality tests.
Internal functions#
These functions manage the representation directly. They are needed only to write new low-level functions; see The fmpz representation. Those returning mpz_ptr are declared only if gmp.h is included before the FLINT headers.
mpz_ptr _fmpz_new_mpz(void)void _fmpz_clear_mpz(fmpz f)Take an initialised mpz with at least MPZ_MIN_ALLOC limbs from the memory manager, respectively give back the mpz that the large value points to. The mpz given back must still have at least MPZ_MIN_ALLOC limbs.
void _fmpz_cleanup_mpz_content(void)void _fmpz_cleanup(void)Free the mpzs cached by the current thread (the second also frees the cache array). Called by flint_cleanup; they do nothing in builds configured with --enable-reentrant.
mpz_ptr _fmpz_promote(fmpz_t f)mpz_ptr _fmpz_promote_val(fmpz_t f)Ensure that holds an mpz and return it. _fmpz_promote does not preserve the value of a small (the mpz holds garbage); it is used before overwriting . _fmpz_promote_val preserves the value. Either may leave temporarily non-canonical.
void _fmpz_demote(fmpz_t f)void _fmpz_demote_val(fmpz_t f)_fmpz_demote releases the mpz of , if any, and sets to 0 (any value then fits inline). _fmpz_demote_val moves the value of back inline if it fits, restoring canonical form.
void _fmpz_promote_set_ui(fmpz_t f, ulong v)void _fmpz_promote_set_si(fmpz_t f, slong v)void _fmpz_promote_neg_ui(fmpz_t f, ulong v)void _fmpz_promote_set_uiui(fmpz_t f, ulong hi, ulong lo)void _fmpz_promote_neg_uiui(fmpz_t f, ulong hi, ulong lo)void _fmpz_init_promote_set_ui(fmpz_t f, ulong v)void _fmpz_init_promote_set_si(fmpz_t f, slong v)Out-of-line parts of the inline assignment functions: set an initialised (_init_ versions: an uninitialised) to a value that does not fit inline. Calling them with a value that fits leaves non-canonical.
void _fmpz_init_readonly_mpz(fmpz_t f, const mpz_t z)void _fmpz_clear_readonly_mpz(mpz_t z)Helpers of the read-only conversions: wrap the mpz in a fresh mpz slot of , respectively release the mpz of such an when its value is small.
int _fmpz_is_canonical(const fmpz_t x)Returns whether is in canonical form: inline, or an mpz whose value does not fit inline and whose top limb is nonzero. Used in tests.
void _fmpz_smod(fmpz_t r, const fmpz_t a, const fmpz_t m, int sign, fmpz_t t)Sets to modulo in a range selected by sign: for sign the remainder of truncating division (, sign of ), for 0 that of floor division (, sign of ), and for sign the symmetric remainder . The scratch variable must not alias any other argument.
slong _fmpz_remove(fmpz_t x, const fmpz_t f, double finv)Divides in place by the largest power of dividing it and returns the exponent. finv must be n_precompute_inverse(f) if is small and 0 otherwise. and must be distinct.
References#
- [PSW1980] C. Pomerance, J. L. Selfridge and S. S. Wagstaff, Jr., "The pseudoprimes to 25·10^9", Mathematics of Computation 35 (1980), 1003–1026
- [LukPatWil1996] R. F. Lukes and C. D. Patterson and H. C. Williams "Some results on pseudosquares" Math. Comp. 1996, no. 65, 361–372
The FLINT team, “fmpz.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/fmpz/index.html (generated 2026-11-02).
@misc{flint-doc-fmpz-index,
author = {{The FLINT team}},
title = {{fmpz.h (module overview)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/fmpz/index.html},
note = {Generated 2026-11-02}
}