beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / get_flt.c
blob0fb66c32d0ce07240af58f062e7cafd1cac33afe
1 /* mpfr_get_flt -- convert a mpfr_t to a machine single precision float
3 Copyright 2009-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <float.h> /* for FLT_MIN */
25 #define MPFR_NEED_LONGLONG_H
26 #include "mpfr-impl.h"
28 #include "ieee_floats.h"
30 #define FLT_NEG_ZERO ((float) DBL_NEG_ZERO)
31 #define MPFR_FLT_INFM ((float) MPFR_DBL_INFM)
32 #define MPFR_FLT_INFP ((float) MPFR_DBL_INFP)
34 float
35 mpfr_get_flt (mpfr_srcptr src, mpfr_rnd_t rnd_mode)
37 int negative;
38 mpfr_exp_t e;
39 float d;
41 /* in case of NaN, +Inf, -Inf, +0, -0, the conversion from double to float
42 is exact */
43 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
44 return (float) mpfr_get_d (src, rnd_mode);
46 e = MPFR_GET_EXP (src);
47 negative = MPFR_IS_NEG (src);
49 if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDA))
50 rnd_mode = negative ? MPFR_RNDD : MPFR_RNDU;
52 /* the smallest positive normal float number is 2^(-126) = 0.5*2^(-125),
53 and the smallest positive subnormal number is 2^(-149) = 0.5*2^(-148) */
54 if (MPFR_UNLIKELY (e < -148))
56 /* |src| < 2^(-149), i.e., |src| is smaller than the smallest positive
57 subnormal number.
58 In round-to-nearest mode, 2^(-150) is rounded to zero.
60 d = negative ?
61 (rnd_mode == MPFR_RNDD ||
62 (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, -1, -150) < 0)
63 ? -FLT_MIN : FLT_NEG_ZERO) :
64 (rnd_mode == MPFR_RNDU ||
65 (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, 1, -150) > 0)
66 ? FLT_MIN : 0.0);
67 if (d != 0.0) /* we multiply FLT_MIN = 2^(-126) by FLT_EPSILON = 2^(-23)
68 to get +-2^(-149) */
69 d *= FLT_EPSILON;
71 /* the largest normal number is 2^128*(1-2^(-24)) = 0.111...111e128 */
72 else if (MPFR_UNLIKELY (e > 128))
74 d = negative ?
75 (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDU ?
76 -FLT_MAX : MPFR_FLT_INFM) :
77 (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDD ?
78 FLT_MAX : MPFR_FLT_INFP);
80 else /* -148 <= e <= 127 */
82 int nbits;
83 mp_size_t np, i;
84 mp_limb_t tp[MPFR_LIMBS_PER_FLT];
85 int carry;
86 double dd;
88 nbits = IEEE_FLT_MANT_DIG; /* 24 */
89 if (MPFR_UNLIKELY (e < -125))
90 /*In the subnormal case, compute the exact number of significant bits*/
92 nbits += (125 + e);
93 MPFR_ASSERTD (nbits >= 1);
95 np = MPFR_PREC2LIMBS (nbits);
96 MPFR_ASSERTD(np <= MPFR_LIMBS_PER_FLT);
97 carry = mpfr_round_raw_4 (tp, MPFR_MANT(src), MPFR_PREC(src), negative,
98 nbits, rnd_mode);
99 /* we perform the reconstruction using the 'double' type here,
100 knowing the result is exactly representable as 'float' */
101 if (MPFR_UNLIKELY(carry))
102 dd = 1.0;
103 else
105 /* The following computations are exact thanks to the previous
106 mpfr_round_raw. */
107 dd = (double) tp[0] / MP_BASE_AS_DOUBLE;
108 for (i = 1 ; i < np ; i++)
109 dd = (dd + tp[i]) / MP_BASE_AS_DOUBLE;
110 /* dd is the mantissa (between 1/2 and 1) of the argument rounded
111 to 24 bits */
113 dd = mpfr_scale2 (dd, e);
114 if (negative)
115 dd = -dd;
117 /* convert (exacly) to float */
118 d = (float) dd;
121 return d;