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

mercurial at gmplib.org mercurial at gmplib.org
Mon Mar 25 18:02:55 CET 2024


details:   /var/hg/gmp/rev/35977b656259
changeset: 18466:35977b656259
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:07:09 2024 +0100
description:
mpz/millerrabin.c: Update limit for checked values

details:   /var/hg/gmp/rev/8484c9d327fb
changeset: 18467:8484c9d327fb
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:14:22 2024 +0100
description:
mpz/millerrabin.c: Swap function order to avoid a (wrong) prototype

details:   /var/hg/gmp/rev/b513297d4aac
changeset: 18468:b513297d4aac
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:15:56 2024 +0100
description:
mini-gmp/tests/t-import.c: Do not print unused value.

details:   /var/hg/gmp/rev/ecd302aa86e3
changeset: 18469:ecd302aa86e3
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:32:22 2024 +0100
description:
mpn/generic/sec_div.c: Declare qh only when needed.

details:   /var/hg/gmp/rev/4ac76064639e
changeset: 18470:4ac76064639e
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:35:09 2024 +0100
description:
printf/: Trust return value of {v}sprintf, and use it.

details:   /var/hg/gmp/rev/f960a2dbe636
changeset: 18471:f960a2dbe636
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:39:19 2024 +0100
description:
tests/misc/t-printf.c: Test last changes on printf functions.

details:   /var/hg/gmp/rev/1040c6303455
changeset: 18472:1040c6303455
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Mar 25 17:57:27 2024 +0100
description:
ChangeLog

diffstat:

 ChangeLog                 |   11 ++++
 mini-gmp/ChangeLog        |    6 +-
 mini-gmp/tests/t-import.c |    4 +-
 mpn/generic/sec_div.c     |    3 +-
 mpz/millerrabin.c         |  116 ++++++++++++++++++++++-----------------------
 printf/doprntf.c          |    3 +-
 printf/repl-vsnprintf.c   |    6 +-
 printf/sprintffuns.c      |    6 +-
 tests/misc/t-printf.c     |   61 +++++++++++++-----------
 9 files changed, 115 insertions(+), 101 deletions(-)

diffs (truncated from 424 to 300 lines):

diff -r 7ecb3b2beea1 -r 1040c6303455 ChangeLog
--- a/ChangeLog	Sun Feb 18 20:20:57 2024 +0100
+++ b/ChangeLog	Mon Mar 25 17:57:27 2024 +0100
@@ -1,3 +1,14 @@
+2024-03-25 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/millerrabin.c: Swap function order to avoid a (wrong) prototype,
+	typo spoted by Case Van Horsen. Update also tested values.
+	* mpn/generic/sec_div.c: Declare qh only when needed.
+
+	* printf/doprntf.c: Trust return value of sprintf, and use it.
+	* printf/repl-vsnprintf.c: Likewise, with vsprintf.
+	* printf/sprintffuns.c: Likewise. Bugs spotted by Vincent Lefevre.
+	* tests/misc/t-printf.c: Test those changes.
+
 2024-02-18  Niels Möller  <nisse at lysator.liu.se>
 
 	* tests/mpz/t-gcd.c (gcdext_valid_p): Stricter check that cofactors
diff -r 7ecb3b2beea1 -r 1040c6303455 mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Sun Feb 18 20:20:57 2024 +0100
+++ b/mini-gmp/ChangeLog	Mon Mar 25 17:57:27 2024 +0100
@@ -1,7 +1,11 @@
+2024-03-25 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* tests/t-import.c: Do not print unused value.
+
 2024-02-18  Niels Möller  <nisse at lysator.liu.se>
 
 	* mini-gmp.c (mpz_gcdext): Fix logic to canonicalize cofactors.
-	But reported by Guido Vranken.
+	Bug reported by Guido Vranken.
 
 	* tests/t-gcd.c (gcdext_valid_p): Stricter check that cofactors
 	match the documented behavior.
diff -r 7ecb3b2beea1 -r 1040c6303455 mini-gmp/tests/t-import.c
--- a/mini-gmp/tests/t-import.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/mini-gmp/tests/t-import.c	Mon Mar 25 17:57:27 2024 +0100
@@ -65,8 +65,8 @@
 	    if (mpz_cmp (a, res))
 	      {
 		fprintf (stderr, "mpz_import failed:\n"
-			 "in_count %lu, out_count %lu, endian = %d, order = %d\n",
-			 (unsigned long) in_count, (unsigned long) out_count, endian, order);
+			 "in_count %lu, endian = %d, order = %d\n",
+			 (unsigned long) in_count, endian, order);
 		dump ("a", a);
 		dump ("res", res);
 		abort ();
