summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2015-07-02 01:17:21 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2015-07-02 01:17:21 +0000
commitefa390ebecd6cbaa0315fe4062899acfe52a9314 (patch)
tree426ed59c5ca7da9331196939d83e94591e8a78fd
parent8c0dc57ed072a12ab727d10f3b1c347db8bc9a87 (diff)
downloadmpfr-efa390ebecd6cbaa0315fe4062899acfe52a9314.tar.gz
[src/{div_2si.c,div_2ui.c,mul_2si.c}] Fixed overflow case when n = 0.
[tests/tmul_2exp.c] Added overflow tests (triggering the above bugs). (merged changesets r9593,9595-9598 from the trunk, with mpfr_flags_t replaced by unsigned int) git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@9608 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--src/div_2si.c2
-rw-r--r--src/div_2ui.c2
-rw-r--r--src/mul_2si.c2
-rw-r--r--tests/tmul_2exp.c75
4 files changed, 78 insertions, 3 deletions
diff --git a/src/div_2si.c b/src/div_2si.c
index c7da6a058..0a86b79f2 100644
--- a/src/div_2si.c
+++ b/src/div_2si.c
@@ -49,7 +49,7 @@ mpfr_div_2si (mpfr_ptr y, mpfr_srcptr x, long int n, mpfr_rnd_t rnd_mode)
rnd_mode = MPFR_RNDZ;
return mpfr_underflow (y, rnd_mode, MPFR_SIGN(y));
}
- else if (MPFR_UNLIKELY(n < 0 && (__gmpfr_emax < MPFR_EMIN_MIN - n ||
+ else if (MPFR_UNLIKELY(n <= 0 && (__gmpfr_emax < MPFR_EMIN_MIN - n ||
exp > __gmpfr_emax + n)) )
return mpfr_overflow (y, rnd_mode, MPFR_SIGN(y));
diff --git a/src/div_2ui.c b/src/div_2ui.c
index 12df43bb7..3aa8b6ae8 100644
--- a/src/div_2ui.c
+++ b/src/div_2ui.c
@@ -32,7 +32,7 @@ mpfr_div_2ui (mpfr_ptr y, mpfr_srcptr x, unsigned long n, mpfr_rnd_t rnd_mode)
rnd_mode),
("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec(y), mpfr_log_prec, y, inexact));
- if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
+ if (MPFR_UNLIKELY (n == 0 || MPFR_IS_SINGULAR (x)))
return mpfr_set (y, x, rnd_mode);
else
{
diff --git a/src/mul_2si.c b/src/mul_2si.c
index b9293e2a1..0b02e2940 100644
--- a/src/mul_2si.c
+++ b/src/mul_2si.c
@@ -39,7 +39,7 @@ mpfr_mul_2si (mpfr_ptr y, mpfr_srcptr x, long int n, mpfr_rnd_t rnd_mode)
{
mpfr_exp_t exp = MPFR_GET_EXP (x);
MPFR_SETRAW (inexact, y, x, exp, rnd_mode);
- if (MPFR_UNLIKELY( n > 0 && (__gmpfr_emax < MPFR_EMIN_MIN + n ||
+ if (MPFR_UNLIKELY(n >= 0 && (__gmpfr_emax < MPFR_EMIN_MIN + n ||
exp > __gmpfr_emax - n)))
return mpfr_overflow (y, rnd_mode, MPFR_SIGN(y));
else if (MPFR_UNLIKELY(n < 0 && (__gmpfr_emin > MPFR_EMAX_MAX + n ||
diff --git a/tests/tmul_2exp.c b/tests/tmul_2exp.c
index 61ad34d82..5c5972bad 100644
--- a/tests/tmul_2exp.c
+++ b/tests/tmul_2exp.c
@@ -242,6 +242,76 @@ large0 (void)
large (MPFR_EMAX_MAX);
}
+/* Cases where the function overflows on n = 0 when rounding is like
+ away from zero. */
+static void
+overflow0 (mpfr_exp_t emax)
+{
+ mpfr_exp_t old_emax;
+ mpfr_t x, y1, y2;
+ int neg, r, op;
+ static char *sop[4] = { "mul_2ui", "mul_2si", "div_2ui", "div_2si" };
+
+ old_emax = mpfr_get_emax ();
+ set_emax (emax);
+
+ mpfr_init2 (x, 8);
+ mpfr_inits2 (6, y1, y2, (mpfr_ptr) 0);
+
+ mpfr_set_inf (x, 1);
+ mpfr_nextbelow (x);
+
+ for (neg = 0; neg <= 1; neg++)
+ {
+ RND_LOOP (r)
+ {
+ int inex1, inex2;
+ unsigned int flags1, flags2;
+
+ /* Even if there isn't an overflow (rounding ~ toward zero),
+ the result is the same as the one of an overflow. */
+ inex1 = mpfr_overflow (y1, (mpfr_rnd_t) r, neg ? -1 : 1);
+ flags1 = MPFR_FLAGS_INEXACT;
+ if (mpfr_inf_p (y1))
+ flags1 |= MPFR_FLAGS_OVERFLOW;
+ for (op = 0; op < 4; op++)
+ {
+ mpfr_clear_flags ();
+ inex2 =
+ op == 0 ? mpfr_mul_2ui (y2, x, 0, (mpfr_rnd_t) r) :
+ op == 1 ? mpfr_mul_2si (y2, x, 0, (mpfr_rnd_t) r) :
+ op == 2 ? mpfr_div_2ui (y2, x, 0, (mpfr_rnd_t) r) :
+ op == 3 ? mpfr_div_2si (y2, x, 0, (mpfr_rnd_t) r) :
+ (MPFR_ASSERTN (0), 0);
+ flags2 = __gmpfr_flags;
+ if (!(mpfr_equal_p (y1, y2) &&
+ SAME_SIGN (inex1, inex2) &&
+ flags1 == flags2))
+ {
+ printf ("Error in overflow0 for %s, mpfr_%s, emax = %"
+ MPFR_EXP_FSPEC "d,\nx = ",
+ mpfr_print_rnd_mode ((mpfr_rnd_t) r), sop[op],
+ (mpfr_eexp_t) emax);
+ mpfr_dump (x);
+ printf ("Expected ");
+ mpfr_dump (y1);
+ printf (" with inex = %d, flags =", inex1);
+ flags_out (flags1);
+ printf ("Got ");
+ mpfr_dump (y2);
+ printf (" with inex = %d, flags =", inex2);
+ flags_out (flags2);
+ exit (1);
+ }
+ }
+ }
+ mpfr_neg (x, x, MPFR_RNDN);
+ }
+
+ mpfr_clears (x, y1, y2, (mpfr_ptr) 0);
+ set_emax (old_emax);
+}
+
int
main (int argc, char *argv[])
{
@@ -334,6 +404,11 @@ main (int argc, char *argv[])
underflow0 ();
large0 ();
+ if (mpfr_get_emax () != MPFR_EMAX_MAX)
+ overflow0 (mpfr_get_emax ());
+ overflow0 (MPFR_EMAX_MAX);
+ overflow0 (-1);
+
tests_end_mpfr ();
return 0;
}