GNU MP

The GNU Multiple Precision Arithmetic Library

Edition 2.0.2 June 1996

by Torbjörn Granlund, TMG Datakonsult

Copyright \begin{matrix}c \\ ◯ \end{matrix} 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.

GNU MP Copying Conditions

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.

1 Introduction to GNU MP

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.

1.1 How to use this Manual

Everyone should read Chapter 3 [MP Basics], page 5. If you need to install the library yourself, you need to read Chapter 2 [Installing MP], page 3, too.

The rest of the manual can be used for later reference, although it is probably a good idea to glance through it.

2 Installing MP

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 an 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:

1. In most cases, ‘./configure --target=cpu-vendor-os’, should work both for native and cross-compilation. If you get error messages, your machine might not be supported. If you want to compile in a separate object directory, cd to that directory, and prefix the configure command with the path to the MP source directory. Not all ‘make’ programs have the necessary features to support this. In particular, SunOS and Slowaris ‘make’ have bugs that makes them unable to build from a separate object directory. Use GNU ‘make’ instead. In addition to the standard cpu-vendor-os tuples, MP recognizes sparc8 and supersparc as valid CPU names. Specifying these CPU names for relevant systems will improve performance significantly. In general, if you want a library that runs as fast as possible, you should make sure you configure MP for the exact CPU type your system uses. If you have gcc in your PATH, it will be used by default. To override this, pass ‘-with-gcc=no’

to ‘configure’.
2. ‘make’

This will compile MP, and create a library archive file ‘libgmp.a’ in the working directory.

3. ‘make check’

This will make sure MP was built correctly. If you get error messages, please report this to ‘bug-gmp@prep.ai.mit.edu’. (See Chapter 4 [Reporting Bugs], page 8, for information on what to include in useful bug reports.)

4. ‘make install’

This will copy the file ‘gmp.h’ and ‘libgmp.a’, as well as the info files, to ‘/usr/local’ (or if you passed the ‘--prefix’ option to ‘configure’, to the directory given as argument to

‘--prefix’).

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:

• ‘doc’

Create a DVI version of the manual, in ‘gmp.dvi’ and a set of info files, in ‘gmp.info’,

‘gmp.info-1’, ‘gmp.info-2’, etc.

• ‘ps’ Create a Postscript version of the manual, in ‘gmp.ps’.

• ‘html’

Create a HTML version of the manual, in ‘gmp.html’.

  • ‘clean’

Delete all object files and archive files, but not the configuration files.

  • ‘distclean’

Delete all files not included in the distribution.

  • ‘uninstall’

Delete all files copied by ‘make install’.

2.1 Known Build Problems

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.)

The system C compiler under SunOS 4 has a bug that makes it miscompile mpq/get d.c. This will make ‘make check’ fail.

Please report other problems to ‘bug-gmp@prep.ai.mit.edu’. See Chapter 4 [Reporting Bugs], page 8.

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 ""

3 MP Basics

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.

3.1 Nomenclature and Types

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.

3.2 Function Classes

There are six classes of functions in the MP library:

1. Functions for signed integer arithmetic, with names beginning with mpz_. The associated type is mpz_t. There are about 100 functions in this class.

2. Functions for rational number arithmetic, with names beginning with 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.

3. Functions for floating-point arithmetic, with names beginning with mpf_. The associated type is mpf_t. There are about 50 functions is this class.

4. Functions compatible with Berkeley MP, such as itom, madd, and mult. The associated type

    is MINT.

5. Fast low-level functions that operate on natural numbers. These are used by the functions in the preceding groups, and you can also call them directly from very time-critical user programs. These functions’ names begin with mpn_. There are about 30 (hard-to-use) functions in this class. The associated type is array of mp_limb_t.

6. Miscellaneous functions. Functions for setting up custom allocation.

3.3 MP Variable Conventions

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 puts 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.

3.4 Useful Macros and Constants

Global Constant const int mp bits per limb

The number of bits per limb.

Macro GNU MP VERSION

Macro GNU MP VERSION MINOR

The major and minor MP version, respectively, as integers.

3.5 Compatibility with Version 1.x

This version of MP is upward compatible with previous versions of MP, with a few exceptions.

