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

mercurial at gmplib.org mercurial at gmplib.org
Wed Mar 13 20:20:08 CET 2013


details:   /var/hg/gmp/rev/a0d834cea52f
changeset: 15570:a0d834cea52f
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Mar 13 20:14:02 2013 +0100
description:
ChangeLog

details:   /var/hg/gmp/rev/c635c93d6f22
changeset: 15571:c635c93d6f22
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Mar 13 20:17:47 2013 +0100
description:
mpz/cong_2exp.c: Write loops in a cleaner way.

details:   /var/hg/gmp/rev/29ed75b29e2f
changeset: 15572:29ed75b29e2f
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Mar 13 20:18:17 2013 +0100
description:
mini-gmp/mini-gmp.c: Write loops in a cleaner way.

details:   /var/hg/gmp/rev/acb367fcca9c
changeset: 15573:acb367fcca9c
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Mar 13 20:19:43 2013 +0100
description:
gmp-impl.h (mpz_zero_p): Rewrite the for loop with a while.

details:   /var/hg/gmp/rev/6e071cc68409
changeset: 15574:6e071cc68409
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Wed Mar 13 20:19:58 2013 +0100
description:
ChangeLog

diffstat:

 ChangeLog           |  10 ++++++
 gmp-impl.h          |   5 +-
 mini-gmp/mini-gmp.c |  81 ++++++++++++++++++++++++++++++++++------------------
 mpz/cong_2exp.c     |  45 +++++++++++++----------------
 4 files changed, 84 insertions(+), 57 deletions(-)

diffs (truncated from 410 to 300 lines):

diff -r 2c97edbdd90d -r 6e071cc68409 ChangeLog
--- a/ChangeLog	Wed Mar 13 14:47:27 2013 +0100
+++ b/ChangeLog	Wed Mar 13 20:19:58 2013 +0100
@@ -1,3 +1,9 @@
+2013-03-13 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/cong_2exp.c: Write loops in a cleaner way.
+	* mini-gmp/mini-gmp.c: Likewise.
+	* gmp-impl.h (mpz_zero_p): Likewise.
+
 2013-03-12  Niels Möller  <nisse at lysator.liu.se>
 
 	New names mpn_cnd_add_n and mpn_cnd_sub_n.
@@ -88,6 +94,10 @@
 	* mpn/arm/v7a/cora15/mul_1.asm: New file.
 	* mpn/arm/v7a/cora15/addmul_1.asm: New file.
 
+2013-03-09 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* tests/mpz/t-cong_2exp.c: Improve coverage.
+
 2013-03-09  Torbjorn Granlund  <tege at gmplib.org>
 
 	* mpn/sparc64/ultrasparc1234/add_n.asm: Use g5 instead of g4.
diff -r 2c97edbdd90d -r 6e071cc68409 gmp-impl.h
--- a/gmp-impl.h	Wed Mar 13 14:47:27 2013 +0100
+++ b/gmp-impl.h	Wed Mar 13 20:19:58 2013 +0100
@@ -4633,10 +4633,9 @@
 static inline int
 mpn_zero_p (mp_srcptr ap, mp_size_t n)
 {
-  mp_size_t i;
-  for (i = n - 1; i >= 0; i--)
+  while (--n >= 0)
     {
-      if (ap[i] != 0)
+      if (ap[n] != 0)
 	return 0;
     }
   return 1;
diff -r 2c97edbdd90d -r 6e071cc68409 mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Wed Mar 13 14:47:27 2013 +0100
+++ b/mini-gmp/mini-gmp.c	Wed Mar 13 20:19:58 2013 +0100
@@ -342,10 +342,10 @@
 int
 mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
 {
-  for (; n > 0; n--)
+  while (--n >= 0)
     {
-      if (ap[n-1] != bp[n-1])
-	return ap[n-1] > bp[n-1] ? 1 : -1;
+      if (ap[n] != bp[n])
+	return ap[n] > bp[n] ? 1 : -1;
     }
   return 0;
 }
@@ -375,14 +375,16 @@
   mp_size_t i;
 
   assert (n > 0);
-
-  for (i = 0; i < n; i++)
+  i = 0;
+  do
     {
       mp_limb_t r = ap[i] + b;
       /* Carry out */
       b = (r < b);
       rp[i] = r;
     }
+  while (++i < n);
+
   return b;
 }
 
@@ -425,7 +427,8 @@
 
   assert (n > 0);
 
-  for (i = 0; i < n; i++)
+  i = 0;
+  do
     {
       mp_limb_t a = ap[i];
       /* Carry out */
@@ -433,6 +436,8 @@
       rp[i] = a - b;
       b = cy;
     }
+  while (++i < n);
+
   return b;
 }
 
@@ -598,7 +603,7 @@
   retval = low_limb >> tnc;
   high_limb = (low_limb << cnt);
 
