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

mercurial at gmplib.org mercurial at gmplib.org
Sun May 24 07:51:20 UTC 2015


details:   /var/hg/gmp/rev/55878c8317d7
changeset: 16649:55878c8317d7
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 24 07:53:54 2015 +0200
description:
mpz/swap.c: Use MP*_SWAP macros.

details:   /var/hg/gmp/rev/8d86fc2da363
changeset: 16650:8d86fc2da363
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 24 07:56:03 2015 +0200
description:
mpq/div.c: Reduce memory use.

details:   /var/hg/gmp/rev/df3688afd640
changeset: 16651:df3688afd640
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 24 09:47:58 2015 +0200
description:
mpq/div.c: Remove two vars.

details:   /var/hg/gmp/rev/22634990320f
changeset: 16652:22634990320f
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 24 09:51:09 2015 +0200
description:
ChangeLog

diffstat:

 ChangeLog  |   5 +++
 mpq/div.c  |  81 +++++++++++++++++++++++++++++++++++++++----------------------
 mpz/swap.c |  23 +++--------------
 3 files changed, 60 insertions(+), 49 deletions(-)

diffs (182 lines):

diff -r 3477399f9e33 -r 22634990320f ChangeLog
--- a/ChangeLog	Fri May 22 07:57:53 2015 +0200
+++ b/ChangeLog	Sun May 24 09:51:09 2015 +0200
@@ -1,3 +1,8 @@
+2015-05-24 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpq/div.c: Reduce memory use.
+	* mpz/swap.c: Use _SWAP macros.
+
 2015-05-18  Torbjörn Granlund  <torbjorng at google.com>
 
 	* configure.ac (arm): Let compiler decide about arm vs thumb encoding.
diff -r 3477399f9e33 -r 22634990320f mpq/div.c
--- a/mpq/div.c	Fri May 22 07:57:53 2015 +0200
+++ b/mpq/div.c	Sun May 24 09:51:09 2015 +0200
@@ -1,6 +1,6 @@
 /* mpq_div -- divide two rational numbers.
 
-Copyright 1991, 1994-1996, 2000, 2001 Free Software Foundation, Inc.
+Copyright 1991, 1994-1996, 2000, 2001, 2015 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -37,22 +37,48 @@
 {
   mpz_t gcd1, gcd2;
   mpz_t tmp1, tmp2;
-  mpz_t numtmp;
-  mp_size_t op1_num_size;
-  mp_size_t op1_den_size;
-  mp_size_t op2_num_size;
-  mp_size_t op2_den_size;
+  mp_size_t op1_size;
+  mp_size_t op2_size;
   mp_size_t alloc;
   TMP_DECL;
 
-  op2_num_size = ABSIZ(NUM(op2));
+  op2_size = SIZ(NUM(op2));
 
-  if (UNLIKELY (op2_num_size == 0))
+  if (UNLIKELY (op2_size == 0))
     DIVIDE_BY_ZERO;
 
-  op1_num_size = ABSIZ(NUM(op1));
+  if (op1 == op2)
+    {
+      PTR(NUM(quot))[0] = 1;
+      SIZ(NUM(quot)) = 1;
+      PTR(DEN(quot))[0] = 1;
+      SIZ(DEN(quot)) = 1;
+      return;      
+    }
 
-  if (op1_num_size == 0)
+  if (quot == op2)
+    {
+      /* We checked for op1 == op2: we are not in the x=x/x case.
+	 We compute x=y/x by computing x=inv(x)*y */
+      MPN_PTR_SWAP (PTR(NUM(quot)), ALLOC(NUM(quot)),
+		    PTR(DEN(quot)), ALLOC(DEN(quot)));
+      if (op2_size > 0)
+	{
+	  SIZ(NUM(quot)) = SIZ(DEN(quot));
+	  SIZ(DEN(quot)) = op2_size;
+	}
+      else
+	{
+	  SIZ(NUM(quot)) = - SIZ(DEN(quot));
+	  SIZ(DEN(quot)) = - op2_size;
+	}
+      mpq_mul (quot, quot, op1);
+      return;
+    }
+
+  op1_size = ABSIZ(NUM(op1));
+
+  if (op1_size == 0)
     {
       /* We special case this to simplify allocation logic; gcd(0,x) = x
 	 is a singular case for the allocations.  */
@@ -62,30 +88,29 @@
       return;
     }
 
