GNU Multiple Precision Arithmetic Library
Developer(s) | The GNU Project |
---|---|
Stable release | 5.0.1
/ February 6, 2010 |
Repository | |
Written in | C |
Operating system | Cross-platform |
Type | Mathematical software |
License | LGPL |
Website | http://gmplib.org/ |
The GNU Multiple-Precision Library, also known as GMP, is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on (operand dimension limit is 231 bits on 32-bit machines and 237 bits on 64-bit machines[1]). GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for C but wrappers exist for other languages including C++, C#, OCaml, Perl, PHP, and Python. Kaffe Java virtual machine have also used GMP to support Java built-in arbitrary precision arithmetics in the past. This feature has been removed from the recent releases, causing protests from people that claim they used Kaffe for the sole reason GMP arithmetic being much faster than the typical implementations in other Java distributions[2]. As a result, GMP support has been added to GNU Classpath [3].
The main target applications for GMP are cryptography applications and research, Internet security applications, and computer algebra systems.
GMP aims to be faster than any other bignum library for all operand sizes. Some important factors towards this end are:
- Using fullwords as the basic arithmetic type.
- Using different algorithms for different operand sizes. The algorithms that are fastest for really big numbers are seldom fastest for small numbers.
- Highly optimized assembly code for the most important inner loops, specialized for different processors.
Here is an example of C code using the GMP library. It shows how GMP can calculate large numbers.
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int main(void)
{
mpz_t x;
mpz_t y;
mpz_t result;
mpz_init(x);
mpz_init(y);
mpz_init(result);
mpz_set_str(x, "7612058254738945", 10);
mpz_set_str(y, "9263591128439081", 10);
mpz_mul(result, x, y);
gmp_printf("\n %Zd\n*\n %Zd\n--------------------\n%Zd\n\n", x, y, result);
return EXIT_SUCCESS;
}
This code calculates the value of 7612058254738945 * 9263591128439081.
Compiling and running this program gives this result. (You need to use the -lgmp flag if compiling under Unix-type systems.)
7612058254738945
*
9263591128439081
--------------------
70514995317761165008628990709545
The first GMP release was made in 1991. It is continually developed and maintained. The current release is 5.0.1.
GMP is part of the GNU project (although the fact that its website is not on gnu.org might cause confusion), and is distributed under the GNU LGPL.
GMP is used for integer arithmetic in many computer algebra systems such as Mathematica[4] and Maple[5].
GMP is required for building GCC.[6]
Language Bindings
See also
- MPFR - library for arbitrary-precision computations with correct rounding, based on GNU MP
References
- ^ Future releases
- ^ http://www.mail-archive.com/kaffe@kaffe.org/msg13209.html
- ^ http://www.gnu.org/software/classpath/announce/20090205.html
- ^ Some Notes On Internal Implementation
- ^ The GNU Multiple Precision (GMP) Library
- ^ GCC uses the MPFR library, which in turn relies on GMP. GCC 4.3 Release Series Changes, New Features, and Fixes, last accessed 2010-01-26.