Skip to content

Commit

Permalink
Merge pull request #1944 from albinahlback/faster_tests
Browse files Browse the repository at this point in the history
Refactor some tests
  • Loading branch information
albinahlback authored May 3, 2024
2 parents d843715 + 87e5cdc commit 7a7e569
Show file tree
Hide file tree
Showing 136 changed files with 549 additions and 1,030 deletions.
57 changes: 25 additions & 32 deletions doc/source/fmpz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,63 +215,56 @@ Memory management
Random generation
--------------------------------------------------------------------------------

For thread-safety, the randomisation methods take as one of their
parameters an object of type ``flint_rand_t``. Before calling
any of the randomisation functions such an object first has to be
initialised with a call to :func:`flint_randinit`. When one is
finished generating random numbers, one should call
:func:`flint_randclear` to clean up.
For thread-safety, the randomisation methods take as one of their parameters an
object of type ``flint_rand_t``. Before calling any of the randomisation
functions such an object first has to be initialised with a call to
:func:`flint_randinit`. When one is finished generating random numbers, one
should call :func:`flint_randclear` to clean up.

.. function:: void fmpz_randbits(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)
.. function:: void fmpz_randbits_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)
void fmpz_randbits(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

Generates a random signed integer whose absolute value has precisely
the given number of bits.

.. function:: void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

Generates a random signed integer whose absolute value has a number
of bits which is random from `0` up to ``bits`` inclusive.
Generates a random integer whose absolute value has precisely the given
number of bits.

.. function:: void fmpz_randtest_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)
void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

Generates a random unsigned integer whose value has a number
of bits which is random from `0` up to ``bits`` inclusive.
Generates a random integer whose absolute value has a number of bits which
is random from `0` up to ``bits`` inclusive.

.. function:: void fmpz_randtest_not_zero(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

As per ``fmpz_randtest``, but the result will not be `0`.
If ``bits`` is set to `0`, an exception will result.
As per ``fmpz_randtest``, but the result will not be `0`. If ``bits`` is set
to `0`, an exception will result.

.. function:: void fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m)

Generates a random integer in the range `0` to `m - 1` inclusive.

.. function:: void fmpz_randtest_mod(fmpz_t f, flint_rand_t state, const fmpz_t m)

Generates a random integer in the range `0` to `m - 1` inclusive,
with an increased probability of generating values close to
the endpoints.
Generates a random integer in the range `0` to `m - 1` inclusive, with an
increased probability of generating values close to the endpoints.

.. function:: void fmpz_randtest_mod_signed(fmpz_t f, flint_rand_t state, const fmpz_t m)

Generates a random integer in the range `(-m/2, m/2]`, with an
increased probability of generating values close to the
endpoints or close to zero.
Generates a random integer in the range `(-m/2, m/2]`, with an increased
probability of generating values close to the endpoints or close to zero.

.. function:: void fmpz_randprime(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits, int proved)

Generates a random prime number with the given number of bits.

The generation is performed by choosing a random number and then
finding the next largest prime, and therefore does not quite
give a uniform distribution over the set of primes with that
many bits.
The generation is performed by choosing a random number and then finding the
next largest prime, and therefore does not quite give a uniform distribution
over the set of primes with that many bits.

Random number generation is performed using the standard Flint
random number generator, which is not suitable for cryptographic use.
Random number generation is performed using the standard FLINT random number
generator, which is not suitable for cryptographic use.

If ``proved`` is nonzero, then the integer returned is
guaranteed to actually be prime.
If ``proved`` is nonzero, then the integer returned is guaranteed to
actually be prime.



Expand Down
2 changes: 1 addition & 1 deletion doc/source/fmpz_poly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,7 @@ The Eulerian numbers can be expressed explicitly via the formula
Note: Not to be confused with Euler numbers and polynomials.

.. function:: void arith_eulerian_polynomial(fmpz_poly_t res, ulong n)
.. function:: void fmpz_eulerian_polynomial(fmpz_poly_t res, ulong n)

Sets ``res`` to the Eulerian polynomial `A_n(x)`, where we define
`A_0(x) = 1`. The polynomial is calculated via a recursive relation.
Expand Down
6 changes: 0 additions & 6 deletions doc/source/ulong_extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,6 @@ Square root and perfect power testing
`x \leftarrow x - (x\cdot x\cdot x - a)\cdot x/(2\cdot x\cdot x\cdot x + a)` for getting a good estimate,
as mentioned in the paper by W. Kahan [Kahan1991]_ .

.. function:: ulong n_cbrt_newton_iteration(ulong n)

