real.c (mpfr_from_real): Handle Inf and NaN, and allow the rounding mode to be specif...
authorBrooks Moses <brooks.moses@codesourcery.com>
Wed, 25 Apr 2007 02:12:47 +0000 (02:12 +0000)
committerBrooks Moses <brooks@gcc.gnu.org>
Wed, 25 Apr 2007 02:12:47 +0000 (19:12 -0700)
* 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.

From-SVN: r124139

gcc/ChangeLog
gcc/builtins.c
gcc/real.c
gcc/real.h

index ba37f732c2a536f999bfb24a72830fa0b10c7322..cf05f74fb7267fd159058568962c7fd3c4c74a45 100644 (file)
@@ -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
index 498aea62e6ab9c86a7d13610a09cc45598a4f46a..2cf15797d95f96d7b7765f80cb8e96820ae390bd 100644 (file)
@@ -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);
index 2e288187b77c4ddae0139d71a902ac8381e29b1e..48d9e9e23fa3bd4728db466bd5a32c22199ffaf0 100644 (file)
@@ -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.  */
index eab53d7dadf6a3084ae138641823ca8b22c7bb12..6fae2254a8e6b261bffd6d341c070b8fbff8c36d 100644 (file)
@@ -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);