[Haskell-cafe] MPFR / FFI - Nefarious (Simple?) bug

Aleš Bizjak ales.bizjak0 at gmail.com
Mon Oct 6 12:26:26 EDT 2008

> In order to create an arbitrary precision floating point / drop in
> replacement for Double, I'm trying to wrap MPFR (http://www.mpfr.org/)
> using the FFI but despite all my efforts the simplest bit of code
> doesn't work. It compiles, it runs, but it crashes mockingly after
> pretending to work for a while.

I've done an interface to MPFR. See Michal's message. It isn't  
particularly fast
but it works. You could improve/suggest improvements there, if you are  

> Why does the C version work, but the Haskell version flake out?

I'm more or less guessing here, but it might have something to do with mpfr
calling __gmp_allocate_func in init2.c which is called inside  
mpfr_init_set_si, coupled
with what Judah Jacobson said. This is the only possible problem I can  
think of because
when using custom memory interface and allocating with malloc all the  
problems seem to disappear.

If you substitute your mpfr_set_signed_int for the one below, the noworks  
should become works.

mpfr_ptr mpf_set_signed_int(int x)
   mpfr_ptr result = mpf_new_mpfr();
   mp_limb_t * limb = malloc(mpfr_custom_get_size(mpfr_get_default_prec()));
   mpfr_custom_init(limb, mpfr_get_default_prec());
   mpfr_custom_init_set(result, MPFR_NAN_KIND, 0, mpfr_get_default_prec(),  
   if (result == NULL)
     return NULL;
   mpfr_set_si(result, x, GMP_RNDN);
   return result;

More information about the Haskell-Cafe mailing list