1. Integer division functions round the result differently. The old functions (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.

2. The function mpz_mod now compute the true mod function.

3. The functions mpz_powm and mpz_powm_ui now use mod for reduction.

4. The assignment functions for rational numbers do no longer canonicalize their results. In the case a non-canonical result could arise from an assignment, the user need to insert an explicit call to mpq_canonicalize. This change was made for efficiency.

5. Output generated by 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.

6. Several mpn functions have changed. But they were intentionally undocumented in previous releases.

7. The functions mpz_cmp_ui, mpz_cmp_si, and mpq_cmp_ui are now implementated as macros, and thereby sometimes evaluate their arguments multiple times.

8. The functions mpz_pow_ui and mpz_ui_pow_ui now yield 1 for 0^0. (In version 1, they yielded 0.)

3.6 Getting the Latest Version of MP

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.

4 Reporting Bugs

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.

5 Integer Functions

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.

5.1 Initialization and Assignment Functions

The functions for integer arithmetic assume that all integer objects are initialized. You do that by calling the function mpz_init.

Function void mpz init (mpz_t integer)

Initialize integer with limb space and set the initial numeric value to 0. Each variable should normally only be initialized once, or at least cleared out (using 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.

Function void mpz clear (mpz_t integer)

Free the limb space occupied by integer. Make sure to call this function for all mpz_t variables when you are done with them.

Function void * mpz realloc (mpz_t integer, mp_size_t new alloc)

Change the limb space allocation to new alloc limbs. This function is not normally called from user code, but it can be used to give memory back to the heap, or to increase the space of a variable to avoid repeated automatic re-allocation.

Function void mpz array init (mpz_t integer array[], size_t array size,

mp_size_t fixed num bits) Allocate fixed limb space for all array size integers in integer array. The fixed allocation for each integer in the array is enough to store fixed num bits. If the fixed space will be insufficient for storing the result of a subsequent calculation, the result is unpredictable.

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!

5.1.1 Assignment Functions

These functions assign new values to already initialized integers (see Section 5.1 [Initializing Integers], page 9).

Function void mpz set (mpz_t rop, mpz_t op)

Function void mpz set ui (mpz_t rop, unsigned long int op)
Function void mpz set si (mpz_t rop, signed long int op)

Function void mpz set d (mpz_t rop, double op) Function void mpz set q (mpz_t rop, mpq_t op) Function void mpz set f (mpz_t rop, mpf_t op)

Set the value of rop from op.

Function int mpz set str (mpz_t rop, char *str, int base)

Set the value of rop from str, a ’\0’-terminated C string in base base. White space is allowed in the string, and is simply ignored. The base may vary from 2 to 36. If base is 0, the actual base is determined from the leading characters: if the first two characters are ‘0x’ or ‘0X’, hexadecimal is assumed, otherwise if the first character is ‘0’, octal is assumed, otherwise decimal is assumed.

This function returns 0 if the entire string up to the ’\0’ is a valid number in base base. Otherwise it returns −1.

5.1.2 Combined Initialization and Assignment Functions

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!

Function void mpz init set (mpz_t rop, mpz_t op) Function void mpz init set ui (mpz_t rop, unsigned long int op) Function void mpz init set si (mpz_t rop, signed long int op) Function void mpz init set d (mpz_t rop, double op)

Initialize rop with limb space and set the initial numeric value from op.

Function int mpz init set str (mpz_t rop, char *str, int base)

Initialize rop and set its value like mpz_set_str (see its documentation above for details).

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.)

5.2 Conversion Functions

This section describes functions for converting arbitrary precision integers to standard C types. Functions for converting to arbitrary precision integers are described in Section 5.1.1 [Assigning Integers], page 10 and Section 5.6 [I/O of Integers], page 17.

Function unsigned long int mpz get ui (mpz_t op)

Return the least significant part from op. This function combined with

mpz_tdiv_q_2exp(. . ., op, CHAR_BIT*sizeof(unsigned long int)) can be used to

extract the limbs of an integer.

Function signed long int mpz get si (mpz_t op)

If op fits into a 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.

Function double mpz get d (mpz_t op)

Convert op to a double.

Function char * mpz get str (char *str, int base, mpz_t op)

Convert op to a string of digits in base base. The base may vary from 2 to 36.

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.

5.3 Arithmetic Functions

Function void mpz add (mpz_t rop, mpz_t op1, mpz_t op2)

Function void mpz add ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to op1 + op2.

Function void mpz sub (mpz_t rop, mpz_t op1, mpz_t op2)

Function void mpz sub ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to op1 − op2.

Function void mpz mul (mpz_t rop, mpz_t op1, mpz_t op2)

Function void mpz mul ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to op1 × op2.

Function void mpz mul 2exp (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to op1 × 2^{op2}. This operation can also be defined as a left shift, op2 steps.

Function void mpz neg (mpz_t rop, mpz_t op)

Set rop to −op.

Function void mpz abs (mpz_t rop, mpz_t op)

Set rop to the absolute value of op.

Function void mpz fac ui (mpz_t rop, unsigned long int op)

Set rop to op!, the factorial of op.

5.3.1 Division functions

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:

• Functions that truncate the quotient towards 0. The names of these functions start with mpz_tdiv. The ‘t’ in the name is short for ‘truncate’.

• Functions that round the quotient towards −infinity. The names of these routines start with mpz_fdiv. The ‘f’ in the name is short for ‘floor’.

• Functions that round the quotient towards +infinity. The names of these routines start with 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.

Function void mpz tdiv q (mpz_t rop, mpz_t op1, mpz_t op2)

Function void mpz tdiv q ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to [op1/op2]. The quotient is truncated towards 0.

Function void mpz tdiv r (mpz_t rop, mpz_t op1, mpz_t op2)

Function void mpz tdiv r ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to (op1 - [op1/op2] * op2). Unless the remainder is zero, it has the same sign as the dividend.

Function void mpz tdiv qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2) Function void mpz tdiv qr ui (mpz_t rop1, mpz_t rop2, mpz_t op1, unsigned

    long int op2)

Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards 0. Unless the remainder is zero, it has the same sign as the dividend.

If rop1 and rop2 are the same variable, the results are undefined.

Function void mpz fdiv q (mpz_t rop1, mpz_t op1, mpz_t op2)

Function void mpz fdiv q ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to ⌊op1/op2⌋. (I.e., round the quotient towards −∞.)

Function void mpz fdiv r (mpz_t rop, mpz_t op1, mpz_t op2)

Function unsigned long int mpz fdiv r ui (mpz_t rop, mpz_t op1, unsigned
    long int op2)

Divide op1 by op2 and put the remainder in rop. Unless the remainder is zero, it has the same sign as the divisor.

For mpz_fdiv_r_ui the remainder is small enough to fit in an unsigned long int, and is therefore returned.

Function void mpz fdiv qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2)

Function unsigned long int mpz fdiv qr ui (mpz_t rop1, mpz_t rop2, mpz_t
    op1, unsigned long int op2)

Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards −infinity. Unless the remainder is zero, it has the same sign as the divisor.

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.

Function unsigned long int mpz fdiv ui (mpz_t op1, unsigned long int op2)

This function is similar to mpz_fdiv_r_ui, but the remainder is only returned; it is not stored anywhere.

Function void mpz cdiv q (mpz_t rop1, mpz_t op1, mpz_t op2)

Function void mpz cdiv q ui (mpz_t rop, mpz_t op1, unsigned long int op2)

Set rop to ⌈op1/op2⌉. (I.e., round the quotient towards +∞.)

Function void mpz cdiv r (mpz_t rop, mpz_t op1, mpz_t op2)

Function unsigned long int mpz cdiv r ui (mpz_t rop, mpz_t op1, unsigned
    long int op2)

Divide op1 by op2 and put the remainder in rop. Unless the remainder is zero, it has the opposite sign as the divisor.

For mpz_cdiv_r_ui the negated remainder is small enough to fit in an unsigned long int, and it is therefore returned.

Function void mpz cdiv qr (mpz_t rop1, mpz_t rop2, mpz_t op1, mpz_t op2)

Function unsigned long int mpz cdiv qr ui (mpz_t rop1, mpz_t rop2, mpz_t
    op1, unsigned long int op2)

Divide op1 by op2 and put the quotient in rop1 and the remainder in rop2. The quotient is rounded towards +infinity. Unless the remainder is zero, it has the opposite sign as the divisor.

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.

Function unsigned long int mpz cdiv ui (mpz_t op1, unsigned long int op2)

Return the negated remainder, similar to mpz_cdiv_r_ui. (The difference is that this function doesn’t store the remainder anywhere.)

Function void mpz mod (mpz_t rop, mpz_t op1, mpz_t op2)

Function unsigned long int mpz mod ui (mpz_t rop, mpz_t op1, unsigned
    long int op2)

Set rop to op1 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.

Function void mpz divexact (mpz_t rop, mpz_t op1, mpz_t op2)

Set rop to op1/op2. This function produces correct results only when it is known in advance that op2 divides op1.

Since mpz divexact is much faster than any of the other routines that produce the quotient (see [References], page 39 Jebelean), it is the best choice for instances in which exact division is known to occur, such as reducing a rational to lowest terms.

Function void mpz tdiv q 2exp (mpz_t rop, mpz_t op1, unsigned long int

op2) Set rop to op1/2^{op2}. The quotient is rounded towards 0.

Function void mpz tdiv r 2exp (mpz_t rop, mpz_t op1, unsigned long int

op2) Divide op1 by 2^{op2} and put the remainder in rop. Unless it is zero, rop will have the same sign as op1.

Function void mpz fdiv q 2exp (mpz_t rop, mpz_t op1, unsigned long int

op2) Set rop to ⌊op1/2^{op2}⌋. The quotient is rounded towards −∞.

Function void mpz fdiv r 2exp (mpz_t rop, mpz_t op1, unsigned long int

op2) Divide op1 by 2^{op2} and put the remainder in rop. The sign of rop will always be positive.