-  op2_den_size =   SIZ(DEN(op2));
-  op1_den_size =   SIZ(DEN(op1));
+  op2_size = ABS(op2_size);
 
   TMP_MARK;
 
-  alloc = MIN (op1_num_size, op2_num_size);
+  alloc = MIN (op1_size, op2_size);
   MPZ_TMP_INIT (gcd1, alloc);
 
-  alloc = MIN (op1_den_size, op2_den_size);
+  alloc = MAX (op1_size, op2_size);
+  MPZ_TMP_INIT (tmp1, alloc);
+
+  op2_size = SIZ(DEN(op2));
+  op1_size = SIZ(DEN(op1));
+
+  alloc = MIN (op1_size, op2_size);
   MPZ_TMP_INIT (gcd2, alloc);
 
-  alloc = MAX (op1_num_size, op2_num_size);
-  MPZ_TMP_INIT (tmp1, alloc);
-
-  alloc = MAX (op1_den_size, op2_den_size);
+  alloc = MAX (op1_size, op2_size);
   MPZ_TMP_INIT (tmp2, alloc);
 
-  alloc = op1_num_size + op2_den_size;
-  MPZ_TMP_INIT (numtmp, alloc);
-
-  /* QUOT might be identical to either operand, so don't store the result there
-     until we are finished with the input operands.  We can overwrite the
-     numerator of QUOT when we are finished with the numerators of OP1 and
-     OP2.  */
+  /* QUOT might be identical to OP1, so don't store the result there
+     until we are finished with the input operand.  We can overwrite
+     the numerator of QUOT when we are finished with the numerator of
+     OP1. */
 
   mpz_gcd (gcd1, NUM(op1), NUM(op2));
   mpz_gcd (gcd2, DEN(op2), DEN(op1));
@@ -93,17 +118,13 @@
   mpz_divexact_gcd (tmp1, NUM(op1), gcd1);
   mpz_divexact_gcd (tmp2, DEN(op2), gcd2);
 
-  mpz_mul (numtmp, tmp1, tmp2);
+  mpz_mul (NUM(quot), tmp1, tmp2);
 
   mpz_divexact_gcd (tmp1, NUM(op2), gcd1);
   mpz_divexact_gcd (tmp2, DEN(op1), gcd2);
 
   mpz_mul (DEN(quot), tmp1, tmp2);
 
-  /* We needed to go via NUMTMP to take care of QUOT being the same as OP2.
-     Now move NUMTMP to QUOT->_mp_num.  */
-  mpz_set (NUM(quot), numtmp);
-
   /* Keep the denominator positive.  */
   if (SIZ(DEN(quot)) < 0)
     {
diff -r 3477399f9e33 -r 22634990320f mpz/swap.c
--- a/mpz/swap.c	Fri May 22 07:57:53 2015 +0200
+++ b/mpz/swap.c	Sun May 24 09:51:09 2015 +0200
@@ -1,6 +1,6 @@
 /* mpz_swap (dest_integer, src_integer) -- Swap U and V.
 
-Copyright 1997, 1998, 2001, 2012 Free Software Foundation, Inc.
+Copyright 1997, 1998, 2001, 2012, 2015 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -34,22 +34,7 @@
 void
 mpz_swap (mpz_ptr u, mpz_ptr v) __GMP_NOTHROW
 {
-  mp_ptr up, vp;
-  mp_size_t usize, vsize;
-  mp_size_t ualloc, valloc;
-
-  ualloc = ALLOC (u);
-  valloc = ALLOC (v);
-  ALLOC (v) = ualloc;
-  ALLOC (u) = valloc;
-
-  usize = SIZ (u);
-  vsize = SIZ (v);
-  SIZ (v) = usize;
-  SIZ (u) = vsize;
-
-  up = PTR (u);
-  vp = PTR (v);
-  PTR (v) = up;
-  PTR (u) = vp;
+  MP_SIZE_T_SWAP (ALLOC(u), ALLOC(v));
+  MP_SIZE_T_SWAP (SIZ(u), SIZ(v));
+  MP_PTR_SWAP (PTR(v), PTR(u));
 }


More information about the gmp-commit mailing list