-  for (i = n - 1; i != 0; i--)
+  for (i = n; --i != 0;)
     {
       low_limb = *--up;
       *--rp = high_limb | (low_limb >> tnc);
@@ -626,7 +631,7 @@
   retval = (high_limb << tnc);
   low_limb = high_limb >> cnt;
 
-  for (i = n - 1; i != 0; i--)
+  for (i = n; --i != 0;)
     {
       high_limb = *up++;
       *rp++ = low_limb | (high_limb << tnc);
@@ -921,7 +926,8 @@
 
   r0 = np[nn - 1];
 
-  for (i = nn - 2; i >= 0; i--)
+  i = nn - 2;
+  do
     {
       mp_limb_t n0, q;
       n0 = np[i];
@@ -930,6 +936,7 @@
       if (qp)
 	qp[i] = q;
     }
+  while (--i >= 0);
 
   if (shift > 0)
     {
@@ -982,7 +989,8 @@
    * by            <d1,          d0,        dp[dn-3],  ..., dp[0] >
    */
 
-  for (i = nn - dn; i >= 0; i--)
+  i = nn - dn;
+  do
     {
       mp_limb_t n0 = np[dn-1+i];
 
@@ -1014,6 +1022,7 @@
       if (qp)
 	qp[i] = q;
     }
+  while (--i >= 0);
 
   np[dn - 1] = n1;
 }
@@ -1537,8 +1546,9 @@
   f = (mp_limb_t) x;
   x -= f;
   assert (x < 1.0);
-  rp[rn-1] = f;
-  for (i = rn-1; i-- > 0; )
+  i = rn-1;
+  rp[i] = f;
+  while (--i >= 0)
     {
       x = B * x;
       f = (mp_limb_t) x;
@@ -2316,7 +2326,7 @@
 	    {
 	      /* r > 0, need to flip sign. */
 	      rp[i] = ~rp[i] + 1;
-	      for (i++; i < rn; i++)
+	      while (++i < rn)
 		rp[i] = ~rp[i];
 
 	      rp[rn-1] &= mask;
@@ -2927,12 +2937,16 @@
   mpz_t tr;
   mpz_init_set_ui (tr, 1);
 
-  for (bit = GMP_ULONG_HIGHBIT; bit > 0; bit >>= 1)
+  bit = GMP_ULONG_HIGHBIT;
+  do
     {
       mpz_mul (tr, tr, tr);
       if (e & bit)
 	mpz_mul (tr, tr, b);
+      bit >>= 1;
     }
+  while (bit > 0);
+
   mpz_swap (r, tr);
   mpz_clear (tr);
 }
@@ -3020,7 +3034,8 @@
       mp_limb_t w = e->_mp_d[en];
       mp_limb_t bit;
 
-      for (bit = GMP_LIMB_HIGHBIT; bit > 0; bit >>= 1)
+      bit = GMP_LIMB_HIGHBIT;
+      do
 	{
 	  mpz_mul (tr, tr, tr);
 	  if (w & bit)
@@ -3030,7 +3045,9 @@
 	      mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
 	      tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
 	    }
+	  bit >>= 1;
 	}
+      while (bit > 0);
     }
 
   /* Final reduction */
@@ -3377,7 +3394,8 @@
   up = u->_mp_d;
   vp = v->_mp_d;
 
-  for (i = 0; i < vn; i++)
+  i = 0;
+  do
     {
       ul = (up[i] ^ ux) + uc;
       uc = ul < uc;
@@ -3389,6 +3407,7 @@
       rc = rl < rc;
       rp[i] = rl;
     }
+  while (++i < vn);
   assert (vc == 0);
 
   for (; i < rn; i++)
@@ -3448,7 +3467,8 @@
   up = u->_mp_d;
   vp = v->_mp_d;
 
-  for (i = 0; i < vn; i++)
+  i = 0;
+  do
     {
       ul = (up[i] ^ ux) + uc;
       uc = ul < uc;
@@ -3460,6 +3480,7 @@
       rc = rl < rc;
       rp[i] = rl;
     }
+  while (++i < vn);
   assert (vc == 0);
 
   for (; i < rn; i++)
@@ -3515,7 +3536,8 @@
   up = u->_mp_d;
   vp = v->_mp_d;
 
-  for (i = 0; i < vn; i++)
+  i = 0;
+  do
     {
       ul = (up[i] ^ ux) + uc;
       uc = ul < uc;
@@ -3527,6 +3549,7 @@
       rc = rl < rc;
       rp[i] = rl;
     }
+  while (++i < vn);
   assert (vc == 0);
 
   for (; i < un; i++)
@@ -3675,24 +3698,20 @@
   mp_limb_t limb, ux;
 
   us = u->_mp_size;
+  ux = - (mp_limb_t) (us >= 0);
   un = GMP_ABS (us);
   i = starting_bit / GMP_LIMB_BITS;
 
   /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
      u<0.  Notice this test picks up all cases of u==0 too. */
   if (i >= un)
-    return (us >= 0 ? starting_bit : ~(mp_bitcnt_t) 0);
+    return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
 
   up = u->_mp_d;
-  ux = GMP_LIMB_MAX;
-  limb = ~ up[i];
-
-  if (us < 0)
-    {
-      ux = mpn_zero_p (up, i);
-      limb = ~ (limb + ux);
-      ux = 0;
-    }
+  limb = up[i] ^ ux;
+
+  if (ux == 0)
+    limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
 
   /* Mask all bits before starting_bit, thus ignoring them. */
   limb &= (GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS));
@@ -3743,11 +3762,15 @@
   mpn_copyi (tp, up, un);
   mpn_div_qr_1_invert (&bi, base);
 
-  for (ndigits = 0; un > 0; ndigits++)
+  ndigits = 0;
+  do


More information about the gmp-commit mailing list