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

mercurial at gmplib.org mercurial at gmplib.org
Tue Dec 25 06:20:28 UTC 2018


details:   /var/hg/gmp/rev/8d3e7b1210a5
changeset: 17739:8d3e7b1210a5
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 06:37:51 2018 +0100
description:
mpn/generic/fib2m.c: Remove unused variables.
mpn/generic/strongfibo.c: Likewise.

details:   /var/hg/gmp/rev/7b027cf771c5
changeset: 17740:7b027cf771c5
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 06:40:08 2018 +0100
description:
mini-gmp/tests/t-mpq_addsub.c: Heal a warning.
mini-gmp/tests/t-mpq_muldiv_2exp.c: Likewise.

details:   /var/hg/gmp/rev/842dc74aaf07
changeset: 17741:842dc74aaf07
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 06:42:06 2018 +0100
description:
Prototype

details:   /var/hg/gmp/rev/a0ce313a51c9
changeset: 17742:a0ce313a51c9
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 06:44:51 2018 +0100
description:
mini-gmp/mini-gmp.c: Simplify _ui functions, using init_set_ui.

details:   /var/hg/gmp/rev/6f4f7057d64c
changeset: 17743:6f4f7057d64c
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 06:59:51 2018 +0100
description:
mini-gmp/mini-gmp.c: Simplify more _ui functions, by Niels.

details:   /var/hg/gmp/rev/ba431100f429
changeset: 17744:ba431100f429
user:      "Marco Bodrato <bodrato at mail.dm.unipi.it>"
date:      Fri Dec 21 07:01:55 2018 +0100
description:
mini-gmp/mini-gmp.c: Reindent.

diffstat:

 mini-gmp/mini-gmp.c                |  279 +++++-------------------------------
 mini-gmp/tests/t-mpq_addsub.c      |    2 +-
 mini-gmp/tests/t-mpq_muldiv_2exp.c |    2 +-
 mini-gmp/tests/testutils.c         |    1 -
 mini-gmp/tests/testutils.h         |    9 +-
 mpn/generic/fib2m.c                |   29 ++-
 mpn/generic/strongfibo.c           |    1 -
 7 files changed, 69 insertions(+), 254 deletions(-)

diffs (truncated from 505 to 300 lines):

diff -r a6330486a8cd -r ba431100f429 mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Fri Dec 21 07:21:55 2018 +0100
+++ b/mini-gmp/mini-gmp.c	Fri Dec 21 07:01:55 2018 +0100
@@ -992,36 +992,6 @@
   return r >> inv->shift;
 }
 
-static mp_limb_t
-mpn_div_qr_1 (mp_ptr qp, mp_srcptr np, mp_size_t nn, mp_limb_t d)
-{
-  assert (d > 0);
-
-  /* Special case for powers of two. */
-  if ((d & (d-1)) == 0)
-    {
-      mp_limb_t r = np[0] & (d-1);
-      if (qp)
-	{
-	  if (d <= 1)
-	    mpn_copyi (qp, np, nn);
-	  else
-	    {
-	      unsigned shift;
-	      gmp_ctz (shift, d);
-	      mpn_rshift (qp, np, nn, shift);
-	    }
-	}
-      return r;
-    }
-  else
-    {
-      struct gmp_div_inverse inv;
-      mpn_div_qr_1_invert (&inv, d);
-      return mpn_div_qr_1_preinv (qp, np, nn, &inv);
-    }
-}
-
 static void
 mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
 		     const struct gmp_div_inverse *inv)
@@ -1580,17 +1550,8 @@
 int
 mpz_fits_slong_p (const mpz_t u)
 {
-  mp_size_t us = u->_mp_size;
-
-  if (GMP_LIMB_BITS < GMP_ULONG_BITS)
     return (LONG_MAX + LONG_MIN == 0 || mpz_cmp_ui (u, LONG_MAX) <= 0) &&
       mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, LONG_MIN)) <= 0;
-  else if (us == 1)
-    return u->_mp_d[0] <= LONG_MAX;
-  else if (us == -1)
-    return u->_mp_d[0] <= GMP_NEG_CAST (mp_limb_t, LONG_MIN);
-  else
-    return (us == 0);
 }
 
 static int
@@ -1610,10 +1571,7 @@
 {
   mp_size_t us = u->_mp_size;
 
-  if (GMP_LIMB_BITS < GMP_ULONG_BITS)
     return us >= 0 && mpn_absfits_ulong_p (u->_mp_d, us);
-
-  return (us == (us > 0));
 }
 
 long int
@@ -1886,16 +1844,8 @@
     return mpz_cmp_ui (u, v);
   else if (usize >= 0)
     return 1;
-  else if (GMP_LIMB_BITS < GMP_ULONG_BITS)
+  else
     return - mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, v));
-  else if (usize < -1)
-    return -1;
-  else /* usize == -1 */
-    {
-      unsigned long uu = mpz_get_ui (u);
-      unsigned long vv = GMP_NEG_CAST (mp_limb_t, v);
-      return GMP_CMP(vv, uu);
-    }
 }
 
 int
@@ -1905,15 +1855,8 @@
 
   if (usize < 0)
     return -1;
-  else if (GMP_LIMB_BITS < GMP_ULONG_BITS)
+  else
     return mpz_cmpabs_ui (u, v);
-  else if (usize > 1)
-    return 1;
-  else
-    {
-      unsigned long uu = mpz_get_ui (u);
-      return GMP_CMP(uu, v);
-    }
 }
 
 int
