mpz_gcd_ui(NULL, ...) with small limbs

Marc Glisse marc.glisse at inria.fr
Tue Feb 8 10:05:57 CET 2022


On Tue, 8 Feb 2022, Marco Bodrato wrote:

> Ciao Marc,
>
> Il 2022-01-22 23:40 Marc Glisse ha scritto:
>> the documentation for mpz_gcd_ui(rop, op1, op2) says "If rop is not
>> NULL, store the result there." and indeed the main code contains two
>> tests "if (w != NULL)". However, mpz_gcd_ui also contains special code
>> for the case where op2 (an unsigned long) does not fit in a limb. And
>> that code calls mpz_gcd without checking if the first argument is
>> NULL. That probably does not affect any platform, since nails are not
>> supported and I don't see anything defining __GMP_SHORT_LIMB.
>
> What about the following (untested)?

Makes sense to me. I am just not sure if 3 is the right number and if we 
are consistent about it. Shouldn't 2 be enough? Or is it to be safe in 
case mpz_gcd decides to overallocate? 3 isn't a problem at all, but I 
think a comment explaining it may avoid confusion in the future.

(replacing 2-3 with something larger would be up to a potential small-limb 
branch)

I don't think we need to set SIZ(lw) but it looks cleaner and can't be 
very expensive.


>
> diff -r ed0406cf3c70 mpz/gcd_ui.c
> --- a/mpz/gcd_ui.c      Wed Feb 02 19:16:36 2022 +0100
> +++ b/mpz/gcd_ui.c      Tue Feb 08 09:30:53 2022 +0100
> @@ -41,7 +41,16 @@
>   if (v > GMP_NUMB_MAX)
>     {
>       mpz_t vz;
> -      mp_limb_t vlimbs[2];
> +      mpz_t lw;
> +      mp_limb_t vlimbs[2], wlimbs[3];
> +
> +      if (w == NULL)
> +       {
> +         PTR(lw) = wlimbs;
> +         ALLOC(lw) = 3;
> +         SIZ(lw) = 0;
> +         w = lw;
> +       }
>       vlimbs[0] = v & GMP_NUMB_MASK;
>       vlimbs[1] = v >> GMP_NUMB_BITS;
>       PTR(vz) = vlimbs;
>
> Ĝis,
> m
>

-- 
Marc Glisse


More information about the gmp-bugs mailing list