[Gmp-commit] /var/hg/gmp: mpz/gcdext.c: Save an allocation if both cofactors ...

mercurial at gmplib.org mercurial at gmplib.org
Sun Dec 18 07:54:07 UTC 2016


details:   /var/hg/gmp/rev/84ce52f0d360
changeset: 17163:84ce52f0d360
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sat Dec 17 21:26:50 2016 +0100
description:
mpz/gcdext.c: Save an allocation if both cofactors are needed.

diffstat:

 mpz/gcdext.c |  13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diffs (40 lines):

diff -r 8d8745aa7375 -r 84ce52f0d360 mpz/gcdext.c
--- a/mpz/gcdext.c	Tue Dec 13 19:20:31 2016 +0100
+++ b/mpz/gcdext.c	Sat Dec 17 21:26:50 2016 +0100
@@ -64,7 +64,7 @@
 
       if (g != NULL)
 	{
-	  gp = MPZ_REALLOC (g, asize);
+	  gp = MPZ_NEWALLOC (g, asize);
 	  MPN_COPY (gp, PTR (a), asize);
 	  SIZ (g) = asize;
 	}
@@ -80,12 +80,12 @@
 
   TMP_MARK;
 
-  TMP_ALLOC_LIMBS_2 (tmp_ap, asize, tmp_bp, bsize);
+  TMP_ALLOC_LIMBS_2 (tmp_gp, bsize, tmp_sp, asize + bsize + bsize + 1);
+  tmp_ap = tmp_sp + bsize + 1;
+  tmp_bp = tmp_ap + asize;
   MPN_COPY (tmp_ap, PTR (a), asize);
   MPN_COPY (tmp_bp, PTR (b), bsize);
 
-  TMP_ALLOC_LIMBS_2 (tmp_gp, bsize, tmp_sp, bsize + 1);
-
   gsize = mpn_gcdext (tmp_gp, tmp_sp, &tmp_ssize, tmp_ap, asize, tmp_bp, bsize);
 
   ssize = ABS (tmp_ssize);
@@ -102,7 +102,10 @@
       PTR (stmp) = tmp_sp;
       SIZ (stmp) = tmp_ssize;
 
-      MPZ_TMP_INIT (x, ssize + asize + 1);
+      ASSERT (ssize <= bsize); /* ssize*2 + asize + 1 <= asize + bsize*2 + 1 */
+      PTR (x) = tmp_sp + ssize;
+      ALLOC (x) = ssize + asize + 1;
+
       mpz_mul (x, stmp, a);
       mpz_sub (x, gtmp, x);
       mpz_divexact (t, x, b);


More information about the gmp-commit mailing list