@@ -1980,40 +1923,10 @@
 void
 mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
 {
-  if (b > GMP_LIMB_MAX)
-    {
-      mpz_t bb;
-      mpz_init_set_ui (bb, b);
-      mpz_add (r, a, bb);
-      mpz_clear (bb);
-    }
-  else
-    {
-      mp_size_t an = a->_mp_size;
-
-      if (an == 0)
-        mpz_set_ui (r, b);
-      else if (an > 0)
-        {
-          mp_ptr rp = MPZ_REALLOC (r, an + 1);
-          mp_limb_t cy = mpn_add_1 (rp, a->_mp_d, an, b);
-          rp[an] = cy;
-          r->_mp_size = an + cy;
-        }
-      else
-        {
-          mp_ptr rp = MPZ_REALLOC (r, -an);
-          an = -an;
-
-          if (mpn_sub_1 (rp, a->_mp_d, an, b) != 0)
-            {
-              *rp = - *rp;
-              r->_mp_size = 1;
-            }
-          else
-            r->_mp_size = - mpn_normalized_size (rp, an);
-        }
-    }
+  mpz_t bb;
+  mpz_init_set_ui (bb, b);
+  mpz_add (r, a, bb);
+  mpz_clear (bb);
 }
 
 void
@@ -2120,35 +2033,11 @@
 void
 mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
 {
-  mp_size_t un, us;
-  mp_ptr tp;
-  mp_limb_t cy;
-
-  us = u->_mp_size;
-
-  if (us == 0 || v == 0)
-    {
-      r->_mp_size = 0;
-      return;
-    }
-
-  if (v > GMP_LIMB_MAX)
-    {
-      mpz_t vv;
-      mpz_init_set_ui (vv, v);
-      mpz_mul (r, u, vv);
-      mpz_clear (vv);
-      return;
-    }
-
-  un = GMP_ABS (us);
-
-  tp = MPZ_REALLOC (r, un + 1);
-  cy = mpn_mul_1 (tp, u->_mp_d, un, v);
-  tp[un] = cy;
-
-  un += (cy > 0);
-  r->_mp_size = (us < 0) ? - un : un;
+  mpz_t vv;
+  mpz_init_set_ui (vv, v);
+  mpz_mul (r, u, vv);
+  mpz_clear (vv);
+  return;
 }
 
 void
@@ -2227,8 +2116,8 @@
 mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
 {
   mpz_t t;
-  mpz_init (t);
-  mpz_mul_ui (t, u, v);
+  mpz_init_set_ui (t, v);
+  mpz_mul (t, u, t);
   mpz_add (r, r, t);
   mpz_clear (t);
 }
@@ -2237,8 +2126,8 @@
 mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
 {
   mpz_t t;
-  mpz_init (t);
-  mpz_mul_ui (t, u, v);
+  mpz_init_set_ui (t, v);
+  mpz_mul (t, u, t);
   mpz_sub (r, r, t);
   mpz_clear (t);
 }
@@ -2634,74 +2523,20 @@
 mpz_div_qr_ui (mpz_t q, mpz_t r,
 	       const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
 {
-  mp_size_t ns, qn;
-  mp_ptr qp;
-  mp_limb_t rl;
-  mp_size_t rs;
-
-  ns = n->_mp_size;
-  if (ns == 0)
-    {
-      if (q)
-	q->_mp_size = 0;
-      if (r)
-	r->_mp_size = 0;
-      return 0;
-    }
-
-  if (d > GMP_LIMB_MAX)
-    {
-      unsigned long ret;
-      mpz_t rr, dd;
-
-      mpz_init (rr);
-      mpz_init_set_ui (dd, d);
-      mpz_div_qr (q, rr, n, dd, mode);
-      mpz_clear (dd);
-      ret = mpz_get_ui (rr);
-
-      if (r)
-	mpz_swap (r, rr);
-      mpz_clear (rr);
-
-      return ret;
-    }
-
-  qn = GMP_ABS (ns);
-  if (q)
-    qp = MPZ_REALLOC (q, qn);
-  else
-    qp = NULL;
-
-  rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
-  assert (rl < d);
-
-  rs = rl > 0;
-  rs = (ns < 0) ? -rs : rs;
-
-  if (rl > 0 && ( (mode == GMP_DIV_FLOOR && ns < 0)
-		  || (mode == GMP_DIV_CEIL && ns >= 0)))
-    {
-      if (q)
-	gmp_assert_nocarry (mpn_add_1 (qp, qp, qn, 1));
-      rl = d - rl;
-      rs = -rs;
-    }
+  unsigned long ret;
+  mpz_t rr, dd;
+
+  mpz_init (rr);
+  mpz_init_set_ui (dd, d);
+  mpz_div_qr (q, rr, n, dd, mode);
+  mpz_clear (dd);
+  ret = mpz_get_ui (rr);
 
   if (r)
-    {
-      MPZ_REALLOC (r, 1)[0] = rl;
-      r->_mp_size = rs;
-    }
-  if (q)
-    {
-      qn -= (qp[qn-1] == 0);
-      assert (qn == 0 || qp[qn-1] > 0);
-
-      q->_mp_size = (ns < 0) ? - qn : qn;
-    }
-
-  return rl;
+    mpz_swap (r, rr);
+  mpz_clear (rr);
+
+  return ret;
 }
 
 unsigned long
@@ -2840,34 +2675,16 @@


More information about the gmp-commit mailing list