Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
Published by the Free Software Foundation
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.
This library is free; this means that everyone is free to use it and free to redistribute it on a free basis. The library is not in the public domain; it is copyrighted and there are restrictions on its distribution, but these restrictions are designed to permit everything that a good cooperating citizen would want to do. What is not allowed is to try to prevent others from further sharing any version of this library that they might get from you.
Specifically, we want to make sure that you have the right to give away copies of the library, that you receive source code or else can get it if you want it, that you can change this library or use pieces of it in new free programs, and that you know you can do these things.
To make sure that everyone has such rights, we have to forbid you to deprive anyone else of these rights. For example, if you distribute copies of the GNU MP library, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights.
Also, for our own protection, we must make certain that everyone finds out that there is no warranty for the GNU MP library. If it is modified by someone else and passed on, we want their recipients to know that what they have is not what we distributed, so that any problems introduced by others will not reflect on our reputation.
The precise conditions of the license for the GNU MP library are found in the Library General Public License that accompany the source code.
GNU MP is a portable library written in C for arbitrary precision arithmetic on integers, rational numbers, and floating-point numbers. It aims to provide the fastest possible arithmetic for all applications that need higher precision than is directly supported by the basic C types.
Many applications use just a few hundred bits of precision; but some applications may need thousands or even millions of bits. MP is designed to give good performance for both, by choosing algorithms based on the sizes of the operands, and by carefully keeping the overhead at a minimum.
The speed of MP is achieved by using fullwords as the basic arithmetic type, by using sophisticated algorithms, by including carefully optimized assembly code for the most common inner loops for many different CPUs, and by a general emphasis on speed (as opposed to simplicity or elegance).
There is carefully optimized assembly code for these CPUs: DEC Alpha, Amd 29000, HPPA 1.0 and 1.1, Intel Pentium and generic x86, Intel i960, Motorola MC68000, MC68020, MC88100, and MC88110, Motorola/IBM PowerPC, National NS32000, IBM POWER, MIPS R3000, R4000, SPARCv7, SuperSPARC, generic SPARCv8, and DEC VAX. Some optimizations also for ARM, Clipper, IBM ROMP (RT), and Pyramid AP/XP.
This version of MP is released under a more liberal license than previous versions. It is now permitted to link MP to non-free programs, as long as MP source code is provided when distributing the non-free program.
Everyone should read section MP Basics. If you need to install the library yourself, you need to read section Installing MP, too.
The rest of the manual can be used for later reference, although it is probably a good idea too glance through it.
To build MP, you first have to configure it for your CPU and operating system. You need a C compiler, preferably GCC, but any reasonable compiler should work. And you need a standard Unix `make' program, plus some other standard Unix utility programs.
(If you're on a MS-DOS machine, your can build MP using `make.bat'. It requires that djgpp is installed. It does not require configuration, nor is `make' needed; `make.bat' both configures and builds the library.)
Here are the steps needed to install the library on Unix systems:
gcc
in your PATH
, it will be used by default. To
override this, pass `-with-gcc=no' to `configure'.
If you wish to build and install the BSD MP compatible functions, use `make libmp.a' and `make install-bsdmp'.
There are some other useful make targets:
GCC 2.7.2 (as well as 2.6.3) for the RS/6000 and PowerPC can not be used to compile MP, due to a bug in GCC. If you want to use GCC for these machines, you need to apply the patch below to GCC, or use a later version of the compiler.
If you are on a Sequent Symmetry, use the GNU assembler instead of the system's assembler, since the latter has serious bugs.
The system compiler on NeXT is a massacred and old gcc, even if the compiler calls itself `cc'. This compiler cannot be used to build MP. You need to get a real gcc, and install that before you compile MP. (NeXT might have fixed this in newer releases of their system.)
Please report other problems to `bug-gmp@prep.ai.mit.edu'. See section Reporting Bugs.
Patch to apply to GCC 2.6.3 and 2.7.2:
*** config/rs6000/rs6000.md Sun Feb 11 08:22:11 1996 --- config/rs6000/rs6000.md.new Sun Feb 18 03:33:37 1996 *************** *** 920,926 **** (set (match_operand:SI 0 "gpc_reg_operand" "=r") (not:SI (match_dup 1)))] "" ! "nor. %0,%2,%1" [(set_attr "type" "compare")]) (define_insn "" --- 920,926 ---- (set (match_operand:SI 0 "gpc_reg_operand" "=r") (not:SI (match_dup 1)))] "" ! "nor. %0,%1,%1" [(set_attr "type" "compare")]) (define_insn ""
All declarations needed to use MP are collected in the include file `gmp.h'. It is designed to work with both C and C++ compilers.
In this manual, integer usually means a multiple precision integer, as
defined by the MP library. The C data type for such integers is mpz_t
.
Here are some examples of how to declare such integers:
mpz_t sum; struct foo { mpz_t x, y; }; mpz_t vec[20];
Rational number means a multiple precision fraction. The C data type
for these fractions is mpq_t
. For example:
mpq_t quotient;
Floating point number or Float for short, is an arbitrary precision
mantissa with an limited precision exponent. The C data type for such objects
is mpf_t
.
A limb means the part of a multi-precision number that fits in a single
word. (We chose this word because a limb of the human body is analogous to a
digit, only larger, and containing several digits.) Normally a limb contains
32 or 64 bits. The C data type for a limb is mp_limb_t
.
There are six classes of functions in the MP library:
mpz_
. The associated type is mpz_t
. There are about 100
functions in this class.
mpq_
. The associated type is mpq_t
. There are about 20
functions in this class, but the functions in the previous class can be used
for performing arithmetic on the numerator and denominator separately.
mpf_
. The associated type is mpf_t
. There are about 50
functions is this class.
itom
, madd
, and
mult
. The associated type is MINT
.
mpn_
. There are about 30 (hard-to-use) functions in this class.
The associated type is array of mp_limb_t
.
As a general rule, all MP functions expect output arguments before input arguments. This notation is based on an analogy with the assignment operator. (The BSD MP compatibility functions disobey this rule, having the output argument(s) last.)
MP allows you to use the same variable for both input and output in the same
expression. For example, the main function for integer multiplication,
mpz_mul
, can be used like this: mpz_mul (x, x, x);
. This
computes the square of x and put the result back in x.
Before you can assign to an MP variable, you need to initialize it by calling one of the special initialization functions. When you're done with a variable, you need to clear it out, using one of the functions for that purpose. Which function to use depends on the type of variable. See the chapters on integer functions, rational number functions, and floating-point functions for details.
A variable should only be initialized once, or at least cleared out between each initialization. After a variable has been initialized, it may be assigned to any number of times.
For efficiency reasons, avoid to initialize and clear out a variable in loops. Instead, initialize it before entering the loop, and clear it out after the loop has exited.
You don't need to be concerned about allocating additional space for MP variables. All functions in MP automatically allocate additional space when a variable does not already have enough space. They do not, however, reduce the space when a smaller number is stored in the object. Most of the time, this policy is best, since it avoids frequent re-allocation.
This version of MP is upward compatible with previous versions of MP, with a few exceptions.
mpz_div
, mpz_divmod
, mpz_mdiv
,
mpz_mdivmod
, etc) now all use floor rounding (i.e., they round the
quotient to -infinity). There are a lot of new functions for integer
division, giving the user better control over the rounding.
mpz_mod
now compute the true mod function.
mpz_powm
and mpz_powm_ui
now use
mod for reduction.
mpq_canonicalize
. This change was made for efficiency.
mpz_out_raw
in this release cannot be read
by mpz_inp_raw
in previous releases. This change was made for making
the file format truly portable between machines with different word sizes.
mpn
functions have changed. But they were intentionally
undocumented in previous releases.
The latest version of the MP library is available by anonymous ftp from from `prep.ai.mit.edu'. The file name is `/pub/gnu/gmp-M.N.tar.gz'. Many sites around the world mirror `prep'; please use a mirror site near you.
If you think you have found a bug in the MP library, please investigate it and report it. We have made this library available to you, and it is not to ask too much from you, to ask you to report the bugs that you find.
There are a few things you should think about when you put your bug report together.
You have to send us a test case that makes it possible for us to reproduce the bug. Include instructions on how to run the test case.
You also have to explain what is wrong; if you get a crash, or if the results printed are incorrect and in that case, in what way.
It is not uncommon that an observed problem is actually due to a bug in the compiler used when building MP; the MP code tends to explore interesting corners in compilers. Therefore, please include compiler version information in your bug report. This can be extracted using `what `which cc`', or, if you're using gcc, `gcc -v'. Also, include the output from `uname -a'.
If your bug report is good, we will do our best to help you to get a corrected version of the library; if the bug report is poor, we won't do anything about it (aside of chiding you to send better bug reports).
Send your bug report to: `bug-gmp@prep.ai.mit.edu'.
If you think something in this manual is unclear, or downright incorrect, or if the language needs to be improved, please send a note to the same address.
This chapter describes the MP functions for performing integer arithmetic.
These functions start with the prefix mpz_
.
Arbitrary precision integers are stored in objects of type mpz_t
.
The functions for integer arithmetic assume that all integer objects are
initialized. You do that by calling the function mpz_init
.
mpz_clear
) between each initialization.
Here is an example of using mpz_init
:
{ mpz_t integ; mpz_init (integ); ... mpz_add (integ, ...); ... mpz_sub (integ, ...); /* Unless the program is about to exit, do ... */ mpz_clear (integ); }
As you can see, you can store new values any number of times, once an object is initialized.
mpz_t
variables when you are done with them.
This function is useful for decreasing the working set for some algorithms that use large integer arrays.
There is no way to de-allocate the storage allocated by this function.
Don't call mpz_clear
!
These functions assign new values to already initialized integers (see section Initialization and Assignment Functions).
This function returns 0 if the entire string up to the '\0' is a valid number in base base. Otherwise it returns -1.
For convenience, MP provides a parallel series of initialize-and-set functions
which initialize the output and then store the value there. These functions'
names have the form mpz_init_set...
Here is an example of using one:
{ mpz_t pie; mpz_init_set_str (pie, "3141592653589793238462643383279502884", 10); ... mpz_sub (pie, ...); ... mpz_clear (pie); }
Once the integer has been initialized by any of the mpz_init_set...
functions, it can be used as the source or destination operand for the ordinary
integer functions. Don't use an initialize-and-set function on a variable
already initialized!
If the string is a correct base base number, the function returns 0;
if an error occurs it returns -1. rop is initialized even if
an error occurs. (I.e., you have to call mpz_clear
for it.)
This section describes functions for converting arbitrary precision integers to standard C types. Functions for converting to arbitrary precision integers are described in section Assignment Functions and section Input and Output Functions.
mpz_tdiv_q_2exp(..., op, CHAR_BIT*sizeof(unsigned
long int))
can be used to extract the limbs of an integer efficiently.
signed long int
return the value of op.
Otherwise return the least significant part of op, with the same sign
as op.
If op is too large to fit in a signed long int
, the returned
result is probably not very useful.
If str is NULL, space for the result string is allocated using the default allocation function, and a pointer to the string is returned.
If str is not NULL, it should point to a block of storage enough large
for the result. To find out the right amount of space to provide for
str, use mpz_sizeinbase (op, base) + 2
. The two
extra bytes are for a possible minus sign, and for the terminating null
character.
Division is undefined if the divisor is zero, and passing a zero divisor to
the divide or modulo functions, as well passing a zero mod argument to the
mpz_powm
and mpz_powm_ui
functions, will make these functions
intentionally divide by zero. This gives the user the possibility to handle
arithmetic exceptions in these functions in the same manner as other
arithmetic exceptions.
There are three main groups of division functions:
mpz_tdiv
. The `t' in the name is short for
`truncate'.
mpz_fdiv
. The `f' in the name is
short for `floor'.
mpz_cdiv
. The `c' in the name is
short for `ceil'.
For each rounding mode, there are a couple of variants. Here `q' means that the quotient is computed, while `r' means that the remainder is computed. Functions that compute both the quotient and remainder have `qr' in the name.
If rop1 and rop2 are the same variable, the results are undefined.
For mpz_fdiv_r_ui
the remainder is small enough to fit in an
unsigned long int
, and is therefore returned.
For mpz_fdiv_qr_ui
the remainder is small enough to fit in an
unsigned long int
, and is therefore returned.
If rop1 and rop2 are the same variable, the results are undefined.
mpz_fdiv_r_ui
, but the remainder is only
returned; it is not stored anywhere.
For mpz_cdiv_r_ui
the negated remainder is small enough to fit in an
unsigned long int
, and it is therefore returned.
For mpz_cdiv_qr_ui
the negated remainder is small enough to fit in an
unsigned long int
, and it is therefore returned.
If rop1 and rop2 are the same variable, the results are undefined.
mpz_cdiv_r_ui
. (The
difference is that this function doesn't store the remainder anywhere.)
mod
op2. The sign of the divisor is
ignored, and the result is always non-negative.
For mpz_mod_ui
the remainder is small enough to fit in an
unsigned long int
, and is therefore returned.
Since mpz_divexact is much faster than any of the other routines that produce the quotient (see section References Jebelean), it is the best choice for instances in which exact division is known to occur, such as reducing a rational to lowest terms.
mpz_sqrt
. Set rop2 to
(i.e., zero if op is a perfect square).
If rop1 and rop2 are the same variable, the results are undefined.
An implementation of the probabilistic primality test found in Seminumerical Algorithms (see section References Knuth).
mod
mod. If
exp is negative, the result is undefined.
If the result is small enough to fit in an unsigned long int
, it is
returned. If the result does not fit, 0 is returned, and the result is equal
to the argument op1. Note that the result will always fit if op2
is non-zero.
gcd
(a, b). If t is
NULL, that argument is not computed.
This operation can also be defined as masking of the op2 least significant bits.
These functions behave as if two's complement arithmetic were used (although sign-magnitude is used by the actual implementation).
It is possible to extend this function to return a useful value when the operands are both negative, but the current implementation returns MAX_ULONG in this case. Do not depend on this behavior, since it will change in future versions of the library.
Functions that perform input from a stdio stream, and functions that output to
a stdio stream. Passing a NULL pointer for a stream argument to any of
these functions will make them read from stdin
and write to
stdout
, respectively.
When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions.
Return the number of bytes written, or if an error occurred, return 0.
Return the number of bytes read, or if an error occurred, return 0.
The output can be read with mpz_inp_raw
.
Return the number of bytes written, or if an error occurred, return 0.
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_raw
, and put the result in rop. 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.
This function is obsolete. It will disappear from future MP releases.
This function is useful in order to allocate the right amount of space before
converting op 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 terminating '\0').
This chapter describes the MP functions for performing arithmetic on rational
numbers. These functions start with the prefix mpq_
.
Rational numbers are stored in objects of type mpq_t
.
All rational arithmetic functions assume operands have a canonical form, and canonicalize their result. The canonical from means that the denominator and the numerator have no common factors, and that the denominator is positive. Zero has the unique representation 0/1.
Pure assignment functions do not canonicalize the assigned variable. It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable. Note that this is an incompatible change from version 1 of the library.
mpq_clear
) between each initialization.
mpq_t
variables when you are done with them.
mpq_canonicalize
before any operations are performed on rop.
To determine if two rationals are equal, mpq_equal
is faster than this
mpq_cmp
.
This routine allows that num2 and den2 have common factors.
mpq_cmp
can be used for the same purpose, this
function is much faster.
The set of mpq
functions is quite small. In particular, there are no
functions for either input or output. But there are two macros that allow us
to apply any mpz
function on the numerator or denominator of a rational
number. If these macros are used to assign to the rational number,
mpq_canonicalize
normally need to be called afterwards.
mpz
functions can be used on the result of these macros.
These functions assign between either the numerator or denominator of a
rational, and an integer. Instead of using these functions, it is preferable
to use the more general mechanisms mpq_numref
and mpq_denref
,
together with mpz_set
.
mpq_canonicalize
before any operations
are performed on rational.
This function is equivalent to
mpz_set (mpq_numref (rational), numerator)
.
mpq_canonicalize
before any operations are performed on rational.
In version 1 of the library, negative denominators were handled by copying the sign to the numerator. That is no longer done.
This function is equivalent to
mpz_set (mpq_denref (rational), denominators)
.
This function is equivalent to
mpz_set (numerator, mpq_numref (rational))
.
This function is equivalent to
mpz_set (denominator, mpq_denref (rational))
.
This is a description of the preliminary interface for floating-point arithmetic in GNU MP 2.
The floating-point functions expect arguments of type mpf_t
.
The MP floating-point functions have an interface that is similar to the MP
integer functions. The function prefix for floating-point operations is
mpf_
.
There is one significant characteristic of floating-point numbers that has motivated a difference between this function class and other MP function classes: the inherent inexactness of floating point arithmetic. The user has to specify the precision of each variable. A computation that assigns a variable will take place with the precision of the assigned variable; the precision of variables used as input is ignored.
The precision of a calculation is defined as follows: Compute the requested operation exactly (with "infinite precision"), and truncate the result to the destination variable precision. Even if the user has asked for a very high precision, MP will not calculate with superfluous digits. For example, if two low-precision numbers of nearly equal magnitude are added, the precision of the result will be limited to what is required to represent the result accurately.
The MP floating-point functions are not intended as a smooth extension to the IEEE P754 arithmetic. Specifically, the results obtained on one computer often differs from the results obtained on a computer with a different word size.
mpf_init
will use this precision, but previously
initialized variables are unaffected.
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_clear
, between initializations. The
precision of x is undefined unless a default precision has already been
established by a call to mpf_set_default_prec
.
mpf_clear
, between initializations.
mpf_t
variables when you are done with them.
Here is an example on how to initialize floating-point variables:
{ mpf_t x, y; mpf_init (x); /* use default precision */ mpf_init2 (y, 256); /* precision at least 256 bits */ ... /* Unless the program is about to exit, do ... */ mpf_clear (x); mpf_clear (y); }
The following three functions are useful for changing the precision during a calculation. A typical use would be for adjusting the precision gradually in iterative algorithms like Newton-Raphson, making the computation precision closely match the actual accurate part of the numbers.
realloc
, this routine
should not be called in a tight loop.
mpf_get_prec
. It is crucial that the precision of rop is
ultimately reset to exactly the value returned by mpf_get_prec
.
These functions assign new values to already initialized floats (see section Initialization and Assignment Functions).
The argument base 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 base 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 function returns 0 if the entire string up to the '\0' is a valid number in base base. Otherwise it returns -1.
For convenience, MP provides a parallel series of initialize-and-set functions
which initialize the output and then store the value there. These functions'
names have the form mpf_init_set...
Once the float has been initialized by any of the mpf_init_set...
functions, it can be used as the source or destination operand for the ordinary
float functions. Don't use an initialize-and-set function on a variable
already initialized!
The precision of rop will be taken from the active default precision, as
set by mpf_set_default_prec
.
mpf_set_str
above for details on the assignment operation.
Note that rop is initialized even if an error occurs. (I.e., you have to
call mpf_clear
for it.)
The precision of rop will be taken from the active default precision, as
set by mpf_set_default_prec
.
If str is NULL, space for the mantissa is allocated using the default allocation function, and a pointer to the string is returned.
If str is not NULL, it should point to a block of storage enough large for the mantissa, i.e., n_digits + 2. The two extra bytes are for a possible minus sign, and for the terminating null character.
The exponent is written through the pointer expptr.
If n_digits is 0, the maximum number of digits meaningfully achievable from the precision of op will be generated. Note that the space requirements for str in this case will be impossible for the user to predetermine. Therefore, you need to pass NULL for the string argument whenever n_digits is 0.
The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. For example, the number 3.1416 would be returned as "31416" in the string and 1 written at expptr.
Division is undefined if the divisor is zero, and passing a zero divisor to the divide functions will make these functions intentionally divide by zero. This gives the user the possibility to handle arithmetic exceptions in these functions in the same manner as other arithmetic exceptions.
Functions that perform input from a stdio stream, and functions that output to
a stdio stream. Passing a NULL pointer for a stream argument to any of
these functions will make them read from stdin
and write to
stdout
, respectively.
When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions.
In addition to the significant digits, a leading `0.' and a trailing exponent, in the form `@NNN', are printed.
Return the number of bytes written, or if an error occurred, return 0.
The argument base 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 base 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.
Return the number of bytes read, or if an error occurred, return 0.
This chapter describes low-level MP functions, used to implement the high-level MP functions, but also intended for time-critical user code.
These functions start with the prefix mpn_
.
The mpn
functions are designed to be as fast as possible, not
to provide a coherent calling interface. The different functions have somewhat
similar interfaces, but there are variations that make them hard to use. These
functions do as little as possible apart from the real multiple precision
computation, so that no time is spent on things that not all callers need.
A source operand is specified by a pointer to the least significant limb and a limb count. A destination operand is specified by just a pointer. It is the responsibility of the caller to ensure that the destination has enough space for storing the result.
With this way of specifying operands, it is possible to perform computations on subranges of an argument, and store the result into a subrange of a destination.
A common requirement for all functions is that each source area needs at least one limb. No size argument may be zero.
The mpn
functions is the base for the implementation of the mpz_
,
mpf_
, and mpq_
functions.
This example adds the number beginning at src1_ptr and the number beginning at src2_ptr and writes the sum at dest_ptr. All areas have size limbs.
cy = mpn_add_n (dest_ptr, src1_ptr, src2_ptr, size)
In the notation used here, a source operand is identified by the pointer to the least significant limb, and the limb count in braces. For example, {s1_ptr, s1_size}.
This is the lowest-level function for addition. It is the preferred function
for addition, since it is written in assembly for most targets. For addition
of a variable to itself (i.e., src1_ptr equals src2_ptr, use
mpn_lshift
with a count of 1 for optimal speed.
This function requires that src1_size is greater than or equal to src2_size.
This is the lowest-level function for subtraction. It is the preferred function for subtraction, since it is written in assembly for most targets.
This function requires that src1_size is greater than or equal to src2_size.
The destination has to have space for 2size limbs, even if the significant result might be one limb smaller.
This is a low-level function that is a building block for general multiplication as well as other operations in MP. It is written in assembly for most targets.
Don't call this function if src2_limb is a power of 2; use
mpn_lshift
with a count equal to the logarithm of src2_limb
instead, for optimal speed.
This is a low-level function that is a building block for general multiplication as well as other operations in MP. It is written in assembly for most targets.
This is a low-level function that is a building block for general multiplication and division as well as other operations in MP. It is written in assembly for most targets.
The destination has to have space for src1_size + src1_size limbs, even if the result might be one limb smaller.
This function requires that src1_size is greater than or equal to src2_size. The destination must be distinct from either input operands.
In addition to an integer quotient, xsize fraction limbs are developed, and stored after the integral limbs. For most usages, xsize will be zero.
It is required that rs2size is greater than or equal to s3size. It is required that the most significant bit of the divisor is set.
If the quotient is not needed, pass rs2p + s3size as r1p. Aside from that special case, no overlap between arguments is permitted.
Return the most significant limb of the quotient, either 0 or 1.
The area at r1p needs to be rs2size - s3size + xsize limbs large.
In addition to an integer quotient, xsize fraction limbs are developed, and stored after the integral limbs. For most usages, xsize will be zero.
The areas at r1p and s2p have to be identical or completely separate, not partially overlapping.
mpn_divrem
in its stead.
mpn_divrem_1
in its stead.
mpn_mod_1
in its stead.
{s1p, s1size} - q * {s2p, s2size} mod 2^(s1size*BITS_PER_MP_LIMB) is placed at s1p. Since the low [d / BITS_PER_MP_LIMB] limbs of this difference are zero, it is possible to overwrite the low limbs at s1p with this difference, provided dest_ptr <= s1p.
This function requires that s1size * BITS_PER_MP_LIMB >= D, and that {s2p, s2size} is odd.
This interface is preliminary. It might change incompatibly in future revisions.
Overlapping of the destination space and the source space is allowed in this function, provided dest_ptr >= src_ptr.
This function is written in assembly for most targets.
Overlapping of the destination space and the source space is allowed in this function, provided dest_ptr <= src_ptr.
This function is written in assembly for most targets.
{src1_ptr, src1_size} must be odd, and {src2_ptr, src2_size} must have at least as many bits as {src1_ptr, src1_size}.
This interface is preliminary. It might change incompatibly in future revisions.
This interface is preliminary. It might change incompatibly in future revisions.
Return the size of the remainder, whether r2p was NULL or non-NULL. Iff the operand was a perfect square, the return value will be 0.
The areas at r1p and sp have to be distinct. The areas at r2p and sp have to be identical or completely separate, not partially overlapping.
The area at r1p needs to be size / 2 limbs large. The area at r2p needs to be size limbs large.
This interface is preliminary. It might change incompatibly in future revisions.
The area at s1p is clobbered.
Return the number of characters in str.
The area at str has to have space for the largest possible number represented by a s1size long limb array, plus one extra character.
Return the number of limbs stored in r1p.
It is required that there be a clear bit within the area at s1p at or beyond bit position bit, so that the function has something to return.
This interface is preliminary. It might change incompatibly in future revisions.
It is required that there be a set bit within the area at s1p at or beyond bit position bit, so that the function has something to return.
This interface is preliminary. It might change incompatibly in future revisions.
The generated random numbers are intended for testing the correctness of the
implementation of the mpn
routines.
These functions are intended to be fully compatible with the Berkeley MP library which is available on many BSD derived U*ix systems.
The original Berkeley MP library has a usage restriction: you cannot use the same variable as both source and destination in a single function call. The compatible functions in GNU MP do not share this restriction--inputs and outputs may overlap.
It is not recommended that new programs are written using these functions.
Apart from the incomplete set of functions, the interface for initializing
MINT
objects is more error prone, and the pow
function collides
with pow
in `libm.a'.
Include the header `mp.h' to get the definition of the necessary types and functions. If you are on a BSD derived system, make sure to include GNU `mp.h' if you are going to link the GNU `libmp.a' to you program. This means that you probably need to give the -I<dir> option to the compiler, where <dir> is the directory where you have GNU `mp.h'.
MINT
object and dynamic limb space.
Initialize the integer to initial_value. Return a pointer to the
MINT
object.
MINT
object and dynamic limb space.
Initialize the integer from initial_value, a hexadecimal, '\0'-terminate
C string. Return a pointer to the MINT
object.
Some implementations of these functions work differently--or not at all--for negative arguments.
mpz_sqrt
. Set remainder to
(i.e., zero if operand is a perfect square).
If root and remainder are the same variable, the results are undefined.
stdin
, and put the read integer in
dest. SPC and TAB are allowed in the number string, and are ignored.
stdout
, as a decimal string. Also output a newline.
malloc
by default.
itom
or xtom
.
By default, the MP functions use malloc
, realloc
, and
free
for memory allocation. If malloc
or realloc
fails,
the MP library terminates execution after printing a fatal error message to
standard error.
For some applications, you may wish to allocate memory in other ways, or you may not want to have a fatal error when there is no more memory available. To accomplish this, you can specify alternative memory allocation functions.
Make sure to call this function in such a way that there are no active MP objects that were allocated using the previously active allocation function! Usually, that means that you have to call this function before any other MP function.
The functions you supply should fit the following declarations:
You can assume that the space at ptr was formerly returned from
allocate_function
or reallocate_function
, for a request for
old_size storage units.
You can assume that the space at ptr was formerly returned from
allocate_function
or reallocate_function
, for a request for
size storage units.
(A storage unit is the unit in which the sizeof
operator returns
the size of an object, normally an 8 bit byte.)
I would like to thank Gunnar Sjoedin and Hans Riesel for their help with mathematical problems, Richard Stallman for his help with design issues and for revising the first version of this manual, Brian Beuning and Doug Lea for their testing of early versions of the library.
John Amanatides of York University in Canada contributed the function
mpz_probab_prime_p
.
Ken Weber (Kent State University, Universidade Federal do Rio Grande do Sul)
contributed mpz_gcd
, mpz_divexact
, mpn_gcd
, and
mpn_bdivmod
, partially supported by CNPq (Brazil) grant 301314194-2.
Per Bothner of Cygnus Support helped to set up MP to use Cygnus' configure. He has also made valuable suggestions and tested numerous intermediary releases.
Joachim Hollman was involved in the design of the mpf
interface, and in
the mpz
design revisions for version 2.
Bennet Yee contributed the functions mpz_jacobi
and
mpz_legendre
.
Andreas Schwab contributed the files m68k/lshift.S
and
m68k/rshift.S
.
The development of floating point functions of GNU MP 2, were supported in part by the ESPRIT-BRA (Basic Research Activities) 6846 project POSSO (POlynomial System SOlving).
GNU MP 2 was finished and released by TMG Datakonsult, Sodermannagatan 5, 116 23 STOCKHOLM, SWEDEN, in cooperation with the IDA Center for Computing Sciences, USA.
Jump to: a - b - c - f - g - i - l - m - o - r - u
Jump to: _ - a - d - g - i - m - p - r - s - x
mp_limb_t
mpf_t
mpq_t
mpz_t
This document was generated on 7 November 1998 using the texi2html translator version 1.52.