diff --git a/src/arf/get.c b/src/arf/get.c index ac0c1e990b..b731b26a92 100644 --- a/src/arf/get.c +++ b/src/arf/get.c @@ -179,14 +179,12 @@ arf_get_fmpz_2exp(fmpz_t man, fmpz_t exp, const arf_t x) else { mpz_ptr z = _fmpz_promote(man); - - if (z->_mp_alloc < xn) - mpz_realloc(z, xn); + mp_ptr zptr = FLINT_MPZ_REALLOC(z, xn); if (shift == 0) - flint_mpn_copyi(z->_mp_d, xptr, xn); + flint_mpn_copyi(zptr, xptr, xn); else - mpn_rshift(z->_mp_d, xptr, xn, shift); + mpn_rshift(zptr, xptr, xn, shift); /* top limb cannot be zero */ z->_mp_size = ARF_SGNBIT(x) ? -xn : xn; @@ -321,10 +319,7 @@ arf_get_fmpz(fmpz_t z, const arf_t x, arf_rnd_t rnd) zn = (exp + (rnd != ARF_RND_DOWN) + FLINT_BITS - 1) / FLINT_BITS; zz = _fmpz_promote(z); - if (zz->_mp_alloc < zn) - mpz_realloc(zz, zn); - - zp = zz->_mp_d; + zp = FLINT_MPZ_REALLOC(zz, zn); if (rnd == ARF_RND_DOWN) { diff --git a/src/fft/fermat_to_mpz.c b/src/fft/fermat_to_mpz.c index 057c27f1eb..d7411a4e5b 100644 --- a/src/fft/fermat_to_mpz.c +++ b/src/fft/fermat_to_mpz.c @@ -15,24 +15,22 @@ void fermat_to_mpz(mpz_t m, mp_limb_t * i, mp_size_t limbs) { mp_limb_signed_t hi; + mp_ptr mptr = FLINT_MPZ_REALLOC(m, limbs + 1); - if (m->_mp_alloc < limbs + 1) - mpz_realloc(m, limbs + 1); - - flint_mpn_copyi(m->_mp_d, i, limbs + 1); + flint_mpn_copyi(mptr, i, limbs + 1); hi = i[limbs]; if (hi < WORD(0)) { - mpn_neg(m->_mp_d, m->_mp_d, limbs + 1); + mpn_neg(mptr, mptr, limbs + 1); m->_mp_size = limbs + 1; - while ((m->_mp_size) && (!m->_mp_d[m->_mp_size - 1])) + while ((m->_mp_size) && (!mptr[m->_mp_size - 1])) m->_mp_size--; m->_mp_size = -m->_mp_size; } else { m->_mp_size = limbs + 1; - while ((m->_mp_size) && (!m->_mp_d[m->_mp_size - 1])) + while ((m->_mp_size) && (!mptr[m->_mp_size - 1])) m->_mp_size--; } } diff --git a/src/fft_small/fmpz_poly_mul.c b/src/fft_small/fmpz_poly_mul.c index 08f1af9b11..18c2c8a51e 100644 --- a/src/fft_small/fmpz_poly_mul.c +++ b/src/fft_small/fmpz_poly_mul.c @@ -200,10 +200,9 @@ void fmpz_neg_ui_array(fmpz_t out, const ulong * in, slong in_len) else { mpz_ptr mpz = _fmpz_promote(out); - if (mpz->_mp_alloc < size) - mpz_realloc2(mpz, FLINT_BITS * size); + mp_ptr mp = FLINT_MPZ_REALLOC(mpz, size); mpz->_mp_size = -size; - flint_mpn_copyi(mpz->_mp_d, in, size); + flint_mpn_copyi(mp, in, size); } } diff --git a/src/flint.h.in b/src/flint.h.in index cf7c0306ee..5362e59a5e 100644 --- a/src/flint.h.in +++ b/src/flint.h.in @@ -484,7 +484,10 @@ typedef fmpq fmpq_t[1]; /* The minimum number of limbs that the fmpz collector should spit out */ #define MPZ_MIN_ALLOC 2 -#define MPZ_REALLOC(ptr, sz) ((int) (sz) > ((ptr)->_mp_alloc) ? (mp_ptr) mpz_realloc(ptr, sz) : ((ptr)->_mp_d)) + +/* NOTE: Never allocate less than MPZ_MIN_ALLOC */ +#define FLINT_MPZ_REALLOC(ptr, sz) ((int) (sz) > ((ptr)->_mp_alloc) ? (mp_ptr) mpz_realloc(ptr, sz) : (ptr)->_mp_d) +#define FLINT_MPZ_REALLOC_TIGHT(ptr, sz) ((mp_ptr) mpz_realloc(ptr, FLINT_MAX(sz, MPZ_MIN_ALLOC))) #ifdef __cplusplus } diff --git a/src/fmpz/addmul.c b/src/fmpz/addmul.c index 3cea76e55f..a485ed22f4 100644 --- a/src/fmpz/addmul.c +++ b/src/fmpz/addmul.c @@ -16,12 +16,6 @@ #include "ulong_extras.h" #include "fmpz.h" -#define MPZ_FIT_SIZE(z, nlimbs) \ - do { \ - if (z->_mp_alloc < nlimbs) \ - _mpz_realloc(z, nlimbs); \ - } while (0) - /* Will not get called with x or y small. */ void _flint_mpz_addmul_large(mpz_ptr z, mpz_srcptr x, mpz_srcptr y, int negate) @@ -122,8 +116,7 @@ _flint_mpz_addmul_large(mpz_ptr z, mpz_srcptr x, mpz_srcptr y, int negate) tn -= (top == 0); alloc = FLINT_MAX(tn, zn) + 1; - MPZ_FIT_SIZE(z, alloc); - zd = z->_mp_d; + zd = FLINT_MPZ_REALLOC(z, alloc); if (sgn >= 0) { diff --git a/src/fmpz/aors_ui.c b/src/fmpz/aors_ui.c index 9a0d59973b..ef445ffaf0 100644 --- a/src/fmpz/aors_ui.c +++ b/src/fmpz/aors_ui.c @@ -69,10 +69,7 @@ fmpz_add_ui(fmpz_t res, const fmpz_t x, ulong y) else rp = _fmpz_promote_val(res); - if (rp->_mp_alloc < xn + 1) - _mpz_realloc(rp, xn + 1); - - rd = rp->_mp_d; + rd = FLINT_MPZ_REALLOC(rp, xn + 1); xd = xp->_mp_d; if (xn_signed >= 0) /* positive + nonnegative */ @@ -182,10 +179,7 @@ fmpz_sub_ui(fmpz_t res, const fmpz_t x, ulong y) else rp = _fmpz_promote_val(res); - if (rp->_mp_alloc < xn + 1) - _mpz_realloc(rp, xn + 1); - - rd = rp->_mp_d; + rd = FLINT_MPZ_REALLOC(rp, xn + 1); xd = xp->_mp_d; if (xn_signed <= 0) /* positive + nonnegative */ diff --git a/src/fmpz/bit_unpack.c b/src/fmpz/bit_unpack.c index 3a27fb6b60..b2481cb3da 100644 --- a/src/fmpz/bit_unpack.c +++ b/src/fmpz/bit_unpack.c @@ -77,9 +77,7 @@ fmpz_bit_unpack(fmpz_t coeff, nn_srcptr arr, flint_bitcnt_t shift, b = bits % FLINT_BITS; /* allocate space for l limbs only */ - if (mcoeff->_mp_alloc < (int) l) - mpz_realloc(mcoeff, l); - p = mcoeff->_mp_d; + p = FLINT_MPZ_REALLOC_TIGHT(mcoeff, l); /* shift in l limbs */ if (shift) @@ -176,9 +174,7 @@ fmpz_bit_unpack_unsigned(fmpz_t coeff, nn_srcptr arr, b = bits % FLINT_BITS; /* allocate space for l limbs only */ - if (mcoeff->_mp_alloc < (int) l) - mpz_realloc(mcoeff, l); - p = mcoeff->_mp_d; + p = FLINT_MPZ_REALLOC_TIGHT(mcoeff, l); /* shift in l limbs */ if (shift) diff --git a/src/fmpz/fdiv.c b/src/fmpz/fdiv.c index 39a919e9b6..3742ecddb8 100644 --- a/src/fmpz/fdiv.c +++ b/src/fmpz/fdiv.c @@ -202,8 +202,7 @@ void _mpz_tdiv_qr_preinvn(mpz_ptr q, mpz_ptr r, nn_ptr qp, rp, ap, dp, tp, sp; - if ((ulong) r->_mp_alloc < usize1 + nm) - mpz_realloc(r, usize1 + nm); + rp = FLINT_MPZ_REALLOC(r, usize1 + nm); if (usize1 < usize2) /* special case preinv code can't deal with */ { @@ -213,13 +212,9 @@ void _mpz_tdiv_qr_preinvn(mpz_ptr q, mpz_ptr r, return; } - if ((ulong) q->_mp_alloc < qsize + nm) - mpz_realloc(q, qsize + nm); - dp = d->_mp_d; ap = a->_mp_d; - qp = q->_mp_d; - rp = r->_mp_d; + qp = FLINT_MPZ_REALLOC(q, qsize + nm); TMP_START; if ((r == d || q == d) && !nm) /* we have alias with d */ diff --git a/src/fmpz/init2.c b/src/fmpz/init2.c index 15d8d8d14d..14c51b407f 100644 --- a/src/fmpz/init2.c +++ b/src/fmpz/init2.c @@ -20,8 +20,7 @@ fmpz_init2(fmpz_t f, ulong limbs) { mpz_ptr mf = _fmpz_new_mpz(); *f = PTR_TO_COEFF(mf); - if ((int) limbs > mf->_mp_alloc) - mpz_realloc(mf, limbs); + FLINT_MPZ_REALLOC(mf, limbs); } else { diff --git a/src/fmpz/mul.c b/src/fmpz/mul.c index 1a039ddd1b..88c4bcfd97 100644 --- a/src/fmpz/mul.c +++ b/src/fmpz/mul.c @@ -48,9 +48,7 @@ flint_mpz_mul(mpz_ptr z, mpz_srcptr x, mpz_srcptr y) } zn = xn + yn; - if (z->_mp_alloc < zn) - _mpz_realloc(z, zn); - zd = z->_mp_d; + zd = FLINT_MPZ_REALLOC(z, zn); /* Important: read after possibly resizing z, so that the pointers are valid in case of aliasing. */ xd = x->_mp_d; diff --git a/src/fmpz/mul_2exp.c b/src/fmpz/mul_2exp.c index 6be9efd0dd..60ac96e9bb 100644 --- a/src/fmpz/mul_2exp.c +++ b/src/fmpz/mul_2exp.c @@ -43,23 +43,8 @@ fmpz_mul_2exp(fmpz_t f, const fmpz_t g, ulong exp) ulong * limbs; mpz_ptr mf; - /* Ensure enough limbs are allocated for f */ - if (!COEFF_IS_MPZ(*f)) - { - /* TODO: Initialize the new mpz with alloc limbs instead of - * reallocating them. */ - mf = _fmpz_new_mpz(); - *f = PTR_TO_COEFF(mf); - if (mf->_mp_alloc < alloc) - mpz_realloc(mf, alloc); - } - else - { - mf = COEFF_TO_PTR(*f); - if (mf->_mp_alloc < alloc) - mpz_realloc(mf, alloc); - } - limbs = mf->_mp_d; + mf = _fmpz_promote(f); + limbs = FLINT_MPZ_REALLOC(mf, alloc); mf->_mp_size = (c1 > 0) ? alloc : -alloc; memset(limbs, 0, sizeof(ulong) * alloc); diff --git a/src/fmpz/primorial.c b/src/fmpz/primorial.c index 7cc74058f8..0310928536 100644 --- a/src/fmpz/primorial.c +++ b/src/fmpz/primorial.c @@ -136,6 +136,7 @@ fmpz_primorial(fmpz_t res, ulong n) slong len, pi; ulong bits; mpz_ptr mres; + mp_ptr rp; const ulong * primes; if (n <= LARGEST_ULONG_PRIMORIAL) @@ -153,9 +154,8 @@ fmpz_primorial(fmpz_t res, ulong n) bits = FLINT_BIT_COUNT(primes[pi - 1]); mres = _fmpz_promote(res); - if (mres->_mp_alloc < (int) ((pi * bits) / FLINT_BITS) + 1) - mpz_realloc(mres, (pi * bits) / FLINT_BITS + 1); + rp = FLINT_MPZ_REALLOC(mres, (pi * bits) / FLINT_BITS + 1); - len = mpn_prod_limbs(mres->_mp_d, primes, pi, bits); + len = mpn_prod_limbs(rp, primes, pi, bits); mres->_mp_size = len; } diff --git a/src/fmpz/set.c b/src/fmpz/set.c index b709a2110a..8ddd0f50e7 100644 --- a/src/fmpz/set.c +++ b/src/fmpz/set.c @@ -187,11 +187,10 @@ fmpz_set_signed_uiuiui(fmpz_t r, ulong hi, ulong mid, ulong lo) else { mpz_ptr z = _fmpz_promote(r); - if (z->_mp_alloc < 3) - mpz_realloc(z, 3); - z->_mp_d[0] = lo; - z->_mp_d[1] = mid; - z->_mp_d[2] = hi; + mp_ptr zp = FLINT_MPZ_REALLOC(z, 3); + zp[0] = lo; + zp[1] = mid; + zp[2] = hi; z->_mp_size = negate ? -3 : 3; } } @@ -217,24 +216,23 @@ void fmpz_set_ui_array(fmpz_t out, const ulong * in, slong in_len) else { mpz_ptr mpz = _fmpz_promote(out); - if (mpz->_mp_alloc < size) - mpz_realloc(mpz, size); + mp_ptr mp = FLINT_MPZ_REALLOC(mpz, size); mpz->_mp_size = size; - flint_mpn_copyi(mpz->_mp_d, in, size); + flint_mpn_copyi(mp, in, size); } } void fmpz_set_mpn_large(fmpz_t z, nn_srcptr src, slong n, int negative) { mpz_ptr zz; + mp_ptr zp; slong i; - zz = _fmpz_promote(z); - if (zz->_mp_alloc < n) - mpz_realloc(zz, n); + zz = _fmpz_promote(z); + zp = FLINT_MPZ_REALLOC(zz, n); for (i = 0; i < n; i++) - zz->_mp_d[i] = src[i]; + zp[i] = src[i]; zz->_mp_size = negative ? -n : n; } diff --git a/src/fmpz_extras/lshift_mpn.c b/src/fmpz_extras/lshift_mpn.c index 67ec30cd12..cf8cf75402 100644 --- a/src/fmpz_extras/lshift_mpn.c +++ b/src/fmpz_extras/lshift_mpn.c @@ -26,10 +26,7 @@ fmpz_lshift_mpn(fmpz_t z, nn_srcptr d, slong dn, int sgnbit, flint_bitcnt_t shif shift_bits = shift % FLINT_BITS; zn = dn + shift_limbs + (shift_bits != 0); - if (zmpz->_mp_alloc < zn) - mpz_realloc(zmpz, zn); - - zp = zmpz->_mp_d; + zp = FLINT_MPZ_REALLOC(zmpz, zn); flint_mpn_zero(zp, shift_limbs); if (shift_bits == 0) diff --git a/src/fmpz_factor/ecm.c b/src/fmpz_factor/ecm.c index e4f435bc74..1c13c3d3d1 100644 --- a/src/fmpz_factor/ecm.c +++ b/src/fmpz_factor/ecm.c @@ -98,11 +98,11 @@ fmpz_factor_ecm(fmpz_t f, ulong curves, ulong B1, ulong B2, fmpz_sub_ui(nm8, n_in, 8); ret = 0; + /* FIXME: Wait to promote f until after stage 2 precomputations? */ fac = _fmpz_promote(f); { int alloc = fmpz_size(n_in); - if (fac->_mp_alloc < alloc) - mpz_realloc(fac, alloc); + FLINT_MPZ_REALLOC(fac, alloc); } /************************ STAGE I PRECOMPUTATIONS ************************/ diff --git a/src/fmpz_factor/factor_pp1.c b/src/fmpz_factor/factor_pp1.c index 8ef84ecc17..3e756fad8e 100644 --- a/src/fmpz_factor/factor_pp1.c +++ b/src/fmpz_factor/factor_pp1.c @@ -335,16 +335,14 @@ int fmpz_factor_pp1(fmpz_t fac, const fmpz_t n_in, ulong B1, ulong B2sqrt, ulong for (i = 0; (ulong) i < num_roots; i++) { mpz_ptr m = _fmpz_promote(roots + i); - if (m->_mp_alloc < nn) - mpz_realloc(m, nn); + FLINT_MPZ_REALLOC(m, nn); } roots2 = _fmpz_vec_init(num_roots); for (i = 0; (ulong) i < num_roots; i++) { mpz_ptr m = _fmpz_promote(roots2 + i); - if (m->_mp_alloc < nn) - mpz_realloc(m, nn); + FLINT_MPZ_REALLOC(m, nn); } evals = _fmpz_vec_init(num_roots); @@ -553,9 +551,8 @@ int fmpz_factor_pp1(fmpz_t fac, const fmpz_t n_in, ulong B1, ulong B2sqrt, ulong if (ret) { mpz_ptr fm = _fmpz_promote(fac); - if (fm->_mp_alloc < r) - mpz_realloc(fm, r); - flint_mpn_copyi(fm->_mp_d, factor, r); + mp_ptr fp = FLINT_MPZ_REALLOC(fm, r); + flint_mpn_copyi(fp, factor, r); fm->_mp_size = r; _fmpz_demote_val(fac); } diff --git a/src/fmpz_factor/pollard_brent.c b/src/fmpz_factor/pollard_brent.c index a035245d6b..8540e3a0f8 100644 --- a/src/fmpz_factor/pollard_brent.c +++ b/src/fmpz_factor/pollard_brent.c @@ -72,8 +72,7 @@ fmpz_factor_pollard_brent(fmpz_t p_factor, flint_rand_t state, fmpz_t n_in, flint_mpn_preinvn(ninv, n, n_size); fac = _fmpz_promote(p_factor); - if (fac->_mp_alloc < (int) n_size) - mpz_realloc(fac, n_size); + FLINT_MPZ_REALLOC(fac, n_size); fac->_mp_size = n_size; while (max_tries--) diff --git a/src/fmpz_factor/pollard_brent_single.c b/src/fmpz_factor/pollard_brent_single.c index 0d114054fd..601331a23a 100644 --- a/src/fmpz_factor/pollard_brent_single.c +++ b/src/fmpz_factor/pollard_brent_single.c @@ -224,8 +224,7 @@ fmpz_factor_pollard_brent_single(fmpz_t p_factor, fmpz_t n_in, fmpz_t yi, flint_mpn_preinvn(ninv, n, n_size); fac = _fmpz_promote(p_factor); - if (fac->_mp_alloc < (int) n_size) - mpz_realloc(fac, n_size); + FLINT_MPZ_REALLOC(fac, n_size); fac->_mp_size = n_size; mpn_zero(a, n_size); diff --git a/src/fmpz_poly/bit_pack.c b/src/fmpz_poly/bit_pack.c index d86eb816d1..4d08b9f459 100644 --- a/src/fmpz_poly/bit_pack.c +++ b/src/fmpz_poly/bit_pack.c @@ -47,6 +47,7 @@ fmpz_poly_bit_pack(fmpz_t f, const fmpz_poly_t poly, mpz_ptr mpz; slong i, d; int negate; + mp_ptr ptr; len = fmpz_poly_length(poly); @@ -58,21 +59,20 @@ fmpz_poly_bit_pack(fmpz_t f, const fmpz_poly_t poly, mpz = _fmpz_promote(f); d = (len * bit_size - 1) / FLINT_BITS + 1; - if (mpz->_mp_alloc < d) - mpz_realloc(mpz, d); + ptr = FLINT_MPZ_REALLOC(mpz, d); - flint_mpn_zero(mpz->_mp_d, d); + flint_mpn_zero(ptr, d); if (fmpz_sgn(fmpz_poly_lead(poly)) < 0) negate = -1; else negate = 0; - _fmpz_poly_bit_pack(mpz->_mp_d, poly->coeffs, len, bit_size, negate); + _fmpz_poly_bit_pack(ptr, poly->coeffs, len, bit_size, negate); for (i = d - 1; i >= 0; i--) { - if (mpz->_mp_d[i] != 0) + if (ptr[i] != 0) break; } d = i + 1; diff --git a/src/gmpcompat-longlong.h.in b/src/gmpcompat-longlong.h.in index 6e6c0c60da..1a3524fb8f 100644 --- a/src/gmpcompat-longlong.h.in +++ b/src/gmpcompat-longlong.h.in @@ -15,11 +15,6 @@ #include #include "longlong.h" -#define FLINT_MPZ_REALLOC(z, len) \ - ((len) > ((z)->_mp_alloc) \ - ? (mp_ptr) _mpz_realloc(z, len) \ - : ((z)->_mp_d)) - static inline void flint_mpz_add_uiui(mpz_ptr a, mpz_srcptr b, ulong c1, ulong c0) { diff --git a/src/gmpcompat.h.in b/src/gmpcompat.h.in index 2973ee9f3d..215685fc36 100644 --- a/src/gmpcompat.h.in +++ b/src/gmpcompat.h.in @@ -15,11 +15,6 @@ #include #include "longlong.h" -#define FLINT_MPZ_REALLOC(z, len) \ - ((len) > ((z)->_mp_alloc) \ - ? (mp_ptr) _mpz_realloc(z, len) \ - : ((z)->_mp_d)) - static inline void flint_mpz_add_uiui(mpz_ptr a, mpz_srcptr b, ulong c1, ulong c0) { diff --git a/src/mpoly/unpack_vec.c b/src/mpoly/unpack_vec.c index 524ecdce4c..066acdc93b 100644 --- a/src/mpoly/unpack_vec.c +++ b/src/mpoly/unpack_vec.c @@ -100,10 +100,9 @@ void mpoly_unpack_vec_fmpz(fmpz * exp1, const ulong * exp2, flint_bitcnt_t bits, else { mpz_ptr mpz = _fmpz_promote(exp1); - if ((ulong) mpz->_mp_alloc < words_per_field) - mpz_realloc(mpz, words_per_field); + mp_ptr zp = FLINT_MPZ_REALLOC(mpz, words_per_field); mpz->_mp_size = size; - flint_mpn_copyi(mpz->_mp_d, exp2, size); + flint_mpn_copyi(zp, exp2, size); } exp1++; } diff --git a/src/nmod_poly/bit_pack.c b/src/nmod_poly/bit_pack.c index c9e846e80e..89dd8b4348 100644 --- a/src/nmod_poly/bit_pack.c +++ b/src/nmod_poly/bit_pack.c @@ -130,6 +130,7 @@ nmod_poly_bit_pack(fmpz_t f, const nmod_poly_t poly, { slong len, limbs; mpz_ptr mpz; + mp_ptr mp; slong i; len = nmod_poly_length(poly); @@ -142,14 +143,13 @@ nmod_poly_bit_pack(fmpz_t f, const nmod_poly_t poly, mpz = _fmpz_promote(f); limbs = (len * bit_size - 1) / FLINT_BITS + 1; - if (mpz->_mp_alloc < limbs) - mpz_realloc(mpz, limbs); + mp = FLINT_MPZ_REALLOC(mpz, limbs); - _nmod_poly_bit_pack(mpz->_mp_d, poly->coeffs, len, bit_size); + _nmod_poly_bit_pack(mp, poly->coeffs, len, bit_size); for (i = limbs - 1; i >= 0; i--) { - if (mpz->_mp_d[i] != 0) + if (mp[i] != 0) break; } diff --git a/src/qsieve/large_prime_variant.c b/src/qsieve/large_prime_variant.c index f9c42dbf26..767e83917a 100644 --- a/src/qsieve/large_prime_variant.c +++ b/src/qsieve/large_prime_variant.c @@ -295,13 +295,12 @@ relation_t qsieve_parse_relation(qs_t qs_inf) else { mpz_ptr mY = _fmpz_new_mpz(); + mp_ptr ptr; mY->_mp_size = Ysz; + ptr = FLINT_MPZ_REALLOC(mY, FLINT_ABS(Ysz)); - if (mY->_mp_alloc < FLINT_ABS(Ysz)) - _mpz_realloc(mY, FLINT_ABS(Ysz)); - - fread(mY->_mp_d, sizeof(ulong), FLINT_ABS(Ysz), (FILE *) qs_inf->siqs); + fread(ptr, sizeof(ulong), FLINT_ABS(Ysz), (FILE *) qs_inf->siqs); *rel.Y = PTR_TO_COEFF(mY); }