High-level wrapper for GNU MP (GMP)

Package Information

Version0.0.3 (2017-Apr-27)
CopyrightPer Nordlöw
AuthorsPer Nordlöw
Registered byPer Nordlöw



To use this package, put the following dependency into your project's dependencies section:




D-language high-level wrapper for GNU MP (GMP) library that aims to be mostly compatible with std.bigint.BigInt (copy construction excluded) and @safe pure nothrow @nogc except when converting to string.


  • Integers (GMP's mpz_t) are wrapped in gmp.z.MpZ
  • Rationals (GMP's mpq_t) are wrapped in gmp.q.MpQ

along with most of their common operations implemented either as member functions, free functions or both.


Copy construction is disabled (for now) to prevent inadvertent copying. Instead use f(move(z)) or f(z.move) (from std.algorithm.mutation) to pass by move or f(z.dup) to pass by value (via .dup member function).

Implementation is optimized through

  • mapping of GMP's C macros into D inline functions that operate directly on the internal C-representations __mpz_struct and __mpq_struct,

  • passing of MpZ-typed parameters as auto ref const in combination with conditional compilation for r-value MpZ passed parameters via __traits(isRef). This enables clever reuse of mpz_t instances when passed to __gmpz-functions. For instance, x + 42.Z can be lowered to

__gmpz_add(rhs._ptr, this._ptr, rhs._ptr);
return move(rhs);


opBinary!"+"()(auto ref const MpZ rhs)


Because, wrapper types don't have to be templatized, compilation is very fast; DMD compiles gmp-d in 0.04 seconds on my machine and test-build in 0.22 seconds.


Note that D's __traits(isRef) currently cannot be used to distinguish l-value from r-value passing of this (it should). This severly limits the possibilities of using C++-style expression templates to realize lazy evaluation in operator overloading. If this limitation were to be fixed, this library could implement lowering of expressions such

Z result = base^^exp % modulo

to the builtin

__gmpz_powm(result._ptr, base._ptr, expr._ptr, modulo._ptr)

See the unittests for MpzAddExpr, MpzMulExpr, etc for details on how this currently can be implemented and verified (in ccc-version) with free functions such add and sub.


There are more mpz_t functions that could be wrapped but these are good start.

Available versions

0.0.3 0.0.2 0.0.1 ~master