mpz_limbs interface

Niels Möller nisse at
Sat Feb 8 07:27:33 UTC 2014

bodrato at writes:

> Il Gio, 6 Febbraio 2014 9:56 am, Niels ha scritto:
>> What do you think?
>>   void
>>   mpz_set_d (mpz_ptr r, double d)
>>   {
>>     int negative;
>> !   mp_limb_t tp[MPN_SET_D_SIZE];
>> !   mpz_t t = MPZ_ROINIT_N (tp, MPN_SET_D_SIZE);
> Uhm, in tests/mpz/t-limbs.c we test MPZ_ROINIT_N only
> #if __STDC_VERSION__ >= 199901

I wasn't sure about whether or not that type of initializer, with one
member initialized with a pointer to a local variable, is allowed in
c89. But I wrote a small test program which compiled with no complaints
from gcc -std=c89. My interpretation of that was that a pointer to a
local variable is "constant enough" to use in an initializer.

But of course, if that doesn't work portably, MPZ_ROINIT_N mustn't be
used here.

>   exp = mpn_set_d (mpz_limbs_write (r, MPN_SET_D_SIZE), d);

Not sure if it really matters, but my intention was to try to avoid
doing realloc twice. With the above, you may realloc once growing the
allocation to MPN_SET_D_SIZE (which could be 2), and then realloc again
in mpn_mul_2exp.


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