www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - libgmp deimos library

reply Andrew Hall <andrew.m.h96 gmail.com> writes:
I've been writing D bindings for GMP and I was wondering if they 
warrant inclusion in the Deimos git repo. I know Dlang has 
bignum, but GMP is still the fastest library for extremely large 
numbers.

The repos is in 'Deimos' format so if you care to use it, please 
tell me how to proceed.
https://github.com/andrew-m-h/libgmp
Jan 11 2017
next sibling parent reply Guillaume Chatelet <chatelet.guillaume gmail.com> writes:
On Wednesday, 11 January 2017 at 10:29:26 UTC, Andrew Hall wrote:
 I've been writing D bindings for GMP and I was wondering if 
 they warrant inclusion in the Deimos git repo. I know Dlang has 
 bignum, but GMP is still the fastest library for extremely 
 large numbers.

 The repos is in 'Deimos' format so if you care to use it, 
 please tell me how to proceed.
 https://github.com/andrew-m-h/libgmp
FYI I wrote a MPFR Deimos binding: https://github.com/gchatelet/deimos-mpfr MPFR relies on GMP so I had to create this file: https://github.com/gchatelet/deimos-mpfr/blob/master/source/deimos/gmp.d If you register your project on https://code.dlang.org/ I'll be able to depend on your project and reuse your bindings.
Jan 11 2017
parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Wednesday, 11 January 2017 at 10:52:40 UTC, Guillaume Chatelet 
wrote:
 On Wednesday, 11 January 2017 at 10:29:26 UTC, Andrew Hall 
 wrote:
 I've been writing D bindings for GMP and I was wondering if 
 they warrant inclusion in the Deimos git repo. I know Dlang 
 has bignum, but GMP is still the fastest library for extremely 
 large numbers.

 The repos is in 'Deimos' format so if you care to use it, 
 please tell me how to proceed.
 https://github.com/andrew-m-h/libgmp
FYI I wrote a MPFR Deimos binding: https://github.com/gchatelet/deimos-mpfr MPFR relies on GMP so I had to create this file: https://github.com/gchatelet/deimos-mpfr/blob/master/source/deimos/gmp.d If you register your project on https://code.dlang.org/ I'll be able to depend on your project and reuse your bindings.
Cheers, I've done as you suggest, and 'libgmp' is registered under DUB. Feel free to have a look at the code because this is the first binding I've written in D. That said, it does have a test suite (unit tests) that do work. What is the procedure for being accepted by Deimos?
Jan 11 2017
parent reply Guillaume Chatelet <chatelet.guillaume gmail.com> writes:
On Wednesday, 11 January 2017 at 11:34:55 UTC, Andrew Hall wrote:
 Cheers, I've done as you suggest, and 'libgmp' is registered 
 under DUB. Feel free to have a look at the code because this is 
 the first binding I've written in D. That said, it does have a 
 test suite (unit tests) that do work. What is the procedure for 
 being accepted by Deimos?
Can you add the dub component to the "Development library" -> "D language binding" category? Also you should probably post about your library on the "Announce" list. I'm not at all an expert on D bindings, I found the following links: - This page explains the procedure: http://wiki.dlang.org/D_binding_for_C - But this one too: http://dlang.org/spec/interfaceToC.html - This page seems out of date: https://wiki.dlang.org/Bindings. - And most bindings are discoverable through https://code.dlang.org/?sort=updated&category=library.binding The old way seems to gather all bindings under https://github.com/D-Programming-Deimos. The new way seems to use https://code.dlang.org/. Is someone aware of an official position on this?
Jan 11 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-01-11 13:33, Guillaume Chatelet wrote:

 I'm not at all an expert on D bindings, I found the following links:
 - This page explains the procedure: http://wiki.dlang.org/D_binding_for_C
 - But this one too: http://dlang.org/spec/interfaceToC.html
 - This page seems out of date: https://wiki.dlang.org/Bindings.
 - And most bindings are discoverable through
 https://code.dlang.org/?sort=updated&category=library.binding

 The old way seems to gather all bindings under
 https://github.com/D-Programming-Deimos.
 The new way seems to use https://code.dlang.org/.

 Is someone aware of an official position on this?
Nothing official but I would say: * Use DStep [1] to generate the bindings automatically * Use code.dlang.org instead of Deimos. On code.dlang.org you can have your own repository and don't need to be dependent on someone from the core team to merge pull requests. Anything on code.dlang.org will be easily accessible to other projects using Dub [1] https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
Jan 11 2017
prev sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 11 January 2017 at 10:29:26 UTC, Andrew Hall wrote:
 https://github.com/andrew-m-h/libgmp
