[Gmp-commit] /var/hg/gmp: mpz: Lazy allocation.

mercurial at gmplib.org mercurial at gmplib.org
Mon Nov 9 21:31:16 UTC 2015


details:   /var/hg/gmp/rev/299ec6187305
changeset: 16953:299ec6187305
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon Nov 09 22:30:52 2015 +0100
description:
mpz: Lazy allocation.

diffstat:

 mpz/2fac_ui.c      |   4 ++--
 mpz/aors_ui.h      |   2 +-
 mpz/bin_ui.c       |   2 +-
 mpz/bin_uiui.c     |   6 +++---
 mpz/cdiv_qr_ui.c   |   2 +-
 mpz/cdiv_r_ui.c    |   2 +-
 mpz/cfdiv_q_2exp.c |   2 +-
 mpz/clear.c        |   3 ++-
 mpz/clears.c       |   3 ++-
 mpz/com.c          |   2 +-
 mpz/fac_ui.c       |   2 +-
 mpz/fdiv_qr_ui.c   |   2 +-
 mpz/fdiv_r_ui.c    |   2 +-
 mpz/fib2_ui.c      |   4 ++--
 mpz/fib_ui.c       |   2 +-
 mpz/gcd.c          |   4 ++--
 mpz/gcd_ui.c       |   2 +-
 mpz/gcdext.c       |   2 +-
 mpz/init.c         |   5 +++--
 mpz/inits.c        |   5 +++--
 mpz/ior.c          |   4 ++--
 mpz/iset_d.c       |   3 +--
 mpz/lucnum2_ui.c   |   4 ++--
 mpz/lucnum_ui.c    |   2 +-
 mpz/mfac_uiui.c    |   2 +-
 mpz/mul.c          |  15 ++++++++-------
 mpz/n_pow_ui.c     |   2 +-
 mpz/oddfac_1.c     |   3 ++-
 mpz/powm.c         |   2 +-
 mpz/powm_sec.c     |   2 +-
 mpz/powm_ui.c      |   4 ++--
 mpz/primorial_ui.c |   4 ++--
 mpz/realloc.c      |  23 +++++++++++++++--------
 mpz/realloc2.c     |  20 ++++++++++++++------
 mpz/set_si.c       |   2 +-
 mpz/set_ui.c       |   2 +-
 mpz/tdiv_qr_ui.c   |   6 ++----
 mpz/tdiv_r_ui.c    |   6 ++----
 mpz/ui_sub.c       |  19 +++++++------------
 rand/randlc2x.c    |   2 +-
 40 files changed, 98 insertions(+), 87 deletions(-)

diffs (truncated from 656 to 300 lines):

diff -r d71a9618f892 -r 299ec6187305 mpz/2fac_ui.c
--- a/mpz/2fac_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/2fac_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -67,8 +67,8 @@
     mpz_mul_2exp (x, x, count);
   } else { /* n is odd */
     if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT) {
-	PTR (x)[0] = __gmp_odd2fac_table[n >> 1];
-	SIZ (x) = 1;
+      MPZ_NEWALLOC (x, 1)[0] = __gmp_odd2fac_table[n >> 1];
+      SIZ (x) = 1;
     } else if (BELOW_THRESHOLD (n, FAC_2DSC_THRESHOLD)) { /* odd basecase, */
       mp_limb_t *factors, prod, max_prod, j;
       TMP_SDECL;
diff -r d71a9618f892 -r 299ec6187305 mpz/aors_ui.h
--- a/mpz/aors_ui.h	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/aors_ui.h	Mon Nov 09 22:30:52 2015 +0100
@@ -80,7 +80,7 @@
   usize = SIZ (u);
   if (usize == 0)
     {
-      PTR (w)[0] = vval;
+      MPZ_NEWALLOC (w, 1)[0] = vval;
       SIZ (w) = VARIATION_NEG (vval != 0);
       return;
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/bin_ui.c
--- a/mpz/bin_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/bin_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -83,7 +83,7 @@
 
   /* Now wanting bin(ni+k,k), with ni positive, and "negate" is the sign (0
      for positive, 1 for negative). */
-  SIZ (r) = 1; PTR (r)[0] = 1;
+  SIZ (r) = 1; MPZ_NEWALLOC (r, 1)[0] = 1;
 
   /* Rewrite bin(n,k) as bin(n,n-k) if that is smaller.  In this case it's
      whether ni+k-k < k meaning ni<k, and if so change to denominator ni+k-k
diff -r d71a9618f892 -r 299ec6187305 mpz/bin_uiui.c
--- a/mpz/bin_uiui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/bin_uiui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -642,7 +642,7 @@
     }
   else
     {
-      PTR (r)[0] = prod;
+      MPZ_NEWALLOC (r, 1)[0] = prod;
       SIZ (r) = 1;
     }
   TMP_FREE;
@@ -677,10 +677,10 @@
     /* Rewrite bin(n,k) as bin(n,n-k) if that is smaller. */
     k = MIN (k, n - k);
     if (k < 2) {
-      PTR(r)[0] = k ? n : 1; /* 1 + ((-k) & (n-1)); */
+      MPZ_NEWALLOC (r, 1)[0] = k ? n : 1; /* 1 + ((-k) & (n-1)); */
       SIZ(r) = 1;
     } else if (n <= ODD_FACTORIAL_EXTTABLE_LIMIT) { /* k >= 2, n >= 4 */
-      PTR(r)[0] = bc_bin_uiui (n, k);
+      MPZ_NEWALLOC (r, 1)[0] = bc_bin_uiui (n, k);
       SIZ(r) = 1;
     } else if (k <= ODD_FACTORIAL_TABLE_LIMIT)
       mpz_smallk_bin_uiui (r, n, k);
diff -r d71a9618f892 -r 299ec6187305 mpz/cdiv_qr_ui.c
--- a/mpz/cdiv_qr_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/cdiv_qr_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -108,7 +108,7 @@
 	      rl = divisor - rl;
 	    }
 
-	  PTR(rem)[0] = rl;
+	  MPZ_NEWALLOC (rem, 1)[0] = rl;
 	  SIZ(rem) = -(rl != 0);
 	}
       qn = nn - (qp[nn - 1] == 0);
