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

mercurial at gmplib.org mercurial at gmplib.org
Thu Jan 12 12:02:27 CET 2012


details:   /var/hg/gmp/rev/bd82dc9ecd82
changeset: 14564:bd82dc9ecd82
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Thu Jan 12 12:00:34 2012 +0100
description:
mpz_divexact: delay realloc.

details:   /var/hg/gmp/rev/a46d83e9cfc7
changeset: 14565:a46d83e9cfc7
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Thu Jan 12 12:02:19 2012 +0100
description:
mpz_lcm: avoid goto.

diffstat:

 ChangeLog      |   9 +++++++--
 mpz/divexact.c |  17 ++++++++---------
 mpz/lcm.c      |  25 ++++++++++---------------
 3 files changed, 25 insertions(+), 26 deletions(-)

diffs (139 lines):

diff -r b06ebaa379ca -r a46d83e9cfc7 ChangeLog
--- a/ChangeLog	Tue Jan 10 20:43:18 2012 +0100
+++ b/ChangeLog	Thu Jan 12 12:02:19 2012 +0100
@@ -1,9 +1,14 @@
+2012-01-12 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mpz/divexact.c: Tight realloc, delayed if variables are reused.
+	* mpz/lcm.c: Smaller temp space, avoid goto.
+
 2012-01-10 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mpn/minithres/gmp-mparam.h: New FAC_ODD_ and FAC_DSC_
-	thresholds.	
+	thresholds.
 	* tune/tuneup.c (tune_fac_ui): Correct minimum for FAC_DSC_.
-	
+
 2012-01-07  Torbjorn Granlund  <tege at gmplib.org>
 
 	* mpz/mul_2exp.c: Rewrite.
diff -r b06ebaa379ca -r a46d83e9cfc7 mpz/divexact.c
--- a/mpz/divexact.c	Tue Jan 10 20:43:18 2012 +0100
+++ b/mpz/divexact.c	Thu Jan 12 12:02:19 2012 +0100
@@ -3,7 +3,7 @@
 Contributed to the GNU project by Niels Möller.
 
 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2005,
-2006, 2007, 2009 Free Software Foundation, Inc.
+2006, 2007, 2009, 2012 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -47,9 +47,6 @@
   nn = ABSIZ (num);
   dn = ABSIZ (den);
 
-  qn = nn - dn + 1;
-  MPZ_REALLOC (quot, qn);
-
   if (nn < dn)
     {
       /* This special case avoids segfaults below when the function is
@@ -59,12 +56,14 @@
       return;
     }
 
+  qn = nn - dn + 1;
+
   TMP_MARK;
 
-  qp = PTR(quot);
-
   if (quot == num || quot == den)
     qp = TMP_ALLOC_LIMBS (qn);
+  else
+    qp = MPZ_REALLOC (quot, qn);
 
   np = PTR(num);
   dp = PTR(den);
@@ -72,10 +71,10 @@
   mpn_divexact (qp, np, nn, dp, dn);
   MPN_NORMALIZE (qp, qn);
 
+  if (qp != PTR(quot))
+    MPN_COPY (MPZ_REALLOC (quot, qn), qp, qn);
+
   SIZ(quot) = (SIZ(num) ^ SIZ(den)) >= 0 ? qn : -qn;
 
-  if (qp != PTR(quot))
-    MPN_COPY (PTR(quot), qp, qn);
-
   TMP_FREE;
 }
diff -r b06ebaa379ca -r a46d83e9cfc7 mpz/lcm.c
--- a/mpz/lcm.c	Tue Jan 10 20:43:18 2012 +0100
+++ b/mpz/lcm.c	Thu Jan 12 12:02:19 2012 +0100
@@ -1,6 +1,6 @@
 /* mpz_lcm -- mpz/mpz least common multiple.
 
-Copyright 1996, 2000, 2001, 2005 Free Software Foundation, Inc.
+Copyright 1996, 2000, 2001, 2005, 2012 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -19,14 +19,12 @@
 
 #include "gmp.h"
 #include "gmp-impl.h"
-#include "longlong.h"
-
 
 void
 mpz_lcm (mpz_ptr r, mpz_srcptr u, mpz_srcptr v)
 {
   mpz_t g;
-  mp_size_t usize, vsize, size;
+  mp_size_t usize, vsize;
   TMP_DECL;
 
   usize = SIZ (u);
@@ -39,13 +37,18 @@
   usize = ABS (usize);
   vsize = ABS (vsize);
 
-  if (vsize == 1)
+  if (vsize == 1 || usize == 1)
     {
       mp_limb_t  vl, gl, c;
       mp_srcptr  up;
       mp_ptr     rp;
 
-    one:
+      if (usize == 1)
+	{
+	  usize = vsize;
+	  MPZ_SRCPTR_SWAP (u, v);
+	}
+
       MPZ_REALLOC (r, usize+1);
 
       up = PTR(u);
@@ -61,16 +64,8 @@
       return;
     }
 
-  if (usize == 1)
-    {
-      usize = vsize;
-      MPZ_SRCPTR_SWAP (u, v);
-      goto one;
-    }
-
   TMP_MARK;
-  size = MAX (usize, vsize);
-  MPZ_TMP_INIT (g, size);
+  MPZ_TMP_INIT (g, usize); /* v != 0 implies |gcd(u,v)| <= |u| */
 
   mpz_gcd (g, u, v);
   mpz_divexact (g, u, g);


More information about the gmp-commit mailing list