Jump to content

GNU Multiple Precision Arithmetic Library: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
ILikeTau (talk | contribs)
m Use a more up-to-date logo that has been properly vectorized
→‎top: Fixed grammar
Tags: Mobile edit Mobile app edit Android app edit
 
(29 intermediate revisions by 23 users not shown)
Line 1: Line 1:
{{Short description|Free software}}
{{Infobox software
{{Infobox software
| name = GNU Multiple Precision Arithmetic Library
| name = GNU Multiple Precision Arithmetic Library
Line 8: Line 9:
| url=https://gmplib.org/download/gmp/archive/
| url=https://gmplib.org/download/gmp/archive/
| title=GNU MP archive
| title=GNU MP archive
| accessdate=2018-12-03}}</ref>
| access-date=2018-12-03}}</ref>
| latest release version = {{wikidata|property|preferred|references|edit|Q1205818|P348|P548=Q2804309}}
| latest release date = {{wikidata|qualifier|preferred|single|Q1205818|P348|P548=Q2804309|P577}}
| latest preview version = {{wikidata|property|preferred|references|edit|Q1205818|P348|P548=Q51930650}}
| latest preview date = {{wikidata|qualifier|preferred|single|Q1205818|P348|P548=Q51930650|P577}}
| programming language = [[C (programming language)|C]], ([[C++]], [[assembly language|assembly]] optionally)
| programming language = [[C (programming language)|C]], ([[C++]], [[assembly language|assembly]] optionally)
| genre = [[Mathematical software]]
| genre = [[Mathematical software]]
Line 16: Line 21:
}}
}}


'''GNU Multiple Precision Arithmetic Library''' ('''GMP''') is a [[free software|free]] library for [[arbitrary-precision arithmetic]], operating on signed [[integer]]s, [[rational number]]s, and [[floating point]] numbers.<ref name=what>{{cite web
'''GNU Multiple Precision Arithmetic Library''' ('''GMP''') is a [[free software|free]] library for [[arbitrary-precision arithmetic]], operating on [[Sign (mathematics)|signed]] [[integer]]s, [[Rational data type|rational numbers]], and [[Floating-point arithmetic|floating-point numbers]].<ref name=what>{{cite web
| url=https://gmplib.org/#WHAT
| url=https://gmplib.org/#WHAT
| title=What is GMP?
| title=What is GMP?
| accessdate=2014-04-07}}</ref> There are no practical limits to the precision except the ones implied by the available [[virtual memory|memory]] (operand dimension limit is 2<sup>32</sup>−1 bits on 32-bit machines and 2<sup>37</sup> bits on 64-bit machines).<ref>{{cite web
| access-date=2014-04-07}}</ref> There are no practical limits to the precision except the ones implied by the available [[virtual memory|memory]] (operands may be of up to 2<sup>32</sup>−1 bits on 32-bit machines and 2<sup>37</sup> bits on 64-bit machines).<ref>{{cite web
| url=https://gmplib.org/list-archives/gmp-bugs/2009-July/001538.html
| url=https://gmplib.org/list-archives/gmp-bugs/2009-July/001538.html
| title=Problems with mpz_set_str and huge strings
| title=Problems with mpz_set_str and huge strings
Line 25: Line 30:
| first=Torbjorn
| first=Torbjorn
| date=2009-07-06
| date=2009-07-06
| accessdate=2013-03-17}}</ref><ref>{{cite web
| access-date=2013-03-17}}</ref><ref>{{cite web
| url=https://gmplib.org/gmp6.0.html
| url=https://gmplib.org/gmp6.0.html
| title=GMP 6.0 News
| title=GMP 6.0 News
| accessdate=2019-10-04}}</ref> GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for [[C (programming language)|C]] but wrappers exist for other languages including [[Ada (programming language)|Ada]], [[C++]], [[C Sharp (programming language)|C#]], [[Julia (programming language)|Julia]], [[.NET]], [[OCaml]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]] and the [[Wolfram Language]]. Prior to 2008, [[Kaffe]], a [[Java virtual machine]], used GMP to support Java built-in arbitrary precision arithmetic.<ref>{{cite web
| access-date=2019-10-04}}</ref> GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for [[C (programming language)|C]], but [[Wrapper function|wrappers]] exist for other languages, including [[Ada (programming language)|Ada]], [[C++]], [[C Sharp (programming language)|C#]], [[Julia (programming language)|Julia]], [[.NET Framework|.NET]], [[OCaml]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], and [[Rust (programming language)|Rust]]. Prior to 2008, [[Kaffe]], a [[Java virtual machine]], used GMP to support Java built-in arbitrary precision arithmetic.<ref>{{cite web
| url=http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html
| url=http://www.kaffe.org/pipermail/kaffe/2008-February/191039.html
| title=Removed GMP math?
| title=Removed GMP math?
Line 34: Line 39:
| first=Andrew John
| first=Andrew John
| date=2008-02-28
| date=2008-02-28
| accessdate=2013-03-17}}</ref> Shortly after, GMP support was added to [[GNU Classpath]].<ref>{{cite web
| access-date=2013-03-17}}</ref> Shortly after, GMP support was added to [[GNU Classpath]].<ref>{{cite web
| url=https://www.gnu.org/software/classpath/announce/20090205.html
| url=https://www.gnu.org/software/classpath/announce/20090205.html
| title=GNU Classpath 0.98 "Better Late Than Never"
| title=GNU Classpath 0.98 "Better Late Than Never"
| date=2009-02-05
| date=2009-02-05
| accessdate=2013-03-17}}</ref>
| access-date=2013-03-17}}</ref>


