OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [gcc/] [realmpfr.c] - Diff between revs 684 and 783

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 684 Rev 783
/* Conversion routines from GCC internal float representation to MPFR.
/* Conversion routines from GCC internal float representation to MPFR.
   Copyright (C) 2010
   Copyright (C) 2010
 
 
   This file is part of GCC.
   This file is part of GCC.
 
 
   GCC is free software; you can redistribute it and/or modify it under
   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   Software Foundation; either version 3, or (at your option) any later
   version.
   version.
 
 
   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.
   for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */
   <http://www.gnu.org/licenses/>.  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "realmpfr.h"
#include "realmpfr.h"
#include "tree.h"       /* For TYPE_MODE in real_from_mpfr.  */
#include "tree.h"       /* For TYPE_MODE in real_from_mpfr.  */
 
 
/* Convert from REAL_VALUE_TYPE to MPFR.  The caller is responsible
/* Convert from REAL_VALUE_TYPE to MPFR.  The caller is responsible
   for initializing and clearing the MPFR parameter.  */
   for initializing and clearing the MPFR parameter.  */
 
 
void
void
mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
{
{
  /* We use a string as an intermediate type.  */
  /* We use a string as an intermediate type.  */
  char buf[128];
  char buf[128];
  int ret;
  int ret;
 
 
  /* Take care of Infinity and NaN.  */
  /* Take care of Infinity and NaN.  */
  if (r->cl == rvc_inf)
  if (r->cl == rvc_inf)
    {
    {
      mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
      mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
      return;
      return;
    }
    }
 
 
  if (r->cl == rvc_nan)
  if (r->cl == rvc_nan)
    {
    {
      mpfr_set_nan (m);
      mpfr_set_nan (m);
      return;
      return;
    }
    }
 
 
  real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
  real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
  /* mpfr_set_str() parses hexadecimal floats from strings in the same
  /* mpfr_set_str() parses hexadecimal floats from strings in the same
     format that GCC will output them.  Nothing extra is needed.  */
     format that GCC will output them.  Nothing extra is needed.  */
  ret = mpfr_set_str (m, buf, 16, rndmode);
  ret = mpfr_set_str (m, buf, 16, rndmode);
  gcc_assert (ret == 0);
  gcc_assert (ret == 0);
}
}
 
 
/* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
/* 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.  */
   mode RNDMODE.  TYPE is only relevant if M is a NaN.  */
 
 
void
void
real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
{
{
  /* We use a string as an intermediate type.  */
  /* We use a string as an intermediate type.  */
  char buf[128], *rstr;
  char buf[128], *rstr;
  mp_exp_t exp;
  mp_exp_t exp;
 
 
  /* Take care of Infinity and NaN.  */
  /* Take care of Infinity and NaN.  */
  if (mpfr_inf_p (m))
  if (mpfr_inf_p (m))
    {
    {
      real_inf (r);
      real_inf (r);
      if (mpfr_sgn (m) < 0)
      if (mpfr_sgn (m) < 0)
        *r = real_value_negate (r);
        *r = real_value_negate (r);
      return;
      return;
    }
    }
 
 
  if (mpfr_nan_p (m))
  if (mpfr_nan_p (m))
    {
    {
      real_nan (r, "", 1, TYPE_MODE (type));
      real_nan (r, "", 1, TYPE_MODE (type));
      return;
      return;
    }
    }
 
 
  rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
  rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
 
 
  /* The additional 12 chars add space for the sprintf below.  This
  /* The additional 12 chars add space for the sprintf below.  This
     leaves 6 digits for the exponent which is supposedly enough.  */
     leaves 6 digits for the exponent which is supposedly enough.  */
  gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
  gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
 
 
  /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
  /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
     mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
     mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
     for that.  */
     for that.  */
  exp *= 4;
  exp *= 4;
 
 
  if (rstr[0] == '-')
  if (rstr[0] == '-')
    sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
    sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
  else
  else
    sprintf (buf, "0x.%sp%d", rstr, (int) exp);
    sprintf (buf, "0x.%sp%d", rstr, (int) exp);
 
 
  mpfr_free_str (rstr);
  mpfr_free_str (rstr);
 
 
  real_from_string (r, buf);
  real_from_string (r, buf);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.