Nice. I'll make use of it in my high-level wrapper at https://github.com/nordlow/phobos-next/blob/master/src/gmp.d instead of my hand-coded externs. It is currently well tested (unittests give 100 % code coverage) but not completed. Majority of integer support is there. Nothing else. I'll put up a DUB package soon. I guess gmp-d would be a good name. It's worth mentioning that compile-and-link of gmp.d of a debug-build including unittests take 0.07 seconds on my machine :)
Jan 11 2017
parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Wednesday, 11 January 2017 at 12:50:15 UTC, Nordlöw wrote:
 On Wednesday, 11 January 2017 at 10:29:26 UTC, Andrew Hall 
 wrote:
 https://github.com/andrew-m-h/libgmp
Nice. I'll make use of it in my high-level wrapper at https://github.com/nordlow/phobos-next/blob/master/src/gmp.d instead of my hand-coded externs. It is currently well tested (unittests give 100 % code coverage) but not completed. Majority of integer support is there. Nothing else. I'll put up a DUB package soon. I guess gmp-d would be a good name. It's worth mentioning that compile-and-link of gmp.d of a debug-build including unittests take 0.07 seconds on my machine :)
If you would like some test cases that I wrote for my mpz_t wrapper, you should look in https://github.com/andrew-m-h/libgmp/blob/master/source/deimos/gmp/operators.d and take the bottom two unittests. The test cases are more complex than just your usual a + b: - a verification of Fermat's Little Theorem for mersenne primes M(p) p < 200 - a 'rediscovery' of Lander & Parkin's counter example to Euler's Sum of Powers Conjecture. i.e. finding: 27^5 + 84^5 + 110^5 + 133^5 = 144^5 They should be trivially translateable to your interface, and test out your library for speed ;) I was planning on extending this wrapper but since you have one, you may as well have the test cases.
Jan 11 2017
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 11 January 2017 at 13:53:18 UTC, Andrew Hall wrote:

 I was planning on extending this wrapper but since you have 
 one, you may as well have the test cases.
Great. I'll merge mine and your's stuff and put it in https://github.com/nordlow/gmp-d.git Could you please give some suggestions on the repo directory structure and contents of dub.sdl for specifying deps to the deimos C wrappers?
Jan 11 2017
next sibling parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Wednesday, 11 January 2017 at 18:32:36 UTC, Nordlöw wrote:
 On Wednesday, 11 January 2017 at 13:53:18 UTC, Andrew Hall 
 wrote:

 I was planning on extending this wrapper but since you have 
 one, you may as well have the test cases.
Great. I'll merge mine and your's stuff and put it in https://github.com/nordlow/gmp-d.git Could you please give some suggestions on the repo directory structure and contents of dub.sdl for specifying deps to the deimos C wrappers?
Basically, gmp.h is split up into 4 files: gmp.d, integer.d, floating.d & rational.d. these are all public import'd by package.d so if you would like the contents of all of gmp.h, import deimos.gmp. However if you only need the integer stuff, you could just import deimos.gmp.gmp, deimos.gmp.integer; operators.d contains my first wrapper of the mpz_t type.
Jan 11 2017
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 11 January 2017 at 21:52:57 UTC, Andrew Hall wrote:
 Basically, gmp.h is split up into 4 files:
 gmp.d, integer.d, floating.d & rational.d.
 these are all public import'd by package.d so if you would like 
 the contents of all of gmp.h,
 import deimos.gmp. However if you only need the integer stuff, 
 you could just import deimos.gmp.gmp, deimos.gmp.integer;

 operators.d contains my first wrapper of the mpz_t type.
Thanks. One important thing...you've forgotten to qualify the D wrapper functions as `pure nogc`. All should be ` nogc` and most should be `pure`. I'm not sure which of them that aren't pure, that is which modify global state. Maybe you can look that up in the GMP manual. I find the pdf version the easiest to search.
Jan 11 2017
parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Wednesday, 11 January 2017 at 22:14:03 UTC, Nordlöw wrote:
 One important thing...you've forgotten to qualify the D wrapper 
 functions as `pure  nogc`.
in the actual bindings, I did include pure & nothrow however I've not had time in the wrapper.
Jan 11 2017
parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Wednesday, 11 January 2017 at 22:18:53 UTC, Andrew Hall wrote:
 On Wednesday, 11 January 2017 at 22:14:03 UTC, Nordlöw wrote:
 One important thing...you've forgotten to qualify the D 
 wrapper functions as `pure  nogc`.
