gmp-d 0.0.2

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.

Copy construction is disabled (for now) to prevent inadvertent copying. Instead use f(move(z)) (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-representation __mpz_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);

in

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

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.

Further note, that as, wrapper types doesn't have to be templatized, compilation is very fast (DMD compiles it in 0.04 seconds on my machine and test-build in 0.22 seconds).

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

Authors:
  • Per Nordlöw
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
Show all 30 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 4 downloads this month

  • 833 downloads total

Score:
1.7
Short URL:
gmp-d.dub.pm