The main target applications of GMP are [[cryptography]] applications and research, Internet security applications, and [[computer algebra system]]s.
The main target applications of GMP are [[cryptography]] applications and research, Internet security applications, and [[computer algebra system]]s.


GMP aims to be faster than any other [[bignum]] library for all operand sizes. Some important factors in doing this are:
GMP aims to be faster than any other [[bignum]] library for all operand sizes. Some important factors in doing this are:
* Using full [[word (data type)|words]] as the basic arithmetic type.
* Full [[word (data type)|words]] are the basic type for all arithmetic.
* Using different [[algorithm]]s for different [[operand]] sizes; algorithms that are faster for very big numbers are usually slower for small numbers.
* Different [[algorithm]]s 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 [[central processing unit|processors]]) is used in the most common inner loops to [[optimization_(computer_science)|optimize]] them as much as possible.
* Highly [[optimization (computer science)|optimized]] [[assembly language]] code for the most important [[inner loop]]s, specialized for different [[central processing unit|processors]].


The first GMP release was made in 1991. It is constantly developed and maintained.<ref name=main>{{cite web
The first GMP release was made in 1991. It is constantly developed and maintained.<ref name=main>{{cite web
| url=https://gmplib.org/
| url=https://gmplib.org/
| title=GNU MP Bignum Library
| title=GNU MP Bignum Library
| accessdate=2018-12-03}}</ref>
| access-date=2018-12-03}}</ref>


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 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).
Line 58: Line 63:
| title=The Mathematica Kernel: Issues in the Design and Implementation
| title=The Mathematica Kernel: Issues in the Design and Implementation
| date=October 2006
| date=October 2006
| accessdate=2013-03-17}}</ref> and [[Maple (software)|Maple]].<ref>{{cite web
| access-date=2013-03-17}}</ref> and [[Maple (software)|Maple]].<ref>{{cite web
| url=https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP
| url=https://www.maplesoft.com/support/help/AddOns/view.aspx?path=GMP
| title= The GNU Multiple Precision (GMP) Library
| title= The GNU Multiple Precision (GMP) Library
| publisher=[[Maplesoft]]
| publisher=[[Maplesoft]]
| accessdate=2013-03-17}}</ref> It is also used in the [[Computational Geometry Algorithms Library]] (CGAL) because geometry algorithms tend to 'explode' when using ordinary floating point CPU math.<ref>{{cite web
| access-date=2013-03-17}}</ref> It is also used in the [[Computational Geometry Algorithms Library]] (CGAL).
| url=https://www.cgal.org/Manual/
| title=CGAL Manuals}}</ref>


GMP is needed to build the [[GNU Compiler Collection]] (GCC).<ref>GCC uses the [[GNU MPFR]] library, which in turn relies on GMP. {{cite web
GMP is needed to build the [[GNU Compiler Collection]] (GCC).<ref>GCC uses the [[GNU MPFR]] library, which in turn relies on GMP. {{cite web
Line 70: Line 73:
| title=GCC 4.3 Release Series: Changes, New Features, and Fixes
| title=GCC 4.3 Release Series: Changes, New Features, and Fixes
| date=2012-11-02
| date=2012-11-02
| accessdate=2013-03-17}}</ref>
| access-date=2013-03-17}}</ref>


== Examples ==
== Examples ==
Line 81: Line 84:


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


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


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


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


return 0;
return 0;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 105: Line 108:
This code calculates the value of 7612058254738945 × 9263591128439081.
This code calculates the value of 7612058254738945 × 9263591128439081.


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


