[Gmp-commit] /home/hgfiles/gmp: Rewrote mpz_divexact to use mpn_divexact.

mercurial at gmplib.org mercurial at gmplib.org
Fri Dec 18 16:26:54 CET 2009


details:   /home/hgfiles/gmp/rev/a4832080f3d5
changeset: 13129:a4832080f3d5
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Fri Dec 18 16:26:49 2009 +0100
description:
Rewrote mpz_divexact to use mpn_divexact.

diffstat:

 ChangeLog      |    2 +
 mpz/divexact.c |  104 +++++++++++++-------------------------------------------
 2 files changed, 27 insertions(+), 79 deletions(-)

diffs (160 lines):

diff -r 06d82b76fd86 -r a4832080f3d5 ChangeLog
--- a/ChangeLog	Fri Dec 18 12:19:14 2009 +0100
+++ b/ChangeLog	Fri Dec 18 16:26:49 2009 +0100
@@ -1,5 +1,7 @@
 2009-12-18  Niels Möller  <nisse at lysator.liu.se>
 
+	* mpz/divexact.c: Rewrite to use mpn_divexact.
+
 	* mpn/generic/bdiv_q_1.c (mpn_bdiv_q_1): Deleted some unused
 	variables.
 
diff -r 06d82b76fd86 -r a4832080f3d5 mpz/divexact.c
--- a/mpz/divexact.c	Fri Dec 18 12:19:14 2009 +0100
+++ b/mpz/divexact.c	Fri Dec 18 16:26:49 2009 +0100
@@ -1,7 +1,9 @@
 /* mpz_divexact -- finds quotient when known that quot * den == num && den != 0.
 
+Contributed to the GNU project by Niels Möller.
+
 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2005,
-2006, 2007 Free Software Foundation, Inc.
+2006, 2007, 2009 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -18,17 +20,6 @@
 You should have received a copy of the GNU Lesser General Public License
 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
 
-/*  Ken Weber (kweber at mat.ufrgs.br, kweber at mcs.kent.edu)
-
-    Funding for this work has been partially provided by Conselho Nacional de
-    Desenvolvimento Cienti'fico e Tecnolo'gico (CNPq) do Brazil, Grant
-    301314194-2, and was done while I was a visiting researcher in the
-    Instituto de Matema'tica at Universidade Federal do Rio Grande do Sul
-    (UFRGS).
-
-    References:
-	T. Jebelean, An algorithm for exact division, Journal of Symbolic
-	Computation, v. 15, 1993, pp. 169-180.	*/
 
 #include "gmp.h"
 #include "gmp-impl.h"
@@ -37,10 +28,10 @@
 void
 mpz_divexact (mpz_ptr quot, mpz_srcptr num, mpz_srcptr den)
 {
-  mp_ptr qp, tp;
-  mp_size_t qsize, tsize;
+  mp_ptr qp;
+  mp_size_t qn;
   mp_srcptr np, dp;
-  mp_size_t nsize, dsize;
+  mp_size_t nn, dn;
   TMP_DECL;
 
 #if WANT_ASSERT
@@ -53,83 +44,38 @@
   }
 #endif
 
-  nsize = ABS (num->_mp_size);
-  dsize = ABS (den->_mp_size);
+  nn = ABSIZ (num);
+  dn = ABSIZ (den);
 
-  qsize = nsize - dsize + 1;
-  if (quot->_mp_alloc < qsize)
-    _mpz_realloc (quot, qsize);
+  qn = nn - dn + 1;
+  MPZ_REALLOC (quot, qn);
 
-  np = num->_mp_d;
-  dp = den->_mp_d;
-  qp = quot->_mp_d;
-
-  if (nsize < dsize)
+  if (nn < dn)
     {
       /* This special case avoids segfaults below when the function is
 	 incorrectly called with |N| < |D|, N != 0.  It also handles the
 	 well-defined case N = 0.  */
-      quot->_mp_size = 0;
-      return;
-    }
-
-  if (dsize <= 1)
-    {
-      if (dsize == 1)
-	{
-	  MPN_DIVREM_OR_DIVEXACT_1 (qp, np, nsize, dp[0]);
-	  qsize -= qp[qsize - 1] == 0;
-	  quot->_mp_size = (num->_mp_size ^ den->_mp_size) >= 0 ? qsize : -qsize;
-	  return;
-	}
-
-      /*  Generate divide-by-zero error since dsize == 0.  */
-      DIVIDE_BY_ZERO;
-    }
-
-  /* Avoid quadratic behaviour, but do it conservatively.  */
-  if (qsize > 1500)
-    {
-      mpz_tdiv_q (quot, num, den);
+      SIZ(quot) = 0;
       return;
     }
 
   TMP_MARK;
 
-  /*  QUOT <-- NUM/2^r, T <-- DEN/2^r where = r number of twos in DEN.  */
-  while (dp[0] == 0)
-    np += 1, nsize -= 1, dp += 1, dsize -= 1;
-  tsize = MIN (qsize, dsize);
-  if ((dp[0] & 1) != 0)
-    {
-      if (quot == den)		/*  QUOT and DEN overlap.  */
-	{
-	  tp = TMP_ALLOC_LIMBS (tsize);
-	  MPN_COPY (tp, dp, tsize);
-	}
-      else
-	tp = (mp_ptr) dp;
-      if (qp != np)
-	MPN_COPY_INCR (qp, np, qsize);
-    }
-  else
-    {
-      unsigned int r;
-      tp = TMP_ALLOC_LIMBS (tsize);
-      count_trailing_zeros (r, dp[0]);
-      mpn_rshift (tp, dp, tsize, r);
-      if (dsize > tsize)
-	tp[tsize - 1] |= (dp[tsize] << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK;
-      mpn_rshift (qp, np, qsize, r);
-      if (nsize > qsize)
-	qp[qsize - 1] |= (np[qsize] << (GMP_NUMB_BITS - r)) & GMP_NUMB_MASK;
-    }
+  qp = PTR(quot);
 
-  /*  Now QUOT <-- QUOT/T.  */
-  mpn_bdivmod (qp, qp, qsize, tp, tsize, qsize * GMP_NUMB_BITS);
-  MPN_NORMALIZE (qp, qsize);
+  if (quot == num || quot == den)
+    qp = TMP_ALLOC_LIMBS (qn);
 
-  quot->_mp_size = (num->_mp_size ^ den->_mp_size) >= 0 ? qsize : -qsize;
+  np = PTR(num);
+  dp = PTR(den);
 
+  mpn_divexact (qp, np, nn, dp, dn);
+  MPN_NORMALIZE (qp, qn);
+
+  SIZ(quot) = (SIZ(num) ^ SIZ(den)) >= 0 ? qn : -qn;
+
+  if (qp != PTR(quot))
+    MPN_COPY (PTR(quot), qp, qn);
+  
   TMP_FREE;
 }


More information about the gmp-commit mailing list