diff options
author | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-11-09 22:30:52 +0100 |
---|---|---|
committer | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-11-09 22:30:52 +0100 |
commit | 7acd63c08b3b969696681ee8125ea63e543998c8 (patch) | |
tree | fccdc23ceb50b4647ac4dd5848c9170972c3030d /mpz | |
parent | e97517c73c6a962a9d2cd519adcc639087cd1a75 (diff) | |
download | gmp-7acd63c08b3b969696681ee8125ea63e543998c8.tar.gz |
mpz: Lazy allocation.
Diffstat (limited to 'mpz')
-rw-r--r-- | mpz/2fac_ui.c | 4 | ||||
-rw-r--r-- | mpz/aors_ui.h | 2 | ||||
-rw-r--r-- | mpz/bin_ui.c | 2 | ||||
-rw-r--r-- | mpz/bin_uiui.c | 6 | ||||
-rw-r--r-- | mpz/cdiv_qr_ui.c | 2 | ||||
-rw-r--r-- | mpz/cdiv_r_ui.c | 2 | ||||
-rw-r--r-- | mpz/cfdiv_q_2exp.c | 2 | ||||
-rw-r--r-- | mpz/clear.c | 3 | ||||
-rw-r--r-- | mpz/clears.c | 3 | ||||
-rw-r--r-- | mpz/com.c | 2 | ||||
-rw-r--r-- | mpz/fac_ui.c | 2 | ||||
-rw-r--r-- | mpz/fdiv_qr_ui.c | 2 | ||||
-rw-r--r-- | mpz/fdiv_r_ui.c | 2 | ||||
-rw-r--r-- | mpz/fib2_ui.c | 4 | ||||
-rw-r--r-- | mpz/fib_ui.c | 2 | ||||
-rw-r--r-- | mpz/gcd.c | 4 | ||||
-rw-r--r-- | mpz/gcd_ui.c | 2 | ||||
-rw-r--r-- | mpz/gcdext.c | 2 | ||||
-rw-r--r-- | mpz/init.c | 5 | ||||
-rw-r--r-- | mpz/inits.c | 5 | ||||
-rw-r--r-- | mpz/ior.c | 4 | ||||
-rw-r--r-- | mpz/iset_d.c | 3 | ||||
-rw-r--r-- | mpz/lucnum2_ui.c | 4 | ||||
-rw-r--r-- | mpz/lucnum_ui.c | 2 | ||||
-rw-r--r-- | mpz/mfac_uiui.c | 2 | ||||
-rw-r--r-- | mpz/mul.c | 15 | ||||
-rw-r--r-- | mpz/n_pow_ui.c | 2 | ||||
-rw-r--r-- | mpz/oddfac_1.c | 3 | ||||
-rw-r--r-- | mpz/powm.c | 2 | ||||
-rw-r--r-- | mpz/powm_sec.c | 2 | ||||
-rw-r--r-- | mpz/powm_ui.c | 4 | ||||
-rw-r--r-- | mpz/primorial_ui.c | 4 | ||||
-rw-r--r-- | mpz/realloc.c | 21 | ||||
-rw-r--r-- | mpz/realloc2.c | 20 | ||||
-rw-r--r-- | mpz/set_si.c | 2 | ||||
-rw-r--r-- | mpz/set_ui.c | 2 | ||||
-rw-r--r-- | mpz/tdiv_qr_ui.c | 6 | ||||
-rw-r--r-- | mpz/tdiv_r_ui.c | 6 | ||||
-rw-r--r-- | mpz/ui_sub.c | 19 |
39 files changed, 96 insertions, 85 deletions
diff --git a/mpz/2fac_ui.c b/mpz/2fac_ui.c index 37e6c468b..c6e2a90ff 100644 --- a/mpz/2fac_ui.c +++ b/mpz/2fac_ui.c @@ -67,8 +67,8 @@ mpz_2fac_ui (mpz_ptr x, unsigned long n) 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 --git a/mpz/aors_ui.h b/mpz/aors_ui.h index 2e3c481ab..eaf02c8e9 100644 --- a/mpz/aors_ui.h +++ b/mpz/aors_ui.h @@ -80,7 +80,7 @@ FUNCTION (mpz_ptr w, mpz_srcptr u, unsigned long int vval) usize = SIZ (u); if (usize == 0) { - PTR (w)[0] = vval; + MPZ_NEWALLOC (w, 1)[0] = vval; SIZ (w) = VARIATION_NEG (vval != 0); return; } diff --git a/mpz/bin_ui.c b/mpz/bin_ui.c index c24b21724..bc5894510 100644 --- a/mpz/bin_ui.c +++ b/mpz/bin_ui.c @@ -83,7 +83,7 @@ mpz_bin_ui (mpz_ptr r, mpz_srcptr n, unsigned long int k) /* 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 --git a/mpz/bin_uiui.c b/mpz/bin_uiui.c index 94a9dc5c7..9d661d54e 100644 --- a/mpz/bin_uiui.c +++ b/mpz/bin_uiui.c @@ -642,7 +642,7 @@ mpz_goetgheluck_bin_uiui (mpz_ptr r, unsigned long int n, unsigned long int k) } else { - PTR (r)[0] = prod; + MPZ_NEWALLOC (r, 1)[0] = prod; SIZ (r) = 1; } TMP_FREE; @@ -677,10 +677,10 @@ mpz_bin_uiui (mpz_ptr r, unsigned long int n, unsigned long int k) /* 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 --git a/mpz/cdiv_qr_ui.c b/mpz/cdiv_qr_ui.c index 1ccfbbe7f..f82841a7a 100644 --- a/mpz/cdiv_qr_ui.c +++ b/mpz/cdiv_qr_ui.c @@ -108,7 +108,7 @@ mpz_cdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long in rl = divisor - rl; } - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; SIZ(rem) = -(rl != 0); } qn = nn - (qp[nn - 1] == 0); diff --git a/mpz/cdiv_r_ui.c b/mpz/cdiv_r_ui.c index 68fb149c4..bd1c82293 100644 --- a/mpz/cdiv_r_ui.c +++ b/mpz/cdiv_r_ui.c @@ -101,7 +101,7 @@ mpz_cdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor) if (ns >= 0) rl = divisor - rl; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; SIZ(rem) = -1; } } diff --git a/mpz/cfdiv_q_2exp.c b/mpz/cfdiv_q_2exp.c index 366d6aa70..f0c36dd15 100644 --- a/mpz/cfdiv_q_2exp.c +++ b/mpz/cfdiv_q_2exp.c @@ -53,7 +53,7 @@ cfdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir) 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 --git a/mpz/clear.c b/mpz/clear.c index d1541e91c..3be7deffa 100644 --- a/mpz/clear.c +++ b/mpz/clear.c @@ -36,5 +36,6 @@ see https://www.gnu.org/licenses/. */ 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 --git a/mpz/clears.c b/mpz/clears.c index dac988738..ac00b9931 100644 --- a/mpz/clears.c +++ b/mpz/clears.c @@ -41,7 +41,8 @@ mpz_clears (mpz_ptr x, ...) 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); } @@ -49,7 +49,7 @@ mpz_com (mpz_ptr dst, mpz_srcptr src) 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 --git a/mpz/fac_ui.c b/mpz/fac_ui.c index e0932272f..18993d9f4 100644 --- a/mpz/fac_ui.c +++ b/mpz/fac_ui.c @@ -60,7 +60,7 @@ mpz_fac_ui (mpz_ptr x, unsigned long n) 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 --git a/mpz/fdiv_qr_ui.c b/mpz/fdiv_qr_ui.c index c8435a423..074952837 100644 --- a/mpz/fdiv_qr_ui.c +++ b/mpz/fdiv_qr_ui.c @@ -107,7 +107,7 @@ mpz_fdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long in rl = divisor - rl; } - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; SIZ(rem) = rl != 0; } qn = nn - (qp[nn - 1] == 0); diff --git a/mpz/fdiv_r_ui.c b/mpz/fdiv_r_ui.c index 98dd56c6f..f256da05f 100644 --- a/mpz/fdiv_r_ui.c +++ b/mpz/fdiv_r_ui.c @@ -99,7 +99,7 @@ mpz_fdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor) if (ns < 0) rl = divisor - rl; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; SIZ(rem) = 1; } } diff --git a/mpz/fib2_ui.c b/mpz/fib2_ui.c index ddf456f58..1a64d66f1 100644 --- a/mpz/fib2_ui.c +++ b/mpz/fib2_ui.c @@ -41,9 +41,9 @@ mpz_fib2_ui (mpz_ptr fn, mpz_ptr fnsub1, unsigned long n) 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 --git a/mpz/fib_ui.c b/mpz/fib_ui.c index f591c0850..9976e0067 100644 --- a/mpz/fib_ui.c +++ b/mpz/fib_ui.c @@ -65,7 +65,7 @@ mpz_fib_ui (mpz_ptr fn, unsigned long n) 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; } @@ -76,14 +76,14 @@ mpz_gcd (mpz_ptr g, mpz_srcptr u, mpz_srcptr v) 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 --git a/mpz/gcd_ui.c b/mpz/gcd_ui.c index e26d03f64..8a3329b4b 100644 --- a/mpz/gcd_ui.c +++ b/mpz/gcd_ui.c @@ -78,7 +78,7 @@ mpz_gcd_ui (mpz_ptr w, mpz_srcptr u, unsigned long int v) if (w != NULL) { - PTR(w)[0] = res; + MPZ_NEWALLOC (w, 1)[0] = res; SIZ(w) = res != 0; } return res; diff --git a/mpz/gcdext.c b/mpz/gcdext.c index 01b1f8379..4863d3bab 100644 --- a/mpz/gcdext.c +++ b/mpz/gcdext.c @@ -72,7 +72,7 @@ mpz_gcdext (mpz_ptr g, mpz_ptr s, mpz_ptr t, mpz_srcptr a, mpz_srcptr b) if (s != NULL) { SIZ (s) = ssize; - PTR (s)[0] = 1; + MPZ_NEWALLOC (s, 1)[0] = 1; } return; } diff --git a/mpz/init.c b/mpz/init.c index 61ef9f87e..d8ba45aeb 100644 --- a/mpz/init.c +++ b/mpz/init.c @@ -34,7 +34,8 @@ see https://www.gnu.org/licenses/. */ 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 --git a/mpz/inits.c b/mpz/inits.c index 12a1dc3a0..a9af358de 100644 --- a/mpz/inits.c +++ b/mpz/inits.c @@ -35,14 +35,15 @@ see https://www.gnu.org/licenses/. */ 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); @@ -126,10 +126,10 @@ mpz_ior (mpz_ptr res, mpz_srcptr op1, mpz_srcptr op2) 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 --git a/mpz/iset_d.c b/mpz/iset_d.c index 2c5b23ab9..43d7d8bbb 100644 --- a/mpz/iset_d.c +++ b/mpz/iset_d.c @@ -35,8 +35,7 @@ see https://www.gnu.org/licenses/. */ void mpz_init_set_d (mpz_ptr dest, double val) { - ALLOC (dest) = 1; - PTR (dest) = __GMP_ALLOCATE_FUNC_LIMBS (1); + ALLOC (dest) = 0; SIZ (dest) = 0; mpz_set_d (dest, val); } diff --git a/mpz/lucnum2_ui.c b/mpz/lucnum2_ui.c index f8e4fb356..06c93b407 100644 --- a/mpz/lucnum2_ui.c +++ b/mpz/lucnum2_ui.c @@ -50,11 +50,11 @@ mpz_lucnum2_ui (mpz_ptr ln, mpz_ptr lnsub1, unsigned long n) mp_limb_t f1 = FIB_TABLE ((int) n - 1); /* L[n] = F[n] + 2F[n-1] */ - PTR(ln)[0] = f + 2*f1; + MPZ_NEWALLOC (ln, 1)[0] = f + 2*f1; SIZ(ln) = 1; /* L[n-1] = 2F[n] - F[n-1], but allow for L[-1]=-1 */ - PTR(lnsub1)[0] = (n == 0 ? 1 : 2*f - f1); + MPZ_NEWALLOC (lnsub1, 1)[0] = (n == 0 ? 1 : 2*f - f1); SIZ(lnsub1) = (n == 0 ? -1 : 1); return; diff --git a/mpz/lucnum_ui.c b/mpz/lucnum_ui.c index d1fe6b54c..1b274fd79 100644 --- a/mpz/lucnum_ui.c +++ b/mpz/lucnum_ui.c @@ -68,7 +68,7 @@ mpz_lucnum_ui (mpz_ptr ln, unsigned long n) if (n <= FIB_TABLE_LUCNUM_LIMIT) { /* L[n] = F[n] + 2F[n-1] */ - PTR(ln)[0] = FIB_TABLE(n) + 2 * FIB_TABLE ((int) n - 1); + MPZ_NEWALLOC (ln, 1)[0] = FIB_TABLE(n) + 2 * FIB_TABLE ((int) n - 1); SIZ(ln) = 1; return; } diff --git a/mpz/mfac_uiui.c b/mpz/mfac_uiui.c index 3ea36bce9..a34727502 100644 --- a/mpz/mfac_uiui.c +++ b/mpz/mfac_uiui.c @@ -59,7 +59,7 @@ mpz_mfac_uiui (mpz_ptr x, unsigned long n, unsigned long m) ASSERT (m != 0); if ((n < 3) | (n - 3 < m - 1)) { /* (n < 3 || n - 1 <= m || m == 0) */ - PTR (x)[0] = n + (n == 0); + MPZ_NEWALLOC (x, 1)[0] = n + (n == 0); SIZ (x) = 1; } else { /* m < n - 1 < GMP_NUMB_MAX */ mp_limb_t g, sn; @@ -104,13 +104,14 @@ mpz_mul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v) wsize = usize + vsize; if (ALLOC (w) < wsize) { - if (wp == up || wp == vp) - { - free_me = wp; - free_me_size = ALLOC (w); - } - else - (*__gmp_free_func) (wp, (size_t) ALLOC (w) * GMP_LIMB_BYTES); + if (ALLOC (w) != 0) + if (wp == up || wp == vp) + { + free_me = wp; + free_me_size = ALLOC (w); + } + else + (*__gmp_free_func) (wp, (size_t) ALLOC (w) * GMP_LIMB_BYTES); ALLOC (w) = wsize; wp = __GMP_ALLOCATE_FUNC_LIMBS (wsize); diff --git a/mpz/n_pow_ui.c b/mpz/n_pow_ui.c index 6c3febe23..c380b981d 100644 --- a/mpz/n_pow_ui.c +++ b/mpz/n_pow_ui.c @@ -183,7 +183,7 @@ mpz_n_pow_ui (mpz_ptr r, mp_srcptr bp, mp_size_t bsize, unsigned long int e) /* b^0 == 1, including 0^0 == 1 */ if (e == 0) { - PTR(r)[0] = 1; + MPZ_NEWALLOC (r, 1)[0] = 1; SIZ(r) = 1; return; } diff --git a/mpz/oddfac_1.c b/mpz/oddfac_1.c index cf0bff065..cf3178f98 100644 --- a/mpz/oddfac_1.c +++ b/mpz/oddfac_1.c @@ -245,6 +245,7 @@ mpz_2multiswing_1 (mpz_ptr x, mp_limb_t n, mp_ptr sieve, mp_ptr factors) } else { + ASSERT (ALLOC (x) > 0); PTR (x)[0] = prod; SIZ (x) = 1; } @@ -290,7 +291,7 @@ mpz_oddfac_1 (mpz_ptr x, mp_limb_t n, unsigned flag) if (n <= ODD_FACTORIAL_TABLE_LIMIT) { - PTR (x)[0] = __gmp_oddfac_table[n]; + MPZ_NEWALLOC (x, 1)[0] = __gmp_oddfac_table[n]; SIZ (x) = 1; } else if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT + 1) diff --git a/mpz/powm.c b/mpz/powm.c index 4d13da739..0ad2b7203 100644 --- a/mpz/powm.c +++ b/mpz/powm.c @@ -86,7 +86,7 @@ mpz_powm (mpz_ptr r, mpz_srcptr b, mpz_srcptr e, mpz_srcptr m) /* b^0 mod m, b is anything and m is non-zero. Result is 1 mod m, i.e., 1 or 0 depending on if m = 1. */ SIZ(r) = n != 1 || mp[0] != 1; - PTR(r)[0] = 1; + MPZ_NEWALLOC (r, 1)[0] = 1; TMP_FREE; /* we haven't really allocated anything here */ return; } diff --git a/mpz/powm_sec.c b/mpz/powm_sec.c index 98644ca49..c693023ab 100644 --- a/mpz/powm_sec.c +++ b/mpz/powm_sec.c @@ -60,7 +60,7 @@ mpz_powm_sec (mpz_ptr r, mpz_srcptr b, mpz_srcptr e, mpz_srcptr m) /* b^0 mod m, b is anything and m is non-zero. Result is 1 mod m, i.e., 1 or 0 depending on if m = 1. */ SIZ(r) = n != 1 || mp[0] != 1; - PTR(r)[0] = 1; + MPZ_NEWALLOC (r, 1)[0] = 1; return; } DIVIDE_BY_ZERO; diff --git a/mpz/powm_ui.c b/mpz/powm_ui.c index e04bb0ad7..046895cbe 100644 --- a/mpz/powm_ui.c +++ b/mpz/powm_ui.c @@ -140,8 +140,8 @@ mpz_powm_ui (mpz_ptr r, mpz_srcptr b, unsigned long int el, mpz_srcptr m) { /* Exponent is zero, result is 1 mod M, i.e., 1 or 0 depending on if M equals 1. */ - SIZ(r) = (mn == 1 && mp[0] == 1) ? 0 : 1; - PTR(r)[0] = 1; + SIZ(r) = mn != 1 || mp[0] != 1; + MPZ_NEWALLOC (r, 1)[0] = 1; return; } diff --git a/mpz/primorial_ui.c b/mpz/primorial_ui.c index 7a964a1f1..21af098bc 100644 --- a/mpz/primorial_ui.c +++ b/mpz/primorial_ui.c @@ -114,7 +114,7 @@ mpz_primorial_ui (mpz_ptr x, unsigned long n) if (n < numberof (table)) { - PTR (x)[0] = table[n]; + MPZ_NEWALLOC (x, 1)[0] = table[n]; SIZ (x) = 1; } else @@ -155,7 +155,7 @@ mpz_primorial_ui (mpz_ptr x, unsigned long n) } else { - PTR (x)[0] = prod; + MPZ_NEWALLOC (x, 1)[0] = prod; SIZ (x) = 1; } diff --git a/mpz/realloc.c b/mpz/realloc.c index 3d2755ca0..050936915 100644 --- a/mpz/realloc.c +++ b/mpz/realloc.c @@ -58,14 +58,21 @@ _mpz_realloc (mpz_ptr m, mp_size_t new_alloc) } } - mp = __GMP_REALLOCATE_FUNC_LIMBS (PTR(m), ALLOC(m), new_alloc); - PTR(m) = mp; - ALLOC(m) = new_alloc; + if (ALLOC (m) == 0) + { + mp = __GMP_ALLOCATE_FUNC_LIMBS (new_alloc); + } + else + { + mp = __GMP_REALLOCATE_FUNC_LIMBS (PTR (m), ALLOC (m), new_alloc); - /* Don't create an invalid number; if the current value doesn't fit after - reallocation, clear it to 0. */ - if (ABSIZ(m) > new_alloc) - SIZ(m) = 0; + /* Don't create an invalid number; if the current value doesn't fit after + reallocation, clear it to 0. */ + if (UNLIKELY (ABSIZ (m) > new_alloc)) + SIZ (m) = 0; + } + PTR (m) = mp; + ALLOC(m) = new_alloc; return (void *) mp; } diff --git a/mpz/realloc2.c b/mpz/realloc2.c index 3ccfaab1f..ac07e59c8 100644 --- a/mpz/realloc2.c +++ b/mpz/realloc2.c @@ -50,11 +50,19 @@ mpz_realloc2 (mpz_ptr m, mp_bitcnt_t bits) } } - PTR(m) = __GMP_REALLOCATE_FUNC_LIMBS (PTR(m), ALLOC(m), new_alloc); - ALLOC(m) = new_alloc; + if (ALLOC (m) == 0) + { + PTR (m) = __GMP_ALLOCATE_FUNC_LIMBS (new_alloc); + } + else + { + PTR (m) = __GMP_REALLOCATE_FUNC_LIMBS (PTR(m), ALLOC(m), new_alloc); - /* Don't create an invalid number; if the current value doesn't fit after - reallocation, clear it to 0. */ - if (ABSIZ(m) > new_alloc) - SIZ(m) = 0; + /* Don't create an invalid number; if the current value doesn't fit after + reallocation, clear it to 0. */ + if (ABSIZ(m) > new_alloc) + SIZ(m) = 0; + } + + ALLOC(m) = new_alloc; } diff --git a/mpz/set_si.c b/mpz/set_si.c index 137036833..1d8b8a99c 100644 --- a/mpz/set_si.c +++ b/mpz/set_si.c @@ -39,7 +39,7 @@ mpz_set_si (mpz_ptr dest, signed long int val) vl = (mp_limb_t) ABS_CAST (unsigned long int, val); - PTR (dest)[0] = vl & GMP_NUMB_MASK; + MPZ_NEWALLOC (dest, 1)[0] = vl & GMP_NUMB_MASK; size = vl != 0; #if GMP_NAIL_BITS != 0 diff --git a/mpz/set_ui.c b/mpz/set_ui.c index f36754cd5..5b4ef58c5 100644 --- a/mpz/set_ui.c +++ b/mpz/set_ui.c @@ -37,7 +37,7 @@ mpz_set_ui (mpz_ptr dest, unsigned long int val) { mp_size_t size; - PTR (dest)[0] = val & GMP_NUMB_MASK; + MPZ_NEWALLOC (dest, 1)[0] = val & GMP_NUMB_MASK; size = val != 0; #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ diff --git a/mpz/tdiv_qr_ui.c b/mpz/tdiv_qr_ui.c index 4f667abe0..1385fc9a8 100644 --- a/mpz/tdiv_qr_ui.c +++ b/mpz/tdiv_qr_ui.c @@ -68,7 +68,7 @@ mpz_tdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long in SIZ(quot) = 0; rl = np[0]; SIZ(rem) = ns >= 0 ? 1 : -1; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; return rl; } @@ -90,10 +90,8 @@ mpz_tdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long in SIZ(rem) = 0; else { - /* Store the single-limb remainder. We don't check if there's space - for just one limb, since no function ever makes zero space. */ SIZ(rem) = ns >= 0 ? 1 : -1; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; } qn = nn - (qp[nn - 1] == 0); } diff --git a/mpz/tdiv_r_ui.c b/mpz/tdiv_r_ui.c index 30addd6db..7b034a3b8 100644 --- a/mpz/tdiv_r_ui.c +++ b/mpz/tdiv_r_ui.c @@ -64,7 +64,7 @@ mpz_tdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor) { rl = np[0]; SIZ(rem) = ns >= 0 ? 1 : -1; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; return rl; } @@ -88,10 +88,8 @@ mpz_tdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor) SIZ(rem) = 0; else { - /* Store the single-limb remainder. We don't check if there's space - for just one limb, since no function ever makes zero space. */ SIZ(rem) = ns >= 0 ? 1 : -1; - PTR(rem)[0] = rl; + MPZ_NEWALLOC (rem, 1)[0] = rl; } } diff --git a/mpz/ui_sub.c b/mpz/ui_sub.c index 579a30079..0730cf72f 100644 --- a/mpz/ui_sub.c +++ b/mpz/ui_sub.c @@ -52,11 +52,8 @@ mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v) } #endif - vp = PTR(v); vn = SIZ(v); - wp = PTR(w); - if (vn > 1) { wp = MPZ_REALLOC (w, vn); @@ -64,24 +61,22 @@ mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v) mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval); wn = -(vn - (wp[vn - 1] == 0)); } - else if (vn == 1) + else if (vn >= 0) { - if (uval >= vp[0]) + mp_limb_t vp0; + vp0 = PTR (v)[0] & - (mp_limb_t) vn; + wp = MPZ_NEWALLOC (w, 1); + if (uval >= vp0) { - wp[0] = uval - vp[0]; + wp[0] = uval - vp0; wn = wp[0] != 0; } else { - wp[0] = vp[0] - uval; + wp[0] = vp0 - uval; wn = -1; } } - else if (vn == 0) - { - wp[0] = uval; - wn = uval != 0; - } else /* (vn < 0) */ { vn = -vn; |