mini-gmp

Niels Möller nisse at lysator.liu.se
Wed Jan 15 22:23:28 UTC 2014


Zimmermann Paul <Paul.Zimmermann at inria.fr> writes:

> I tried to compile MPFR with mini-gmp. I needed to do a few changes:

Cool!

> 1) I had to add #define mpz_init __gmpz_init, because in MPFR
>    configure we check for __gmpz_init

I don't think that's the right way. mini-gmp should not pretend to be the
real thing.

I recommend having a configure check which tests for the *real* gmp, and
if gmp is not available (or some --enable-mini-gmp option is used), have
fallback code in configure which explicitly sets up use of mini-gmp.

> 2) I had to add a line:
>    unsigned int mp_bits_per_limb = GMP_NUMB_BITS;

I wasn't aware of this variable (in the public GMP interface). Adding it
to mini-gmp seems right to me.

> 3) I had to change "static void" to "void" for gmp_default_alloc,
>    gmp_default_realloc and gmp_default_free since we use them in MPFR

Not sure how to handle this. On one hand, I'd prefer if mini-gmp doesn't
export any gmp-internal interfaces. 

In general, when using mini-gmp, I think it's reasonable to add
application-specific glue, a bit like gmp's boostrap.c which #includes
mini-gmp.c, and then adds some additional definitions. Most of your
additions could be handled that way.

But I see no nice and obvious way to make the gmp_default_* functions
non-static with that approach.

> Changes in mini-gmp.h: I had to add the following:
>
>> #define __GNU_MP_VERSION            5
>> #define __GNU_MP_VERSION_MINOR      1
>> #define __GNU_MP_VERSION_PATCHLEVEL 3
>> #define __gmp_const const
>> #define __GMP_DECLSPEC

If these are needed, I really think they belong in some mpfr-specific
file. Either under #if USE_MINI_GMP setup by configure. Or just

#ifndef __GNU_MP_VERSION
#define __GNU_MP_VERSION 5
#endif

etc.

One might argue that mini-gmp should advertise the same version as the
gmp release mini-gmp.c was bundled with. But I doubt that's useful; I
expect these constants to be used mainly for API compatibility things,
and mini-gmp is only a subset, not fully compatible.

>> #define GMP_NUMB_BITS 64

It would make sense to have this (and GMP_LIMB_BITS) in mini-gmp.h. But
the definition (which must be portable and not depending on configure)
is

#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)

which depends on <limits.h>. And unlike GMP's definition, it's not a
preprocessor constant, so it's not a fully compatible substitute (#if
GMP_LIMB_BITS == 64 won't work with mini-gmp).

If you need a preprocessor constant, I think it has to be defined (or
redefined) outside of mini-gmp.h.

> It would be great if those changes could be made upstream.

I hope we can solve all the problems, even if I don't think adding all
these things to mini-gmp is the right solution.

BTW, how do you deal with gmp vs mini-gmp in public mpfr header files?

Regards,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.


More information about the gmp-devel mailing list