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

mercurial at gmplib.org mercurial at gmplib.org
Sun Sep 26 11:57:48 UTC 2021


details:   /var/hg/gmp/rev/0dcc5fb7f1cf
changeset: 18239:0dcc5fb7f1cf
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun Sep 26 13:53:39 2021 +0200
description:
Copyright year

details:   /var/hg/gmp/rev/3dc6c2351642
changeset: 18240:3dc6c2351642
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun Sep 26 13:55:02 2021 +0200
description:
mpz/nextprime.c: Simpler loop on sieved primes.

details:   /var/hg/gmp/rev/c1308d36b305
changeset: 18241:c1308d36b305
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun Sep 26 13:55:55 2021 +0200
description:
mpz/import.c: Use MPN_BSWAP_REVERSE, reorder branches

details:   /var/hg/gmp/rev/c7459a94e914
changeset: 18242:c7459a94e914
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun Sep 26 13:56:18 2021 +0200
description:
mpz/inp_raw.c: Avoid bit size overflows

details:   /var/hg/gmp/rev/0140c03f9c72
changeset: 18243:0140c03f9c72
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun Sep 26 13:56:41 2021 +0200
description:
ChangeLog

diffstat:

 ChangeLog          |  10 ++++++++++
 mpz/import.c       |  29 +++++++++++++----------------
 mpz/inp_raw.c      |  11 +++++++----
 mpz/nextprime.c    |  43 +++++++++----------------------------------
 mpz/primorial_ui.c |   2 +-
 5 files changed, 40 insertions(+), 55 deletions(-)

diffs (209 lines):

diff -r c288a2cb0630 -r 0140c03f9c72 ChangeLog
--- a/ChangeLog	Sun Sep 26 12:08:00 2021 +0200
+++ b/ChangeLog	Sun Sep 26 13:56:41 2021 +0200
@@ -1,3 +1,13 @@
+2021-09-25 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/import.c: Use MPN_BSWAP_REVERSE, reorder branches.
+	* mpz/inp_raw.c: Avoid bit size overflows.
+
+2021-08-21 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/primorial_ui.c: Simpler loop on sieved primes.
+	* mpz/nextprime.c: Likewise.
+
 2021-07-01  Niels Möller  <nisse at lysator.liu.se>
 
 	* mpn/generic/div_qr_1n_pi1.c (mpn_div_qr_1n_pi1): New variants,
diff -r c288a2cb0630 -r 0140c03f9c72 mpz/import.c
--- a/mpz/import.c	Sun Sep 26 12:08:00 2021 +0200
+++ b/mpz/import.c	Sun Sep 26 13:56:41 2021 +0200
@@ -1,6 +1,6 @@
 /* mpz_import -- set mpz from word data.
 
-Copyright 2002, 2012 Free Software Foundation, Inc.
+Copyright 2002, 2012, 2021 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -64,36 +64,33 @@
 
   /* Can't use these special cases with nails currently, since they don't
      mask out the nail bits in the input data.  */
