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

mercurial at gmplib.org mercurial at gmplib.org
Wed Nov 21 07:22:33 UTC 2018


details:   /var/hg/gmp/rev/5a7c0afeffb3
changeset: 17710:5a7c0afeffb3
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Nov 21 08:21:05 2018 +0100
description:
ASSERTs and {UN}LIKELY

details:   /var/hg/gmp/rev/d11fcaf1a0cd
changeset: 17711:d11fcaf1a0cd
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Nov 21 08:22:18 2018 +0100
description:
mpz/millerrabin.c: gmp_randinit only if needed.

diffstat:

 mpn/generic/fib2m.c      |  4 ++--
 mpn/generic/strongfibo.c |  8 ++++----
 mpz/millerrabin.c        |  8 +++++---
 3 files changed, 11 insertions(+), 9 deletions(-)

diffs (85 lines):

diff -r 2c7b911c6329 -r d11fcaf1a0cd mpn/generic/fib2m.c
--- a/mpn/generic/fib2m.c	Sun Nov 18 01:27:27 2018 +0100
+++ b/mpn/generic/fib2m.c	Wed Nov 21 08:22:18 2018 +0100
@@ -55,12 +55,12 @@
           ++n;
           if (x > y)
             {
-              mpn_sub_n (rp, ap, bp, n);
+              ASSERT_NOCARRY (mpn_sub_n (rp, ap, bp, n));
               return 1;
             }
           else
             {
-              mpn_sub_n (rp, bp, ap, n);
+              ASSERT_NOCARRY (mpn_sub_n (rp, bp, ap, n));
               return -1;
             }
         }
diff -r 2c7b911c6329 -r d11fcaf1a0cd mpn/generic/strongfibo.c
--- a/mpn/generic/strongfibo.c	Sun Nov 18 01:27:27 2018 +0100
+++ b/mpn/generic/strongfibo.c	Wed Nov 21 08:22:18 2018 +0100
@@ -52,12 +52,12 @@
           ++n;
           if (x > y)
             {
-              mpn_sub_n (rp, ap, bp, n);
+              ASSERT_NOCARRY (mpn_sub_n (rp, ap, bp, n));
               return 1;
             }
           else
             {
-              mpn_sub_n (rp, bp, ap, n);
+              ASSERT_NOCARRY (mpn_sub_n (rp, bp, ap, n));
               return -1;
             }
         }
@@ -178,7 +178,7 @@
     mpz_t m = MPZ_ROINIT_N(mp, mn);
     b0 = mpz_scan0 (m, 0);
   }
-  if (b0 == mn * GMP_NUMB_BITS)
+  if (UNLIKELY (b0 == mn * GMP_NUMB_BITS))
     {
       en = 1;
       scratch [0] = 1;
@@ -188,7 +188,7 @@
     {
       int cnt = b0 % GMP_NUMB_BITS;
       en = b0 / GMP_NUMB_BITS;
-      if (cnt)
+      if (LIKELY (cnt != 0))
 	mpn_rshift (scratch, mp + en, mn - en, cnt);
       else
 	MPN_COPY (scratch, mp + en, mn - en);
diff -r 2c7b911c6329 -r d11fcaf1a0cd mpz/millerrabin.c
--- a/mpz/millerrabin.c	Sun Nov 18 01:27:27 2018 +0100
+++ b/mpz/millerrabin.c	Wed Nov 21 08:22:18 2018 +0100
@@ -71,7 +71,9 @@
   /* BPSW test */
   mpz_set_ui (x, 2);
   is_prime = millerrabin (n, x, y, q, k) && mpz_stronglucas (n, x, y);
-  if (is_prime)
+
+  reps -= 24;
+  if (reps > 0 && is_prime)
     {
       /* (n-5)/2 */
       mpz_sub_ui (nm, nm, 2L);
@@ -79,14 +81,14 @@
 
       gmp_randinit_default (rstate);
 
-      for (reps -= 24; reps > 0 && is_prime; --reps)
+      do
 	{
 	  /* 3 to (n-1)/2 inclusive, don't want 1, 0 or 2 */
 	  mpz_urandomm (x, rstate, nm);
 	  mpz_add_ui (x, x, 3L);
 
 	  is_prime = millerrabin (n, x, y, q, k);
-	}
+	} while (--reps > 0 && is_prime);
 
       gmp_randclear (rstate);
     }


More information about the gmp-commit mailing list