14a238c70SJohn Marino /* mpfr_get_flt -- convert a mpfr_t to a machine single precision float
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino
234a238c70SJohn Marino #include <float.h> /* for FLT_MIN */
244a238c70SJohn Marino
254a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
264a238c70SJohn Marino #include "mpfr-impl.h"
274a238c70SJohn Marino
284a238c70SJohn Marino #include "ieee_floats.h"
294a238c70SJohn Marino
304a238c70SJohn Marino #define FLT_NEG_ZERO ((float) DBL_NEG_ZERO)
314a238c70SJohn Marino #define MPFR_FLT_INFM ((float) MPFR_DBL_INFM)
324a238c70SJohn Marino #define MPFR_FLT_INFP ((float) MPFR_DBL_INFP)
334a238c70SJohn Marino
344a238c70SJohn Marino float
mpfr_get_flt(mpfr_srcptr src,mpfr_rnd_t rnd_mode)354a238c70SJohn Marino mpfr_get_flt (mpfr_srcptr src, mpfr_rnd_t rnd_mode)
364a238c70SJohn Marino {
374a238c70SJohn Marino int negative;
384a238c70SJohn Marino mpfr_exp_t e;
394a238c70SJohn Marino float d;
404a238c70SJohn Marino
414a238c70SJohn Marino /* in case of NaN, +Inf, -Inf, +0, -0, the conversion from double to float
424a238c70SJohn Marino is exact */
434a238c70SJohn Marino if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
444a238c70SJohn Marino return (float) mpfr_get_d (src, rnd_mode);
454a238c70SJohn Marino
464a238c70SJohn Marino e = MPFR_GET_EXP (src);
474a238c70SJohn Marino negative = MPFR_IS_NEG (src);
484a238c70SJohn Marino
494a238c70SJohn Marino if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDA))
504a238c70SJohn Marino rnd_mode = negative ? MPFR_RNDD : MPFR_RNDU;
514a238c70SJohn Marino
524a238c70SJohn Marino /* the smallest positive normal float number is 2^(-126) = 0.5*2^(-125),
534a238c70SJohn Marino and the smallest positive subnormal number is 2^(-149) = 0.5*2^(-148) */
544a238c70SJohn Marino if (MPFR_UNLIKELY (e < -148))
554a238c70SJohn Marino {
564a238c70SJohn Marino /* |src| < 2^(-149), i.e., |src| is smaller than the smallest positive
574a238c70SJohn Marino subnormal number.
584a238c70SJohn Marino In round-to-nearest mode, 2^(-150) is rounded to zero.
594a238c70SJohn Marino */
604a238c70SJohn Marino d = negative ?
614a238c70SJohn Marino (rnd_mode == MPFR_RNDD ||
624a238c70SJohn Marino (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, -1, -150) < 0)
634a238c70SJohn Marino ? -FLT_MIN : FLT_NEG_ZERO) :
644a238c70SJohn Marino (rnd_mode == MPFR_RNDU ||
654a238c70SJohn Marino (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, 1, -150) > 0)
664a238c70SJohn Marino ? FLT_MIN : 0.0);
674a238c70SJohn Marino if (d != 0.0) /* we multiply FLT_MIN = 2^(-126) by FLT_EPSILON = 2^(-23)
684a238c70SJohn Marino to get +-2^(-149) */
694a238c70SJohn Marino d *= FLT_EPSILON;
704a238c70SJohn Marino }
714a238c70SJohn Marino /* the largest normal number is 2^128*(1-2^(-24)) = 0.111...111e128 */
724a238c70SJohn Marino else if (MPFR_UNLIKELY (e > 128))
734a238c70SJohn Marino {
744a238c70SJohn Marino d = negative ?
754a238c70SJohn Marino (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDU ?
764a238c70SJohn Marino -FLT_MAX : MPFR_FLT_INFM) :
774a238c70SJohn Marino (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDD ?
784a238c70SJohn Marino FLT_MAX : MPFR_FLT_INFP);
794a238c70SJohn Marino }
804a238c70SJohn Marino else /* -148 <= e <= 127 */
814a238c70SJohn Marino {
824a238c70SJohn Marino int nbits;
834a238c70SJohn Marino mp_size_t np, i;
844a238c70SJohn Marino mp_limb_t tp[MPFR_LIMBS_PER_FLT];
854a238c70SJohn Marino int carry;
864a238c70SJohn Marino double dd;
874a238c70SJohn Marino
884a238c70SJohn Marino nbits = IEEE_FLT_MANT_DIG; /* 24 */
894a238c70SJohn Marino if (MPFR_UNLIKELY (e < -125))
904a238c70SJohn Marino /*In the subnormal case, compute the exact number of significant bits*/
914a238c70SJohn Marino {
924a238c70SJohn Marino nbits += (125 + e);
934a238c70SJohn Marino MPFR_ASSERTD (nbits >= 1);
944a238c70SJohn Marino }
95*ab6d115fSJohn Marino np = MPFR_PREC2LIMBS (nbits);
964a238c70SJohn Marino MPFR_ASSERTD(np <= MPFR_LIMBS_PER_FLT);
974a238c70SJohn Marino carry = mpfr_round_raw_4 (tp, MPFR_MANT(src), MPFR_PREC(src), negative,
984a238c70SJohn Marino nbits, rnd_mode);
994a238c70SJohn Marino /* we perform the reconstruction using the 'double' type here,
1004a238c70SJohn Marino knowing the result is exactly representable as 'float' */
1014a238c70SJohn Marino if (MPFR_UNLIKELY(carry))
1024a238c70SJohn Marino dd = 1.0;
1034a238c70SJohn Marino else
1044a238c70SJohn Marino {
1054a238c70SJohn Marino /* The following computations are exact thanks to the previous
1064a238c70SJohn Marino mpfr_round_raw. */
1074a238c70SJohn Marino dd = (double) tp[0] / MP_BASE_AS_DOUBLE;
1084a238c70SJohn Marino for (i = 1 ; i < np ; i++)
1094a238c70SJohn Marino dd = (dd + tp[i]) / MP_BASE_AS_DOUBLE;
1104a238c70SJohn Marino /* dd is the mantissa (between 1/2 and 1) of the argument rounded
1114a238c70SJohn Marino to 24 bits */
1124a238c70SJohn Marino }
1134a238c70SJohn Marino dd = mpfr_scale2 (dd, e);
1144a238c70SJohn Marino if (negative)
1154a238c70SJohn Marino dd = -dd;
1164a238c70SJohn Marino
1174a238c70SJohn Marino /* convert (exacly) to float */
1184a238c70SJohn Marino d = (float) dd;
1194a238c70SJohn Marino }
1204a238c70SJohn Marino
1214a238c70SJohn Marino return d;
1224a238c70SJohn Marino }
1234a238c70SJohn Marino
124