in the actual bindings, I did include pure & nothrow however I've not had time in the wrapper.
also, why nogc?
Jan 11 2017
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 11 January 2017 at 22:19:58 UTC, Andrew Hall wrote:
 also, why nogc?
Because C-functions never use the D garbage collector :)
Jan 11 2017
parent reply Mike Parker <aldacron gmail.com> writes:
On Wednesday, 11 January 2017 at 23:12:30 UTC, Nordlöw wrote:
 On Wednesday, 11 January 2017 at 22:19:58 UTC, Andrew Hall 
 wrote:
 also, why nogc?
Because C-functions never use the D garbage collector :)
They should also be marked nothrow. That and nogc should generally be applied to all C function bindings, but pure only on those which actually are pure.
Jan 11 2017
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 12 January 2017 at 02:12:01 UTC, Mike Parker wrote:

 They should also be marked nothrow.
Which I see they are.
Jan 11 2017
prev sibling parent Andrew Hall <andrew.m.h96 gmail.com> writes:
On Thursday, 12 January 2017 at 02:12:01 UTC, Mike Parker wrote:
 On Wednesday, 11 January 2017 at 23:12:30 UTC, Nordlöw wrote:
 On Wednesday, 11 January 2017 at 22:19:58 UTC, Andrew Hall 
 wrote:
 also, why nogc?
Because C-functions never use the D garbage collector :)
They should also be marked nothrow. That and nogc should generally be applied to all C function bindings, but pure only on those which actually are pure.
Just added nogc. Cheers
Jan 11 2017
prev sibling parent reply Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 2017-01-11 at 18:32 +0000, Nordl=C3=B6w via Digitalmars-d wrote:
[=E2=80=A6]
 Great. I'll merge mine and your's stuff and put it in
=20
 https://github.com/nordlow/gmp-d.git
=20
[=E2=80=A6] Is the intention for this to stand with or replace std.bigint ?=20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jan 12 2017
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 12 January 2017 at 09:19:26 UTC, Russel Winder wrote:
 Is the intention for this to stand with or replace std.bigint ?
I have no plan yet. I'm just gonna work on it for fun until it covers most of GNU MP. Pull requests are very welcome.
Jan 12 2017
parent reply Andrew Hall <andrew.m.h96 gmail.com> writes:
On Thursday, 12 January 2017 at 16:55:10 UTC, Nordlöw wrote:
 On Thursday, 12 January 2017 at 09:19:26 UTC, Russel Winder 
 wrote:
 Is the intention for this to stand with or replace std.bigint ?
I have no plan yet. I'm just gonna work on it for fun until it covers most of GNU MP. Pull requests are very welcome.
What would be cool would be a way for D to generate 'optimal' gmp code. So a template function that takes in an expression, and output's D / GMP code that uses minimal allocation. ulong a = 27, b = 84, c = 110, d = 133; compileGMP!"Integer res = a ^^ 5 + b ^^ 5 + c ^^ 5 + d ^^ 5"(); might generate the code: Integer res, r2; //r2 used as a register of sorts (minimise allocation of Integers) mpz_init(res); mpz_init(r2); mpz_ui_pow_ui(res, a, 5); mpz_ui_pow_ui(r2, b, 5); mpz_add(res, res, r2); mpz_ui_pow_ui(res, c 5); mpz_add(res, res, r2); mpz_ui_pow_ui(r2, d, 5); mpz_add(res, res, r2); Obviously the compileGMP function would need to know a bit about the types, but that can be arranged. Just something to think about ;)
Jan 14 2017
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 15 January 2017 at 03:04:30 UTC, Andrew Hall wrote:
 On Thursday, 12 January 2017 at 16:55:10 UTC, Nordlöw wrote:
 On Thursday, 12 January 2017 at 09:19:26 UTC, Russel Winder 
 wrote:
 Is the intention for this to stand with or replace std.bigint 
 ?
I have no plan yet. I'm just gonna work on it for fun until it covers most of GNU MP. Pull requests are very welcome.
What would be cool would be a way for D to generate 'optimal' gmp code. So a template function that takes in an expression, and output's D / GMP code that uses minimal allocation.
I'm gonna solve this with expression templates instead: https://github.com/nordlow/gmp-d/blob/master/src/gmp.d#L1725 in use for instance here https://github.com/nordlow/gmp-d/blob/master/src/gmp.d#L1744 I'm gonna activate it in the operator overloads for MpZ this week.
Jan 16 2017