This operation can also be defined as masking of the op2 least significant bits.

5.3.2 Exponentialization Functions

Function void mpz powm (mpz_t rop, mpz_t base, mpz_t exp, mpz_t mod)

Function void mpz powm ui (mpz_t rop, mpz_t base, unsigned long int exp,
    mpz_t mod)

Set rop to (base raised to exp) mod mod. If exp is negative, the result is undefined.

Function void mpz pow ui (mpz_t rop, mpz_t base, unsigned long int exp)
Function void mpz ui pow ui (mpz_t rop, unsigned long int base, unsigned
    long int exp)

Set rop to base raised to exp. The case of 0^{0} yields 1.

5.3.3 Square Root Functions

Function void mpz sqrt (mpz_t rop, mpz_t op)

Set rop to ⌊\sqrt{op}⌋, the truncated integer part of the square root of op.

Function void mpz sqrtrem (mpz_t rop1, mpz_t rop2, mpz_t op)

Set rop1 to ⌊\sqrt{op}⌋, like mpz_sqrt. Set rop2 to (op −rop1^{2}), (i.e., zero if op is a perfect square).

If rop1 and rop2 are the same variable, the results are undefined.

Function int mpz perfect square p (mpz_t op)

Return non-zero if op is a perfect square, i.e., if the square root of op is an integer. Return zero otherwise.

5.3.4 Number Theoretic Functions

Function int mpz probab prime p (mpz_t op, int reps)

If this function returns 0, op is definitely not prime. If it returns 1, then op is ‘probably’ prime. The probability of a false positive is (1/4)^{reps}. A reasonable value of reps is 25.

An implementation of the probabilistic primality test found in Seminumerical Algorithms (see [References], page 39 Knuth).

Function void mpz gcd (mpz_t rop, mpz_t op1, mpz_t op2)

Set rop to the greatest common divisor of op1 and op2.

Function unsigned long int mpz gcd ui (mpz_t rop, mpz_t op1, unsigned
    long int op2)

Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there.

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.

Function void mpz gcdext (mpz_t g, mpz_t s, mpz_t t, mpz_t a, mpz_t b)

Compute g, s, and t, such that as + bt = g = gcd (a, b). If t is NULL, that argument is not computed.

Function int mpz invert (mpz_t rop, mpz_t op1, mpz_t op2)

Compute the inverse of op1 modulo op2 and put the result in rop. Return non-zero if an inverse exist, zero otherwise. When the function returns zero, do not assume anything about the value in rop.

Function int mpz jacobi (mpz_t op1, mpz_t op2) Function int mpz legendre (mpz_t op1, mpz_t op2)

Compute the Jacobi and Legendre symbols, respectively.

5.4 Comparison Functions

Function int mpz cmp (mpz_t op1, mpz_t op2)

Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

Macro int mpz cmp ui (mpz_t op1, unsigned long int op2)
Macro int mpz cmp si (mpz_t op1, signed long int op2)

Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

These functions are actually implemented as macros. They evaluate their arguments multiple times.

Macro int mpz sgn (mpz_t op)

Return +1 if op > 0, 0 if op = 0, and −1 if op < 0.

This function is actually implemented as a macro. It evaluates its arguments multiple times.

5.5 Logical and Bit Manipulation Functions

These functions behave as if two’s complement arithmetic were used (although sign-magnitude is used by the actual implementation).

Function void mpz and (mpz_t rop, mpz_t op1, mpz_t op2)

Set rop to op1 logical-and op2.

Function void mpz ior (mpz_t rop, mpz_t op1, mpz_t op2)

Set rop to op1 inclusive-or op2.

Function void mpz com (mpz_t rop, mpz_t op)

Set rop to the one’s complement of op.

Function unsigned long int mpz popcount (mpz_t op)

For non-negative numbers, return the population count of op. For negative numbers, return the largest possible value (MAX ULONG).

Function unsigned long int mpz hamdist (mpz_t op1, mpz_t op2)

If op1 and op2 are both non-negative, return the hamming distance between the two operands. Otherwise, return the largest possible value (MAX ULONG).

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.

Function unsigned long int mpz scan0 (mpz_t op, unsigned long int

starting bit) Scan op, starting with bit starting bit, towards more significant bits, until the first clear bit is found. Return the index of the found bit.

Function unsigned long int mpz scan1 (mpz_t op, unsigned long int

starting bit) Scan op, starting with bit starting bit, towards more significant bits, until the first set bit is found. Return the index of the found bit.

Function void mpz setbit (mpz_t rop, unsigned long int bit index)

Set bit bit index in op1.

Function void mpz clrbit (mpz_t rop, unsigned long int bit index)

Clear bit bit index in op1.

5.6 Input and Output Functions

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.

Function size_t mpz out str (FILE *stream, int base, mpz_t op)

Output op on stdio stream stream, as a string of digits in base base. The base may vary from 2 to 36.

Return the number of bytes written, or if an error occurred, return 0.

Function size_t mpz inp str (mpz_t rop, FILE *stream, int base)

Input a possibly white-space preceded string in base base from stdio stream stream, and put the read integer in rop. The base may vary from 2 to 36. If base is 0, the actual base is determined from the leading characters: if the first two characters are ‘0x’ or ‘0X’, hexadecimal is assumed, otherwise if the first character is ‘0’, octal is assumed, otherwise decimal is assumed.

Return the number of bytes read, or if an error occurred, return 0.

Function size_t mpz out raw (FILE *stream, mpz_t op)

Output op on stdio stream stream, in raw binary format. 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.

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.

Function size_t mpz inp raw (mpz_t rop, FILE *stream)

Input from stdio stream stream in the format written by 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.

5.7 Miscellaneous Functions

Function void mpz random (mpz_t rop, mp_size_t max size)

Generate a random integer of at most max size limbs. The generated random number doesn’t satisfy any particular requirements of randomness. Negative random numbers are generated when max size is negative.

Function void mpz random2 (mpz_t rop, mp_size_t max size)

Generate a random integer of at most max size limbs, with long strings of zeros and ones in the binary representation. 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 max size is negative.

Function size_t mpz size (mpz_t op)

Return the size of op measured in number of limbs. If op is zero, the returned value will be zero.

This function is obsolete. It will disappear from future MP releases.

Function size_t mpz sizeinbase (mpz_t op, int base)

Return the size of op measured in number of digits in base base. The base may vary from 2 to 36. The returned value will be exact or 1 too big. If base is a power of 2, the returned value will always be exact.

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’).

6 Rational Number Functions

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.

Function void mpq canonicalize (mpq_t op)

Remove any factors that are common to the numerator and denominator of op, and make the denominator positive.

6.1 Initialization and Assignment Functions

Function void mpq init (mpq_t dest rational)

Initialize dest rational and set it to 0/1. Each variable should normally only be initialized once, or at least cleared out (using the function mpq_clear) between each initialization.

Function void mpq clear (mpq_t rational number)

Free the space occupied by rational number. Make sure to call this function for all mpq_t variables when you are done with them.

