coretypes.h: Include machmode.h...
[gcc.git] / gcc / realmpfr.c
1 /* Conversion routines from GCC internal float representation to MPFR.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "realmpfr.h"
24 #include "hash-set.h"
25 #include "vec.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "inchash.h"
31 #include "tree.h" /* For TYPE_MODE in real_from_mpfr. */
32 #include "stor-layout.h"
33
34 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
35 for initializing and clearing the MPFR parameter. */
36
37 void
38 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
39 {
40 /* We use a string as an intermediate type. */
41 char buf[128];
42 int ret;
43
44 /* Take care of Infinity and NaN. */
45 if (r->cl == rvc_inf)
46 {
47 mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
48 return;
49 }
50
51 if (r->cl == rvc_nan)
52 {
53 mpfr_set_nan (m);
54 return;
55 }
56
57 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
58 /* mpfr_set_str() parses hexadecimal floats from strings in the same
59 format that GCC will output them. Nothing extra is needed. */
60 ret = mpfr_set_str (m, buf, 16, rndmode);
61 gcc_assert (ret == 0);
62 }
63
64 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
65 mode RNDMODE. TYPE is only relevant if M is a NaN. */
66
67 void
68 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
69 {
70 /* We use a string as an intermediate type. */
71 char buf[128], *rstr;
72 mp_exp_t exp;
73
74 /* Take care of Infinity and NaN. */
75 if (mpfr_inf_p (m))
76 {
77 real_inf (r);
78 if (mpfr_sgn (m) < 0)
79 *r = real_value_negate (r);
80 return;
81 }
82
83 if (mpfr_nan_p (m))
84 {
85 real_nan (r, "", 1, TYPE_MODE (type));
86 return;
87 }
88
89 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
90
91 /* The additional 12 chars add space for the sprintf below. This
92 leaves 6 digits for the exponent which is supposedly enough. */
93 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
94
95 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
96 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
97 for that. */
98 exp *= 4;
99
100 if (rstr[0] == '-')
101 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
102 else
103 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
104
105 mpfr_free_str (rstr);
106
107 real_from_string (r, buf);
108 }
109