From 8f8741e0b947b38ccdbae60e3125680391a3733d Mon Sep 17 00:00:00 2001 From: thevenyp Date: Wed, 15 Apr 2009 17:07:42 +0000 Subject: get_f.c: remove dead code (x and z always have the same number of limbs). tests/tget_f.c: Add tests with every rounding mode, add tests with random values. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@6173 280ebfd0-de03-0410-8827-d642c229c3f4 --- get_f.c | 17 +++--- tests/tget_f.c | 179 +++++++++++++++++++++++++++------------------------------ 2 files changed, 93 insertions(+), 103 deletions(-) diff --git a/get_f.c b/get_f.c index cb795d98e..41aa03a71 100644 --- a/get_f.c +++ b/get_f.c @@ -113,31 +113,30 @@ mpfr_get_f (mpf_ptr x, mpfr_srcptr y, mp_rnd_t rnd_mode) else /* we have to round to precx - sh bits */ { mpfr_t z; - mp_size_t sz, ds; + mp_size_t sz; - /* Recall that precx = (mp_prec_t) sx * BITS_PER_MP_LIMB */ + /* Recall that precx = (mp_prec_t) sx * BITS_PER_MP_LIMB, thus removing + sh bits (sh < BITS_PER_MP_LIMBS) won't reduce the number of limbs. */ mpfr_init2 (z, precx - sh); sz = MPFR_LIMB_SIZE (z); + MPFR_ASSERTN (sx == sz); + inex = mpfr_set (z, y, rnd_mode); /* warning, sh may change due to rounding, but then z is a power of two, thus we can safely ignore its last bit which is 0 */ sh = MPFR_GET_EXP(z) % BITS_PER_MP_LIMB; sh = sh <= 0 ? - sh : BITS_PER_MP_LIMB - sh; - MPFR_ASSERTD (sx >= sz); - ds = sx - sz; - MPFR_ASSERTD (sh >= 0 && ds <= 1); + MPFR_ASSERTD (sh >= 0); if (sh != 0) { mp_limb_t out; - out = mpn_rshift (xp + ds, MPFR_MANT(z), sz, sh); + out = mpn_rshift (xp, MPFR_MANT(z), sz, sh); /* If sh hasn't changed, it is the number of the non-significant bits in the lowest limb of z. Therefore out == 0. */ MPFR_ASSERTD (out == 0); } else - MPN_COPY (xp + ds, MPFR_MANT(z), sz); - if (ds != 0) - xp[0] = 0; + MPN_COPY (xp, MPFR_MANT(z), sz); EXP(x) = (MPFR_GET_EXP(z) + sh) / BITS_PER_MP_LIMB; mpfr_clear (z); } diff --git a/tests/tget_f.c b/tests/tget_f.c index 03929f560..f521660a3 100644 --- a/tests/tget_f.c +++ b/tests/tget_f.c @@ -169,104 +169,95 @@ special_test (void) static void ternary_test (void) { - int inex; + int prec; + mpfr_rnd_t rnd; + int inex, expected_inex; mpf_t x; mpfr_t y; - mpf_init2 (x, BITS_PER_MP_LIMB); - mpfr_init2 (y, 2 * BITS_PER_MP_LIMB); - - /* y == 1 */ - mpfr_set_ui (y, 1, MPFR_RNDN); - - inex = mpfr_get_f (x, y, MPFR_RNDU); - if (inex != 0 || mpfr_cmp_f (y, x) !=0) - { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDU)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex != 0) - printf ("got ternary value = %+d, expected: 0\n", inex); - - exit (1); - } + mpf_init2 (x, 256); + mpfr_init2 (y, 256); - inex = mpfr_get_f (x, y, MPFR_RNDD); - if (inex != 0 || mpfr_cmp_f (y, x) !=0) + for (prec = 2; prec <= 256; prec++) { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDD)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex != 0) - printf ("got ternary value = %+d, expected: 0\n", inex); - - exit (1); - } - - /* y == 1 + epsilon */ - mpfr_nextabove (y); - - inex = mpfr_get_f (x, y, MPFR_RNDU); - if (inex <= 0 || mpfr_cmp_f (y, x) >=0) - { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDU)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex <= 0) - printf ("got ternary value = %+d, expected: +1\n", inex); - - exit (1); - } - inex = mpfr_get_f (x, y, MPFR_RNDD); - if (inex >= 0 || mpfr_cmp_f (y, x) <=0) - { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDD)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex >= 0) - printf ("got ternary value = %+d, expected: -1\n", inex); - - exit (1); - } - - /* y == -1 - epsilon */ - mpfr_neg (y, y, MPFR_RNDN); - - inex = mpfr_get_f (x, y, MPFR_RNDU); - if (inex <= 0 || mpfr_cmp_f (y, x) >=0) - { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDU)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex <= 0) - printf ("got ternary value = %+d, expected: +1\n", inex); - - exit (1); - } - - inex = mpfr_get_f (x, y, MPFR_RNDD); - if (inex >= 0 || mpfr_cmp_f (y, x) <=0) - { - printf ("Error in mpfr_get_f (x, y, MPFR_RNDD)\n"); - printf ("x = "); - mpf_dump (x); - printf ("y = "); - mpfr_dump (y); - if (inex >= 0) - printf ("got ternary value = %+d, expected: -1\n", inex); - - exit (1); + mpf_set_prec (x, prec); + mpfr_set_prec (y, PREC (x) * BITS_PER_MP_LIMB + 1); + + /* y == 1 */ + mpfr_set_ui_2exp (y, 1, prec, MPFR_RNDN); + + RND_LOOP (rnd) + { + inex = mpfr_get_f (x, y, rnd); + + if (inex != 0 || mpfr_cmp_f (y, x) !=0) + { + printf ("Error in mpfr_get_f (x, y, %s)\nx = ", + mpfr_print_rnd_mode (rnd)); + mpf_out_str (stdout, 2, 0, x); + printf ("\ny = "); + mpfr_dump (y); + if (inex != 0) + printf ("got ternary value = %+d, expected: 0\n", inex); + + exit (1); + } + } + + /* y == 1 + epsilon */ + mpfr_nextbelow (y); + + RND_LOOP (rnd) + { + switch (rnd) + { + case MPFR_RNDU: case MPFR_RNDA: + case MPFR_RNDN: + expected_inex = +1; + break; + default : + expected_inex = -1; + } + + inex = mpfr_get_f (x, y, rnd); + + if (! SAME_SIGN (expected_inex, inex) + || SAME_SIGN (expected_inex, mpfr_cmp_f (y, x))) + { + printf ("Error in mpfr_get_f (x, y, %s)\nx = ", + mpfr_print_rnd_mode (rnd)); + mpf_out_str (stdout, 2, 0, x); + printf ("\ny = "); + mpfr_dump (y); + if (! SAME_SIGN (expected_inex, inex)) + printf ("got ternary value = %+d, expected: %+d\n", + inex, expected_inex); + + exit (1); + } + } + + /* y == positive random float */ + mpfr_random2 (y, MPFR_LIMB_SIZE (y), 1024, RANDS); + + RND_LOOP (rnd) + { + inex = mpfr_get_f (x, y, rnd); + + if (! SAME_SIGN (inex, -mpfr_cmp_f (y, x))) + { + printf ("Error in mpfr_get_f (x, y, %s)\nx = ", + mpfr_print_rnd_mode (rnd)); + mpf_out_str (stdout, 2, 0, x); + printf ("\ny = "); + mpfr_dump (y); + printf ("got ternary value = %+d, expected: %+d\n", + inex, -mpfr_cmp_f (y, x)); + + exit (1); + } + } } mpf_clear (x); @@ -349,7 +340,7 @@ main (void) mpfr_dump (y); printf ("x="); mpf_div_2exp (x, x, e); - mpf_dump (x); + mpf_out_str (stdout, 2, 0, x); exit (1); } @@ -365,7 +356,7 @@ main (void) mpfr_dump (y); printf ("x="); mpf_mul_2exp (x, x, e); - mpf_dump (x); + mpf_out_str (stdout, 2, 0, x); exit (1); } } -- cgit v1.2.1