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
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
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/>. */
22 #include "coretypes.h"
27 #include "stor-layout.h"
29 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
30 for initializing and clearing the MPFR parameter. */
33 mpfr_from_real (mpfr_ptr m
, const REAL_VALUE_TYPE
*r
, mp_rnd_t rndmode
)
35 /* We use a string as an intermediate type. */
39 /* Take care of Infinity and NaN. */
42 mpfr_set_inf (m
, r
->sign
== 1 ? -1 : 1);
52 real_to_hexadecimal (buf
, r
, sizeof (buf
), 0, 1);
53 /* mpfr_set_str() parses hexadecimal floats from strings in the same
54 format that GCC will output them. Nothing extra is needed. */
55 ret
= mpfr_set_str (m
, buf
, 16, rndmode
);
56 gcc_assert (ret
== 0);
59 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
60 mode RNDMODE. TYPE is only relevant if M is a NaN. */
63 real_from_mpfr (REAL_VALUE_TYPE
*r
, mpfr_srcptr m
, tree type
, mp_rnd_t rndmode
)
65 /* We use a string as an intermediate type. */
69 /* Take care of Infinity and NaN. */
74 *r
= real_value_negate (r
);
80 real_nan (r
, "", 1, TYPE_MODE (type
));
84 rstr
= mpfr_get_str (NULL
, &exp
, 16, 0, m
, rndmode
);
86 /* The additional 12 chars add space for the sprintf below. This
87 leaves 6 digits for the exponent which is supposedly enough. */
88 gcc_assert (rstr
!= NULL
&& strlen (rstr
) < sizeof (buf
) - 12);
90 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
91 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
96 sprintf (buf
, "-0x.%sp%d", &rstr
[1], (int) exp
);
98 sprintf (buf
, "0x.%sp%d", rstr
, (int) exp
);
100 mpfr_free_str (rstr
);
102 real_from_string (r
, buf
);