diff -r 7ecb3b2beea1 -r 1040c6303455 mpn/generic/sec_div.c
--- a/mpn/generic/sec_div.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/mpn/generic/sec_div.c	Mon Mar 25 17:57:27 2024 +0100
@@ -83,7 +83,7 @@
 
   if (cnt != 0)
     {
-      mp_limb_t qh, cy;
+      mp_limb_t cy;
       mp_ptr np2, dp2;
       dp2 = tp;					/* dn limbs */
       mpn_lshift (dp2, dp, dn, cnt);
@@ -99,6 +99,7 @@
       /* We add nn + dn to tp here, not nn + 1 + dn, as expected.  This is
 	 since nn here will have been incremented.  */
 #if OPERATION_sec_div_qr
+      mp_limb_t qh;
       qh = mpn_sec_pi1_div_qr (np2 + dn, np2, nn, dp2, dn, inv32, tp + nn + dn);
       ASSERT (qh == 0);		/* FIXME: this indicates inefficiency! */
       MPN_COPY (qp, np2 + dn, nn - dn - 1);
diff -r 7ecb3b2beea1 -r 1040c6303455 mpz/millerrabin.c
--- a/mpz/millerrabin.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/mpz/millerrabin.c	Mon Mar 25 17:57:27 2024 +0100
@@ -8,7 +8,7 @@
    With the current implementation, the first 24 MR-tests are substituted by a
    Baillie-PSW probable prime test.
 
-   This implementation of the Baillie-PSW test was checked up to 2463*10^12,
+   This implementation of the Baillie-PSW test was checked up to 2640*10^12,
    for smaller values no MR-test is performed, regardless of reps, and
    2 ("surely prime") is returned if the number was not proved composite.
 
@@ -19,7 +19,7 @@
    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
    FUTURE GNU MP RELEASES.
 
-Copyright 1991, 1993, 1994, 1996-2002, 2005, 2014, 2018-2022 Free
+Copyright 1991, 1993, 1994, 1996-2002, 2005, 2014, 2018-2022, 2024 Free
 Software Foundation, Inc.
 
 Contributed by John Amanatides.
@@ -57,9 +57,54 @@
 #define GMP_BPSW_NOFALSEPOSITIVES_UPTO_64BITS 0
 #endif
 
-static int millerrabin (mpz_srcptr,
-			mpz_ptr, mpz_ptr,
-			mpz_srcptr, unsigned long int);
+static int
+mod_eq_m1 (mpz_srcptr x, mpz_srcptr m)
+{
+  mp_size_t ms;
+  mp_srcptr mp, xp;
+
+  ms = SIZ (m);
+  if (SIZ (x) != ms)
+    return 0;
+  ASSERT (ms > 0);
+
+  mp = PTR (m);
+  xp = PTR (x);
+  ASSERT ((mp[0] - 1) == (mp[0] ^ 1)); /* n is odd */
+
+  if ((*xp ^ CNST_LIMB(1) ^ *mp) != CNST_LIMB(0)) /* xp[0] != mp[0] - 1 */
+    return 0;
+  else
+    {
+      int cmp;
+
+      --ms;
+      ++xp;
+      ++mp;
+
+      MPN_CMP (cmp, xp, mp, ms);
+
+      return cmp == 0;
+    }
+}
+
+static int
+millerrabin (mpz_srcptr n, mpz_ptr x, mpz_ptr y,
+	     mpz_srcptr q, mp_bitcnt_t k)
+{
+  mpz_powm (y, x, q, n);
+
+  if (mpz_cmp_ui (y, 1L) == 0 || mod_eq_m1 (y, n))
+    return 1;
+
+  for (mp_bitcnt_t i = 1; i < k; ++i)
+    {
+      mpz_powm_ui (y, y, 2L, n);
+      if (mod_eq_m1 (y, n))
+	return 1;
+    }
+  return 0;
+}
 
 int
 mpz_millerrabin (mpz_srcptr n, int reps)
@@ -101,12 +146,12 @@
 	  || SIZ (n) - 64 / GMP_NUMB_BITS == (PTR (n) [64 / GMP_NUMB_BITS] < CNST_LIMB(1) << 64 % GMP_NUMB_BITS)
 #endif
 #else
-	  /* Consider numbers up to 35*2^46 that pass the BPSW test as primes.
-	     This implementation was tested up to 2463*10^12 > 2^51+2^47+2^46 */
-	  /* 2^5 < 35 = 0b100011 < 2^6 */
-#define GMP_BPSW_LIMB_CONST CNST_LIMB(35)
-#define GMP_BPSW_BITS_CONST (LOG2C(35) - 1)
-#define GMP_BPSW_BITS_LIMIT (46 + GMP_BPSW_BITS_CONST)
+	  /* Consider numbers up to 75*2^45 that pass the BPSW test as primes.
+	     This implementation was tested up to 264*10^13 > 2^51+2^48+2^46+2^45 */
+	  /* 2^6 < 75 = 0b1001011 < 2^7 */
+#define GMP_BPSW_LIMB_CONST CNST_LIMB(75)
+#define GMP_BPSW_BITS_CONST (LOG2C(75) - 1)
+#define GMP_BPSW_BITS_LIMIT (45 + GMP_BPSW_BITS_CONST)
 
 #define GMP_BPSW_LIMBS_LIMIT (GMP_BPSW_BITS_LIMIT / GMP_NUMB_BITS)
 #define GMP_BPSW_BITS_MOD (GMP_BPSW_BITS_LIMIT % GMP_NUMB_BITS)
@@ -165,52 +210,3 @@
   TMP_FREE;
   return is_prime;
 }
