xref: /dragonfly/contrib/gcc-4.7/gcc/realmpfr.c (revision e4b17023)
1*e4b17023SJohn Marino /* Conversion routines from GCC internal float representation to MPFR.
2*e4b17023SJohn Marino    Copyright (C) 2010
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino    This file is part of GCC.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino    GCC is free software; you can redistribute it and/or modify it under
7*e4b17023SJohn Marino    the terms of the GNU General Public License as published by the Free
8*e4b17023SJohn Marino    Software Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino    version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino    WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*e4b17023SJohn Marino    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*e4b17023SJohn Marino    for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino    You should have received a copy of the GNU General Public License
17*e4b17023SJohn Marino    along with GCC; see the file COPYING3.  If not see
18*e4b17023SJohn Marino    <http://www.gnu.org/licenses/>.  */
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino #include "config.h"
21*e4b17023SJohn Marino #include "system.h"
22*e4b17023SJohn Marino #include "coretypes.h"
23*e4b17023SJohn Marino #include "realmpfr.h"
24*e4b17023SJohn Marino #include "tree.h"	/* For TYPE_MODE in real_from_mpfr.  */
25*e4b17023SJohn Marino 
26*e4b17023SJohn Marino /* Convert from REAL_VALUE_TYPE to MPFR.  The caller is responsible
27*e4b17023SJohn Marino    for initializing and clearing the MPFR parameter.  */
28*e4b17023SJohn Marino 
29*e4b17023SJohn Marino void
mpfr_from_real(mpfr_ptr m,const REAL_VALUE_TYPE * r,mp_rnd_t rndmode)30*e4b17023SJohn Marino mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
31*e4b17023SJohn Marino {
32*e4b17023SJohn Marino   /* We use a string as an intermediate type.  */
33*e4b17023SJohn Marino   char buf[128];
34*e4b17023SJohn Marino   int ret;
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino   /* Take care of Infinity and NaN.  */
37*e4b17023SJohn Marino   if (r->cl == rvc_inf)
38*e4b17023SJohn Marino     {
39*e4b17023SJohn Marino       mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
40*e4b17023SJohn Marino       return;
41*e4b17023SJohn Marino     }
42*e4b17023SJohn Marino 
43*e4b17023SJohn Marino   if (r->cl == rvc_nan)
44*e4b17023SJohn Marino     {
45*e4b17023SJohn Marino       mpfr_set_nan (m);
46*e4b17023SJohn Marino       return;
47*e4b17023SJohn Marino     }
48*e4b17023SJohn Marino 
49*e4b17023SJohn Marino   real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
50*e4b17023SJohn Marino   /* mpfr_set_str() parses hexadecimal floats from strings in the same
51*e4b17023SJohn Marino      format that GCC will output them.  Nothing extra is needed.  */
52*e4b17023SJohn Marino   ret = mpfr_set_str (m, buf, 16, rndmode);
53*e4b17023SJohn Marino   gcc_assert (ret == 0);
54*e4b17023SJohn Marino }
55*e4b17023SJohn Marino 
56*e4b17023SJohn Marino /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
57*e4b17023SJohn Marino    mode RNDMODE.  TYPE is only relevant if M is a NaN.  */
58*e4b17023SJohn Marino 
59*e4b17023SJohn Marino void
real_from_mpfr(REAL_VALUE_TYPE * r,mpfr_srcptr m,tree type,mp_rnd_t rndmode)60*e4b17023SJohn Marino real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
61*e4b17023SJohn Marino {
62*e4b17023SJohn Marino   /* We use a string as an intermediate type.  */
63*e4b17023SJohn Marino   char buf[128], *rstr;
64*e4b17023SJohn Marino   mp_exp_t exp;
65*e4b17023SJohn Marino 
66*e4b17023SJohn Marino   /* Take care of Infinity and NaN.  */
67*e4b17023SJohn Marino   if (mpfr_inf_p (m))
68*e4b17023SJohn Marino     {
69*e4b17023SJohn Marino       real_inf (r);
70*e4b17023SJohn Marino       if (mpfr_sgn (m) < 0)
71*e4b17023SJohn Marino 	*r = real_value_negate (r);
72*e4b17023SJohn Marino       return;
73*e4b17023SJohn Marino     }
74*e4b17023SJohn Marino 
75*e4b17023SJohn Marino   if (mpfr_nan_p (m))
76*e4b17023SJohn Marino     {
77*e4b17023SJohn Marino       real_nan (r, "", 1, TYPE_MODE (type));
78*e4b17023SJohn Marino       return;
79*e4b17023SJohn Marino     }
80*e4b17023SJohn Marino 
81*e4b17023SJohn Marino   rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino   /* The additional 12 chars add space for the sprintf below.  This
84*e4b17023SJohn Marino      leaves 6 digits for the exponent which is supposedly enough.  */
85*e4b17023SJohn Marino   gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
86*e4b17023SJohn Marino 
87*e4b17023SJohn Marino   /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
88*e4b17023SJohn Marino      mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
89*e4b17023SJohn Marino      for that.  */
90*e4b17023SJohn Marino   exp *= 4;
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino   if (rstr[0] == '-')
93*e4b17023SJohn Marino     sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
94*e4b17023SJohn Marino   else
95*e4b17023SJohn Marino     sprintf (buf, "0x.%sp%d", rstr, (int) exp);
96*e4b17023SJohn Marino 
97*e4b17023SJohn Marino   mpfr_free_str (rstr);
98*e4b17023SJohn Marino 
99*e4b17023SJohn Marino   real_from_string (r, buf);
100*e4b17023SJohn Marino }
101*e4b17023SJohn Marino 
102