GMP

Simon Marlow simonmar at microsoft.com
Mon Jun 14 09:54:04 EDT 2004


On 10 June 2004 01:15, John Meacham wrote:

> I was curious what the best way would be to access the various useful
> GMP functions which are not exported for Integers. I was thinking of
> making my own (strict) Integer type, but it would be much easier if I
> can just use the FFI to import the required functions and get at the
> mpz_t inside Integers somehow.
> 
> I am not positive, but it looks like ghc seems to have primitives
> which call the gmp functions internally rather than using the FFI..
> At least I didn't see an obvious example poking around in the library.

That's right.  Interestingly, I think many of the Integer primitives
could be done using the FFI now; this probably wasn't the case when they
were originally implemented.  Efficiency might be slightly worse,
because of the need to allocate temporary MP structs using alloca,
compared to using C stack allocation.  On other other hand, you'd save a
call/return because the primitive would be implemented inline.

Also, GHC needs to be able to implement constant folding on Integer
primops, which isn't possible when they are defined using the FFI.
However, I suspect that constant folding Integer ops is very rarely used
at the moment, because the ops aren't inlined due to being too large, so
the actual primops are never visible to the compiler.

You could implement your own Integer ops using the FFI to call the GMP
functions.  Take a look at the GMP primops in ghc/rts/PrimOps.hc for
hints.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list