GNU Multiple Precision Arithmetic Library

GNU Multiple Precision Arithmetic Library
DeveloperGNU Project
Initial release1991 (1991)[1]
Stable release
6.3.0[2][3]  / 30 July 2023
Written inC, (C++, assembly optionally)
TypeMathematical software
LicenseDual LGPLv3GPLv2[4]
Websitegmplib.org 
Repository

GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.[4] There are no practical limits to the precision except the ones implied by the available memory (operands may be of up to 232−1 bits on 32-bit machines and 237 bits on 64-bit machines).[5][6] 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 Ada, C++, C#, Julia, .NET, OCaml, Perl, PHP, Python, R, Ruby, and Rust.

Before 2008, Kaffe, a Java virtual machine, used GMP to support Java built-in arbitrary precision arithmetic.[7] Shortly after, GMP support was added to GNU Classpath, as the backends to java.math.BigInteger and java.math.BigDecimal.[8]

The main target applications of GMP are cryptography applications and research, Internet security applications, and computer algebra systems.

GMP aims to be faster than any other arbitrary-precision arithmetic (big number) library for all operand sizes. Some important factors in doing this are:

  • Full words are the basic type for all arithmetic.
  • Different algorithms are used for different operand sizes; algorithms which are more efficient with large numbers are not used when dealing with small numbers.
  • Assembly language (specialized for different processors) is used in the most common inner loops to optimize them as much as possible.

The first GMP release was made in 1991. It is constantly developed and maintained.[9]

GMP is part of the GNU project (although its website being off gnu.org may cause confusion), and is distributed under the GNU Lesser General Public License (LGPL).

GMP is used for integer arithmetic in many computer algebra systems such as Mathematica[10] and Maple.[11] It is also used in the Computational Geometry Algorithms Library (CGAL).

GMP is needed to build the GNU Compiler Collection (GCC).[12]

C library interface

The C library interface defines:

  • mpz_t (multiprecision integer)
  • mpq_t (multiprecision rational)
  • mpf_t (multiprecision floating-point number)
  • gmp_randstate_t (random state, used for producing random numbers)

Functions are prefixed with the type name (for example, operations on multiprecision integers are prefixed with mpz, etc.)

The library also provides additional utilities (all prefixed with gmp), such as gmp_scanf, gmp_printf, etc.

Example

Here is an example of C code showing the use of the GMP library to multiply and print large numbers:

#include <stdio.h>
#include <gmp.h>

int main(void) {
    mpz_t x, y, result;

    mpz_init_set_str(x, "7612058254738945", 10);
    mpz_init_set_str(y, "9263591128439081", 10);
    mpz_init(result);

    mpz_mul(result, x, y);
    gmp_printf(
        "    %Zd\n"
        "*\n"
        "    %Zd\n"
        "--------------------\n"
        "%Zd\n",
        x, y, result
    );

    // free used memory
    mpz_clear(x);
    mpz_clear(y);
    mpz_clear(result);

    return 0;
}

This code calculates the value of .

Compiling and running this program gives this result. (The -lgmp flag is used if compiling on Unix-type systems.)

    7612058254738945
*
    9263591128439081
--------------------
70514995317761165008628990709545

C++ library interface

The C++ library interface defines the classes:

  • mpz_class (corresponds to mpz_t)
  • mpq_class (corresponds to mpq_t)
  • mpf_class (corresponds to mpf_t)
  • gmp_randclass (offers random number utilities)

The free-standing functions in the C library are integrated as methods in the C++ classes. The C++ library places all symbols globally, and does not use a library namespace. It is recommended to avoid auto in declarations.[13] For returning to the C type, each class offers a corresponding get_mp_t() method (for example mpz_class::get_mpz_t()).

Example

For comparison, one can write instead the following equivalent C++ program. (The -lgmpxx -lgmp flags are used if compiling on Unix-type systems.)

import <gmpxx.h>;
import std;

int main() {
    mpz_class x("7612058254738945");
    mpz_class y("9263591128439081");
    mpz_class result = x * y;

    std::println(
        "    {}\n"
        "*\n"
        "    {}\n"
        "--------------------\n"
        "{}",
        x.get_str(), y.get_str(), result.get_str()
    );

    return 0;
}

Language bindings

Library name Language License
GNU Multi-Precision Library C, C++ LGPL
Math::GMP Perl LGPL
Math::GMPz, Math::GMPf and Math::GMPq Perl Artistic License v1.0 + GPL v1.0-or-later
General Multiprecision Python Project Python LGPL
R package 'gmp' R GPL
The RubyGems project Ruby Apache 2.0
Rust FFI bindings for GMP, MPFR and MPC Rust LGPL
GNU Multi-Precision Library for PHP PHP PHP
GNU Multi-Precision Routines for SBCL Archived 2020-11-19 at the Wayback Machine Common Lisp Public domain
Ch GMP Ch Proprietary
Parallel GMP Wrapper for BMDFM BMDFM LISP – C Public domain
Glasgow Haskell Compiler (The implementation of Integer is basically a binding to GMP) Haskell BSD
luajit-gmp LuaJIT MIT
gmp-wrapper-for-delphi Delphi MIT
Zarith OCaml LGPL
Math.Gmp.Native Library .NET MIT
nim-gmp Nim MIT
JGMP Java LGPL

See also

References

  1. ^ "GNU MP archive". Retrieved 2018-12-03.
  2. ^ Torbjörn Granlund (30 July 2023). "GMP 6.3.0 released". Retrieved 30 July 2023.
  3. ^ "GMP 6.3 release notes".
  4. ^ a b "What is GMP?". Retrieved 2014-04-07.
  5. ^ Granlund, Torbjorn (2009-07-06). "Problems with mpz_set_str and huge strings". Retrieved 2013-03-17.
  6. ^ "GMP 6.0 News". Retrieved 2019-10-04.
  7. ^ Hughes, Andrew John (2008-02-28). "Removed GMP math?". Retrieved 2013-03-17.
  8. ^ "GNU Classpath 0.98 "Better Late Than Never"". 2009-02-05. Retrieved 2013-03-17.
  9. ^ "GNU MP Bignum Library". Retrieved 2018-12-03.
  10. ^ "The Mathematica Kernel: Issues in the Design and Implementation". October 2006. Retrieved 2013-03-17.
  11. ^ "The GNU Multiple Precision (GMP) Library". Maplesoft. Retrieved 2013-03-17.
  12. ^ GCC uses the GNU MPFR library, which in turn relies on GMP. "GCC 4.3 Release Series: Changes, New Features, and Fixes". 2012-11-02. Retrieved 2013-03-17.
  13. ^ GNU Multiple Precision Arithmetic Library. "12.6 C++ Interface Limitations". gmplib.org. GNU Project.