<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
Line 115: Line 118:
</syntaxhighlight>
</syntaxhighlight>


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


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 149: Line 152:
| [[Perl]]
| [[Perl]]
| [[GNU Lesser General Public License|LGPL]]
| [[GNU Lesser General Public License|LGPL]]
|-
| [https://metacpan.org/pod/Math::GMPz Math::GMPz], [https://metacpan.org/pod/Math::GMPf Math::GMPf] and [https://metacpan.org/pod/Math::GMPq Math::GMPq]
| [[Perl]]
| [[Artistic License]] v1.0 + [[GNU General Public License|GPL]] v1.0-or-later
|-
|-
| [https://github.com/aleaxit/gmpy General Multiprecision Python Project]
| [https://github.com/aleaxit/gmpy General Multiprecision Python Project]
Line 161: Line 168:
| [[Ruby (programming language)|Ruby]]
| [[Ruby (programming language)|Ruby]]
| [[Apache License|Apache 2.0]]
| [[Apache License|Apache 2.0]]
|-
| [https://lib.rs/crates/gmp-mpfr-sys Rust FFI bindings for GMP, MPFR and MPC]
| [[Rust (programming language)|Rust]]
| [[GNU Lesser General Public License|LGPL]]
|-
|-
| [https://www.php.net/gmp GNU Multi-Precision Library for PHP]
| [https://www.php.net/gmp GNU Multi-Precision Library for PHP]
Line 195: Line 206:
|-
|-
| [https://github.com/MachineCognitis/Math.Gmp.Native Math.Gmp.Native Library]
| [https://github.com/MachineCognitis/Math.Gmp.Native Math.Gmp.Native Library]
| [[.NET]]
| [[.NET Framework|.NET]]
| [[MIT License|MIT]]
| [[MIT License|MIT]]
|-
|-
Line 201: Line 212:
| [[Nim (programming language)|Nim]]
| [[Nim (programming language)|Nim]]
| [[MIT License|MIT]]
| [[MIT License|MIT]]
|-
| [https://github.com/jandom-devel/JGMP JGMP]
| [[Java (programming language)|Java]]
| [[GNU Lesser General Public License|LGPL]]
|}
|}


== See also ==
== See also ==
* [[GNU MPFR]] – library for arbitrary-precision computations with correct rounding, based on GNU MP
* [[GNU MPFR]] – a library for arbitrary-precision computations with correct rounding, based on GNU MP
* [[Class Library for Numbers|CLN]] – a class library for arbitrary precision
* [[Class Library for Numbers|CLN]] – a class library for arbitrary precision
* [[MPIR (mathematics software)|MPIR]] – a fork of GMP with mostly compatible interface which aims to provide MSVC-based compilation system for Windows platforms
* [[MPIR (mathematics software)|MPIR]] – a fork of GMP, not maintained any more


== References ==
== References ==
Line 215: Line 230:


{{GNU}}
{{GNU}}
{{Authority control}}


{{DEFAULTSORT:Gnu Multi-Precision Library}}
{{DEFAULTSORT:Gnu Multi-Precision Library}}
[[Category:C libraries]]
[[Category:Assembly language software]]
[[Category:C (programming language) libraries]]
[[Category:Computer arithmetic]]
[[Category:Computer arithmetic]]
[[Category:Free software programmed in C]]
[[Category:Free software programmed in C]]
Line 223: Line 240:
[[Category:Numerical libraries]]
[[Category:Numerical libraries]]
[[Category:Software using the LGPL license]]
[[Category:Software using the LGPL license]]
[[Category:Software written primarily in assembly language]]

Latest revision as of 04:58, 24 July 2024

GNU Multiple Precision Arithmetic Library
Developer(s)GNU Project
Initial release1991; 33 years ago (1991)[1]
Stable release
6.3.0[2] Edit this on Wikidata / 30 July 2023
Repositorygmplib.org/repo/
Written inC, (C++, assembly optionally)
TypMathematical software
LicenseDual LGPLv3 and GPLv2[3]
Websitegmplib.org

GNU Multiple Precision Arithmetic Library (GMP) is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.[3] 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).[4][5] 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. Prior to 2008, Kaffe, a Java virtual machine, used GMP to support Java built-in arbitrary precision arithmetic.[6] Shortly after, GMP support was added to GNU Classpath.[7]

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 bignum 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.[8]

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[9] and Maple.[10] It is also used in the Computational Geometry Algorithms Library (CGAL).

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

Examples

[edit]

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 7612058254738945 × 9263591128439081.

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

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

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

#include <iostream>
#include <gmpxx.h>

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

  std::cout << "    " << x << "\n"
            << "*\n"
            << "    " << y << "\n"
            << "--------------------\n"
            << x * y << "\n";

  return 0;
}

Language bindings

[edit]
Library name Sprache 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 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

[edit]
  • GNU MPFR – a library for arbitrary-precision computations with correct rounding, based on GNU MP
  • CLN – a class library for arbitrary precision
  • MPIR – a fork of GMP, not maintained any more

References

[edit]
  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. ^ a b "What is GMP?". Retrieved 2014-04-07.
  4. ^ Granlund, Torbjorn (2009-07-06). "Problems with mpz_set_str and huge strings". Retrieved 2013-03-17.
  5. ^ "GMP 6.0 News". Retrieved 2019-10-04.
  6. ^ Hughes, Andrew John (2008-02-28). "Removed GMP math?". Retrieved 2013-03-17.
  7. ^ "GNU Classpath 0.98 "Better Late Than Never"". 2009-02-05. Retrieved 2013-03-17.
  8. ^ "GNU MP Bignum Library". Retrieved 2018-12-03.
  9. ^ "The Mathematica Kernel: Issues in the Design and Implementation". October 2006. Retrieved 2013-03-17.
  10. ^ "The GNU Multiple Precision (GMP) Library". Maplesoft. Retrieved 2013-03-17.
  11. ^ 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.
[edit]