GMP for D

Overview

GMP is a muti-precision C library for integer and floating point arithmetic. The sources to the interface to D is here. There are two parts to this interface to the library. The first, in the file gmp.d, is the low-level interface directly to the C API.
The second, in the files mpz.d (integers), mpq.d (rational numbers) and mpf.d (floating point numbers), are D classes providing a high-level interface that can be tuned for high performance when needed.
For more information about GMP or D see http://www.swox.com/gmp and http://www.digitalmars.com/d. Included is a sample program to compute the digits of pi using an algorithm from the "Great Compute Language Shootout" ported to D by Dave Fladebo.

The mpz, mpf and mpq classes are wrappers around the C library structures. The member functions of these classes call the corresponding function in the gmp interface. Arithmetic operations like add, mult, etc allocate a new object and call the low-level gmp function. Object pools can be enabled to fine-tune performace while still using the high-level class interface. The best performance will be obtained, however, by pass the mp field containing the underlying C struct directly to the low-level interface functions.

By default the object pools are not used and new class instances are allocated for every arithmetic operation that returns a new value. To enable recycling call the object constructors with the recyling flag of Recycle.Temp to indicate that the value being constructed is not temporary but that values computed from it are temporary. To save the result of a computation so that it does not get recycled call either save, which marks a given object a non-recyclable, or call assign to update a variable with the new value. See the examples section for more information. For details about the recycling flag see Recycle.

Install

Copy the d files to a location on the D module search path. Import the relevant modules into your code and include them on the command line except for gmp.d. Since gmp.d contains declarations only one should not include it in the linking phase. Otherwise you will get "multiple definition" errors.

On Linux the gmp library is pre-installed. On Windows you can get a pre-built library or rebuild from the source available at the GMP home page. The library should be included in the linking phase using a command option like "-L-lgmp".

Examples

High Level Integer Interface

The simplest way to use the high-level interface is to use the default recycling policy of never recycling objects. For example, running
  mpz x = new mpz(100);
  mpz y = 2*x + 10;
  char[] s = y.toString;
will set s to "210". The value of x will remain 100. To create large integers with more digits that can fit in a long or int, call the constuctor that accepts a string:
  mpz x = new mpz("12345678901234567890");

When recycling is enabled the functions assign and save are used to save the results of a computation. If x is an mpz object then

   assign(x,x+1);
allocates a new mpz, computes the sum and assigns to x. Calling
   mpz_add_ui(x.mp, x.mp, 1)
directly manipulates x and does not return any value. Any result of an arithmetic operation is assumed to be a temporary value that will be recycled by the next arithmetic operation. To use a value in multiple expressions or multiple times in an expression use the save property or assign function. For example:
   mpz x = new mpz("12345678901234567890",10,Recycle.Temp);
   for (int k = 0; k < 10000; k++) {
      assign(x, x-2*(x + 1));
   }
will not allocate any new objects after the first iteration through the loop. The save function can be used to pass arguments to a function. For example,
   void foo(mpz w) { ... use w ... }
   mpz x = new mpz(100,Recycle.Temp);
   mpz y;
   foo(x);   // x will not be recycled so this is safe
   foo(x+1)  // x+1 will be recycled so this is *not* safe
   foo((x+1).save) // the proper way to pass a result directly to a function.
   assign(y,x+1);
   foo(y)    // y will not be reycled so this is safe as well

Low Level Interface

Printing an integer to console:

   
   mpz_t x;
   ...
   gmp_printf("%Zd\n", &x);

Modules

gmp

This module is a translation of the primary functions in gmp.h. The mpn, mp and mpfr functions are not included in this module. Also, none of the inline declarations from gmp.h are included.

For human-readable documentation see http://www.swox.com/gmp/manual/ That documentation is written for C but the only differences gmp.d has from the C calling api are that long types should be declared as gmp_long and that calls to gmp_printf (and other functions that take a variable number of inputs) need to be passed the address of all mpz_t, mpq_t and mpf_t values.