-  if (nail == 0 && GMP_NAIL_BITS == 0)
+  if (nail == 0 && GMP_NAIL_BITS == 0
+      && size == sizeof (mp_limb_t)
+      && (((char *) data - (char *) NULL) % sizeof (mp_limb_t)) == 0 /* align */)
     {
-      unsigned  align = ((char *) data - (char *) NULL) % sizeof (mp_limb_t);
-
-      if (order == -1
-	  && size == sizeof (mp_limb_t)
-	  && endian == HOST_ENDIAN
-	  && align == 0)
+      if (order == -1 && endian == HOST_ENDIAN)
 	{
 	  MPN_COPY (zp, (mp_srcptr) data, (mp_size_t) count);
 	  goto done;
 	}
 
-      if (order == -1
-	  && size == sizeof (mp_limb_t)
-	  && endian == - HOST_ENDIAN
-	  && align == 0)
+      if (order == -1 && endian == - HOST_ENDIAN)
 	{
 	  MPN_BSWAP (zp, (mp_srcptr) data, (mp_size_t) count);
 	  goto done;
 	}
 
-      if (order == 1
-	  && size == sizeof (mp_limb_t)
-	  && endian == HOST_ENDIAN
-	  && align == 0)
+      if (order == 1 && endian == HOST_ENDIAN)
 	{
 	  MPN_REVERSE (zp, (mp_srcptr) data, (mp_size_t) count);
 	  goto done;
 	}
+
+      if (order == 1 && endian == -HOST_ENDIAN)
+	{
+	  MPN_BSWAP_REVERSE (zp, (mp_srcptr) data, (mp_size_t) count);
+	  goto done;
+	}
     }
 
   {
diff -r c288a2cb0630 -r 0140c03f9c72 mpz/inp_raw.c
--- a/mpz/inp_raw.c	Sun Sep 26 12:08:00 2021 +0200
+++ b/mpz/inp_raw.c	Sun Sep 26 13:56:41 2021 +0200
@@ -1,6 +1,6 @@
 /* mpz_inp_raw -- read an mpz_t in raw format.
 
-Copyright 2001, 2002, 2005, 2012, 2016 Free Software Foundation, Inc.
+Copyright 2001, 2002, 2005, 2012, 2016, 2021 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -75,7 +75,7 @@
     fp = stdin;
 
   /* 4 bytes for size */
-  if (fread (csize_bytes, sizeof (csize_bytes), 1, fp) != 1)
+  if (UNLIKELY (fread (csize_bytes, sizeof (csize_bytes), 1, fp) != 1))
     return 0;
 
   size = (((size_t) csize_bytes[0] << 24) + ((size_t) csize_bytes[1] << 16) +
@@ -88,8 +88,11 @@
 
   abs_csize = ABS (csize);
 
+  if (UNLIKELY (abs_csize > ~(mp_bitcnt_t) 0 / 8))
+    return 0; /* Bit size overflows */
+
   /* round up to a multiple of limbs */
-  abs_xsize = BITS_TO_LIMBS (abs_csize*8);
+  abs_xsize = BITS_TO_LIMBS ((mp_bitcnt_t) abs_csize * 8);
 
   if (abs_xsize != 0)
     {
@@ -99,7 +102,7 @@
 	 non-nails case.  */
       xp[0] = 0;
       cp = (char *) (xp + abs_xsize) - abs_csize;
-      if (fread (cp, abs_csize, 1, fp) != 1)
+      if (UNLIKELY (fread (cp, abs_csize, 1, fp) != 1))
 	return 0;
 
       if (GMP_NAIL_BITS == 0)
diff -r c288a2cb0630 -r 0140c03f9c72 mpz/nextprime.c
--- a/mpz/nextprime.c	Sun Sep 26 12:08:00 2021 +0200
+++ b/mpz/nextprime.c	Sun Sep 26 13:56:41 2021 +0200
@@ -1,6 +1,7 @@
 /* mpz_nextprime(p,t) - compute the next prime > t and store that in p.
 
-Copyright 1999-2001, 2008, 2009, 2012, 2020 Free Software Foundation, Inc.
+Copyright 1999-2001, 2008, 2009, 2012, 2020, 2021 Free Software
+Foundation, Inc.
 
 Contributed to the GNU project by Niels Möller and Torbjorn Granlund.
 Improved by Seth Troisi.
@@ -41,41 +42,12 @@
 /*********************************************************/
 
 static mp_limb_t
-id_to_n  (mp_limb_t id)  { return id*3+1+(id&1); }
-
-static mp_limb_t
 n_to_bit (mp_limb_t n) { return ((n-5)|1)/3U; }
 
 static mp_size_t
 primesieve_size (mp_limb_t n) { return n_to_bit(n) / GMP_LIMB_BITS + 1; }
 
 
-/************************************/
-/* Section macros: macros for sieve */
-/************************************/
-
-#define LOOP_ON_SIEVE_BEGIN(prime,start,end,sieve)     \
-  do {                                                 \
-    mp_limb_t __mask, __index, __max_i, __i;           \
-    __i = (start);                                     \
-    __index = __i / GMP_LIMB_BITS;                     \
-    __mask = CNST_LIMB(1) << (__i % GMP_LIMB_BITS);    \
-    __max_i = (end);                                   \
-    do {                                               \
-      ++__i;                                           \
-      if (((sieve)[__index] & __mask) == 0)            \
-        {                                              \
-          mp_limb_t prime = id_to_n(__i)               \
-
-#define LOOP_ON_SIEVE_END                                 \
-        }                                                 \
-      __mask = __mask << 1 | __mask >> (GMP_LIMB_BITS-1); \
-      __index += __mask & 1;                              \
-    }  while (__i <= __max_i);                            \
-  } while (0)
-
-
-
 static const unsigned char primegap_small[] =
 {
   2,2,4,2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,14,4,6,
@@ -208,10 +180,15 @@
 
       i = 0;
       last_prime = 3;
-      LOOP_ON_SIEVE_BEGIN (prime, n_to_bit (5), n_to_bit (sieve_limit), sieve);
+      /* FIXME: we should get rid of sieve_limit and use (i < prime_limit) */
+      for (mp_limb_t j = 4, *sp = sieve; j < sieve_limit; j += GMP_LIMB_BITS * 3)
+	for (mp_limb_t b = j, x = ~ *(sp++); x != 0; b += 3, x >>= 1)
+	  if (x & 1)
+	    {
+	      mp_limb_t prime = b | 1;
         primegap_tmp[i++] = prime - last_prime;
         last_prime = prime;
-      LOOP_ON_SIEVE_END;
+	    }
 
       /* Both primesieve and prime_limit ignore the first two primes. */
       ASSERT(i == prime_limit);
@@ -313,5 +290,3 @@
   return findnext(p, mpz_fdiv_ui, mpz_sub_ui);
 }
 
-#undef LOOP_ON_SIEVE_END
-#undef LOOP_ON_SIEVE_BEGIN
diff -r c288a2cb0630 -r 0140c03f9c72 mpz/primorial_ui.c
--- a/mpz/primorial_ui.c	Sun Sep 26 12:08:00 2021 +0200
+++ b/mpz/primorial_ui.c	Sun Sep 26 13:56:41 2021 +0200
@@ -2,7 +2,7 @@
 
 Contributed to the GNU project by Marco Bodrato.
 
-Copyright 2012, 2015, 2016 Free Software Foundation, Inc.
+Copyright 2012, 2015, 2016, 2021 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 


More information about the gmp-commit mailing list