Skip to content

Commit

Permalink
Change mpz_realloc to FLINT_MPZ_REALLOC(_TIGHT)
Browse files Browse the repository at this point in the history
* FLINT_MPZ_REALLOC only reallocates if necessary. Should be used in
  most functions.

* FLINT_MPZ_REALLOC_TIGHT always reallocates, with a minimum number of
  MPZ_MIN_ALLOC limbs. Should be used in higher-level functions if it
  may be the case that it can improve the performance by using less
  memory, like routines as fmpz_bit_unpack.

Also remove, now redundant, FLINT_MPZ_REALLOC in gmpcompat.h.
  • Loading branch information
albinahlback committed May 17, 2024
1 parent cc60cac commit 3cdc0eb
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 129 deletions.
13 changes: 4 additions & 9 deletions src/arf/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
12 changes: 5 additions & 7 deletions src/fft/fermat_to_mpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
}
5 changes: 2 additions & 3 deletions src/fft_small/fmpz_poly_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/flint.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 1 addition & 8 deletions src/fmpz/addmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 2 additions & 8 deletions src/fmpz/aors_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 2 additions & 6 deletions src/fmpz/bit_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions src/fmpz/fdiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
{
Expand All @@ -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 */
Expand Down
3 changes: 1 addition & 2 deletions src/fmpz/init2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 1 addition & 3 deletions src/fmpz/mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 2 additions & 17 deletions src/fmpz/mul_2exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/fmpz/primorial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
22 changes: 10 additions & 12 deletions src/fmpz/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
5 changes: 1 addition & 4 deletions src/fmpz_extras/lshift_mpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/fmpz_factor/ecm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ************************/
Expand Down
11 changes: 4 additions & 7 deletions src/fmpz_factor/factor_pp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/fmpz_factor/pollard_brent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--)
Expand Down
3 changes: 1 addition & 2 deletions src/fmpz_factor/pollard_brent_single.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 3cdc0eb

Please sign in to comment.