Re: mpz_gcd_ui(NULL, ...) with small limbs

2022-02-08 Thread Marco Bodrato

Ciao Marc,

Il 2022-02-08 10:05 Marc Glisse ha scritto:

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


You are right. 2 should be enough.

I think that if a not realloc-able mpz is used as a target for a 
function, it should contain at least one extra limb. But we are not 
consistent about this.


If ones try
gmp$ grep MPZ_TMP_INIT -r mpz

many +1 are found...

But specifically for the gcd one can find
gmp$ grep MPZ_TMP_INIT -r mpq/aors.c
  MPZ_TMP_INIT (gcd, MIN (op1_den_size, op2_den_size));


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


I agree, not really needed, but it's probably better to always pass a 
well defined mpz_t to functions.



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
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: mpz_gcd_ui(NULL, ...) with small limbs

2022-02-08 Thread Marc Glisse

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
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: mpz_gcd_ui(NULL, ...) with small limbs

2022-02-08 Thread Marco Bodrato

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
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: mpz_gcd_ui(NULL, ...) with small limbs

2022-01-31 Thread Marco Bodrato
Ciao,

Il Sab, 22 Gennaio 2022 11:40 pm, Marc Glisse ha scritto:
> 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

Keeping unused code error-free is not easy, because it is not tested...
But I think we should always correct it, when we run into a possible
issue.
The alternative, IMO, is to remove the unused_and_incorrect code.

Yet another 2-limbs array, and dummy allocation are needed, I believe.

Ĝis,
m

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs