xref: /dflybsd-src/contrib/mpfr/src/get_d.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_get_d, mpfr_get_d_2exp -- convert a multiple precision floating-point
24a238c70SJohn Marino                                   number to a machine double precision float
34a238c70SJohn Marino 
4*ab6d115fSJohn Marino Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
64a238c70SJohn Marino 
74a238c70SJohn Marino This file is part of the GNU MPFR Library.
84a238c70SJohn Marino 
94a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
104a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
114a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
124a238c70SJohn Marino option) any later version.
134a238c70SJohn Marino 
144a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
154a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
164a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
174a238c70SJohn Marino License for more details.
184a238c70SJohn Marino 
194a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
204a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
214a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
224a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
234a238c70SJohn Marino 
244a238c70SJohn Marino #include <float.h>
254a238c70SJohn Marino 
264a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
274a238c70SJohn Marino #include "mpfr-impl.h"
284a238c70SJohn Marino 
294a238c70SJohn Marino #include "ieee_floats.h"
304a238c70SJohn Marino 
314a238c70SJohn Marino /* Assumes IEEE-754 double precision; otherwise, only an approximated
324a238c70SJohn Marino    result will be returned, without any guaranty (and special cases
334a238c70SJohn Marino    such as NaN must be avoided if not supported). */
344a238c70SJohn Marino 
354a238c70SJohn Marino double
mpfr_get_d(mpfr_srcptr src,mpfr_rnd_t rnd_mode)364a238c70SJohn Marino mpfr_get_d (mpfr_srcptr src, mpfr_rnd_t rnd_mode)
374a238c70SJohn Marino {
384a238c70SJohn Marino   double d;
394a238c70SJohn Marino   int negative;
404a238c70SJohn Marino   mpfr_exp_t e;
414a238c70SJohn Marino 
424a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
434a238c70SJohn Marino     {
444a238c70SJohn Marino       if (MPFR_IS_NAN (src))
454a238c70SJohn Marino         return MPFR_DBL_NAN;
464a238c70SJohn Marino 
474a238c70SJohn Marino       negative = MPFR_IS_NEG (src);
484a238c70SJohn Marino 
494a238c70SJohn Marino       if (MPFR_IS_INF (src))
504a238c70SJohn Marino         return negative ? MPFR_DBL_INFM : MPFR_DBL_INFP;
514a238c70SJohn Marino 
524a238c70SJohn Marino       MPFR_ASSERTD (MPFR_IS_ZERO(src));
534a238c70SJohn Marino       return negative ? DBL_NEG_ZERO : 0.0;
544a238c70SJohn Marino     }
554a238c70SJohn Marino 
564a238c70SJohn Marino   e = MPFR_GET_EXP (src);
574a238c70SJohn Marino   negative = MPFR_IS_NEG (src);
584a238c70SJohn Marino 
594a238c70SJohn Marino   if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDA))
604a238c70SJohn Marino     rnd_mode = negative ? MPFR_RNDD : MPFR_RNDU;
614a238c70SJohn Marino 
624a238c70SJohn Marino   /* the smallest normalized number is 2^(-1022)=0.1e-1021, and the smallest
634a238c70SJohn Marino      subnormal is 2^(-1074)=0.1e-1073 */
644a238c70SJohn Marino   if (MPFR_UNLIKELY (e < -1073))
654a238c70SJohn Marino     {
664a238c70SJohn Marino       /* Note: Avoid using a constant expression DBL_MIN * DBL_EPSILON
674a238c70SJohn Marino          as this gives 0 instead of the correct result with gcc on some
684a238c70SJohn Marino          Alpha machines. */
694a238c70SJohn Marino       d = negative ?
704a238c70SJohn Marino         (rnd_mode == MPFR_RNDD ||
714a238c70SJohn Marino          (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp(src, -1, -1075) < 0)
724a238c70SJohn Marino          ? -DBL_MIN : DBL_NEG_ZERO) :
734a238c70SJohn Marino         (rnd_mode == MPFR_RNDU ||
744a238c70SJohn Marino          (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp(src, 1, -1075) > 0)
754a238c70SJohn Marino          ? DBL_MIN : 0.0);
764a238c70SJohn Marino       if (d != 0.0) /* we multiply DBL_MIN = 2^(-1022) by DBL_EPSILON = 2^(-52)
774a238c70SJohn Marino                        to get +-2^(-1074) */
784a238c70SJohn Marino         d *= DBL_EPSILON;
794a238c70SJohn Marino     }
804a238c70SJohn Marino   /* the largest normalized number is 2^1024*(1-2^(-53))=0.111...111e1024 */
814a238c70SJohn Marino   else if (MPFR_UNLIKELY (e > 1024))
824a238c70SJohn Marino     {
834a238c70SJohn Marino       d = negative ?
844a238c70SJohn Marino         (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDU ?
854a238c70SJohn Marino          -DBL_MAX : MPFR_DBL_INFM) :
864a238c70SJohn Marino         (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDD ?
874a238c70SJohn Marino          DBL_MAX : MPFR_DBL_INFP);
884a238c70SJohn Marino     }
894a238c70SJohn Marino   else
904a238c70SJohn Marino     {
914a238c70SJohn Marino       int nbits;
924a238c70SJohn Marino       mp_size_t np, i;
934a238c70SJohn Marino       mp_limb_t tp[ MPFR_LIMBS_PER_DOUBLE ];
944a238c70SJohn Marino       int carry;
954a238c70SJohn Marino 
964a238c70SJohn Marino       nbits = IEEE_DBL_MANT_DIG; /* 53 */
974a238c70SJohn Marino       if (MPFR_UNLIKELY (e < -1021))
984a238c70SJohn Marino         /*In the subnormal case, compute the exact number of significant bits*/
994a238c70SJohn Marino         {
1004a238c70SJohn Marino           nbits += (1021 + e);
1014a238c70SJohn Marino           MPFR_ASSERTD (nbits >= 1);
1024a238c70SJohn Marino         }
103*ab6d115fSJohn Marino       np = MPFR_PREC2LIMBS (nbits);
1044a238c70SJohn Marino       MPFR_ASSERTD ( np <= MPFR_LIMBS_PER_DOUBLE );
1054a238c70SJohn Marino       carry = mpfr_round_raw_4 (tp, MPFR_MANT(src), MPFR_PREC(src), negative,
1064a238c70SJohn Marino                                 nbits, rnd_mode);
1074a238c70SJohn Marino       if (MPFR_UNLIKELY(carry))
1084a238c70SJohn Marino         d = 1.0;
1094a238c70SJohn Marino       else
1104a238c70SJohn Marino         {
1114a238c70SJohn Marino           /* The following computations are exact thanks to the previous
1124a238c70SJohn Marino              mpfr_round_raw. */
1134a238c70SJohn Marino           d = (double) tp[0] / MP_BASE_AS_DOUBLE;
1144a238c70SJohn Marino           for (i = 1 ; i < np ; i++)
1154a238c70SJohn Marino             d = (d + tp[i]) / MP_BASE_AS_DOUBLE;
1164a238c70SJohn Marino           /* d is the mantissa (between 1/2 and 1) of the argument rounded
1174a238c70SJohn Marino              to 53 bits */
1184a238c70SJohn Marino         }
1194a238c70SJohn Marino       d = mpfr_scale2 (d, e);
1204a238c70SJohn Marino       if (negative)
1214a238c70SJohn Marino         d = -d;
1224a238c70SJohn Marino     }
1234a238c70SJohn Marino 
1244a238c70SJohn Marino   return d;
1254a238c70SJohn Marino }
1264a238c70SJohn Marino 
1274a238c70SJohn Marino #undef mpfr_get_d1
1284a238c70SJohn Marino double
mpfr_get_d1(mpfr_srcptr src)1294a238c70SJohn Marino mpfr_get_d1 (mpfr_srcptr src)
1304a238c70SJohn Marino {
1314a238c70SJohn Marino   return mpfr_get_d (src, __gmpfr_default_rounding_mode);
1324a238c70SJohn Marino }
1334a238c70SJohn Marino 
1344a238c70SJohn Marino double
mpfr_get_d_2exp(long * expptr,mpfr_srcptr src,mpfr_rnd_t rnd_mode)1354a238c70SJohn Marino mpfr_get_d_2exp (long *expptr, mpfr_srcptr src, mpfr_rnd_t rnd_mode)
1364a238c70SJohn Marino {
1374a238c70SJohn Marino   double ret;
1384a238c70SJohn Marino   mpfr_exp_t exp;
1394a238c70SJohn Marino   mpfr_t tmp;
1404a238c70SJohn Marino 
1414a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
1424a238c70SJohn Marino     {
1434a238c70SJohn Marino       int negative;
1444a238c70SJohn Marino       *expptr = 0;
1454a238c70SJohn Marino       if (MPFR_IS_NAN (src))
1464a238c70SJohn Marino         return MPFR_DBL_NAN;
1474a238c70SJohn Marino       negative = MPFR_IS_NEG (src);
1484a238c70SJohn Marino       if (MPFR_IS_INF (src))
1494a238c70SJohn Marino         return negative ? MPFR_DBL_INFM : MPFR_DBL_INFP;
1504a238c70SJohn Marino       MPFR_ASSERTD (MPFR_IS_ZERO(src));
1514a238c70SJohn Marino       return negative ? DBL_NEG_ZERO : 0.0;
1524a238c70SJohn Marino     }
1534a238c70SJohn Marino 
1544a238c70SJohn Marino   tmp[0] = *src;        /* Hack copy mpfr_t */
1554a238c70SJohn Marino   MPFR_SET_EXP (tmp, 0);
1564a238c70SJohn Marino   ret = mpfr_get_d (tmp, rnd_mode);
1574a238c70SJohn Marino 
1584a238c70SJohn Marino   if (MPFR_IS_PURE_FP(src))
1594a238c70SJohn Marino     {
1604a238c70SJohn Marino       exp = MPFR_GET_EXP (src);
1614a238c70SJohn Marino 
1624a238c70SJohn Marino       /* rounding can give 1.0, adjust back to 0.5 <= abs(ret) < 1.0 */
1634a238c70SJohn Marino       if (ret == 1.0)
1644a238c70SJohn Marino         {
1654a238c70SJohn Marino           ret = 0.5;
1664a238c70SJohn Marino           exp++;
1674a238c70SJohn Marino         }
1684a238c70SJohn Marino       else if (ret == -1.0)
1694a238c70SJohn Marino         {
1704a238c70SJohn Marino           ret = -0.5;
1714a238c70SJohn Marino           exp++;
1724a238c70SJohn Marino         }
1734a238c70SJohn Marino 
1744a238c70SJohn Marino       MPFR_ASSERTN ((ret >= 0.5 && ret < 1.0)
1754a238c70SJohn Marino                     || (ret <= -0.5 && ret > -1.0));
1764a238c70SJohn Marino       MPFR_ASSERTN (exp >= LONG_MIN && exp <= LONG_MAX);
1774a238c70SJohn Marino     }
1784a238c70SJohn Marino   else
1794a238c70SJohn Marino     exp = 0;
1804a238c70SJohn Marino 
1814a238c70SJohn Marino   *expptr = exp;
1824a238c70SJohn Marino   return ret;
1834a238c70SJohn Marino }
184