[Gmp-commit] /var/hg/gmp: 3 new changesets

mercurial at gmplib.org mercurial at gmplib.org
Sun Jun 14 19:26:32 CEST 2026


details:   /var/hg/gmp/rev/695187d727e4
changeset: 18503:695187d727e4
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sat Jun 13 21:43:04 2026 +0200
description:
mpn/generic/perfpow.c (is_kth_power): Check only one possible square root.

details:   /var/hg/gmp/rev/7d9c2ae0e2e1
changeset: 18504:7d9c2ae0e2e1
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sat Jun 13 22:10:49 2026 +0200
description:
mpn/generic/perfpow.c (mpn_perfect_power_p): Now Valgrind is happy :-)

details:   /var/hg/gmp/rev/33dcc1ae715b
changeset: 18505:33dcc1ae715b
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sat Jun 13 22:15:25 2026 +0200
description:
mpn/generic/perfpow.c (perfpow): Use break instead of goto.

diffstat:

 mpn/generic/perfpow.c |  34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)

diffs (76 lines):

diff -r 1e504d187571 -r 33dcc1ae715b mpn/generic/perfpow.c
--- a/mpn/generic/perfpow.c	Sat Apr 04 14:56:15 2026 +0200
+++ b/mpn/generic/perfpow.c	Sat Jun 13 22:15:25 2026 +0200
@@ -121,7 +121,7 @@
 	      mp_ptr tp)
 {
   mp_bitcnt_t b;
-  mp_size_t rn, xn;
+  mp_size_t rn;
 
   ASSERT (n > 0);
   ASSERT ((k & 1) != 0 || k == 2);
@@ -133,16 +133,22 @@
       rn = 1 + b / GMP_LIMB_BITS;
       if (mpn_bsqrtinv (rp, ip, b, tp) != 0)
 	{
+	  /* There are two possible results of bsqrt, {rp, rn} and its negation.
+	     We don't need to test both, only the one with the most significant
+	     bit in the right position can be the square root of {np, n}. */
+	  if (! (rp [rn - 1 - (b % GMP_LIMB_BITS == 0)] &
+		 CNST_LIMB(1) << (b-1) % GMP_LIMB_BITS))
+	    {
+	      /* Check if (2^b - r)^2 == n, i.e. mpn_neg (rp, rp, rn); */
+	      *rp ^= CNST_LIMB (1);
+	      mpn_com (rp, rp, rn);
+	    }
 	  rp[rn - 1] &= (CNST_LIMB(1) << (b % GMP_LIMB_BITS)) - 1;
-	  xn = rn;
-	  MPN_NORMALIZE (rp, xn);
-	  if (pow_equals (np, n, rp, xn, k, f, tp) != 0)
-	    return 1;
-
-	  /* Check if (2^b - r)^2 == n */
-	  mpn_neg (rp, rp, rn);
-	  rp[rn - 1] &= (CNST_LIMB(1) << (b % GMP_LIMB_BITS)) - 1;
-	  MPN_NORMALIZE (rp, rn);
+	  rn -= (b % GMP_LIMB_BITS == 0); /* Normalize */
+	  ASSERT (rp[rn - 1] != 0);
+	  ASSERT (rn == (n + 1) >> 1);
+	  /* mpn_sqr (tp, rp, rn); */
+	  /* if (mpn_cmp (np, tp, n) == 0) */
 	  if (pow_equals (np, n, rp, rn, k, f, tp) != 0)
 	    return 1;
 	}
@@ -210,7 +216,7 @@
 	      if (is_kth_power (rp, np, k, ip, n, f, tp) != 0)
 		{
 		  ans = 1;
-		  goto ret;
+		  break;
 		}
 	    }
 	}
@@ -222,11 +228,10 @@
 	  if (is_kth_power (rp, np, k, ip, n, f, tp) != 0)
 	    {
 	      ans = 1;
-	      goto ret;
+	      break;
 	    }
 	}
     }
- ret:
   TMP_FREE;
   return ans;
 }
@@ -253,8 +258,7 @@
       n = -n;
     }
 
-  if (n == 0 || (n == 1 && np[0] == 1)) /* Valgrind doesn't like
-					   (n <= (np[0] == 1)) */
+  if (n <= (np[0] == 1)) /* n == 0 || (n == 1 && np[0] == 1) */
     return 1;
 
   TMP_MARK;


More information about the gmp-commit mailing list