This function returns the integer truncation of the cube root of `n`.
Makes use of Newton iterations to get a close value, and then adjusts the
estimate so as to get the correct value.

.. function:: ulong n_cbrt_binary_search(ulong n)

This function returns the integer truncation of the cube root of `n`.
Expand Down
4 changes: 2 additions & 2 deletions src/NTL-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inline void fmpz_set_ZZ(fmpz_t rop, const ZZ& op)
fmpz_set_ui(rop, xp[0]);
else
{
__mpz_struct *mf = _fmpz_promote(rop);
mpz_ptr mf = _fmpz_promote(rop);

mpz_import(mf, lw, -1, sizeof(mp_limb_t), 0, 0, xp);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ inline void fmpz_get_ZZ(NTL_NNS ZZ& rop, const fmpz_t op)

if (COEFF_IS_MPZ(c))
{
__mpz_struct * m = COEFF_TO_PTR(c);
mpz_ptr m = COEFF_TO_PTR(c);
mpn_copyi(xp, m->_mp_d, lw);
} else
{
Expand Down
2 changes: 1 addition & 1 deletion src/acb/dot_fmpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ acb_dot_fmpz(acb_t res, const acb_t initial, int subtract, acb_srcptr x, slong x
}
else
{
__mpz_struct * z = COEFF_TO_PTR(v);
mpz_ptr z = COEFF_TO_PTR(v);

ssize = z->_mp_size;
size = FLINT_ABS(ssize);
Expand Down
2 changes: 1 addition & 1 deletion src/arb/dot_fmpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ arb_dot_fmpz(arb_t res, const arb_t initial, int subtract, arb_srcptr x, slong x
}
else
{
__mpz_struct * z = COEFF_TO_PTR(v);
mpz_ptr z = COEFF_TO_PTR(v);

ssize = z->_mp_size;
size = FLINT_ABS(ssize);
Expand Down
4 changes: 2 additions & 2 deletions src/arf/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ arf_get_fmpz_2exp(fmpz_t man, fmpz_t exp, const arf_t x)
}
else
{
__mpz_struct * z = _fmpz_promote(man);
mpz_ptr z = _fmpz_promote(man);

if (z->_mp_alloc < xn)
mpz_realloc(z, xn);
Expand All @@ -201,7 +201,7 @@ arf_get_fmpz(fmpz_t z, const arf_t x, arf_rnd_t rnd)
int negative, inexact, value, roundup;
mp_size_t xn, zn;
mp_srcptr xp;
__mpz_struct * zz;
mpz_ptr zz;
mp_ptr zp;
mp_limb_t v, v2, v3;

Expand Down
2 changes: 1 addition & 1 deletion src/arf/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void arf_printd(const arf_t y, slong d) { arf_fprintd(stdout, y, d); }
int arf_load_file(arf_t x, FILE * stream)
{
fmpz_t mantissa, exponent;
__mpz_struct *mpz_mantissa, *mpz_exponent;
mpz_ptr mpz_mantissa, mpz_exponent;
int err;

fmpz_init(mantissa);
Expand Down
2 changes: 1 addition & 1 deletion src/calcium.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ CALCIUM_INLINE ulong calcium_fmpz_hash(const fmpz_t x)
return *x;
else
{
__mpz_struct * z = COEFF_TO_PTR(*x);
mpz_ptr z = COEFF_TO_PTR(*x);
return (z->_mp_size > 0) ? z->_mp_d[0] : -z->_mp_d[0];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fexpr/set_fmpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fexpr_set_fmpz(fexpr_t res, const fmpz_t c)
else
{
slong nlimbs;
__mpz_struct * z = COEFF_TO_PTR(*c);
mpz_ptr z = COEFF_TO_PTR(*c);

nlimbs = FLINT_ABS(z->_mp_size);
fexpr_fit_size(res, 1 + nlimbs);
Expand Down
2 changes: 1 addition & 1 deletion src/fft_small/fmpz_poly_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void fmpz_neg_ui_array(fmpz_t out, const ulong * in, slong in_len)
}
else
{
__mpz_struct * mpz = _fmpz_promote(out);
mpz_ptr mpz = _fmpz_promote(out);
if (mpz->_mp_alloc < size)
mpz_realloc2(mpz, FLINT_BITS * size);
mpz->_mp_size = -size;
Expand Down
16 changes: 12 additions & 4 deletions src/flint.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,22 @@ void flint_rand_free(flint_rand_s * state)
}

/* defined in ulong_extras, but used throughout the test code */
ulong n_randint(flint_rand_t, ulong);
ulong n_randlimb(flint_rand_t);
ulong n_randtest(flint_rand_t);
ulong n_randtest_not_zero(flint_rand_t);

FLINT_INLINE ulong n_randint(flint_rand_t state, ulong limit)
{
if (limit == UWORD(0))
return n_randlimb(state);
else
return n_randlimb(state) % limit;
}

#if FLINT_USES_GC
#define FLINT_GC_INIT() GC_init()
# define FLINT_GC_INIT() GC_init()
#else
#define FLINT_GC_INIT()
# define FLINT_GC_INIT()
#endif

#define FLINT_TEST_INIT(xxx) \
Expand Down Expand Up @@ -367,7 +375,7 @@ ulong n_randtest_not_zero(flint_rand_t);
((shift == FLINT_BITS) ? WORD(0) : ((in) << (shift)))

/* Beware when using the unsigned return value in signed arithmetic */
static inline
FLINT_FORCE_INLINE
mp_limb_t FLINT_BIT_COUNT(mp_limb_t x)
{
mp_limb_t zeros = FLINT_BITS;
Expand Down
2 changes: 1 addition & 1 deletion src/fmpq_poly/exp_series.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

static ulong _fmpz_gcd_big_small(const fmpz_t g, ulong h)
{
__mpz_struct * z = COEFF_TO_PTR(*g);
mpz_ptr z = COEFF_TO_PTR(*g);

return n_gcd(mpn_mod_1(z->_mp_d, FLINT_ABS(z->_mp_size), h), h);
}
Expand Down
2 changes: 1 addition & 1 deletion src/fmpq_poly/integral.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

static ulong _fmpz_gcd_big_small(const fmpz_t g, ulong h)
{
__mpz_struct * z = COEFF_TO_PTR(*g);
mpz_ptr z = COEFF_TO_PTR(*g);

return n_gcd(mpn_mod_1(z->_mp_d, FLINT_ABS(z->_mp_size), h), h);
}
Expand Down
2 changes: 1 addition & 1 deletion src/fmpq_poly/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ___fmpq_poly_set_array_mpq(fmpz * poly, fmpz_t den, const mpq_t * a, slong n)

for (i = 0; i < n; i++)
{
__mpz_struct *ptr = _fmpz_promote(poly + i);
mpz_ptr ptr = _fmpz_promote(poly + i);

mpz_divexact(t, d, mpq_denref(a[i]));
mpz_mul(ptr, mpq_numref(a[i]), t);
Expand Down
2 changes: 1 addition & 1 deletion src/fmpq_poly/set_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ __fmpq_poly_set_array_mpq(fmpz * poly, fmpz_t den, const mpq_t * a, slong n)

for (i = 0; i < n; i++)
{
__mpz_struct *ptr = _fmpz_promote(poly + i);
mpz_ptr ptr = _fmpz_promote(poly + i);

mpz_divexact(t, d, mpq_denref(a[i]));
mpz_mul(ptr, mpq_numref(a[i]), t);
Expand Down
7 changes: 4 additions & 3 deletions src/fmpz.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ int _fmpz_is_canonical(const fmpz_t x);

/* Randomisation *************************************************************/

void fmpz_randbits_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randbits(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m);
void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
Expand Down Expand Up @@ -186,7 +187,7 @@ fmpz_get_uiui(mp_limb_t * hi, mp_limb_t * low, const fmpz_t f)
}
else
{
__mpz_struct * mpz = COEFF_TO_PTR(*f);
mpz_ptr mpz = COEFF_TO_PTR(*f);
*low = mpz->_mp_d[0];
*hi = mpz->_mp_size == 2 ? mpz->_mp_d[1] : 0;
}
Expand All @@ -201,7 +202,7 @@ fmpz_set_uiui(fmpz_t f, mp_limb_t hi, mp_limb_t lo)
}
else
{
__mpz_struct *z = _fmpz_promote(f);
mpz_ptr z = _fmpz_promote(f);
if (z->_mp_alloc < 2)
mpz_realloc2(z, 2 * FLINT_BITS);
z->_mp_d[0] = lo;
Expand All @@ -219,7 +220,7 @@ fmpz_neg_uiui(fmpz_t f, mp_limb_t hi, mp_limb_t lo)
}
else
{
__mpz_struct *z = _fmpz_promote(f);
mpz_ptr z = _fmpz_promote(f);
if (z->_mp_alloc < 2)
mpz_realloc2(z, 2 * FLINT_BITS);
z->_mp_d[0] = lo;
Expand Down
2 changes: 1 addition & 1 deletion src/fmpz/abs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fmpz_abs(fmpz_t f1, const fmpz_t f2)
else /* coeff is large */
{
/* No need to retain value in promotion, as if aliased, both already large */
__mpz_struct * mf1 = _fmpz_promote(f1);
mpz_ptr mf1 = _fmpz_promote(f1);
mpz_abs(mf1, COEFF_TO_PTR(*f2));
}
}
2 changes: 1 addition & 1 deletion src/fmpz/abs_lbound_ui_2exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fmpz_abs_lbound_ui_2exp(slong * exp, const fmpz_t x, int bits)
}
else
{
__mpz_struct * z = COEFF_TO_PTR(c);
mpz_ptr z = COEFF_TO_PTR(c);
size = z->_mp_size;
size = FLINT_ABS(size);
e = (size - 1) * FLINT_BITS;
Expand Down
2 changes: 1 addition & 1 deletion src/fmpz/abs_ubound_ui_2exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fmpz_abs_ubound_ui_2exp(slong * exp, const fmpz_t x, int bits)
else
{
/* mpz */
__mpz_struct * z = COEFF_TO_PTR(c);
mpz_ptr z = COEFF_TO_PTR(c);
size = z->_mp_size;
size = FLINT_ABS(size);
e = (size - 1) * FLINT_BITS;
Expand Down
14 changes: 7 additions & 7 deletions src/fmpz/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void fmpz_add(fmpz_t f, const fmpz_t g, const fmpz_t h)
fmpz_set_si(f, c1 + c2);
} else /* g is small, h is large */
{
__mpz_struct * mpz3 = _fmpz_promote(f); /* g is saved and h is large */
__mpz_struct * mpz2 = COEFF_TO_PTR(c2);
mpz_ptr mpz3 = _fmpz_promote(f); /* g is saved and h is large */
mpz_ptr mpz2 = COEFF_TO_PTR(c2);
if (c1 < WORD(0)) flint_mpz_sub_ui(mpz3, mpz2, -c1);
else flint_mpz_add_ui(mpz3, mpz2, c1);
_fmpz_demote_val(f); /* may have cancelled */
Expand All @@ -37,17 +37,17 @@ void fmpz_add(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
if (!COEFF_IS_MPZ(c2)) /* g is large, h is small */
{
__mpz_struct * mpz3 = _fmpz_promote(f); /* h is saved and g is large */
__mpz_struct * mpz1 = COEFF_TO_PTR(c1);
mpz_ptr mpz3 = _fmpz_promote(f); /* h is saved and g is large */
mpz_ptr mpz1 = COEFF_TO_PTR(c1);
if (c2 < WORD(0)) flint_mpz_sub_ui(mpz3, mpz1, -c2);
else flint_mpz_add_ui(mpz3, mpz1, c2);
_fmpz_demote_val(f); /* may have cancelled */
}
else /* g and h are large */
{
__mpz_struct * mpz3 = _fmpz_promote(f); /* aliasing means f is already large */
__mpz_struct * mpz1 = COEFF_TO_PTR(c1);
__mpz_struct * mpz2 = COEFF_TO_PTR(c2);
mpz_ptr mpz3 = _fmpz_promote(f); /* aliasing means f is already large */
mpz_ptr mpz1 = COEFF_TO_PTR(c1);
mpz_ptr mpz2 = COEFF_TO_PTR(c2);
mpz_add(mpz3, mpz1, mpz2);
_fmpz_demote_val(f); /* may have cancelled */
}
Expand Down
2 changes: 1 addition & 1 deletion src/fmpz/addmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ _flint_mpz_addmul_large(mpz_ptr z, mpz_srcptr x, mpz_srcptr y, int negate)
void fmpz_addmul(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
fmpz c1, c2, c3;
__mpz_struct * mf;
mpz_ptr mf;

c1 = *g;
c2 = *h;
Expand Down
4 changes: 2 additions & 2 deletions src/fmpz/aors_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fmpz_add_ui(fmpz_t res, const fmpz_t x, ulong y)
}
else
{
__mpz_struct * mpz_res = _fmpz_promote(res);
mpz_ptr mpz_res = _fmpz_promote(res);
flint_mpz_set_si(mpz_res, b);
}
#else
Expand Down Expand Up @@ -150,7 +150,7 @@ fmpz_sub_ui(fmpz_t res, const fmpz_t x, ulong y)
}
else
{
__mpz_struct * mpz_res = _fmpz_promote(res);
mpz_ptr mpz_res = _fmpz_promote(res);
flint_mpz_set_si(mpz_res, b);
}
#else
Expand Down
Loading

0 comments on commit 7a7e569

Please sign in to comment.