mpz_gcd_ui(NULL, ...) with small limbs

Marco Bodrato bodrato at mail.dm.unipi.it
Tue Feb 8 09:38:53 CET 2022


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)?

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


More information about the gmp-bugs mailing list