Function void mpq set (mpq_t rop, mpq_t op) Function void mpq set z (mpq_t rop, mpz_t op)

Assign rop from op.

Function void mpq set ui (mpq_t rop, unsigned long int op1, unsigned long

int op2)

Function void mpq set si (mpq_t rop, signed long int op1, unsigned long int

op2) Set the value of rop to op1/op2. Note that if op1 and op2 have common factors, rop has to be passed to mpq_canonicalize before any operations are performed on rop.

6.2 Arithmetic Functions

Function void mpq add (mpq_t sum, mpq_t addend1, mpq_t addend2)

Set sum to addend1 + addend2.

Function void mpq sub (mpq_t difference, mpq_t minuend, mpq_t subtrahend)

Set difference to minuend − subtrahend.

Function void mpq mul (mpq_t product, mpq_t multiplier, mpq_t multiplicand)

Set product to multiplier × multiplicand.

Function void mpq div (mpq_t quotient, mpq_t dividend, mpq_t divisor)

Set quotient to dividend/divisor.

Function void mpq neg (mpq_t negated operand, mpq_t operand)

Set negated operand to −operand.

Function void mpq inv (mpq_t inverted number, mpq_t number)

Set inverted number to 1/number. If the new denominator is zero, this routine will divide by zero.

6.3 Comparison Functions

Function int mpq cmp (mpq_t op1, mpq_t op2)

Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

To determine if two rationals are equal, mpq_equal is faster than mpq_cmp.

Macro int mpq cmp ui (mpq_t op1, unsigned long int num2, unsigned long

int den2) Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2.

This routine allows that num2 and den2 have common factors.

This function is actually implemented as a macro. It evaluates its arguments multiple times.

Macro int mpq sgn (mpq_t op)

Return +1 if op > 0, 0 if op = 0, and −1 if op < 0.

This function is actually implemented as a macro. It evaluates its arguments multiple times.

Function int mpq equal (mpq_t op1, mpq_t op2)

Return non-zero if op1 and op2 are equal, zero if they are non-equal. Although mpq_cmp can be used for the same purpose, this function is much faster.

6.4 Applying Integer Functions to Rationals

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.

Macro mpz_t mpq numref (mpq_t op) Macro mpz_t mpq denref (mpq_t op)

Return a reference to the numerator and denominator of op, respectively. The mpz functions can be used on the result of these macros.

6.5 Miscellaneous Functions

Function double mpq get d (mpq_t op)

Convert op to a double.

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.

Function void mpq set num (mpq_t rational, mpz_t numerator)

Copy numerator to the numerator of rational. When this risks to make the numerator and denominator of rational have common factors, you have to pass rational to mpq_ canonicalize before any operations are performed on rational.

This function is equivalent to mpz_set (mpq_numref (rational), numerator).

Function void mpq set den (mpq_t rational, mpz_t denominator)

Copy denominator to the denominator of rational. When this risks to make the numerator and denominator of rational have common factors, or if the denominator might be negative, you have to pass rational to 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).

Function void mpq get num (mpz_t numerator, mpq_t rational)

Copy the numerator of rational to the integer numerator, to prepare for integer operations on the numerator.

This function is equivalent to mpz_set (numerator, mpq_numref (rational)).

Function void mpq get den (mpz_t denominator, mpq_t rational)

Copy the denominator of rational to the integer denominator, to prepare for integer operations on the denominator.

This function is equivalent to mpz_set (denominator, mpq_denref (rational)).

7 Floating-point Functions

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.

7.1 Initialization and Assignment Functions

Function void mpf set default prec (unsigned long int prec)

Set the default precision to be at least prec bits. All subsequent calls to 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.

Function void mpf init (mpf_t x)

Initialize x to 0. Normally, a variable should be initialized once only or at least be cleared, using 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.

Function void mpf init2 (mpf_t x, unsigned long int prec)

Initialize x to 0 and set its precision to be at least prec bits. Normally, a variable should be initialized once only or at least be cleared, using mpf_clear, between initializations.

Function void mpf clear (mpf_t x)

Free the space occupied by x. Make sure to call this function for all 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.

Function void mpf set prec (mpf_t rop, unsigned long int prec)

Set the precision of rop to be at least prec bits. Since changing the precision involves calls to realloc, this routine should not be called in a tight loop.

Function unsigned long int mpf get prec (mpf_t op)

Return the precision actually used for assignments of op.

Function void mpf set prec raw (mpf_t rop, unsigned long int prec)

Set the precision of rop to be at least prec bits. This is a low-level function that does not change the allocation. The prec argument must not be larger that the precision previously returned by mpf_get_prec. It is crucial that the precision of rop is ultimately reset to exactly the value returned by mpf_get_prec.

7.1.1 Assignment Functions

These functions assign new values to already initialized floats (see Section 7.1 [Initializing Floats], page 22).

Function void mpf set (mpf_t rop, mpf_t op)

Function void mpf set ui (mpf_t rop, unsigned long int op)
Function void mpf set si (mpf_t rop, signed long int op)

Function void mpf set d (mpf_t rop, double op) Function void mpf set z (mpf_t rop, mpz_t op) Function void mpf set q (mpf_t rop, mpq_t op)

Set the value of rop from op.

Function int mpf set str (mpf_t rop, char *str, int base)

Set the value of rop from the string in str. 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 base. The exponent is either in the specified base or, if base is negative, in decimal.

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.

7.1.2 Combined Initialization and Assignment Functions

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!

Function void mpf init set (mpf_t rop, mpf_t op) Function void mpf init set ui (mpf_t rop, unsigned long int op) Function void mpf init set si (mpf_t rop, signed long int op) Function void mpf init set d (mpf_t rop, double op)

Initialize rop and set its value from op.

The precision of rop will be taken from the active default precision, as set by mpf_set_

default_prec.

Function int mpf init set str (mpf_t rop, char *str, int base)

Initialize rop and set its value from the string in str. See 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.

7.2 Conversion Functions

Function double mpf get d (mpf_t op)

Convert op to a double.

Function char * mpf get str (char *str, mp_exp_t *expptr, int base, size_t

n digits, mpf_t op) Convert op to a string of digits in base base. The base may vary from 2 to 36. Generate at most n digits significant digits, or if n digits is 0, the maximum number of digits accurately representable by op.

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.

7.3 Arithmetic Functions

Function void mpf add (mpf_t rop, mpf_t op1, mpf_t op2)