gmppool

Manage pools of gmp objects for efficient managment of temporary values.
enum Recycle
Recycling flags stored with each object saying how to treat temporary values. Binary operations that involve values with recycling flags Self or Temp will generate values with recycling flag Self, which means those values will be recycled at the next operation. Operations that involve an object with flag Never will not generate values marked for recycling (ie the Never flag is sticky).
Self
Recycle this object and recycle values computed from this object
Temp
Do not recycle this value but recycle computed values
Never
Do not recycle this value or computed values
class TGmpPool(T)
An object pool of recycled objects of the same type T available for operations to reuse. The type T must be a class.
T[] freelist
Pool of objects to be recycled.
uint freen
The number of available objects in the pool
T allocate()
Allocate an object from either the builtin allocator or reuse an object that had been previously put in recycling.
void recycle(T val)
Add val to the free list.
void clearAll
Clears the pool and releases the references to any allocated objects.

mpz

Module for arbitrary precision integers.
class mpz
Class interface to arbitrary precision integers. Object wrapper for mpz_t.
mpz_t mp
The opaque C struct for direct library access.
int recycle
Flag to determine recycling behavior. For more details see the Recycle section.
this()
Construct and initialize to 0
this(mpz y, Recycle recycle = Recycle.Never)
Duplicate y with given recycling policy.
this(gmp_ulong y, Recycle recycle = Recycle.Never)
Construct from long y with given recycling policy.
this(int y, Recycle recycle = Recycle.Never)
Construct from int y with given recycling policy.
this(char[] y, int base, Recycle recycle = Recycle.Never)
Construct from string y (digits in the specified base) with given recycling policy.
mpz save
Set the recycling flag so that this object is not automatically recycled.
static mpz abs(mpz x)
Return a new mpz containing the absolute value of x. The input is recycled if allowed.
mpz opAdd(int y)
Return a new mpz containing the sum of the object and y. The object is recycled if allowed.
mpz opAdd(mpz y)
Return a new mpz containing the sum of the object and y. The object and y are recycled if allowed.
mpz opAddAssign(int y)
Add y to the object and return the result.
mpz opAddAssign(mpz y)
Add y to the object and return the result. The input y is recycled if allowed.
static mpz bin(mpz n, int k)
Return a new mpz containing the binomial coefficient n choose k. The input n is recycled if allowed.
int opCmp(int y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object is recycled if allowed.
int opCmp(mpz y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object and y are recycled if allowed.
mpz opDiv(int y)
Return a new mpz that is the quotient of the object and y. The object is recycled if allowed.
mpz opDiv(mpz y)
Return a new mpz that is the quotient of the object and y. The object and y are recycled if allowed.
gmp_long opDiv_r(int y)
Return the quotient of the object and y. The object is recycled if allowed.
mpz opDivAssign(int y)
Divide the object by y and return the result.
mpz opDivAssign(mpz y)
Divide the object by y and return the result. The input y is recycled if allowed.
int opEqual(int y)
Return non-zero if the object equals y. The object is recycled if allowed.
int opEqual(mpz y)
Return non-zero if the object equals y. The object and y are recycled if allowed.
gmp_ulong ui
Read/write property for the object as an unsigned int. The object is recycled if allowed on read.
gmp_long si
Read/write property for the object as a signed int. The object is recycled if allowed on read.
double get_d
Read-only property for the object as a double. The object is recycled if allowed.
char[] toString
Return the object as a string in base 10. The object is recycled if allowed.
gmp_ulong opMod(int y)
Return the modulus of the object and y. The object is recycled if allowed.
mpz opMod(mpz y)
Return the modulus of the object and y. The object and y are recycled if allowed.
mpz opMod(int y)
Take the modulus of the object and y and return the result.
mpz opMod(mpz y)
Take the modulus of the object and y and return the result. The input y is recycled if allowed.
gmp_long opMod_r(int y)
Return the modulus of y and the object. The object is recycled if allowed.
mpz opMul(int y)
Return the product of the object and y. The object is recycled if allowed.
mpz opMul(mpz y)
Return the product of the object and y. The object and y are recycled if allowed.
mpz opMulAssign(int y)
Multiply the object by y and return the result.
mpz opMulAssign(mpz y)
Multiply the object by y and return the result. The input y is recycled if allowed.
mpz opNeg()
Return the negative of the object. The object is recycled if allowed.
mpz opPostDec()
Decrement the object and return the original value.
mpz opPostInc()
Increment the object and return the original value.
static mpz pow(mpz x, int y)
Return the x^y power. The object is recycled if allowed.
static mpz root(mpz x, int n)
Return the nth root of x. The object is recycled if allowed.
static mpz sqrt(mpz x)
Return the square root of x. The object is recycled if allowed.
mpz opShl(int y)
Return the object shifted left by y (product with 2^y). The object is recycled if allowed.
mpz opShr(int y)
Return the object shifted right by y (quotient with 2^y). The object is recycled if allowed.
mpz opSub(int y)
Return a new mpz containing the difference of the object and y. The object is recycled if allowed.
mpz opSub_r(int y)
Return a new mpz containing the difference of y and the object. The object is recycled if allowed.
mpz opSub(mpz y)
Return a new mpz containing the difference of the object and y. The object and y are recycled if allowed.
mpz opSubAssign(int y)
Subtract y from the object and return the result.
mpz opSubAssign(mpz y)
Subtract y from the object and return the result. The input y is recycled if allowed.
mpz urandomb(gmp_randstate_t seed, ulong n)
Set the object to a random number and return the object.

TGmpPool!(mpz) mpzpool
Pool of mpz objects that have been recycled.
void assign(inout mpz x, mpz val)
Recycle any previous value in x, assign val to x and mark val as not being a temporary object.

mpf

Module for arbitrary precision floating point numbers.
class mpf
Class interface to arbitrary precision floats Object wrapper for mpf_t.
mpf_t mp
The opaque C struct for direct library access.
int recycle
Flag to determine recycling behavior. For more details see the Recycle section.
this()
Construct and initialize to 0
this(mpf y, Recycle recycle = Recycle.Never)
Duplicate y with given recycling policy.
this(double y, Recycle recycle = Recycle.Never)
Construct from double y with given recycling policy.
this(gmp_ulong y, Recycle recycle = Recycle.Never)
Construct from uint y with given recycling policy.
this(int y, Recycle recycle = Recycle.Never)
Construct from int y with given recycling policy.
this(char[] y, int base, Recycle recycle = Recycle.Never)
Construct from string y (digits in the specified base) with given recycling policy.
mpf save
Set the recycling flag so that this object is not automatically recycled.
static mpf abs(mpf x)
Return a new mpf containing the absolute value of x. The input is recycled if allowed.
mpf opAdd(int y)
Return a new mpf containing the sum of the object and y. The object is recycled if allowed.
mpf opAdd(mpf y)
Return a new mpf containing the sum of the object and y. The object and y are recycled if allowed.
mpf opAddAssign(int y)
Add y to the object and return the result.
mpf opAddAssign(mpf y)
Add y to the object and return the result. The input y is recycled if allowed.
int approx(mpf y, int n)
Return true if the object and y are equal to n bits of precision
int opCmp(int y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object is recycled if allowed.
int opCmp(double y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object is recycled if allowed.
int opCmp(mpf y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object and y are recycled if allowed.
mpf opDiv(int y)
Return a new mpf that is the quotient of the object and y. The object is recycled if allowed.
mpf opDiv(mpf y)
Return a new mpf that is the quotient of the object and y. The object and y are recycled if allowed.
mpf opDivAssign(int y)
Divide the object by y and return the result.
mpf opDivAssign(mpf y)
Divide the object by y and return the result. The input y is recycled if allowed.
int opEqual(mpf y)
Return non-zero if the object equals y. The object and y are recycled if allowed.
double get_d
Read-only property for the object as a double. The object is recycled if allowed.
char[] toString
Return the object as a string in base 10. The object is recycled if allowed.
mpf opMul(mpf y)
Return the product of the object and y. The object and y are recycled if allowed.
mpf opMulAssign(mpf y)
Multiply the object by y and return the result. The input y is recycled if allowed.
mpf opNeg()
Return the negative of the object. The object is recycled if allowed.
mpf opSub(mpf y)
Return a new mpf containing the difference of the object and y. The object and y are recycled if allowed.
mpf opSubAssign(mpf y)
Subtract y from the object and return the result. The input y is recycled if allowed.

TGmpPool!(mpf) mpfpool
Pool of mpf objects that have been recycled.
void assign(inout mpf x, mpf val)
Recycle any previous value in x, assign val to x and mark val as not being a temporary object.
void setPrecision(gmp_ulong n)
Set the default precision for all new mpf objects and clear the existing object pool
gmp_ulong getPrecision()
Get the current default precision.

mpq

Module for arbitrary precision rational numbers.
class mpq
Class interface to arbitrary precision rational numbers. Object wrapper for mpq_t.
mpq_t mp
The opaque C struct for direct library access.
int recycle
Flag to determine recycling behavior. For more details see the Recycle section.
this()
Construct and initialize to 0
this(mpf y, Recycle recycle = Recycle.Never)
Duplicate y with given recycling policy.
this(double y, Recycle recycle = Recycle.Never)
Construct from double y with given recycling policy.
this(gmp_ulong num, gmp_ulong den, Recycle recycle = Recycle.Never)
Construct from uint num/den with given recycling policy.
this(int num, int den, Recycle recycle = Recycle.Never)
Construct from int num/den with given recycling policy.
this(char[] y, int base, Recycle recycle = Recycle.Never)
Construct from string y (digits in the specified base) with given recycling policy.
mpf save
Set the recycling flag so that this object is not automatically recycled.
static mpq abs(mpq x)
Return a new mpq containing the absolute value of x. The input is recycled if allowed.
mpq opAdd(mpq y)
Return a new mpq containing the sum of the object and y. The object and y are recycled if allowed.
mpq opAddAssign(mpq y)
Add y to the object and return the result. The input y is recycled if allowed.
int opCmp(int y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object is recycled if allowed.
int opCmp(mpq y)
Return -1 if the object is less than y, 0 if equal and 1 if greater. The object and y are recycled if allowed.
mpq canonicalize
Put the object in lowest terms.
mpq opDiv(mpq y)
Return a new mpq that is the quotient of the object and y. The object and y are recycled if allowed.
mpq opDivAssign(mpq y)
Divide the object by y and return the result. The input y is recycled if allowed.
int opEqual(int y)
Return non-zero if the object equals y. The object is recycled if allowed.
int opEqual(mpq y)
Return non-zero if the object equals y. The object and y are recycled if allowed.
mpq invert
Return the reciprocal of the object. The object is recycled if allowed.
mpz num
Return the numerator.
mpz den
Return the denominator.
double get_d
Read-only property for the object as a double. The object is recycled if allowed.
char[] toString
Return the object as a string in base 10. The object is recycled if allowed.
mpq opMul(mpq y)
Return the product of the object and y. The object and y are recycled if allowed.
mpq opMulAssign(mpq y)
Multiply the object by y and return the result. The input y is recycled if allowed.
mpq opNeg()
Return the negative of the object. The object is recycled if allowed.
mpq opSub(mpq y)
Return a new mpq containing the difference of the object and y. The object and y are recycled if allowed.
mpq opSubAssign(mpq y)
Subtract y from the object and return the result. The input y is recycled if allowed.

TGmpPool!(mpq) mpqpool
Pool of mpq objects that have been recycled.
void assign(inout mpq x, mpq val)
Recycle any previous value in x, assign val to x and mark val as not being a temporary object.