From 0ffd847b15b2aed098e41feedc98ea25afc9465f Mon Sep 17 00:00:00 2001 From: thevenyp Date: Mon, 2 Dec 2013 16:47:36 +0000 Subject: [tests\] Add random generators for mpfr_t and mpc_t parameter. git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/branches/benchs_tests@1345 211d60ee-9f03-0410-a15a-8952a2c7a4e4 --- tests/random.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/random.c b/tests/random.c index 0730fee..5fb7adf 100644 --- a/tests/random.c +++ b/tests/random.c @@ -158,3 +158,74 @@ test_default_random (mpc_ptr z, mpfr_exp_t emin, mpfr_exp_t emax, if (((r>>8) & 0xFF) < negative_probability) mpfr_neg (mpc_imagref (z), mpc_imagref (z), MPFR_RNDN); } + +/* Set x to a non zero value random value with absolute values greater than + or equal to 2^{emin-1} and less than 2^emax. + + x is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void +test_random_mpfr (mpfr_ptr x, mpfr_exp_t emin, mpfr_exp_t emax, + unsigned int negative_probability) +{ + const unsigned long range = (unsigned long int) (emax - emin) + 1; + unsigned long r; + + if (!rands_initialized) + { + fprintf (stderr, + "Put test_start at the beginning of your test function.\n"); + exit (1); + } + + do + { + mpfr_urandom (x, rands, MPFR_RNDN); + } while (mpfr_zero_p (x)); + + mpfr_set_exp (x, (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + if (negative_probability > 256) + negative_probability = 256; + r = gmp_urandomb_ui (rands, 8); + if ((r & 0xFF) < negative_probability) + mpfr_neg (x, x, MPFR_RNDN); +} + +/* Set z to a non zero value random value with absolute values of Re(z) and + Im(z) greater than or equal to 2^{emin-1} and less than 2^emax. + + Each part is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void +test_random_mpc (mpc_ptr z, mpfr_exp_t emin, mpfr_exp_t emax, + unsigned int negative_probability) +{ + const unsigned long range = (unsigned long int) (emax - emin) + 1; + unsigned long r; + + if (!rands_initialized) + { + fprintf (stderr, + "Put test_start at the beginning of your test function.\n"); + exit (1); + } + + do + { + mpc_urandom (z, rands); + } while (mpfr_zero_p (mpc_realref (z)) || mpfr_zero_p (mpc_imagref (z))); + + mpfr_set_exp (mpc_realref (z), + (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + + mpfr_set_exp (mpc_imagref (z), + (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + + if (negative_probability > 256) + negative_probability = 256; + r = gmp_urandomb_ui (rands, 16); + if ((r & 0xFF) < negative_probability) + mpfr_neg (mpc_realref (z), mpc_realref (z), MPFR_RNDN); + if (((r>>8) & 0xFF) < negative_probability) + mpfr_neg (mpc_imagref (z), mpc_imagref (z), MPFR_RNDN); +} -- cgit v1.2.1