Skip to content

Commit

Permalink
Merge pull request #1928 from flintlib/random
Browse files Browse the repository at this point in the history
Fix flint_mpn random functions
  • Loading branch information
fredrik-johansson authored Apr 20, 2024
2 parents 306b477 + ce2e73d commit 1be94a3
Show file tree
Hide file tree
Showing 45 changed files with 99 additions and 146 deletions.
7 changes: 3 additions & 4 deletions doc/source/mpn_extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,16 @@ Random Number Generation
--------------------------------------------------------------------------------


.. function:: void flint_mpn_rrandom(mp_limb_t * rp, gmp_randstate_t state, mp_size_t n)
.. function:: void flint_mpn_rrandom(mp_ptr rp, flint_rand_t state, mp_size_t n)

Generates a random number with ``n`` limbs and stores
it on ``rp``. The number it generates will tend to have
long strings of zeros and ones in the binary representation.

Useful for testing functions and algorithms, since this kind of random
numbers have proven to be more likely to trigger corner-case bugs.


.. function:: void flint_mpn_urandomb(mp_limb_t * rp, gmp_randstate_t state, flint_bitcnt_t n)
.. function:: void flint_mpn_urandomb(mp_ptr rp, flint_rand_t state, flint_bitcnt_t n)

Generates a uniform random number of ``n`` bits and stores
it on ``rp``.
Expand Down
4 changes: 1 addition & 3 deletions src/arb/test/t-atan_taylor_rs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(arb_atan_taylor_rs, state)
{
slong iter;

_flint_rand_init_gmp(state);

for (iter = 0; iter < 100000 * 0.1 * flint_test_multiplier(); iter++)
{
mp_ptr x, y1, y2, t;
Expand All @@ -36,7 +34,7 @@ TEST_FUNCTION_START(arb_atan_taylor_rs, state)
y2 = flint_malloc(sizeof(mp_limb_t) * xn);
t = flint_malloc(sizeof(mp_limb_t) * xn);

flint_mpn_rrandom(x, state->gmp_state, xn);
flint_mpn_rrandom(x, state, xn);
x[xn - 1] &= (LIMB_ONES >> 4);

_arb_atan_taylor_naive(y1, &err1, x, xn, N, alternating);
Expand Down
8 changes: 3 additions & 5 deletions src/arb/test/t-exp_taylor_rs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(arb_exp_taylor_rs, state)
{
slong iter;

_flint_rand_init_gmp(state);

for (iter = 0; iter < 100000 * 0.1 * flint_test_multiplier(); iter++)
{
mp_ptr x, y1, y2, t;
Expand All @@ -35,9 +33,9 @@ TEST_FUNCTION_START(arb_exp_taylor_rs, state)
y2 = flint_malloc(sizeof(mp_limb_t) * (xn + 1));
t = flint_malloc(sizeof(mp_limb_t) * (xn + 1));

flint_mpn_rrandom(x, state->gmp_state, xn);
flint_mpn_rrandom(y1, state->gmp_state, xn + 1);
flint_mpn_rrandom(y2, state->gmp_state, xn + 1);
flint_mpn_rrandom(x, state, xn);
flint_mpn_rrandom(y1, state, xn + 1);
flint_mpn_rrandom(y2, state, xn + 1);
x[xn - 1] &= (LIMB_ONES >> 4);

_arb_exp_taylor_naive(y1, &err1, x, xn, N);
Expand Down
12 changes: 5 additions & 7 deletions src/arb/test/t-sin_cos_taylor_rs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(arb_sin_cos_taylor_rs, state)
{
slong iter;

_flint_rand_init_gmp(state);

for (iter = 0; iter < 100000 * 0.1 * flint_test_multiplier(); iter++)
{
mp_ptr x, y1s, y1c, y2s, y2c, t;
Expand All @@ -37,11 +35,11 @@ TEST_FUNCTION_START(arb_sin_cos_taylor_rs, state)
y2c = flint_malloc(sizeof(mp_limb_t) * xn);
t = flint_malloc(sizeof(mp_limb_t) * xn);

flint_mpn_rrandom(x, state->gmp_state, xn);
flint_mpn_rrandom(y1s, state->gmp_state, xn);
flint_mpn_rrandom(y1c, state->gmp_state, xn);
flint_mpn_rrandom(y2s, state->gmp_state, xn);
flint_mpn_rrandom(y2c, state->gmp_state, xn);
flint_mpn_rrandom(x, state, xn);
flint_mpn_rrandom(y1s, state, xn);
flint_mpn_rrandom(y1c, state, xn);
flint_mpn_rrandom(y2s, state, xn);
flint_mpn_rrandom(y2c, state, xn);
x[xn - 1] &= (LIMB_ONES >> 4);

_arb_sin_cos_taylor_naive(y1s, y1c, &err1, x, xn, N);
Expand Down
4 changes: 2 additions & 2 deletions src/fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ mp_limb_t flint_mpn_sumdiff_n(mp_ptr s, mp_ptr d, mp_srcptr x, mp_srcptr y, mp_s
nn[limbs] = 1; \
} else { \
if (n_randint(state, 2) == 0) \
flint_mpn_rrandom(nn, state->gmp_state, limbs); \
flint_mpn_rrandom(nn, state, limbs); \
else \
flint_mpn_urandomb(nn, state->gmp_state, limbs*FLINT_BITS); \
flint_mpn_urandomb(nn, state, limbs * FLINT_BITS); \
nn[limbs] = n_randint(state, 1024); \
} \
if (n_randint(state, 2)) \
Expand Down
7 changes: 2 additions & 5 deletions src/fft/profile/p-mul_mfa_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ main(void)
flint_printf("mul_mfa_truncate_sqrt2....");
fflush(stdout);


_flint_rand_init_gmp(state);

depth = 13;
w = 2;
iters = 1;
Expand All @@ -49,8 +46,8 @@ main(void)
r1 = i2 + int_limbs;
/* r2 = r1 + 2*int_limbs; */

flint_mpn_urandomb(i1, state->gmp_state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i2, state->gmp_state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i1, state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i2, state, int_limbs*FLINT_BITS);

for (j = 0; j < iters; j++)
mul_mfa_truncate_sqrt2(r1, i1, int_limbs, i2, int_limbs, depth, w);
Expand Down
7 changes: 2 additions & 5 deletions src/fft/profile/p-mul_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ main(void)
flint_printf("mul_truncate_sqrt2....");
fflush(stdout);


_flint_rand_init_gmp(state);

depth = 13;
w = 1;
iters = 1;
Expand All @@ -49,8 +46,8 @@ main(void)
r1 = i2 + int_limbs;
/* r2 = r1 + 2*int_limbs; */

flint_mpn_urandomb(i1, state->gmp_state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i2, state->gmp_state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i1, state, int_limbs*FLINT_BITS);
flint_mpn_urandomb(i2, state, int_limbs*FLINT_BITS);

/* mpn_mul(r2, i1, int_limbs, i2, int_limbs); */
for (j = 0; j < iters; j++)
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-adjust.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ TEST_FUNCTION_START(fft_adjust, state)
mpz_t p, m2a, m2b, mn1;
mp_limb_t * nn1, * r1;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(m2a);
mpz_init(m2b);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-adjust_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ TEST_FUNCTION_START(fft_adjust_sqrt2, state)
mpz_t p, m2a, m2b, mn1;
mp_limb_t * nn1, * r1, * temp;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(m2a);
mpz_init(m2b);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-butterfly.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ TEST_FUNCTION_START(fft_ifft_butterfly, state)
mpz_t p, ma, mb, m2a, m2b, mn1, mn2;
mp_limb_t * nn1, * nn2, * r1, * r2;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(ma);
mpz_init(mb);
Expand Down
4 changes: 1 addition & 3 deletions src/fft/test/t-butterfly_lshB.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ TEST_FUNCTION_START(butterfly_lshB, state)
mpz_t p, ma, mb, m2a, m2b, mn1, mn2;
mp_limb_t * nn1, * nn2, * r1, * r2;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(ma);
mpz_init(mb);
Expand Down Expand Up @@ -79,7 +77,7 @@ TEST_FUNCTION_START(butterfly_lshB, state)
nn2 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
r1 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
r2 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
flint_mpn_rrandom(nn1, state->gmp_state, limbs);
flint_mpn_rrandom(nn1, state, limbs);
random_fermat(nn1, state, limbs);
random_fermat(nn2, state, limbs);

Expand Down
4 changes: 1 addition & 3 deletions src/fft/test/t-butterfly_rshB.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ TEST_FUNCTION_START(butterfly_rshB, state)
mpz_t p, ma, mb, m2a, m2b, mn1, mn2;
mp_limb_t * nn1, * nn2, * r1, * r2;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(ma);
mpz_init(mb);
Expand Down Expand Up @@ -93,7 +91,7 @@ TEST_FUNCTION_START(butterfly_rshB, state)
nn2 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
r1 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
r2 = flint_malloc((limbs + 1)*sizeof(mp_limb_t));
flint_mpn_rrandom(nn1, state->gmp_state, limbs);
flint_mpn_rrandom(nn1, state, limbs);
random_fermat(nn1, state, limbs);
random_fermat(nn2, state, limbs);

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-butterfly_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ TEST_FUNCTION_START(fft_ifft_butterfly_sqrt2, state)
mpz_t p, ma, mb, m2a, m2b, mn1, mn2;
mp_limb_t * nn1, * nn2, * r1, * r2, * temp;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(ma);
mpz_init(mb);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-butterfly_twiddle.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ TEST_FUNCTION_START(fft_ifft_butterfly_twiddle, state)
mp_limb_t * nn1, * nn2, * r1, * r2;
flint_bitcnt_t b1, b2;

_flint_rand_init_gmp(state);

mpz_init(p);
mpz_init(ma);
mpz_init(mb);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-convolution.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_convolution, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 13 :
(flint_test_multiplier() > 1) ? 12 : 11;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-convolution_precache.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_convolution_precache, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 13 :
(flint_test_multiplier() > 1) ? 12 : 11;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-div_2expmod_2expp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ TEST_FUNCTION_START(mpn_div_2expmod_2expp1, state)
mp_limb_t * nn, * r;
mpz_t p, m1, m2, mn1, mn2;

_flint_rand_init_gmp(state);

mpz_init(m1);
mpz_init(m2);
mpz_init(mn1);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-fft_ifft_mfa_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_ifft_mfa_truncate_sqrt2, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 13 :
(flint_test_multiplier() > 1) ? 12 : 11;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-fft_ifft_negacyclic.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_ifft_negacyclic, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-fft_ifft_radix2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_ifft_radix2, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-fft_ifft_truncate.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_ifft_truncate, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-fft_ifft_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(fft_ifft_truncate_sqrt2, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-mul_2expmod_2expp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ TEST_FUNCTION_START(mpn_mul_2expmod_2expp1, state)
mp_limb_t * nn, * r;
mpz_t p, m1, m2, mn1, mn2;

_flint_rand_init_gmp(state);

mpz_init(m1);
mpz_init(m2);
mpz_init(mn1);
Expand Down
6 changes: 2 additions & 4 deletions src/fft/test/t-mul_fft_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(flint_mpn_mul_fft_main, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down Expand Up @@ -65,8 +63,8 @@ TEST_FUNCTION_START(flint_mpn_mul_fft_main, state)
r1 = i2 + n2;
r2 = r1 + n1 + n2;

flint_mpn_urandomb(i1, state->gmp_state, b1);
flint_mpn_urandomb(i2, state->gmp_state, b2);
flint_mpn_urandomb(i1, state, b1);
flint_mpn_urandomb(i2, state, b2);

mpn_mul(r2, i1, n1, i2, n2);
flint_mpn_mul_fft_main(r1, i1, n1, i2, n2);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-mul_mfa_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(mul_mfa_truncate_sqrt2, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() >= 10) ? 13 :
(flint_test_multiplier() >= 1) ? 12 : 11;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-mul_truncate_sqrt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ TEST_FUNCTION_START(mul_truncate_sqrt2, state)
{
flint_bitcnt_t depth, w, maxdepth;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 12 :
(flint_test_multiplier() > 1) ? 11 : 10;

Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-mulmod_2expp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ TEST_FUNCTION_START(fft_mulmod_2expp1, state)
flint_bitcnt_t depth, w, maxdepth;
int iters;

_flint_rand_init_gmp(state);

maxdepth = (flint_test_multiplier() > 10) ? 18 : 15;

for (iters = 0; iters < 100; iters++)
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-negmod_2expp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ TEST_FUNCTION_START(mpn_negmod_2expp1, state)
mp_limb_t * a, * z;
mpz_t p, z1, z2;

_flint_rand_init_gmp(state);

mpz_init(z1);
mpz_init(z2);
mpz_init(p);
Expand Down
2 changes: 0 additions & 2 deletions src/fft/test/t-normmod_2expp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ TEST_FUNCTION_START(mpn_normmod_2expp1, state)
mp_limb_t * nn;
mpz_t p, m1, m2;

_flint_rand_init_gmp(state);

mpz_init(m1);
mpz_init(m2);
mpz_init(p);
Expand Down
4 changes: 1 addition & 3 deletions src/fft/test/t-split_combine_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ TEST_FUNCTION_START(fft_split_combine_bits, state)
int i;
mp_size_t j;

_flint_rand_init_gmp(state);

for (i = 0; i < 1000 * flint_test_multiplier(); i++)
{
mp_size_t total_limbs = n_randint(state, 1000) + 1;
Expand All @@ -35,7 +33,7 @@ TEST_FUNCTION_START(fft_split_combine_bits, state)
for (j = 0; j < length; j++)
poly[j] = flint_malloc((limbs + 1)*sizeof(mp_limb_t));

flint_mpn_urandomb(in, state->gmp_state, total_limbs*FLINT_BITS);
flint_mpn_urandomb(in, state, total_limbs*FLINT_BITS);

fft_split_bits(poly, in, total_limbs, bits, limbs);
fft_combine_bits(out, poly, length, bits, limbs, total_limbs);
Expand Down
Loading

0 comments on commit 1be94a3

Please sign in to comment.