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

mercurial at gmplib.org mercurial at gmplib.org
Sun Jan 15 23:06:40 CET 2012


details:   /var/hg/gmp-proj/mini-gmp/rev/b92ac6523287
changeset: 85:b92ac6523287
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Sun Jan 15 23:04:41 2012 +0100
description:
Simplified exact division.

enum mpz_div_round_mode: Deleted DIV_EXACT.

mpz_div_qr: New return value, indicating if remainder was zero.
Deleted DIV_EXACT asserts. Deleted redundant copying of remainder.

mpz_divexact: Do a mpz_div_qr(...DIV_TRUNC) division, assert on return
value.

mpz_div_qr_ui: Deleted DIV_EXACT asserts.

mpz_divexact_ui. Changes analogous to mpz_divexact.

details:   /var/hg/gmp-proj/mini-gmp/rev/cd34986b819d
changeset: 86:cd34986b819d
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Sun Jan 15 23:06:30 2012 +0100
description:
Implemented mpz_divisible_p and mpz_divisible_ui_p.

diffstat:

 mini-gmp.c |  49 ++++++++++++++++++++++++-------------------------
 mini-gmp.h |   4 ++++
 2 files changed, 28 insertions(+), 25 deletions(-)

diffs (143 lines):

diff -r 202e6902f894 -r cd34986b819d mini-gmp.c
--- a/mini-gmp.c	Sun Jan 15 13:22:48 2012 +0100
+++ b/mini-gmp.c	Sun Jan 15 23:06:30 2012 +0100
@@ -32,12 +32,6 @@
    mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
    mpn/generic/submul_1.c. */
 
-/* Missing functions, needed by guile:
-
-     mpz_divisible_p
-     mpz_divisible_ui_p
-*/
-
 #include <assert.h>
 #include <ctype.h>
 #include <limits.h>
@@ -1990,10 +1984,10 @@
 
 
 /* MPZ division */
-enum mpz_div_round_mode { DIV_FLOOR, DIV_CEIL, DIV_TRUNC, DIV_EXACT };
-
-/* Allows q or r to be zero. */
-static void
+enum mpz_div_round_mode { DIV_FLOOR, DIV_CEIL, DIV_TRUNC };
+
+/* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
+static int
 mpz_div_qr (mpz_t q, mpz_t r,
 	    const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
 {
@@ -2010,7 +2004,7 @@
 	q->_mp_size = 0;
       if (r)
 	r->_mp_size = 0;
-      return;
+      return 0;
     }
 
   nn = GMP_ABS (ns);
@@ -2020,8 +2014,6 @@
 
   if (nn < dn)
     {
-      assert (mode != DIV_EXACT);
-
       if (mode == DIV_CEIL && qs >= 0)
 	{
 	  /* q = 1, r = n - d */
@@ -2046,6 +2038,7 @@
 	  if (q)
 	    q->_mp_size = 0;
 	}
+      return 1;
     }
   else
     {
@@ -2076,14 +2069,8 @@
 	  tq->_mp_size = qs < 0 ? -qn : qn;
 	}
       rn = mpn_normalized_size (np, dn);
-
-      assert (mode != DIV_EXACT || rn == 0);
-
-      if (r)
-	{
-	  mpn_copyi (tr->_mp_d, np, rn);
-	  tr->_mp_size = ns < 0 ? - rn : rn;
-	}
+      tr->_mp_size = ns < 0 ? - rn : rn;
+
       if (mode == DIV_FLOOR && qs < 0 && rn != 0)
 	{
 	  if (q)
@@ -2108,6 +2095,8 @@
 	mpz_swap (tr, r);
 
       mpz_clear (tr);
+
+      return rn != 0;
     }
 }
 
@@ -2346,7 +2335,13 @@
 void
 mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
 {
-  mpz_div_qr (q, NULL, n, d, DIV_EXACT);
+  gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, DIV_TRUNC));
+}
+
+int
+mpz_divisible_p (const mpz_t n, const mpz_t d)
+{
+  return mpz_div_qr (NULL, NULL, n, d, DIV_TRUNC) == 0;
 }
 
 static unsigned long
@@ -2377,8 +2372,6 @@
   rl = mpn_div_qr_1 (qp, n->_mp_d, qn, d);
   assert (rl < d);
 
-  assert (mode != DIV_EXACT || rl == 0);
-
   rs = rl > 0;
   rs = (ns < 0) ? -rs : rs;
 
@@ -2464,7 +2457,13 @@
 void
 mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
 {
-  mpz_div_qr_ui (q, NULL, n, d, DIV_EXACT);
+  gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, DIV_TRUNC));
+}
+
+int
+mpz_divisible_ui_p (const mpz_t n, unsigned long d)
+{
+  return mpz_div_qr_ui (NULL, NULL, n, d, DIV_TRUNC) == 0;
 }
 
 
diff -r 202e6902f894 -r cd34986b819d mini-gmp.h
--- a/mini-gmp.h	Sun Jan 15 13:22:48 2012 +0100
+++ b/mini-gmp.h	Sun Jan 15 23:06:30 2012 +0100
@@ -135,6 +135,8 @@
 
 void mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d);
 
+int mpz_divisible_p (const mpz_t, const mpz_t);
+
 unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
 unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
 unsigned long mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
@@ -147,6 +149,8 @@
 
 void mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d);
 
+int mpz_divisible_ui_p (const mpz_t, unsigned long);
+
 unsigned long mpz_gcd_ui (mpz_t, const mpz_t, unsigned long);
 void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
 void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);


More information about the gmp-commit mailing list