static void check_intmax (void) { #ifdef _MPFR_H_HAVE_INTMAX_T mpfr_t x; mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT); /* Check NAN */ mpfr_set_nan(x); if (mpfr_fits_uintmax_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_intmax_p(x, GMP_RNDN)) ERROR1; /* Check INF */ mpfr_set_inf(x, 1); if (mpfr_fits_uintmax_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_intmax_p(x, GMP_RNDN)) ERROR1; /* Check Zero */ MPFR_SET_ZERO(x); if (!mpfr_fits_uintmax_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_intmax_p(x, GMP_RNDN)) ERROR2; /* Check small op */ mpfr_set_str1 (x, "1@-1"); if (!mpfr_fits_uintmax_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_intmax_p(x, GMP_RNDN)) ERROR2; /* Check all other values */ mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN); mpfr_add_ui (x, x, 1, GMP_RNDN); if (mpfr_fits_uintmax_p (x, GMP_RNDN)) ERROR1; mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN); if (!mpfr_fits_uintmax_p (x, GMP_RNDN)) ERROR2; mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN); mpfr_add_ui (x, x, 1, GMP_RNDN); if (mpfr_fits_intmax_p (x, GMP_RNDN)) ERROR1; mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN); if (!mpfr_fits_intmax_p (x, GMP_RNDN)) ERROR2; mpfr_set_sj (x, INTMAX_MIN, GMP_RNDN); if (!mpfr_fits_intmax_p (x, GMP_RNDN)) ERROR2; mpfr_sub_ui (x, x, 1, GMP_RNDN); if (mpfr_fits_intmax_p (x, GMP_RNDN)) ERROR1; mpfr_clear (x); #endif }
static void check_set_uj (mpfr_prec_t pmin, mpfr_prec_t pmax, int N) { mpfr_t x, y; mpfr_prec_t p; int inex1, inex2, n; mp_limb_t limb; mpfr_inits2 (pmax, x, y, (mpfr_ptr) 0); for ( p = pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); mpfr_set_prec (y, p); for (n = 0 ; n < N ; n++) { /* mp_limb_t may be unsigned long long */ limb = (unsigned long) randlimb (); inex1 = mpfr_set_uj (x, limb, MPFR_RNDN); inex2 = mpfr_set_ui (y, limb, MPFR_RNDN); if (mpfr_cmp (x, y)) { printf ("ERROR for mpfr_set_uj and j=%lu and p=%lu\n", (unsigned long) limb, (unsigned long) p); printf ("X="); mpfr_dump (x); printf ("Y="); mpfr_dump (y); exit (1); } if (inexact_sign (inex1) != inexact_sign (inex2)) { printf ("ERROR for inexact(set_uj): j=%lu p=%lu\n" "Inexact1= %d Inexact2= %d\n", (unsigned long) limb, (unsigned long) p, inex1, inex2); exit (1); } } } /* Special case */ mpfr_set_prec (x, sizeof(uintmax_t)*CHAR_BIT); inex1 = mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); if (inex1 != 0 || mpfr_sgn(x) <= 0) ERROR ("inexact / UINTMAX_MAX"); inex1 = mpfr_add_ui (x, x, 1, MPFR_RNDN); if (inex1 != 0 || !mpfr_powerof2_raw (x) || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) ) ERROR ("power of 2"); mpfr_set_uj (x, 0, MPFR_RNDN); if (!MPFR_IS_ZERO (x)) ERROR ("Setting 0"); mpfr_clears (x, y, (mpfr_ptr) 0); }
static __inline__ void decrease_bound_ui(mpfr_t b,mpfr_prec_t pr,mp_limb_t d) { mpfr_t dF; mpfr_init2(dF, FLINT_BITS); mpfr_t log2_d; mpfr_init2(log2_d,pr); mpfr_set_uj(dF, d, MPFR_RNDZ); mpfr_log2(log2_d, dF, MPFR_RNDZ); mpfr_sub(b, b, log2_d, MPFR_RNDU); mpfr_clear(log2_d); mpfr_clear(dF); }
/* We can't use fits_u.h <= mpfr_cmp_ui */ int mpfr_fits_uintmax_p (mpfr_srcptr f, mp_rnd_t rnd) { mp_exp_t exp; mp_prec_t prec; uintmax_t s; mpfr_t x, y; int res; if (MPFR_IS_NAN(f) || MPFR_IS_INF(f) || MPFR_SIGN(f) < 0) return 0; /* does not fit */ if (MPFR_IS_ZERO(f)) return 1; /* zero always fits */ /* now it fits if (a) f <= MAXIMUM (b) round(f, prec(slong), rnd) <= MAXIMUM */ exp = MPFR_EXP(f); if (exp < 1) return 1; /* |f| < 1: always fits */ /* first compute prec(MAXIMUM) */ for (s = UINTMAX_MAX, prec = 0; s != 0; s /= 2, prec ++); /* MAXIMUM needs prec bits, i.e. 2^(prec-1) <= |MAXIMUM| < 2^prec */ /* if exp < prec - 1, then f < 2^(prec-1) < |MAXIMUM| */ if ((mpfr_prec_t) exp < prec - 1) return 1; /* if exp > prec + 1, then f >= 2^prec > MAXIMUM */ if ((mpfr_prec_t) exp > prec + 1) return 0; /* remains cases exp = prec-1 to prec+1 */ /* hard case: first round to prec bits, then check */ mpfr_init2 (x, prec); mpfr_init2 (y, prec); mpfr_set (x, f, rnd); res = mpfr_set_uj (y, UINTMAX_MAX, GMP_RNDN); MPFR_ASSERTD (res == 0); res = mpfr_cmp (x, y) <= 0; mpfr_clear (y); mpfr_clear (x); return res; }
static __inline__ int comp_bound_ui(mpfr_t b,mp_limb_t q) // 1 iff log2(q) rounded down <= b { // TODO: use MPFR_DECL_INIT instead of mpfr_init2 mpfr_t log2; mpfr_init(log2); mpfr_t qF; mpfr_init2(qF, FLINT_BITS); mpfr_set_uj(qF, q, MPFR_RNDZ); mpfr_log2(log2, qF, MPFR_RNDZ); int r=(mpfr_cmp(log2,b)<0); mpfr_clear(qF); mpfr_clear(log2); return r; }
static void check_erange (void) { mpfr_t x; uintmax_t dl; intmax_t d; /* Test for ERANGE flag + correct behaviour if overflow */ mpfr_init2 (x, 256); mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_uj (x, MPFR_RNDN); if (dl != MPFR_UINTMAX_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_uj + ERANGE + UINTMAX_MAX (1)\n"); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); dl = mpfr_get_uj (x, MPFR_RNDN); if (dl != MPFR_UINTMAX_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_uj + ERANGE + UINTMAX_MAX (2)\n"); exit (1); } mpfr_set_sj (x, -1, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_uj (x, MPFR_RNDN); if (dl != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_uj + ERANGE + -1 \n"); exit (1); } mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_sj (x, MPFR_RNDN); if (d != MPFR_INTMAX_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_sj + ERANGE + INTMAX_MAX (1)\n"); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_sj (x, MPFR_RNDN); if (d != MPFR_INTMAX_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_sj + ERANGE + INTMAX_MAX (2)\n"); exit (1); } mpfr_set_sj (x, MPFR_INTMAX_MIN, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_sj (x, MPFR_RNDN); if (d != MPFR_INTMAX_MIN || mpfr_erangeflag_p ()) { printf ("ERROR for get_sj + ERANGE + INTMAX_MIN (1)\n"); exit (1); } mpfr_sub_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_sj (x, MPFR_RNDN); if (d != MPFR_INTMAX_MIN || !mpfr_erangeflag_p ()) { printf ("ERROR for get_sj + ERANGE + INTMAX_MIN (2)\n"); exit (1); } mpfr_set_nan (x); mpfr_clear_erangeflag (); d = mpfr_get_uj (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_uj + NaN\n"); exit (1); } mpfr_clear_erangeflag (); d = mpfr_get_sj (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_sj + NaN\n"); exit (1); } mpfr_clear (x); }
static void check_intmax (void) { #ifdef _MPFR_H_HAVE_INTMAX_T mpfr_t x, y; int i, r; mpfr_init2 (x, sizeof (uintmax_t) * CHAR_BIT); mpfr_init2 (y, 8); RND_LOOP (r) { /* Check NAN */ mpfr_set_nan (x); if (mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (52); if (mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (53); /* Check INF */ mpfr_set_inf (x, 1); if (mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (54); if (mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (55); /* Check Zero */ MPFR_SET_ZERO (x); if (!mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (56); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (57); /* Check positive small op */ mpfr_set_str1 (x, "1@-1"); if (!mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (58); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (59); /* Check 17 */ mpfr_set_ui (x, 17, MPFR_RNDN); if (!mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (60); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (61); /* Check hugest */ mpfr_set_ui_2exp (x, 42, sizeof (uintmax_t) * 32, MPFR_RNDN); if (mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (62); if (mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (63); /* Check all other values */ mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); mpfr_add_ui (x, x, 1, MPFR_RNDN); if (mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (64); mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); if (!mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r)) ERROR1 (65); mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN); mpfr_add_ui (x, x, 1, MPFR_RNDN); if (mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (66); mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (67); mpfr_set_sj (x, MPFR_INTMAX_MIN, MPFR_RNDN); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (68); mpfr_sub_ui (x, x, 1, MPFR_RNDN); if (mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (69); /* Check negative op */ for (i = 1; i <= 4; i++) { int inv; mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN); mpfr_rint (y, x, (mpfr_rnd_t) r); inv = MPFR_NOTZERO (y); if (!mpfr_fits_uintmax_p (x, (mpfr_rnd_t) r) ^ inv) ERROR1 (70); if (!mpfr_fits_intmax_p (x, (mpfr_rnd_t) r)) ERROR1 (71); } } mpfr_clear (x); mpfr_clear (y); #endif }