gmp-d 0.0.3
High-level wrapper for GNU MP (GMP)
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
gmp-d
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
.
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.
Features
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 asauto ref const
in combination with conditional compilation for r-valueMpZ
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)
Performance
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.
Limitations
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
.
Future
There are more mpz_t
functions that could be wrapped but these are good start.
- Registered by Per Nordlöw
- 0.0.3 released 7 years ago
- nordlow/gmp-d
- Boost
- Per Nordlöw
- Authors:
- Dependencies:
- libgmp
- Versions:
-
0.2.17 2022-Dec-20 0.2.16 2022-Oct-15 0.2.15 2022-Sep-12 0.2.14 2022-Jul-27 0.2.13 2022-Jul-27 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
833 downloads total
-
- Score:
- 1.7
- Short URL:
- gmp-d.dub.pm