[Gmp-commit] /var/hg/gmp: Rewrite.

mercurial at gmplib.org mercurial at gmplib.org
Sat Jan 7 13:22:54 CET 2012


details:   /var/hg/gmp/rev/3c22cac2c9c9
changeset: 14562:3c22cac2c9c9
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Sat Jan 07 13:22:49 2012 +0100
description:
Rewrite.

diffstat:

 ChangeLog         |   5 +++
 mpz/mul_2exp.c    |  71 +++++++++++++++++++++++++-----------------------------
 mpz/tdiv_q_2exp.c |  38 +++++++++++++---------------
 3 files changed, 56 insertions(+), 58 deletions(-)

diffs (171 lines):

diff -r 697d12cfff0c -r 3c22cac2c9c9 ChangeLog
--- a/ChangeLog	Thu Jan 05 02:12:40 2012 +0100
+++ b/ChangeLog	Sat Jan 07 13:22:49 2012 +0100
@@ -1,3 +1,8 @@
+2012-01-07  Torbjorn Granlund  <tege at gmplib.org>
+
+	* mpz/mul_2exp.c: Rewrite.
+	* mpz/tdiv_q_2exp.c: Rewrite.
+
 2012-01-05 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* gen-fac_ui.c: Remove currently unused constants; add new odd
diff -r 697d12cfff0c -r 3c22cac2c9c9 mpz/mul_2exp.c
--- a/mpz/mul_2exp.c	Thu Jan 05 02:12:40 2012 +0100
+++ b/mpz/mul_2exp.c	Sat Jan 07 13:22:49 2012 +0100
@@ -1,6 +1,7 @@
 /* mpz_mul_2exp -- Multiply a bignum by 2**CNT
 
-Copyright 1991, 1993, 1994, 1996, 2001, 2002 Free Software Foundation, Inc.
+Copyright 1991, 1993, 1994, 1996, 2001, 2002, 2012 Free Software Foundation,
+Inc.
 
 This file is part of the GNU MP Library.
 
@@ -21,47 +22,41 @@
 #include "gmp-impl.h"
 
 void
-mpz_mul_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
+mpz_mul_2exp (mpz_ptr r, mpz_srcptr u, mp_bitcnt_t cnt)
 {
-  mp_size_t usize = u->_mp_size;
-  mp_size_t abs_usize = ABS (usize);
-  mp_size_t wsize;
+  mp_size_t un, rn;
   mp_size_t limb_cnt;
-  mp_ptr wp;
-  mp_limb_t wlimb;
+  mp_ptr rp;
+  mp_srcptr up;
+  mp_limb_t rlimb;
 
-  if (usize == 0)
+  un = ABSIZ (u);
+  limb_cnt = cnt / GMP_NUMB_BITS;
+  rn = un + limb_cnt;
+
+  if (un == 0)
+    rn = 0;
+  else
     {
-      w->_mp_size = 0;
-      return;
+      rp = MPZ_REALLOC (r, rn + 1);
+      up = PTR(u);
+
+      cnt %= GMP_NUMB_BITS;
+      if (cnt != 0)
+	{
+	  rlimb = mpn_lshift (rp + limb_cnt, up, un, cnt);
+	  rp[rn] = rlimb;
+	  rn += (rlimb != 0);
+	}
+      else
+	{
+	  MPN_COPY_DECR (rp + limb_cnt, up, un);
+	}
+
+      /* Zero all whole limbs at low end.  Do it here and not before calling
+	 mpn_lshift, not to lose for U == R.  */
+      MPN_ZERO (rp, limb_cnt);
     }
 
-  limb_cnt = cnt / GMP_NUMB_BITS;
-  wsize = abs_usize + limb_cnt + 1;
-  if (w->_mp_alloc < wsize)
-    _mpz_realloc (w, wsize);
-
-  wp = w->_mp_d;
-  wsize = abs_usize + limb_cnt;
-
-  cnt %= GMP_NUMB_BITS;
-  if (cnt != 0)
-    {
-      wlimb = mpn_lshift (wp + limb_cnt, u->_mp_d, abs_usize, cnt);
-      if (wlimb != 0)
-	{
-	  wp[wsize] = wlimb;
-	  wsize++;
-	}
-    }
-  else
-    {
-      MPN_COPY_DECR (wp + limb_cnt, u->_mp_d, abs_usize);
-    }
-
-  /* Zero all whole limbs at low end.  Do it here and not before calling
-     mpn_lshift, not to lose for U == W.  */
-  MPN_ZERO (wp, limb_cnt);
-
-  w->_mp_size = usize >= 0 ? wsize : -wsize;
+  SIZ(r) = SIZ(u) >= 0 ? rn : -rn;
 }
diff -r 697d12cfff0c -r 3c22cac2c9c9 mpz/tdiv_q_2exp.c
--- a/mpz/tdiv_q_2exp.c	Thu Jan 05 02:12:40 2012 +0100
+++ b/mpz/tdiv_q_2exp.c	Sat Jan 07 13:22:49 2012 +0100
@@ -1,7 +1,8 @@
 /* mpz_tdiv_q_2exp -- Divide an integer by 2**CNT.  Round the quotient
    towards -infinity.
 
-Copyright 1991, 1993, 1994, 1996, 2001, 2002 Free Software Foundation, Inc.
+Copyright 1991, 1993, 1994, 1996, 2001, 2002, 2012 Free Software Foundation,
+Inc.
 
 This file is part of the GNU MP Library.
 
@@ -22,38 +23,35 @@
 #include "gmp-impl.h"
 
 void
-mpz_tdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
+mpz_tdiv_q_2exp (mpz_ptr r, mpz_srcptr u, mp_bitcnt_t cnt)
 {
-  mp_size_t usize, wsize;
+  mp_size_t un, rn;
   mp_size_t limb_cnt;
+  mp_ptr rp;
+  mp_srcptr up;
 
-  usize = u->_mp_size;
+  un = SIZ(u);
   limb_cnt = cnt / GMP_NUMB_BITS;
-  wsize = ABS (usize) - limb_cnt;
-  if (wsize <= 0)
-    w->_mp_size = 0;
+  rn = ABS (un) - limb_cnt;
+
+  if (rn <= 0)
+    rn = 0;
   else
     {
-      mp_ptr wp;
-      mp_srcptr up;
-
-      if (w->_mp_alloc < wsize)
-	_mpz_realloc (w, wsize);
-
-      wp = w->_mp_d;
-      up = u->_mp_d;
+      rp = MPZ_REALLOC (r, rn);
+      up = PTR(u) + limb_cnt;
 
       cnt %= GMP_NUMB_BITS;
       if (cnt != 0)
 	{
-	  mpn_rshift (wp, up + limb_cnt, wsize, cnt);
-	  wsize -= wp[wsize - 1] == 0;
+	  mpn_rshift (rp, up, rn, cnt);
+	  rn -= rp[rn - 1] == 0;
 	}
       else
 	{
-	  MPN_COPY_INCR (wp, up + limb_cnt, wsize);
+	  MPN_COPY_INCR (rp, up, rn);
 	}
+    }
 
-      w->_mp_size = usize >= 0 ? wsize : -wsize;
-    }
+  SIZ(r) = un >= 0 ? rn : -rn;
 }


More information about the gmp-commit mailing list