-
-static int
-mod_eq_m1 (mpz_srcptr x, mpz_srcptr m)
-{
-  mp_size_t ms;
-  mp_srcptr mp, xp;
-
-  ms = SIZ (m);
-  if (SIZ (x) != ms)
-    return 0;
-  ASSERT (ms > 0);
-
-  mp = PTR (m);
-  xp = PTR (x);
-  ASSERT ((mp[0] - 1) == (mp[0] ^ 1)); /* n is odd */
-
-  if ((*xp ^ CNST_LIMB(1) ^ *mp) != CNST_LIMB(0)) /* xp[0] != mp[0] - 1 */
-    return 0;
-  else
-    {
-      int cmp;
-
-      --ms;
-      ++xp;
-      ++mp;
-
-      MPN_CMP (cmp, xp, mp, ms);
-
-      return cmp == 0;
-    }
-}
-
-static int
-millerrabin (mpz_srcptr n, mpz_ptr x, mpz_ptr y,
-	     mpz_srcptr q, mp_bitcnt_t k)
-{
-  mpz_powm (y, x, q, n);
-
-  if (mpz_cmp_ui (y, 1L) == 0 || mod_eq_m1 (y, n))
-    return 1;
-
-  for (mp_bitcnt_t i = 1; i < k; ++i)
-    {
-      mpz_powm_ui (y, y, 2L, n);
-      if (mod_eq_m1 (y, n))
-	return 1;
-    }
-  return 0;
-}
diff -r 7ecb3b2beea1 -r 1040c6303455 printf/doprntf.c
--- a/printf/doprntf.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/printf/doprntf.c	Mon Mar 25 17:57:27 2024 +0100
@@ -267,8 +267,7 @@
 	 mean truncation */
       ASSERT (explen >= 0 && explen < sizeof(exponent)-1);
 #else
-      sprintf (exponent, p->expfmt, expsign, expval);
-      explen = strlen (exponent);
+      explen = sprintf (exponent, p->expfmt, expsign, expval);
       ASSERT (explen < sizeof(exponent));
 #endif
       TRACE (printf ("  expfmt %s gives %s\n", p->expfmt, exponent));
diff -r 7ecb3b2beea1 -r 1040c6303455 printf/repl-vsnprintf.c
--- a/printf/repl-vsnprintf.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/printf/repl-vsnprintf.c	Mon Mar 25 17:57:27 2024 +0100
@@ -364,16 +364,14 @@
 
   if (total_width <= buf_size)
     {
-      vsprintf (buf, orig_fmt, orig_ap);
-      len = strlen (buf);
+      len = vsprintf (buf, orig_fmt, orig_ap);
     }
   else
     {
       char  *s;
 
       s = __GMP_ALLOCATE_FUNC_TYPE (total_width, char);
-      vsprintf (s, orig_fmt, orig_ap);
-      len = strlen (s);
+      len = vsprintf (s, orig_fmt, orig_ap);
       if (buf_size != 0)
 	{
 	  size_t  copylen = MIN (len, buf_size-1);
diff -r 7ecb3b2beea1 -r 1040c6303455 printf/sprintffuns.c
--- a/printf/sprintffuns.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/printf/sprintffuns.c	Mon Mar 25 17:57:27 2024 +0100
@@ -53,9 +53,9 @@
 {
   char  *buf = *bufp;
   int   ret;
-  vsprintf (buf, fmt, ap);
-  ret = strlen (buf);
-  *bufp = buf + ret;
+  ret = vsprintf (buf, fmt, ap);
+  if (ret > 0)
+    *bufp = buf + ret;
   return ret;
 }
 
diff -r 7ecb3b2beea1 -r 1040c6303455 tests/misc/t-printf.c
--- a/tests/misc/t-printf.c	Sun Feb 18 20:20:57 2024 +0100
+++ b/tests/misc/t-printf.c	Mon Mar 25 17:57:27 2024 +0100
@@ -128,12 +128,11 @@
 }
 
 void
-check_vsprintf (const char *want, const char *fmt, va_list ap)
+check_vsprintf (const char *want, int want_len, const char *fmt, va_list ap)
 {
   char  got[MAX_OUTPUT];
-  int   got_len, want_len;
+  int   got_len;
 
-  want_len = strlen (want);
   got_len = gmp_vsprintf (got, fmt, ap);
 
   if (got_len != want_len || strcmp (got, want) != 0)
@@ -149,14 +148,12 @@
 }
 
 void
-check_vfprintf (const char *want, const char *fmt, va_list ap)
+check_vfprintf (const char *want, int want_len, const char *fmt, va_list ap)
 {
   char  got[MAX_OUTPUT];
-  int   got_len, want_len, fread_len;
+  int   got_len, fread_len;
   long  ftell_len;
 


More information about the gmp-commit mailing list