diff -r d71a9618f892 -r 299ec6187305 mpz/cdiv_r_ui.c
--- a/mpz/cdiv_r_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/cdiv_r_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -101,7 +101,7 @@
 	  if (ns >= 0)
 	    rl = divisor - rl;
 
-	  PTR(rem)[0] = rl;
+	  MPZ_NEWALLOC (rem, 1)[0] = rl;
 	  SIZ(rem) = -1;
 	}
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/cfdiv_q_2exp.c
--- a/mpz/cfdiv_q_2exp.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/cfdiv_q_2exp.c	Mon Nov 09 22:30:52 2015 +0100
@@ -53,7 +53,7 @@
   if (wsize <= 0)
     {
       /* u < 2**cnt, so result 1, 0 or -1 according to rounding */
-      PTR(w)[0] = 1;
+      MPZ_NEWALLOC (w, 1)[0] = 1;
       SIZ(w) = (usize == 0 || (usize ^ dir) < 0 ? 0 : dir);
       return;
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/clear.c
--- a/mpz/clear.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/clear.c	Mon Nov 09 22:30:52 2015 +0100
@@ -36,5 +36,6 @@
 void
 mpz_clear (mpz_ptr x)
 {
-  __GMP_FREE_FUNC_LIMBS (PTR (x), ALLOC(x));
+  if (ALLOC (x))
+    __GMP_FREE_FUNC_LIMBS (PTR (x), ALLOC(x));
 }
diff -r d71a9618f892 -r 299ec6187305 mpz/clears.c
--- a/mpz/clears.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/clears.c	Mon Nov 09 22:30:52 2015 +0100
@@ -41,7 +41,8 @@
 
   while (x != NULL)
     {
-      __GMP_FREE_FUNC_LIMBS (PTR (x), ALLOC(x));
+      if (ALLOC (x))
+	__GMP_FREE_FUNC_LIMBS (PTR (x), ALLOC (x));
       x = va_arg (ap, mpz_ptr);
     }
 
diff -r d71a9618f892 -r 299ec6187305 mpz/com.c
--- a/mpz/com.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/com.c	Mon Nov 09 22:30:52 2015 +0100
@@ -49,7 +49,7 @@
       if (UNLIKELY (size == 0))
 	{
 	  /* special case, as mpn_add_1 wants size!=0 */
-	  PTR (dst)[0] = 1;
+	  MPZ_NEWALLOC (dst, 1)[0] = 1;
 	  SIZ (dst) = -1;
 	}
       else
diff -r d71a9618f892 -r 299ec6187305 mpz/fac_ui.c
--- a/mpz/fac_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/fac_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -60,7 +60,7 @@
 
   if (n < numberof (table))
     {
-      PTR (x)[0] = table[n];
+      MPZ_NEWALLOC (x, 1)[0] = table[n];
       SIZ (x) = 1;
     }
   else if (BELOW_THRESHOLD (n, FAC_ODD_THRESHOLD))
diff -r d71a9618f892 -r 299ec6187305 mpz/fdiv_qr_ui.c
--- a/mpz/fdiv_qr_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/fdiv_qr_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -107,7 +107,7 @@
 	      rl = divisor - rl;
 	    }
 
-	  PTR(rem)[0] = rl;
+	  MPZ_NEWALLOC (rem, 1)[0] = rl;
 	  SIZ(rem) = rl != 0;
 	}
       qn = nn - (qp[nn - 1] == 0);
diff -r d71a9618f892 -r 299ec6187305 mpz/fdiv_r_ui.c
--- a/mpz/fdiv_r_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/fdiv_r_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -99,7 +99,7 @@
 	  if (ns < 0)
 	    rl = divisor - rl;
 
