Ticket #3489 (closed feature request: fixed)

Opened 4 years ago

Last modified 11 months ago

Adding some gmp bindings to integer-gmp (copied from the cvs-ghc list)

Reported by: pumpkin Owned by: igloo
Priority: normal Milestone: 7.6.1
Component: Compiler Version: 6.11
Keywords: Cc: leather@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

This is my first patch for GHC, so I apologize in advance if I do something wrong! I've actually attached three patches for libraries/base, libraries/integer-gmp, and libraries/integer-simple, but because there is no change to any exposed API and only a couple of extra functions added to GHC.Integer, I'm sending it here instead of to libraries at haskell.org.

Basically, I added cmm bindings to mpz_powm(_ui), mpz_tstbit, and mpz_sizeinbase. The modular exponentiation function is significantly faster than anything I could find in pure haskell, and the bit testing is way more efficient than the default Data.Bits implementation involving a potentially massive left shift (in fact, might that be better phrased as a right shift of the tested value?). The sizeinbase function (essentially an integer logarithm, unfortunately with the base restricted to a maximum of 62) also looked handy so I added it while I was there (at Bertram Felgenhauer's suggestion).

So these patches amount to:

  • A one-line change to Data.Bits in base, adding our specialized testBit function.
  • Some cmm code additions in integer-gmp, plus the Haskell glue to make them usable from outside.
  • A very simplistic implementation of testBitInteger for integer-simple, so that the one-line change to Data.Bits doesn't fail

when building with integer-simple.

I've tested the code and it appears to be correct, and have validated it with both integer-simple and -gmp, in both cases encountering two unexpected ghci test failures (in OS X): ghci028 and 2816. Manuel Chakravarty said these were normal and that he'd experienced the build failures under OS X too, so I didn't look into them any more deeply.

Attachments

integer-gmp.patch Download (10.0 KB) - added by pumpkin 4 years ago.
integer-simple.patch Download (1.9 KB) - added by pumpkin 4 years ago.
base.patch Download (8.8 KB) - added by pumpkin 4 years ago.

Change History

Changed 4 years ago by pumpkin

Changed 4 years ago by pumpkin

Changed 4 years ago by pumpkin

Changed 4 years ago by igloo

  • difficulty set to Unknown
  • milestone set to 6.14.1

Thanks for the patch. We'll take a look in the 6.14 branch.

Changed 4 years ago by spl

  • cc leather@… added

Changed 3 years ago by ross

  • failure set to None/Unknown
  • type changed from proposal to feature request

Changed 2 years ago by igloo

  • milestone changed from 7.0.1 to 7.0.2

Changed 2 years ago by igloo

  • milestone changed from 7.0.2 to 7.2.1

Changed 21 months ago by igloo

  • status changed from new to patch

Changed 21 months ago by igloo

  • milestone changed from 7.2.1 to 7.4.1

Changed 17 months ago by igloo

  • milestone changed from 7.4.1 to 7.6.1

Changed 14 months ago by igloo

  • owner set to igloo

Changed 11 months ago by igloo

  • status changed from patch to closed
  • resolution set to fixed

Thanks, I've applied the testBit parts of the patches.

It looks like the other parts aren't used by Integer, and I don't think we want to make bindings for the whole GMP API. The internals are exposed, though, so a separate package could do so.

Note: See TracTickets for help on using tickets.