Function void mpf add ui (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1 + op2.

Function void mpf sub (mpf_t rop, mpf_t op1, mpf_t op2)

Function void mpf ui sub (mpf_t rop, unsigned long int op1, mpf_t op2)
Function void mpf sub ui (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1 − op2.

Function void mpf mul (mpf_t rop, mpf_t op1, mpf_t op2)

Function void mpf mul ui (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1 × op2.

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.

Function void mpf div (mpf_t rop, mpf_t op1, mpf_t op2)

Function void mpf ui div (mpf_t rop, unsigned long int op1, mpf_t op2)
Function void mpf div ui (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1/op2.

Function void mpf sqrt (mpf_t rop, mpf_t op)

Function void mpf sqrt ui (mpf_t rop, unsigned long int op)

Set rop to \sqrt{op}.

Function void mpf neg (mpf_t rop, mpf_t op)

Set rop to −op.

Function void mpf abs (mpf_t rop, mpf_t op)

Set rop to the absolute value of op.

Function void mpf mul 2exp (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1 × 2^{op2}.

Function void mpf div 2exp (mpf_t rop, mpf_t op1, unsigned long int op2)

Set rop to op1/2^{op2}.

7.4 Comparison Functions

Function int mpf cmp (mpf_t op1, mpf_t op2)

Function int mpf cmp ui (mpf_t op1, unsigned long int op2)

Function int mpf cmp si (mpf_t op1, signed long int op2)

Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2.

Function int mpf eq (mpf_t op1, mpf_t op2, unsigned long int op3)

Return non-zero if the first op3 bits of op1 and op2 are equal, zero otherwise. I.e., test of op1 and op2 are approximately equal.

Function void mpf reldiff (mpf_t rop, mpf_t op1, mpf_t op2)

Compute the relative difference between op1 and op2 and store the result in rop.

Macro int mpf sgn (mpf_t op)

Return +1 if op > 0, 0 if op = 0, and −1 if op < 0.

This function is actually implemented as a macro. It evaluates its arguments multiple times.

7.5 Input and Output Functions

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.

Function size_t mpf out str (FILE *stream, int base, size_t n digits, mpf_t

op) Output op on stdio stream stream, as a string of digits in base base. The base may vary from 2 to 36. Print at most n digits significant digits, or if n digits is 0, the maximum number of digits accurately representable by op.

In addition to the significant digits, a leading ‘0.’ and a trailing exponent, in the form ‘eNNN’, are printed. If base is greater than 10, ‘@’ will be used instead of ‘e’ as exponent delimiter.

Return the number of bytes written, or if an error occurred, return 0.

Function size_t mpf inp str (mpf_t rop, FILE *stream, int base)

Input a string in base base from stdio stream stream, and put the read float in rop. 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 base. The exponent is either in the specified base or, if base is negative, in decimal.

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.

7.6 Miscellaneous Functions

Function void mpf random2 (mpf_t rop, mp_size_t max size, mp_exp_t

max exp) Generate a random float of at most max size limbs, with long strings of zeros and ones in the binary representation. The exponent of the number is in the interval −exp to exp. This function is 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 max size is negative.

8 Low-level Functions

This chapter describes low-level MP functions, used to implement the high-level MP functions, but also intended for time-critical user code.

These functions start with the prefix mpn_.

The mpn functions are designed to be as fast as possible, not to provide a coherent calling interface. The different functions have somewhat similar interfaces, but there are variations that make them hard to use. These functions do as little as possible apart from the real multiple precision computation, so that no time is spent on things that not all callers need.

A source operand is specified by a pointer to the least significant limb and a limb count. A destination operand is specified by just a pointer. It is the responsibility of the caller to ensure that the destination has enough space for storing the result.

With this way of specifying operands, it is possible to perform computations on subranges of an argument, and store the result into a subrange of a destination.

A common requirement for all functions is that each source area needs at least one limb. No size argument may be zero.

The mpn functions is the base for the implementation of the mpz_, mpf_, and mpq_functions.

This example adds the number beginning at src1 ptr and the number beginning at src2 ptr and writes the sum at dest ptr. All areas have size limbs.

     cy = mpn_add_n (dest_ptr, src1_ptr, src2_ptr, size)

In the notation used here, a source operand is identified by the pointer to the least significant limb, and the limb count in braces. For example, {s1 ptr, s1 size}.

Function mp_limb_t mpn add n (mp_limb_t * dest ptr, const mp_limb_t *
         src1 ptr, const mp_limb_t * src2 ptr, mp_size_t size)

Add {src1 ptr, size} and {src2 ptr, size}, and write the size least significant limbs of the result to dest ptr. Return carry, either 0 or 1.

This is the lowest-level function for addition. It is the preferred function for addition, since it is written in assembly for most targets. For addition of a variable to itself (i.e., src1 ptr equals src2 ptr, use mpn_lshift with a count of 1 for optimal speed.

Function mp_limb_t mpn add 1 (mp_limb_t * dest ptr, const mp_limb_t *
         src1 ptr, mp_size_t size, mp_limb_t src2 limb)

Add {src1 ptr, size} and src2 limb, and write the size least significant limbs of the result to dest ptr. Return carry, either 0 or 1.

Function mp_limb_t mpn add (mp_limb_t * dest ptr, const mp_limb_t *
         src1 ptr, mp_size_t src1 size, const mp_limb_t * src2 ptr, mp_size_t

src2 size) Add {src1 ptr, src1 size} and {src2 ptr, src2 size}, and write the src1 size least significant limbs of the result to dest ptr. Return carry, either 0 or 1.

This function requires that src1 size is greater than or equal to src2 size.

Function mp_limb_t mpn sub n (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, const mp_limb_t * src2 ptr, mp_size_t size)

Subtract {src2 ptr, src2 size} from {src1 ptr, size}, and write the size least significant limbs of the result to dest ptr. Return borrow, either 0 or 1.

This is the lowest-level function for subtraction. It is the preferred function for subtraction, since it is written in assembly for most targets.

Function mp_limb_t mpn sub 1 (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t size, mp_limb_t src2 limb)

Subtract src2 limb from {src1 ptr, size}, and write the size least significant limbs of the result to dest ptr. Return borrow, either 0 or 1.

Function mp_limb_t mpn sub (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t src1 size, const mp_limb_t * src2 ptr, mp_size_t

src2 size) Subtract {src2 ptr, src2 size} from {src1 ptr, src1 size}, and write the src1 size least significant limbs of the result to dest ptr. Return borrow, either 0 or 1.

This function requires that src1 size is greater than or equal to src2 size.

Function void mpn mul n (mp_limb_t * dest ptr, const mp_limb_t * src1 ptr,
    const mp_limb_t * src2 ptr, mp_size_t size)

Multiply {src1 ptr, size} and {src2 ptr, size}, and write the entire result to dest ptr.

The destination has to have space for 2size limbs, even if the significant result might be one limb smaller.

Function mp_limb_t mpn mul 1 (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t size, mp_limb_t src2 limb)

Multiply {src1 ptr, size} and src2 limb, and write the size least significant limbs of the product to dest ptr. Return the most significant limb of the product.

This is a low-level function that is a building block for general multiplication as well as other operations in MP. It is written in assembly for most targets.

Don’t call this function if src2 limb is a power of 2; use mpn_lshift with a count equal to the logarithm of src2 limb instead, for optimal speed.

Function mp_limb_t mpn addmul 1 (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t size, mp_limb_t src2 limb)

Multiply {src1 ptr, size} and src2 limb, and add the size least significant limbs of the product to {dest ptr, size} and write the result to dest ptr dest ptr. Return the most significant limb of the product, plus carry-out from the addition.

This is a low-level function that is a building block for general multiplication as well as other operations in MP. It is written in assembly for most targets.

Function mp_limb_t mpn submul 1 (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t size, mp_limb_t src2 limb)

Multiply {src1 ptr, size} and src2 limb, and subtract the size least significant limbs of the product from {dest ptr, size} and write the result to dest ptr. Return the most significant limb of the product, minus borrow-out from the subtraction.

This is a low-level function that is a building block for general multiplication and division as well as other operations in MP. It is written in assembly for most targets.

Function mp_limb_t mpn mul (mp_limb_t * dest ptr, const mp_limb_t *
    src1 ptr, mp_size_t src1 size, const mp_limb_t * src2 ptr, mp_size_t

src2 size) Multiply {src1 ptr, src1 size} and {src2 ptr, src2 size}, and write the result to dest ptr. Return the most significant limb of the result.

The destination has to have space for src1 size + src1 size limbs, even if the result might be one limb smaller.

This function requires that src1 size is greater than or equal to src2 size. The destination must be distinct from either input operands.

Function mp_size_t mpn divrem (mp_limb_t * r1p, mp_size_t xsize,
    mp_limb_t * rs2p, mp_size_t rs2size, const mp_limb_t * s3p, mp_size_t

s3size) Divide {rs2p, rs2size} by {s3p, s3size}, and write the quotient at r1p, with the exception of the most significant limb, which is returned. The remainder replaces the dividend at rs2p.

In addition to an integer quotient, xsize fraction limbs are developed, and stored after the integral limbs. For most usages, xsize will be zero.

It is required that rs2size is greater than or equal to s3size. It is required that the most significant bit of the divisor is set.

If the quotient is not needed, pass rs2p + s3size as r1p. Aside from that special case, no overlap between arguments is permitted.

Return the most significant limb of the quotient, either 0 or 1.

The area at r1p needs to be rs2size − s3size + xsize limbs large.

Function mp_limb_t mpn divrem 1 (mp_limb_t * r1p, mp_size_t xsize,
    mp_limb_t * s2p, mp_size_t s2size, mp_limb_t s3limb)

Divide {s2p, s2size} by s3limb, and write the quotient at r1p. Return the remainder.

In addition to an integer quotient, xsize fraction limbs are developed, and stored after the integral limbs. For most usages, xsize will be zero.

The areas at r1p and s2p have to be identical or completely separate, not partially overlapping.

Function mp_size_t mpn divmod (mp_limb_t * r1p, mp_limb_t * rs2p,
    mp_size_t rs2size, const mp_limb_t * s3p, mp_size_t s3size)

This interface is obsolete. It will disappear from future releases. Use mpn_divrem in its stead.

Function mp_limb_t mpn divmod 1 (mp_limb_t * r1p, mp_limb_t * s2p,
    mp_size_t s2size, mp_limb_t s3limb)

This interface is obsolete. It will disappear from future releases. Use mpn_divrem_1 in its stead.

Function mp_limb_t mpn mod 1 (mp_limb_t * s1p, mp_size_t s1size,
    mp_limb_t s2limb)

Divide {s1p, s1size} by s2limb, and return the remainder.

Function mp_limb_t mpn preinv mod 1 (mp_limb_t * s1p, mp_size_t s1size,

    mp_limb_t s2limb, mp_limb_t s3limb)

This interface is obsolete. It will disappear from future releases. Use mpn_mod_1 in its stead.

Function mp_limb_t mpn bdivmod (mp_limb_t * dest ptr, mp_limb_t * s1p,
    mp_size_t s1size, const mp_limb_t * s2p, mp_size_t s2size, unsigned long
    int d)

The function puts the low [d/BITS PER MP LIMB] limbs of q = {s1p, s1size}/{s2p, s2size} mod 2^d at dest ptr, and returns the high d mod BITS PER MP LIMB bits of q.

{s1p, s1size} - q * {s2p, s2size} mod 2^(s1size*BITS PER MP LIMB) is placed at s1p. Since the low [d/BITS PER MP LIMB] limbs of this difference are zero, it is possible to overwrite the low limbs at s1p with this difference, provided dest ptr <= s1p.

This function requires that s1size * BITS PER MP LIMB >= D, and that {s2p, s2size} is odd.

This interface is preliminary. It might change incompatibly in future revisions.

Function mp_limb_t mpn lshift (mp_limb_t * dest ptr, const mp_limb_t *
    src ptr, mp_size_t src size, unsigned long int count)

Shift {src ptr, src size} count bits to the left, and write the src size least significant limbs of the result to dest ptr. count might be in the range 1 to n − 1, on an n-bit machine. The bits shifted out to the left are returned.

Overlapping of the destination space and the source space is allowed in this function, provided dest ptr >= src ptr.

This function is written in assembly for most targets.

Function mp_limp_t mpn rshift (mp_limb_t * dest ptr, const mp_limb_t *
    src ptr, mp_size_t src size, unsigned long int count)

Shift {src ptr, src size} count bits to the right, and write the src size most significant limbs of the result to dest ptr. count might be in the range 1 to n − 1, on an n-bit machine. The bits shifted out to the right are returned.

Overlapping of the destination space and the source space is allowed in this function, provided dest ptr <= src ptr.

This function is written in assembly for most targets.

Function int mpn cmp (const mp_limb_t * src1 ptr, const mp_limb_t *

src2 ptr, mp_size_t size) Compare {src1 ptr, size} and {src2 ptr, size} and return a positive value if src1 > src2, 0 of they are equal, and a negative value if src1 < src2.

Function mp_size_t mpn gcd (mp_limb_t * dest ptr, mp_limb_t * src1 ptr,
    mp_size_t src1 size, mp_limb_t * src2 ptr, mp_size_t src2 size)

Puts at dest ptr the greatest common divisor of {src1 ptr, src1 size} and {src2 ptr, src2 size}; both source operands are destroyed by the operation. The size in limbs of the greatest common divisor is returned.

{src1 ptr, src1 size} must be odd, and {src2 ptr, src2 size} must have at least as many bits as {src1 ptr, src1 size}.

This interface is preliminary. It might change incompatibly in future revisions.

Function mp_limb_t mpn gcd 1 (const mp_limb_t * src1 ptr, mp_size_t

src1 size, mp_limb_t src2 limb) Return the greatest common divisor of {src1 ptr, src1 size} and src2 limb, where src2 limb (as well as src1 size) must be different from 0.

Function mp_size_t mpn gcdext (mp_limb_t * r1p, mp_limb_t * r2p,
    mp_limb_t * s1p, mp_size_t s1size, mp_limb_t * s2p, mp_size_t s2size)

Puts at r1p the greatest common divisor of {s1p, s1size} and {s2p, s2size}. The first cofactor is written at r2p. Both source operands are destroyed by the operation. The size in limbs of the greatest common divisor is returned.

This interface is preliminary. It might change incompatibly in future revisions.

Function mp_size_t mpn sqrtrem (mp_limb_t * r1p, mp_limb_t * r2p, const
    mp_limb_t * sp, mp_size_t size)

Compute the square root of {sp, size} and put the result at r1p. Write the remainder at r2p, unless r2p is NULL.

Return the size of the remainder, whether r2p was NULL or non-NULL. Iff the operand was a perfect square, the return value will be 0.

The areas at r1p and sp have to be distinct. The areas at r2p and sp have to be identical or completely separate, not partially overlapping.

The area at r1p needs to have space for ⌈size/2⌉ limbs. The area at r2p needs to be size limbs large.

This interface is preliminary. It might change incompatibly in future revisions.

Function mp_size_t mpn get str (unsigned char *str, int base, mp_limb_t *
    s1p, mp_size_t s1size)

Convert {s1p, s1size} to a raw unsigned char array in base base. The string is not in ASCII; to convert it to printable format, add the ASCII codes for ‘0’ or ‘A’, depending on the base and range. There may be leading zeros in the string.

The area at s1p is clobbered.

Return the number of characters in str.

The area at str has to have space for the largest possible number represented by a s1size long limb array, plus one extra character.

Function mp_size_t mpn set str (mp_limb_t * r1p, const char *str, size_t
    strsize, int base)

Convert the raw unsigned char array at str of length strsize to a limb array {s1p, s1size}. The base of str is base.

Return the number of limbs stored in r1p.

Function unsigned long int mpn scan0 (const mp_limb_t * s1p, unsigned
    long int bit)

Scan s1p from bit position bit for the next clear bit.

It is required that there be a clear bit within the area at s1p at or beyond bit position bit, so that the function has something to return.

This interface is preliminary. It might change incompatibly in future revisions.

Function unsigned long int mpn scan1 (const mp_limb_t * s1p, unsigned
    long int bit)

Scan s1p from bit position bit for the next set bit.

It is required that there be a set bit within the area at s1p at or beyond bit position bit, so that the function has something to return.

This interface is preliminary. It might change incompatibly in future revisions.

Function void mpn random2 (mp_limb_t * r1p, mp_size_t r1size)

Generate a random number of length r1size with long strings of zeros and ones in the binary representation, and store it at r1p.

The generated random numbers are intended for testing the correctness of the implementation of the mpn routines.

Function unsigned long int mpn popcount (const mp_limb_t * s1p,
    unsigned long int size)

Count the number of set bits in {s1p, size}.

Function unsigned long int mpn hamdist (const mp_limb_t * s1p, const
    mp_limb_t * s2p, unsigned long int size)

Compute the hamming distance between {s1p, size} and {s2p, size}.

34 GNU MP 2.0.2 Function int mpn perfect square p (const mp_limb_t * s1p, mp_size_t size)

Return non-zero iff {s1p, size} is a perfect square.

9 Berkeley MP Compatible Functions

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’.

Function MINT * itom (signed short int initial value)

Allocate an integer consisting of a MINT object and dynamic limb space. Initialize the integer to initial value. Return a pointer to the MINT object.

Function MINT * xtom (char *initial value)

Allocate an integer consisting of a 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.

Function void move (MINT *src, MINT *dest)

Set dest to src by copying. Both variables must be previously initialized.

Function void madd (MINT *src 1, MINT *src 2, MINT *destination)

Add src 1 and src 2 and put the sum in destination.

Function void msub (MINT *src 1, MINT *src 2, MINT *destination)

Subtract src 2 from src 1 and put the difference in destination.

Function void mult (MINT *src 1, MINT *src 2, MINT *destination)

Multiply src 1 and src 2 and put the product in destination.

Function void mdiv (MINT *dividend, MINT *divisor, MINT *quotient, MINT

*remainder)

Function void sdiv (MINT *dividend, signed short int divisor, MINT *quotient,

         signed short int *remainder)

Set quotient to dividend/divisor, and remainder to dividend mod divisor. The quotient is rounded towards zero; the remainder has the same sign as the dividend unless it is zero.

Some implementations of these functions work differently—or not at all—for negative arguments.

Function void msqrt (MINT *operand, MINT *root, MINT *remainder)

Set root to ⌊\sqrt{operand}⌋, like mpz_sqrt. Set remainder to (operand − root^{2}), (i.e., zero if operand is a perfect square).

If root and remainder are the same variable, the results are undefined.

Function void pow (MINT *base, MINT *exp, MINT *mod, MINT *dest)

Set dest to (base raised to exp) modulo mod.

Function void rpow (MINT *base, signed short int exp, MINT *dest)

Set dest to base raised to exp.

Function void gcd (MINT *operand1, MINT *operand2, MINT *res)

Set res to the greatest common divisor of operand1 and operand2.

Function int mcmp (MINT *operand1, MINT *operand2)

Compare operand1 and operand2. Return a positive value if operand1 > operand2, zero if operand1 = operand2, and a negative value if operand1 < operand2.

Function void min (MINT *dest)

Input a decimal string from stdin, and put the read integer in dest. SPC and TAB are allowed in the number string, and are ignored.

Function void mout (MINT *src)

Output src to stdout, as a decimal string. Also output a newline.

Function char * mtox (MINT *operand)

Convert operand to a hexadecimal string, and return a pointer to the string. The returned string is allocated using the default memory allocation function, malloc by default.

Function void mfree (MINT *operand)

De-allocate, the space used by operand. This function should only be passed a value returned by itom or xtom.

10 Custom Allocation

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.

Function void mp set memory functions (

void *(*alloc func ptr) (size_t),

    void *(*realloc func ptr) (void *, size_t, size_t),
    void (*free func ptr) (void *, size_t))

Replace the current allocation functions from the arguments. If an argument is NULL, the corresponding default function is retained.

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:

Function void * allocate function (size_t alloc size)

This function should return a pointer to newly allocated space with at least alloc size storage units.

Function void * reallocate function (void *ptr, size_t old size, size_t

new size) This function should return a pointer to newly allocated space of at least new size storage units, after copying at least the first old size storage units from ptr. It should also de-allocate the space at ptr.

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.

Function void deallocate function (void *ptr, size_t size)

De-allocate the space pointed to by ptr.

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.)

Contributors

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.

Paul Zimmermann of Inria sparked the development of GMP 2, with his comparisons between bignum packages.

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 ‘mpn/m68k/lshift.S’ and ‘mpn/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 STOCK-HOLM, SWEDEN, in cooperation with the IDA Center for Computing Sciences, USA.

References

• Donald E. Knuth, "The Art of Computer Programming", vol 2, "Seminumerical Algorithms", 2nd edition, Addison-Wesley, 1981.

• John D. Lipson, "Elements of Algebra and Algebraic Computing", The Benjamin Cummings Publishing Company Inc, 1981.

• Richard M. Stallman, "Using and Porting GCC", Free Software Foundation, 1995.

• Peter L. Montgomery, "Modular Multiplication Without Trial Division", in Mathematics of Computation, volume 44, number 170, April 1985.

• Torbjorn Granlund and Peter L. Montgomery, "Division by Invariant Integers using Multiplication", in Proceedings of the SIGPLAN PLDI’94 Conference, June 1994.

• Tudor Jebelean, "An algorithm for exact division", Journal of Symbolic Computation, v. 15, 1993, pp. 169-180.

• Kenneth Weber, "The accelerated integer GCD algorithm", ACM Transactions on Mathematical Software, v. 21 (March), 1995, pp. 111-122.

40 GNU MP 2.0.2

Concept Index

A

Arithmetic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 11, 25

B

Bit manipulation functions . . . . . . . . . . . . . . . . . . . . . . . . . 16 BSD MP compatible functions . . . . . . . . . . . . . . . . . . . . . 35

C

Comparison functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Conditions for copying GNU MP . . . . . . . . . . . . . . . . . . . . 1 Conversion functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 11, 24 Copying conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

F

Float arithmetic functions . . . . . . . . . . . . . . . . . . . . . . . . . 25 Float assignment functions . . . . . . . . . . . . . . . . . . . . . . . . . 23 Float comparisons functions. . . . . . . . . . . . . . . . . . . . . . . . 26 Float functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Float input and output functions. . . . . . . . . . . . . . . . . . . 26 Floating-point functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Floating-point number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

G

‘gmp.h’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

I

I/O functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17, 26 Initialization and assignment functions . . . . . . . . . 10, 24 Input functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17, 26 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Integer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Integer arithmetic functions . . . . . . . . . . . . . . . . . . . . . . . . 11 Integer assignment functions . . . . . . . . . . . . . . . . . . . . . . . 10 Integer conversion functions . . . . . . . . . . . . . . . . . . . . . . . . 11 Integer functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Integer input and output functions . . . . . . . . . . . . . . . . . 17

L

Limb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Logical functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Low-level functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

M

Miscellaneous float functions . . . . . . . . . . . . . . . . . . . . . . . 27 Miscellaneous integer functions. . . . . . . . . . . . . . . . . . . . . 18 ‘mp.h’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

O

Output functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17, 26

R

Rational number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Rational number functions . . . . . . . . . . . . . . . . . . . . . . . . . 19 Reporting bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

U

User-defined precision. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Function and Type Index 41

Function and Type Index

GNU MP VERSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 GNU MP VERSION MINOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 mpz realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

A

allocate function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

D

deallocate function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

G

gcd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

I

itom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

M

madd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 mcmp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 mdiv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 mfree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 mout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 move. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 mp limb t. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mp set memory functions . . . . . . . . . . . . . . . . . . . . . . . . . . 37 mpf abs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf add. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf add ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 mpf cmp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf cmp si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf cmp ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf div. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf div 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf div ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf eq. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf get d. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf get prec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf get str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 mpf init set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init set d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init set si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init set str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init set ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 mpf init2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 mpf inp str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 mpf mul. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf mul 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf mul ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf neg. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf out str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf random2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 mpf reldiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set d. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set default prec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 mpf set prec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set prec raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set q. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf set z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 mpf sgn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 mpf sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf sqrt ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf sub ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mpf ui div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpf ui sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 mpn add. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 mpn add 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 mpn add n. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 mpn addmul 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn bdivmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mpn cmp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn divmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 mpn divmod 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mpn divrem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 mpn divrem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 mpn gcd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn gcd 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn gcdext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn get str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn hamdist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn lshift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mpn mod 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mpn mul. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 mpn mul 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn mul n. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn perfect square p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 mpn popcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn preinv mod 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

42 GNU MP 2.0.2

mpn random2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn rshift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mpn scan0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn scan1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn set str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 mpn sqrtrem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 mpn sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn sub 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn sub n. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpn submul 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 mpq add. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq canonicalize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq cmp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq cmp ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq denref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq div. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq get d. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq get den . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq get num . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq inv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq mul. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq neg. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq numref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq set den . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq set num . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 mpq set si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq set ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq set z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq sgn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 mpq sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 mpq t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mpz abs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz add. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz add ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz and. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz array init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpz cdiv q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz cdiv q ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz cdiv qr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz cdiv qr ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz cdiv r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz cdiv r ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz cdiv ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpz clrbit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz cmp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz cmp si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz cmp ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz com. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz divexact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz fac ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz fdiv q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv q 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz fdiv q ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv qr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv qr ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv r 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz fdiv r ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz fdiv ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz gcd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz gcd ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz gcdext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz get d. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz get si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz get str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz get ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz hamdist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz init. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mpz init set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz init set d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz init set si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz init set str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz init set ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz inp raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz inp str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz invert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz ior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz jacobi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz legendre . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz mod. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz mod ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz mul. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz mul 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz mul ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz neg. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz out raw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz out str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz perfect square p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz popcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz pow ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz powm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz powm ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz probab prime p. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz random2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz scan0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz scan1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz set d. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz set f. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz set q. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz set si . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz set str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Function and Type Index 43

mpz set ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 mpz setbit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 mpz sgn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 mpz size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz sizeinbase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 mpz sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz sqrtrem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 mpz sub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz sub ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 mpz t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 mpz tdiv q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz tdiv q 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz tdiv q ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz tdiv qr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz tdiv qr ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 mpz tdiv r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz tdiv r 2exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 mpz tdiv r ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 mpz ui pow ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 msqrt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 msub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 mtox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 mult. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

P

pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

R

reallocate function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 rpow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

S

sdiv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

X

xtom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Table of Contents

GNU MP Copying Conditions . . . . . . . . . . . . . . . . . . . . . . 1

1 Introduction to GNU MP . . . . . . . . . . . . . . . . . . . . . . . 2

1.1 How to use this Manual. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Installing MP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1 Known Build Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 MP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3.1 Nomenclature and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Function Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.3 MP Variable Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.4 Useful Macros and Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.5 Compatibility with Version 1.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.6 Getting the Latest Version of MP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Reporting Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

5 Integer Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5.1 Initialization and Assignment Functions. . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.1.1 Assignment Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 5.1.2 Combined Initialization and Assignment Functions . . . . . . 10

5.2 Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 5.3 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

5.3.1 Division functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 5.3.2 Exponentialization Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.3.3 Square Root Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.3.4 Number Theoretic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5.4 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5.5 Logical and Bit Manipulation Functions. . . . . . . . . . . . . . . . . . . . . . . . . 16 5.6 Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 5.7 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6 Rational Number Functions . . . . . . . . . . . . . . . . . . . . 19

6.1 Initialization and Assignment Functions. . . . . . . . . . . . . . . . . . . . . . . . . 19 6.2 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 6.3 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 6.4 Applying Integer Functions to Rationals . . . . . . . . . . . . . . . . . . . . . . . . 20 6.5 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

7 Floating-point Functions . . . . . . . . . . . . . . . . . . . . . . . 22

7.1 Initialization and Assignment Functions. . . . . . . . . . . . . . . . . . . . . . . . . 22 7.1.1 Assignment Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7.1.2 Combined Initialization and Assignment Functions . . . . . . 24

7.2 Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 ii GNU MP 2.0.2

7.3 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 7.4 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 7.5 Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 7.6 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

8 Low-level Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 9 Berkeley MP Compatible Functions . . . . . . . . . . . . 35 10 Custom Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Concept Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Function and Type Index. . . . . . . . . . . . . . . . . . . . . . . . . . 41