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