-	  PTR(rem)[0] = rl;
+	  MPZ_NEWALLOC (rem, 1)[0] = rl;
 	  SIZ(rem) = 1;
 	}
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/fib2_ui.c
--- a/mpz/fib2_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/fib2_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -41,9 +41,9 @@
 
   if (n <= FIB_TABLE_LIMIT)
     {
-      PTR(fn)[0] = FIB_TABLE (n);
+      MPZ_NEWALLOC (fn, 1)[0] = FIB_TABLE (n);
       SIZ(fn) = (n != 0);      /* F[0]==0, others are !=0 */
-      PTR(fnsub1)[0] = FIB_TABLE ((int) n - 1);
+      MPZ_NEWALLOC (fnsub1, 1)[0] = FIB_TABLE ((int) n - 1);
       SIZ(fnsub1) = (n != 1);  /* F[1-1]==0, others are !=0 */
       return;
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/fib_ui.c
--- a/mpz/fib_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/fib_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -65,7 +65,7 @@
 
   if (n <= FIB_TABLE_LIMIT)
     {
-      PTR(fn)[0] = FIB_TABLE (n);
+      MPZ_NEWALLOC (fn, 1)[0] = FIB_TABLE (n);
       SIZ(fn) = (n != 0);      /* F[0]==0, others are !=0 */
       return;
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/gcd.c
--- a/mpz/gcd.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/gcd.c	Mon Nov 09 22:30:52 2015 +0100
@@ -76,14 +76,14 @@
   if (usize == 1)
     {
       SIZ (g) = 1;
-      PTR (g)[0] = mpn_gcd_1 (vp, vsize, up[0]);
+      MPZ_NEWALLOC (g, 1)[0] = mpn_gcd_1 (vp, vsize, up[0]);
       return;
     }
 
   if (vsize == 1)
     {
       SIZ(g) = 1;
-      PTR (g)[0] = mpn_gcd_1 (up, usize, vp[0]);
+      MPZ_NEWALLOC (g, 1)[0] = mpn_gcd_1 (up, usize, vp[0]);
       return;
     }
 
diff -r d71a9618f892 -r 299ec6187305 mpz/gcd_ui.c
--- a/mpz/gcd_ui.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/gcd_ui.c	Mon Nov 09 22:30:52 2015 +0100
@@ -78,7 +78,7 @@
 
   if (w != NULL)
     {
-      PTR(w)[0] = res;
+      MPZ_NEWALLOC (w, 1)[0] = res;
       SIZ(w) = res != 0;
     }
   return res;
diff -r d71a9618f892 -r 299ec6187305 mpz/gcdext.c
--- a/mpz/gcdext.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/gcdext.c	Mon Nov 09 22:30:52 2015 +0100
@@ -72,7 +72,7 @@
       if (s != NULL)
 	{
 	  SIZ (s) = ssize;
-	  PTR (s)[0] = 1;
+	  MPZ_NEWALLOC (s, 1)[0] = 1;
 	}
       return;
     }
diff -r d71a9618f892 -r 299ec6187305 mpz/init.c
--- a/mpz/init.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/init.c	Mon Nov 09 22:30:52 2015 +0100
@@ -34,7 +34,8 @@
 void
 mpz_init (mpz_ptr x)
 {
-  ALLOC (x) = 1;
-  PTR (x) = __GMP_ALLOCATE_FUNC_LIMBS (1);
+  static const mp_limb_t dummy_limb=0xc1a0;
+  ALLOC (x) = 0;
+  PTR (x) = &dummy_limb;
   SIZ (x) = 0;
 }
diff -r d71a9618f892 -r 299ec6187305 mpz/inits.c
--- a/mpz/inits.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/inits.c	Mon Nov 09 22:30:52 2015 +0100
@@ -35,14 +35,15 @@
 void
 mpz_inits (mpz_ptr x, ...)
 {
+  static const mp_limb_t dummy_limb=0xc1a0;
   va_list  ap;
 
   va_start (ap, x);
 
   while (x != NULL)
     {
-      ALLOC (x) = 1;
-      PTR (x) = __GMP_ALLOCATE_FUNC_LIMBS (1);
+      ALLOC (x) = 0;
+      PTR (x) = &dummy_limb;
       SIZ (x) = 0;
 
       x = va_arg (ap, mpz_ptr);
diff -r d71a9618f892 -r 299ec6187305 mpz/ior.c
--- a/mpz/ior.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/ior.c	Mon Nov 09 22:30:52 2015 +0100
@@ -126,10 +126,10 @@
 	      break;
 	  res_size = i + 1;
 
+	  res_ptr = MPZ_NEWALLOC (res, res_size + 1);
+
 	  if (res_size != 0)
 	    {
-	      res_ptr = MPZ_NEWALLOC (res, res_size + 1);
-
 	      /* Second loop computes the real result.  */
 	      mpn_and_n (res_ptr, op1_ptr, op2_ptr, res_size);
 
diff -r d71a9618f892 -r 299ec6187305 mpz/iset_d.c
--- a/mpz/iset_d.c	Mon Nov 09 07:32:59 2015 +0100
+++ b/mpz/iset_d.c	Mon Nov 09 22:30:52 2015 +0100
@@ -35,8 +35,7 @@
 void
 mpz_init_set_d (mpz_ptr dest, double val)
 {


More information about the gmp-commit mailing list