summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/ChangeLog9
-rw-r--r--gcc/builtins.c16
-rw-r--r--gcc/real.c39
-rw-r--r--gcc/real.h4
4 files changed, 53 insertions, 15 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index ba37f732c2a..cf05f74fb72 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2007-04-24 Brooks Moses <brooks.moses@codesourcery.com>
+
+ * real.c (mpfr_from_real): Handle Inf and NaN, and allow the
+ rounding mode to be specified by the caller.
+ (real_to_mpfr) Likewise.
+ * real.h: Update mpfr_from_real, mpfr_to_real prototypes to
+ include new arguments.
+ * builtins.c: Update mpfr_from_real, mpfr_to_real calls.
+
2007-04-24 Ian Lance Taylor <iant@google.com>
PR tree-optimization/31605
diff --git a/gcc/builtins.c b/gcc/builtins.c
index 498aea62e6a..2cf15797d95 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -12205,7 +12205,7 @@ do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
{
REAL_VALUE_TYPE rr;
- real_from_mpfr (&rr, m);
+ real_from_mpfr (&rr, m, type, GMP_RNDN);
/* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
check for overflow/underflow. If the REAL_VALUE_TYPE is zero
but the mpft_t is not, then we underflowed in the
@@ -12258,7 +12258,7 @@ do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
mpfr_t m;
mpfr_init2 (m, prec);
- mpfr_from_real (m, ra);
+ mpfr_from_real (m, ra, GMP_RNDN);
mpfr_clear_flags ();
inexact = func (m, m, GMP_RNDN);
result = do_mpfr_ckconv (m, type, inexact);
@@ -12301,8 +12301,8 @@ do_mpfr_arg2 (tree arg1, tree arg2, tree type,
mpfr_t m1, m2;
mpfr_inits2 (prec, m1, m2, NULL);
- mpfr_from_real (m1, ra1);
- mpfr_from_real (m2, ra2);
+ mpfr_from_real (m1, ra1, GMP_RNDN);
+ mpfr_from_real (m2, ra2, GMP_RNDN);
mpfr_clear_flags ();
inexact = func (m1, m1, m2, GMP_RNDN);
result = do_mpfr_ckconv (m1, type, inexact);
@@ -12349,9 +12349,9 @@ do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
mpfr_t m1, m2, m3;
mpfr_inits2 (prec, m1, m2, m3, NULL);
- mpfr_from_real (m1, ra1);
- mpfr_from_real (m2, ra2);
- mpfr_from_real (m3, ra3);
+ mpfr_from_real (m1, ra1, GMP_RNDN);
+ mpfr_from_real (m2, ra2, GMP_RNDN);
+ mpfr_from_real (m3, ra3, GMP_RNDN);
mpfr_clear_flags ();
inexact = func (m1, m1, m2, m3, GMP_RNDN);
result = do_mpfr_ckconv (m1, type, inexact);
@@ -12393,7 +12393,7 @@ do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
mpfr_t m, ms, mc;
mpfr_inits2 (prec, m, ms, mc, NULL);
- mpfr_from_real (m, ra);
+ mpfr_from_real (m, ra, GMP_RNDN);
mpfr_clear_flags ();
inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
result_s = do_mpfr_ckconv (ms, type, inexact);
diff --git a/gcc/real.c b/gcc/real.c
index 2e288187b77..48d9e9e23fa 100644
--- a/gcc/real.c
+++ b/gcc/real.c
@@ -4991,29 +4991,58 @@ real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
for initializing and clearing the MPFR parameter. */
void
-mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r)
+mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
{
/* We use a string as an intermediate type. */
char buf[128];
int ret;
+ /* Take care of Infinity and NaN. */
+ if (r->cl == rvc_inf)
+ {
+ mpfr_set_inf (m, r->sign);
+ return;
+ }
+
+ if (r->cl == rvc_nan)
+ {
+ mpfr_set_nan (m);
+ return;
+ }
+
real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
/* mpfr_set_str() parses hexadecimal floats from strings in the same
format that GCC will output them. Nothing extra is needed. */
- ret = mpfr_set_str (m, buf, 16, GMP_RNDN);
+ ret = mpfr_set_str (m, buf, 16, rndmode);
gcc_assert (ret == 0);
}
-/* Convert from MPFR to REAL_VALUE_TYPE. */
+/* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
+ mode RNDMODE. TYPE is only relevant if M is a NaN. */
void
-real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m)
+real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
{
/* We use a string as an intermediate type. */
char buf[128], *rstr;
mp_exp_t exp;
- rstr = mpfr_get_str (NULL, &exp, 16, 0, m, GMP_RNDN);
+ /* Take care of Infinity and NaN. */
+ if (mpfr_inf_p (m))
+ {
+ real_inf (r);
+ if (mpfr_sgn (m) < 0)
+ *r = REAL_VALUE_NEGATE (*r);
+ return;
+ }
+
+ if (mpfr_nan_p (m))
+ {
+ real_nan (r, "", 1, TYPE_MODE (type));
+ return;
+ }
+
+ rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
/* The additional 12 chars add space for the sprintf below. This
leaves 6 digits for the exponent which is supposedly enough. */
diff --git a/gcc/real.h b/gcc/real.h
index eab53d7dadf..6fae2254a8e 100644
--- a/gcc/real.h
+++ b/gcc/real.h
@@ -434,8 +434,8 @@ extern void real_copysign (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
/* Convert between MPFR and REAL_VALUE_TYPE. The caller is
responsible for initializing and clearing the MPFR parameter. */
-extern void real_from_mpfr (REAL_VALUE_TYPE *, mpfr_srcptr);
-extern void mpfr_from_real (mpfr_ptr, const REAL_VALUE_TYPE *);
+extern void real_from_mpfr (REAL_VALUE_TYPE *, mpfr_srcptr, tree, mp_rnd_t);
+extern void mpfr_from_real (mpfr_ptr, const REAL_VALUE_TYPE *, mp_rnd_t);
/* Check whether the real constant value given is an integer. */
extern bool real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode);