static void check_overflow (void) { mpfr_t sum1, sum2, x, y; mpfr_ptr t[2 * NOVFL]; mpfr_exp_t emin, emax; int i, r; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); mpfr_inits2 (32, sum1, sum2, x, y, (mpfr_ptr) 0); mpfr_setmax (x, mpfr_get_emax ()); mpfr_neg (y, x, MPFR_RNDN); for (i = 0; i < 2 * NOVFL; i++) t[i] = i < NOVFL ? x : y; /* Two kinds of test: * i = 1: overflow. * i = 2: intermediate overflow (exact sum is 0). */ for (i = 1; i <= 2; i++) RND_LOOP(r) { int inex1, inex2; inex1 = mpfr_add (sum1, x, i == 1 ? x : y, (mpfr_rnd_t) r); inex2 = mpfr_sum (sum2, t, i * NOVFL, (mpfr_rnd_t) r); MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check_overflow on %s, i = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), i); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } mpfr_clears (sum1, sum2, x, y, (mpfr_ptr) 0); set_emin (emin); set_emax (emax); }
static mpfr_prec_t get_prec_max (mpfr_t *tab, unsigned long n, mpfr_prec_t f) { mpfr_prec_t res; mpfr_exp_t min, max; unsigned long i; i = 0; while (MPFR_IS_ZERO (tab[i])) { i++; if (i == n) return MPFR_PREC_MIN; /* all values are 0 */ } if (! mpfr_check (tab[i])) { printf ("tab[%lu] is not valid.\n", i); exit (1); } MPFR_ASSERTN (MPFR_IS_FP (tab[i])); min = max = MPFR_GET_EXP(tab[i]); for (i++; i < n; i++) { if (! mpfr_check (tab[i])) { printf ("tab[%lu] is not valid.\n", i); exit (1); } MPFR_ASSERTN (MPFR_IS_FP (tab[i])); if (! MPFR_IS_ZERO (tab[i])) { if (MPFR_GET_EXP(tab[i]) > max) max = MPFR_GET_EXP(tab[i]); if (MPFR_GET_EXP(tab[i]) < min) min = MPFR_GET_EXP(tab[i]); } } res = max - min; res += f; res += __gmpfr_ceil_log2 (n) + 1; return res; }
static void test_extreme (void) { mpfr_t x, y, z; mpfr_exp_t emin, emax; mpfr_prec_t p[4] = { 8, 32, 64, 256 }; int xi, yi, zi, j, r; unsigned int flags, ex_flags; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_emax (MPFR_EMAX_MAX); for (xi = 0; xi < 4; xi++) { mpfr_init2 (x, p[xi]); mpfr_setmax (x, MPFR_EMAX_MAX); MPFR_ASSERTN (mpfr_check (x)); for (yi = 0; yi < 4; yi++) { mpfr_init2 (y, p[yi]); mpfr_setmin (y, MPFR_EMIN_MIN); for (j = 0; j < 2; j++) { MPFR_ASSERTN (mpfr_check (y)); for (zi = 0; zi < 4; zi++) { mpfr_init2 (z, p[zi]); RND_LOOP (r) { mpfr_clear_flags (); mpfr_div (z, x, y, (mpfr_rnd_t) r); flags = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (z)); ex_flags = MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("Bad flags in test_extreme on z = a/b" " with %s and\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); printf ("a = "); mpfr_dump (x); printf ("b = "); mpfr_dump (y); printf ("Expected flags:"); flags_out (ex_flags); printf ("Got flags: "); flags_out (flags); printf ("z = "); mpfr_dump (z); exit (1); } mpfr_clear_flags (); mpfr_div (z, y, x, (mpfr_rnd_t) r); flags = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (z)); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("Bad flags in test_extreme on z = a/b" " with %s and\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); printf ("a = "); mpfr_dump (y); printf ("b = "); mpfr_dump (x); printf ("Expected flags:"); flags_out (ex_flags); printf ("Got flags: "); flags_out (flags); printf ("z = "); mpfr_dump (z); exit (1); } } mpfr_clear (z); } /* zi */ mpfr_nextabove (y); } /* j */ mpfr_clear (y); } /* yi */ mpfr_clear (x); } /* xi */ set_emin (emin); set_emax (emax); }
int main (void) { mpfr_t x, u; mpf_t y, z; mpfr_exp_t emax; unsigned long k, pr; int r, inexact; tests_start_mpfr (); mpf_init (y); mpf_init (z); mpf_set_d (y, 0.0); /* check prototype of mpfr_init_set_f */ mpfr_init_set_f (x, y, MPFR_RNDN); mpfr_set_prec (x, 100); mpfr_set_f (x, y, MPFR_RNDN); mpf_urandomb (y, RANDS, 10 * GMP_NUMB_BITS); mpfr_set_f (x, y, RND_RAND ()); /* bug found by Jean-Pierre Merlet */ mpfr_set_prec (x, 256); mpf_set_prec (y, 256); mpfr_init2 (u, 256); mpfr_set_str (u, "7.f10872b020c49ba5e353f7ced916872b020c49ba5e353f7ced916872b020c498@2", 16, MPFR_RNDN); mpf_set_str (y, "2033033E-3", 10); /* avoid 2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=2033033E-3\n"); exit (1); } mpf_set_str (y, "-2033033E-3", 10); /* avoid -2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); mpfr_neg (u, u, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=-2033033E-3\n"); exit (1); } mpf_set_prec (y, 300); mpf_set_str (y, "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", -2); mpf_mul_2exp (y, y, 600); mpfr_set_prec (x, 300); mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_check (x) == 0) { printf ("Error in mpfr_set_f: corrupted result\n"); mpfr_dump (x); exit (1); } MPFR_ASSERTN(mpfr_cmp_ui_2exp (x, 1, 901) == 0); /* random values */ for (k = 1; k <= 1000; k++) { pr = 2 + (randlimb () & 255); mpf_set_prec (z, pr); mpf_urandomb (z, RANDS, z->_mp_prec); mpfr_set_prec (u, ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_set_f (u, z, MPFR_RNDN); if (mpfr_cmp_f (u , z) != 0) { printf ("Error in mpfr_set_f:\n"); printf ("mpf (precision=%lu)=", pr); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr(precision=%lu)=", ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_set_prec (x, pr); mpfr_set_f (x, z, MPFR_RNDN); mpfr_sub (u, u, x, MPFR_RNDN); mpfr_abs (u, u, MPFR_RNDN); if (mpfr_cmp_ui_2exp (u, 1, -pr - 1) > 0) { printf ("Error in mpfr_set_f: precision=%lu\n", pr); printf ("mpf ="); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr="); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } } /* Check for +0 */ mpfr_set_prec (x, 53); mpf_set_prec (y, 53); mpf_set_ui (y, 0); for (r = 0 ; r < MPFR_RND_MAX ; r++) { int i; for (i = -1; i <= 1; i++) { if (i) mpfr_set_si (x, i, MPFR_RNDN); inexact = mpfr_set_f (x, y, (mpfr_rnd_t) r); if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact) { printf ("mpfr_set_f(x,0) failed for %s, i = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), i); exit (1); } } } /* coverage test */ mpf_set_prec (y, 2); mpfr_set_prec (x, 3 * mp_bits_per_limb); mpf_set_ui (y, 1); for (r = 0; r < mp_bits_per_limb; r++) { mpfr_urandomb (x, RANDS); /* to fill low limbs with random data */ inexact = mpfr_set_f (x, y, MPFR_RNDN); MPFR_ASSERTN(inexact == 0 && mpfr_cmp_ui_2exp (x, 1, r) == 0); mpf_mul_2exp (y, y, 1); } mpf_set_ui (y, 1); mpf_mul_2exp (y, y, ULONG_MAX); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui (u, 1, MPFR_RNDN); mpfr_mul_2ui (u, u, ULONG_MAX, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^ULONG_MAX\n"); exit (1); } emax = mpfr_get_emax (); /* For mpf_mul_2exp, emax must fit in an unsigned long! */ if (emax >= 0 && emax <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^emax\n"); exit (1); } } /* For mpf_mul_2exp, emax - 1 must fit in an unsigned long! */ if (emax >= 1 && emax - 1 <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax - 1); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax - 1, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^(emax-1)\n"); exit (1); } } mpfr_clear (x); mpfr_clear (u); mpf_clear (y); mpf_clear (z); tests_end_mpfr (); return 0; }
int main (void) { mpfr_t a; mp_limb_t *p, tmp; mp_size_t s; mpfr_prec_t pr; int max; tests_start_mpfr (); for(pr = MPFR_PREC_MIN ; pr < 500 ; pr++) { mpfr_init2 (a, pr); if (!mpfr_check(a)) ERROR("for init"); /* Check special cases */ MPFR_SET_NAN(a); if (!mpfr_check(a)) ERROR("for nan"); MPFR_SET_POS(a); MPFR_SET_INF(a); if (!mpfr_check(a)) ERROR("for inf"); MPFR_SET_ZERO(a); if (!mpfr_check(a)) ERROR("for zero"); MPFR_EXP (a) = MPFR_EXP_MIN; if (mpfr_check(a)) ERROR("for EXP = MPFR_EXP_MIN"); /* Check var */ mpfr_set_ui(a, 2, MPFR_RNDN); if (!mpfr_check(a)) ERROR("for set_ui"); mpfr_clear_overflow(); max = 1000; /* Allows max 2^1000 bits for the exponent */ while ((!mpfr_overflow_p()) && (max>0)) { mpfr_mul(a, a, a, MPFR_RNDN); if (!mpfr_check(a)) ERROR("for mul"); max--; } if (max==0) ERROR("can't reach overflow"); mpfr_set_ui(a, 2137, MPFR_RNDN); /* Corrupt a and check for it */ MPFR_SIGN(a) = 2; if (mpfr_check(a)) ERROR("sgn"); MPFR_SET_POS(a); /* Check prec */ MPFR_PREC(a) = MPFR_PREC_MIN - 1; if (mpfr_check(a)) ERROR("precmin"); #if MPFR_VERSION_MAJOR < 3 /* Disable the test with MPFR >= 3 since mpfr_prec_t is now signed. The "if" below is sufficient, but the MPFR_PREC_MAX+1 generates a warning with GCC 4.4.4 even though the test is always false. */ if ((mpfr_prec_t) 0 - 1 > 0) { MPFR_PREC(a) = MPFR_PREC_MAX+1; if (mpfr_check(a)) ERROR("precmax"); } #endif MPFR_PREC(a) = pr; if (!mpfr_check(a)) ERROR("prec"); /* Check exponent */ MPFR_EXP(a) = MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("exp invalid"); MPFR_EXP(a) = -MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("-exp invalid"); MPFR_EXP(a) = 0; if (!mpfr_check(a)) ERROR("exp 0"); /* Check Mantissa */ p = MPFR_MANT(a); MPFR_MANT(a) = NULL; if (mpfr_check(a)) ERROR("Mantissa Null Ptr"); MPFR_MANT(a) = p; /* Check size */ s = MPFR_GET_ALLOC_SIZE(a); MPFR_SET_ALLOC_SIZE(a, 0); if (mpfr_check(a)) ERROR("0 size"); MPFR_SET_ALLOC_SIZE(a, MP_SIZE_T_MIN); if (mpfr_check(a)) ERROR("min size"); MPFR_SET_ALLOC_SIZE(a, MPFR_LIMB_SIZE(a)-1 ); if (mpfr_check(a)) ERROR("size < prec"); MPFR_SET_ALLOC_SIZE(a, s); /* Check normal form */ tmp = MPFR_MANT(a)[0]; if ((pr % GMP_NUMB_BITS) != 0) { MPFR_MANT(a)[0] = MPFR_LIMB_MAX; if (mpfr_check(a)) ERROR("last bits non 0"); } MPFR_MANT(a)[0] = tmp; MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (GMP_NUMB_BITS-1); if (mpfr_check(a)) ERROR("last bits non 0"); /* Final */ mpfr_set_ui(a, 2137, MPFR_RNDN); if (!mpfr_check(a)) ERROR("after last set"); mpfr_clear (a); if (mpfr_check(a)) ERROR("after clear"); } tests_end_mpfr (); return 0; }
/* Test of s * (q * 2^(n-1) - 2^k) + h + i * 2^(-2) + j * 2^(-2) * with h = -1 or 1, -1 <= i odd <= j <= 3, 2 <= q <= 3, s = -1 or 1, * prec n-k. * On a 64-bit machine: * MPFR_RNDN, tmd=2, rbit=0, sst=0, negative is checked with the inputs * -3*2^58, 2^5, -1, 2^(-2), 3*2^(-2) * MPFR_RNDN, tmd=2, rbit=0, sst=1, negative is checked with the inputs * -3*2^58, 2^5, -1, 3*2^(-2), 3*2^(-2) * * Note: This test detects an error in a result when "sq + 3" is replaced * by "sq + 2" (11th argument of the first sum_raw invocation) and the * corresponding assertion d >= 3 is removed, confirming that one cannot * decrease this proved error bound. */ static void check4 (void) { mpfr_t sum1, sum2, s1, s2, s3, s4, t[5]; mpfr_ptr p[5]; int h, i, j, k, n, q, r, s, prec, inex1, inex2; mpfr_inits2 (257, sum1, sum2, s1, s2, s3, s4, (mpfr_ptr) 0); for (i = 0; i < 5; i++) { mpfr_init2 (t[i], 2); p[i] = t[i]; } /* No GNU style for the many nested loops... */ for (k = 1; k <= 64; k++) { mpfr_set_si_2exp (t[0], -1, k, MPFR_RNDN); for (n = k + MPFR_PREC_MIN; n <= k + 65; n++) { prec = n - k; mpfr_set_prec (sum1, prec); mpfr_set_prec (sum2, prec); for (q = 2; q <= 3; q++) { mpfr_set_si_2exp (t[1], q, n - 1, MPFR_RNDN); inex1 = mpfr_add (s1, t[0], t[1], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (s = -1; s <= 1; s += 2) { mpfr_neg (t[0], t[0], MPFR_RNDN); mpfr_neg (t[1], t[1], MPFR_RNDN); mpfr_neg (s1, s1, MPFR_RNDN); for (h = -1; h <= 1; h += 2) { mpfr_set_si (t[2], h, MPFR_RNDN); inex1 = mpfr_add (s2, s1, t[2], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (i = -1; i <= 3; i += 2) { mpfr_set_si_2exp (t[3], i, -2, MPFR_RNDN); inex1 = mpfr_add (s3, s2, t[3], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (j = i; j <= 3; j++) { mpfr_set_si_2exp (t[4], j, -2, MPFR_RNDN); inex1 = mpfr_add (s4, s3, t[4], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); RND_LOOP (r) { inex1 = mpfr_set (sum1, s4, (mpfr_rnd_t) r); inex2 = mpfr_sum (sum2, p, 5, (mpfr_rnd_t) r); MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check4 on %s, " "k = %d, n = %d (prec %d), " "q = %d, s = %d, h = %d, i = %d, j = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), k, n, prec, q, s, h, i, j); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } } } } } } } for (i = 0; i < 5; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, s1, s2, s3, s4, (mpfr_ptr) 0); }
/* t[i] = (2^17 - 1) * 2^(17*(i-8)) for 0 <= i <= 16. * t[17] = 2^(17*9+1) * j for -4 <= j <= 4. * t[18] = 2^(-1) * k for -1 <= k <= 1. * t[19] = 2^(-17*8) * m for -3 <= m <= 3. * prec = MPFR_PREC_MIN and 17*9+4 */ static void check3 (void) { mpfr_t sum1, sum2, s1, s2, s3, s4, t[20]; mpfr_ptr p[20]; mpfr_flags_t flags1, flags2; int i, s, j, k, m, q, r, inex1, inex2; int prec[2] = { MPFR_PREC_MIN, 17*9+4 }; mpfr_init2 (s1, 17*17); mpfr_init2 (s2, 17*17+4); mpfr_init2 (s3, 17*17+4); mpfr_init2 (s4, 17*17+5); mpfr_set_ui (s1, 0, MPFR_RNDN); for (i = 0; i < 20; i++) { mpfr_init2 (t[i], 20); p[i] = t[i]; if (i < 17) { mpfr_set_ui_2exp (t[i], 0x1ffff, 17*(i-8), MPFR_RNDN); inex1 = mpfr_add (s1, s1, t[i], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); } } for (s = 1; s >= -1; s -= 2) { for (j = -4; j <= 4; j++) { mpfr_set_si_2exp (t[17], j, 17*9+1, MPFR_RNDN); inex1 = mpfr_add (s2, s1, t[17], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (k = -1; k <= 1; k++) { mpfr_set_si_2exp (t[18], k, -1, MPFR_RNDN); inex1 = mpfr_add (s3, s2, t[18], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (m = -3; m <= 3; m++) { mpfr_set_si_2exp (t[19], m, -17*8, MPFR_RNDN); inex1 = mpfr_add (s4, s3, t[19], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (q = 0; q < 2; q++) { mpfr_inits2 (prec[q], sum1, sum2, (mpfr_ptr) 0); RND_LOOP (r) { mpfr_clear_flags (); inex1 = mpfr_set (sum1, s4, (mpfr_rnd_t) r); flags1 = __gmpfr_flags; mpfr_clear_flags (); inex2 = mpfr_sum (sum2, p, 20, (mpfr_rnd_t) r); flags2 = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2) && flags1 == flags2)) { printf ("Error in check3 on %s, " "s = %d, j = %d, k = %d, m = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), s, j, k, m); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d and flags =", inex1); flags_out (flags1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d and flags =", inex2); flags_out (flags2); exit (1); } } mpfr_clears (sum1, sum2, (mpfr_ptr) 0); } /* q */ } /* m */ } /* k */ } /* j */ for (i = 0; i < 17; i++) mpfr_neg (t[i], t[i], MPFR_RNDN); mpfr_neg (s1, s1, MPFR_RNDN); } /* s */ for (i = 0; i < 20; i++) mpfr_clear (t[i]); mpfr_clears (s1, s2, s3, s4, (mpfr_ptr) 0); }
/* With N = 2 * GMP_NUMB_BITS: i * 2^N + j + k * 2^(-1) + f1 * 2^(-N) + f2 * 2^(-N), with i = -1 or 1, j = 0 or i, -1 <= k <= 1, -1 <= f1 <= 1, -1 <= f2 <= 1 ulp(exact sum) = 2^0. */ static void check2 (void) { mpfr_t sum1, sum2, s1, s2, s3, s4, t[5]; mpfr_ptr p[5]; int i, j, k, f1, f2, prec, r, inex1, inex2; #define N (2 * GMP_NUMB_BITS) mpfr_init2 (sum1, N+1); mpfr_init2 (sum2, N+1); mpfr_init2 (s1, N+1); mpfr_init2 (s2, N+2); mpfr_init2 (s3, 2*N+1); mpfr_init2 (s4, 2*N+1); for (i = 0; i < 5; i++) { mpfr_init2 (t[i], 2); p[i] = t[i]; } for (i = -1; i <= 1; i += 2) { mpfr_set_si_2exp (t[0], i, N, MPFR_RNDN); for (j = 0; j != 2*i; j += i) { mpfr_set_si (t[1], j, MPFR_RNDN); inex1 = mpfr_add (s1, t[0], t[1], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (k = -1; k <= 1; k++) { mpfr_set_si_2exp (t[2], k, -1, MPFR_RNDN); inex1 = mpfr_add (s2, s1, t[2], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (f1 = -1; f1 <= 1; f1++) { mpfr_set_si_2exp (t[3], f1, -N, MPFR_RNDN); inex1 = mpfr_add (s3, s2, t[3], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (f2 = -1; f2 <= 1; f2++) { mpfr_set_si_2exp (t[4], f2, -N, MPFR_RNDN); inex1 = mpfr_add (s4, s3, t[4], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); prec = mpfr_get_exp (s4); mpfr_set_prec (sum1, prec); mpfr_set_prec (sum2, prec); RND_LOOP (r) { inex1 = mpfr_set (sum1, s4, (mpfr_rnd_t) r); inex2 = mpfr_sum (sum2, p, 5, (mpfr_rnd_t) r); MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check2 on %s, prec = %d, " "i = %d, j = %d, k = %d, f1 = %d, " "f2 = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), prec, i, j, k, f1, f2); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } } } } } for (i = 0; i < 5; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, s1, s2, s3, s4, (mpfr_ptr) 0); }
/* i * 2^(46+h) + j * 2^(45+h) + k * 2^(44+h) + f * 2^(-2), with -1 <= i, j, k <= 1, i != 0, -3 <= f <= 3, and * prec set up so that ulp(exact sum) = 2^0, then * prec set up so that ulp(exact sum) = 2^(44+h) when possible, i.e. when prec >= MPFR_PREC_MIN. ------ Some explanations: ulp(exact sum) = 2^q means EXP(exact sum) - prec = q where prec is the precision of the output. Thus ulp(exact sum) = 2^0 is achieved by setting prec = EXP(s3), where s3 is the exact sum (computed with mpfr_add's and sufficient precision). Then ulp(exact sum) = 2^(44+h) is achieved by subtracting 44+h from prec. The loop on prec does this. Since EXP(s3) <= 47+h, prec <= 3 at the second iteration, thus there will be at most 2 iterations. Whether a second iteration is done or not depends on EXP(s3), i.e. the values of the parameters, and the value of MPFR_PREC_MIN. */ static void check1 (int h) { mpfr_t sum1, sum2, s1, s2, s3, t[4]; mpfr_ptr p[4]; int i, j, k, f, prec, r, inex1, inex2; mpfr_init2 (sum1, 47 + h); mpfr_init2 (sum2, 47 + h); mpfr_init2 (s1, 3); mpfr_init2 (s2, 3); mpfr_init2 (s3, 49 + h); for (i = 0; i < 4; i++) { mpfr_init2 (t[i], 2); p[i] = t[i]; } for (i = -1; i <= 1; i += 2) { mpfr_set_si_2exp (t[0], i, 46 + h, MPFR_RNDN); for (j = -1; j <= 1; j++) { mpfr_set_si_2exp (t[1], j, 45 + h, MPFR_RNDN); inex1 = mpfr_add (s1, t[0], t[1], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (k = -1; k <= 1; k++) { mpfr_set_si_2exp (t[2], k, 44 + h, MPFR_RNDN); inex1 = mpfr_add (s2, s1, t[2], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (f = -3; f <= 3; f++) { mpfr_set_si_2exp (t[3], f, -2, MPFR_RNDN); inex1 = mpfr_add (s3, s2, t[3], MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); for (prec = mpfr_get_exp (s3); prec >= MPFR_PREC_MIN; prec -= 44 + h) { mpfr_set_prec (sum1, prec); mpfr_set_prec (sum2, prec); RND_LOOP (r) { inex1 = mpfr_set (sum1, s3, (mpfr_rnd_t) r); inex2 = mpfr_sum (sum2, p, 4, (mpfr_rnd_t) r); MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check1 on %s, prec = %d, " "i = %d, j = %d, k = %d, f = %d, " "h = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), prec, i, j, k, f, h); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } } } } } for (i = 0; i < 4; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, s1, s2, s3, (mpfr_ptr) 0); }
static void check_underflow (void) { mpfr_t sum1, sum2, t[NUNFL]; mpfr_ptr p[NUNFL]; mpfr_prec_t precmax = 444; mpfr_exp_t emin, emax; unsigned int ex_flags, flags; int c, i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; mpfr_init2 (sum1, MPFR_PREC_MIN); mpfr_init2 (sum2, precmax); for (i = 0; i < NUNFL; i++) { mpfr_init2 (t[i], precmax); p[i] = t[i]; } for (c = 0; c < 8; c++) { mpfr_prec_t fprec; int n, neg, r; fprec = MPFR_PREC_MIN + (randlimb () % (precmax - MPFR_PREC_MIN + 1)); n = 3 + (randlimb () % (NUNFL - 2)); MPFR_ASSERTN (n <= NUNFL); mpfr_set_prec (sum2, (randlimb () & 1) ? MPFR_PREC_MIN : precmax); mpfr_set_prec (t[0], fprec + 64); mpfr_set_zero (t[0], 1); for (i = 1; i < n; i++) { int inex; mpfr_set_prec (t[i], MPFR_PREC_MIN + (randlimb () % (fprec - MPFR_PREC_MIN + 1))); do mpfr_urandomb (t[i], RANDS); while (MPFR_IS_ZERO (t[i])); mpfr_set_exp (t[i], MPFR_EMIN_MIN); inex = mpfr_sub (t[0], t[0], t[i], MPFR_RNDN); MPFR_ASSERTN (inex == 0); } neg = randlimb () & 1; if (neg) mpfr_nextbelow (t[0]); else mpfr_nextabove (t[0]); RND_LOOP(r) { int inex1, inex2; mpfr_set_zero (sum1, 1); if (neg) mpfr_nextbelow (sum1); else mpfr_nextabove (sum1); inex1 = mpfr_div_2ui (sum1, sum1, 2, (mpfr_rnd_t) r); mpfr_clear_flags (); inex2 = mpfr_sum (sum2, p, n, (mpfr_rnd_t) r); flags = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (flags != ex_flags) { printf ("Bad flags in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected flags:"); flags_out (ex_flags); printf ("Got flags: "); flags_out (flags); printf ("sum = "); mpfr_dump (sum2); exit (1); } if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } for (i = 0; i < NUNFL; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, (mpfr_ptr) 0); set_emin (emin); set_emax (emax); }
int main (void) { mpfr_t a; mp_limb_t *p, tmp; mp_size_t s; mpfr_prec_t pr; int max; tests_start_mpfr (); for(pr = MPFR_PREC_MIN ; pr < 500 ; pr++) { mpfr_init2 (a, pr); if (!mpfr_check(a)) ERROR("for init"); /* Check special cases */ MPFR_SET_NAN(a); if (!mpfr_check(a)) ERROR("for nan"); MPFR_SET_POS(a); MPFR_SET_INF(a); if (!mpfr_check(a)) ERROR("for inf"); MPFR_SET_ZERO(a); if (!mpfr_check(a)) ERROR("for zero"); /* Check var */ mpfr_set_ui(a, 2, GMP_RNDN); if (!mpfr_check(a)) ERROR("for set_ui"); mpfr_clear_overflow(); max = 1000; /* Allows max 2^1000 bits for the exponent */ while ((!mpfr_overflow_p()) && (max>0)) { mpfr_mul(a, a, a, GMP_RNDN); if (!mpfr_check(a)) ERROR("for mul"); max--; } if (max==0) ERROR("can't reach overflow"); mpfr_set_ui(a, 2137, GMP_RNDN); /* Corrupt a and check for it */ MPFR_SIGN(a) = 2; if (mpfr_check(a)) ERROR("sgn"); MPFR_SET_POS(a); /* Check prec */ MPFR_PREC(a) = 1; if (mpfr_check(a)) ERROR("precmin"); MPFR_PREC(a) = MPFR_PREC_MAX+1; if (mpfr_check(a)) ERROR("precmax"); MPFR_PREC(a) = pr; if (!mpfr_check(a)) ERROR("prec"); /* Check exponent */ MPFR_EXP(a) = MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("exp invalid"); MPFR_EXP(a) = -MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("-exp invalid"); MPFR_EXP(a) = 0; if (!mpfr_check(a)) ERROR("exp 0"); /* Check Mantissa */ p = MPFR_MANT(a); MPFR_MANT(a) = NULL; if (mpfr_check(a)) ERROR("Mantissa Null Ptr"); MPFR_MANT(a) = p; /* Check size */ s = MPFR_GET_ALLOC_SIZE(a); MPFR_SET_ALLOC_SIZE(a, 0); if (mpfr_check(a)) ERROR("0 size"); MPFR_SET_ALLOC_SIZE(a, MP_SIZE_T_MIN); if (mpfr_check(a)) ERROR("min size"); MPFR_SET_ALLOC_SIZE(a, MPFR_LIMB_SIZE(a)-1 ); if (mpfr_check(a)) ERROR("size < prec"); MPFR_SET_ALLOC_SIZE(a, s); /* Check normal form */ tmp = MPFR_MANT(a)[0]; if ((pr % BITS_PER_MP_LIMB) != 0) { MPFR_MANT(a)[0] = ~0; if (mpfr_check(a)) ERROR("last bits non 0"); } MPFR_MANT(a)[0] = tmp; MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (BITS_PER_MP_LIMB-1); if (mpfr_check(a)) ERROR("last bits non 0"); /* Final */ mpfr_set_ui(a, 2137, GMP_RNDN); if (!mpfr_check(a)) ERROR("after last set"); mpfr_clear (a); if (mpfr_check(a)) ERROR("after clear"); } tests_end_mpfr (); return 0; }