gmpd 0.1.5
Highlevel wrapper for GNU MP (GMP)
To use this package, put the following dependency into your project's dependencies section:
gmpd
Dlanguage highlevel 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
.
Basics
 Integers (GMP's
mpz_t
) are wrapped ingmp.z.MpZ
 Rationals (GMP's
mpq_t
) are wrapped ingmp.q.MpQ
along with most of their common operations implemented either as member functions, free functions or both.
Sample Usage
MpZ
import gmp.z;
@safe pure nothrow @nogc:
unittest
{
alias Z = MpZ;
assert(Z.mersennePrime(15) == 2^^15  1);
const a = 42.Z;
const b = a.dup; // explicit copying required
}
MpQ
import gmp.q;
@safe pure nothrow @nogc:
unittest
{
alias Q = MpQ;
Q x = Q(4, 6);
assert(x.numerator == 4);
assert(x.denominator == 6);
x.canonicalize();
assert(x.numerator == 2);
assert(x.denominator == 3);
assert(inverse(x) == Q(3, 2));
assert(Q(1, 2) + Q(1, 3) == Q(5, 6));
}
Features
Copy construction is disabled by default (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).
If you really need to have copy construction you can use CopyableMpZ
.
Implementation is optimized through
mapping of GMP's C macros into D inline functions that operate directly on the internal Crepresentations
__mpz_struct
and__mpq_struct
,passing of
MpZ
typed parameters asauto ref const
in combination with conditional compilation for rvalueMpZ
passed parameters via__traits(isRef)
. This enables clever reuse ofmpz_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);
in
opBinary!"+"()(auto ref const MpZ rhs)
Compilation Performance
Compilation is very fast; DMD compiles gmpd
in 0.04 seconds on my machine and
testbuild in 0.22 seconds.
Runtime Performance
Wrapper is as lightweight as possible; Some Dfunctions directly access the internal Cdatastructure exposed by the structs and macros in the Cheaders of GNU GMP.
Limitations
Note that D's __traits(isRef)
currently cannot be used to distinguish lvalue
from rvalue passing of this
. 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
(probably via some introspection mechanism other than the trait isRef
or
nonstruct
member operator overloading), 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
.
Future
There are more mpz_t
 and mpq_t
functions that should be wrapped but these are good start.
 Registered by Per Nordlöw
 0.1.5 released 9 months ago
 nordlow/gmpd
 BSL1.0
 Per Nordlöw
 Authors:
 Dependencies:
 none
 Versions:

0.1.5 2018Jul29 0.1.4 2018Jun03 0.1.3 2018May28 0.1.2 2018May24 0.1.1 2018May23  Download Stats:


0 downloads today

3 downloads this week

5 downloads this month

337 downloads total

 Score:
 1.6
 Short URL:
 gmpd.dub.pm