Lazy mpz allocation

Niels Möller nisse at
Mon Aug 31 07:01:31 UTC 2015

tg at (Torbjörn Granlund) writes:

> "Marco Bodrato" <bodrato at> writes:
>   If we change (I'd agree) mpz_init into
>   void
>   mpz_init (mpz_ptr x)
>   {
>     ALLOC (x) = 0; /* ZERO, any MPZ_REALLOC will allocate */
>     PTR (x) = & static_const_limb_shared_by_all_instances;
>     SIZ (x) = 0;
>   }
> I suppose we should do this!

Me too! Actually, mpz_init could then be

  static const mp_limb_t dummy_limb = 0;
  #define INITIAL_MPZ {0, 0, &dummy_limb};
  static const mpz_t initial_mpz = INITIAL_MPZ;  

  mpz_init (mpz_ptr x)
     *x = initial_mpz; /* Inline or memcpy, depending on compiler */

and later on we can consider some public interface for initializing an
mpz without any library call. 

A use case I had a couple of years ago is initialization of a struct
containing a couple of mpz_t values together with other stuff, and with
the above change one could initialize a complete such a struct using a
single memcpy.


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