xref: /dflybsd-src/contrib/mpfr/src/exp.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_exp -- exponential of a floating-point number
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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 "mpfr-impl.h"
244a238c70SJohn Marino 
254a238c70SJohn Marino /* #define DEBUG */
264a238c70SJohn Marino 
274a238c70SJohn Marino int
mpfr_exp(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)284a238c70SJohn Marino mpfr_exp (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
294a238c70SJohn Marino {
304a238c70SJohn Marino   mpfr_exp_t expx;
314a238c70SJohn Marino   mpfr_prec_t precy;
324a238c70SJohn Marino   int inexact;
334a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
344a238c70SJohn Marino 
354a238c70SJohn Marino   MPFR_LOG_FUNC
364a238c70SJohn Marino     (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
374a238c70SJohn Marino      ("y[%Pu]=%.*Rg inexact=%d",
384a238c70SJohn Marino       mpfr_get_prec (y), mpfr_log_prec, y, inexact));
394a238c70SJohn Marino 
404a238c70SJohn Marino   if (MPFR_UNLIKELY( MPFR_IS_SINGULAR(x) ))
414a238c70SJohn Marino     {
424a238c70SJohn Marino       if (MPFR_IS_NAN(x))
434a238c70SJohn Marino         {
444a238c70SJohn Marino           MPFR_SET_NAN(y);
454a238c70SJohn Marino           MPFR_RET_NAN;
464a238c70SJohn Marino         }
474a238c70SJohn Marino       else if (MPFR_IS_INF(x))
484a238c70SJohn Marino         {
494a238c70SJohn Marino           if (MPFR_IS_POS(x))
504a238c70SJohn Marino             MPFR_SET_INF(y);
514a238c70SJohn Marino           else
524a238c70SJohn Marino             MPFR_SET_ZERO(y);
534a238c70SJohn Marino           MPFR_SET_POS(y);
544a238c70SJohn Marino           MPFR_RET(0);
554a238c70SJohn Marino         }
564a238c70SJohn Marino       else
574a238c70SJohn Marino         {
584a238c70SJohn Marino           MPFR_ASSERTD(MPFR_IS_ZERO(x));
594a238c70SJohn Marino           return mpfr_set_ui (y, 1, rnd_mode);
604a238c70SJohn Marino         }
614a238c70SJohn Marino     }
624a238c70SJohn Marino 
634a238c70SJohn Marino   /* First, let's detect most overflow and underflow cases. */
644a238c70SJohn Marino   {
654a238c70SJohn Marino     mpfr_t e, bound;
664a238c70SJohn Marino 
674a238c70SJohn Marino     /* We must extended the exponent range and save the flags now. */
684a238c70SJohn Marino     MPFR_SAVE_EXPO_MARK (expo);
694a238c70SJohn Marino 
704a238c70SJohn Marino     mpfr_init2 (e, sizeof (mpfr_exp_t) * CHAR_BIT);
714a238c70SJohn Marino     mpfr_init2 (bound, 32);
724a238c70SJohn Marino 
734a238c70SJohn Marino     inexact = mpfr_set_exp_t (e, expo.saved_emax, MPFR_RNDN);
744a238c70SJohn Marino     MPFR_ASSERTD (inexact == 0);
754a238c70SJohn Marino     mpfr_const_log2 (bound, expo.saved_emax < 0 ? MPFR_RNDD : MPFR_RNDU);
764a238c70SJohn Marino     mpfr_mul (bound, bound, e, MPFR_RNDU);
774a238c70SJohn Marino     if (MPFR_UNLIKELY (mpfr_cmp (x, bound) >= 0))
784a238c70SJohn Marino       {
794a238c70SJohn Marino         /* x > log(2^emax), thus exp(x) > 2^emax */
804a238c70SJohn Marino         mpfr_clears (e, bound, (mpfr_ptr) 0);
814a238c70SJohn Marino         MPFR_SAVE_EXPO_FREE (expo);
824a238c70SJohn Marino         return mpfr_overflow (y, rnd_mode, 1);
834a238c70SJohn Marino       }
844a238c70SJohn Marino 
854a238c70SJohn Marino     inexact = mpfr_set_exp_t (e, expo.saved_emin, MPFR_RNDN);
864a238c70SJohn Marino     MPFR_ASSERTD (inexact == 0);
874a238c70SJohn Marino     inexact = mpfr_sub_ui (e, e, 2, MPFR_RNDN);
884a238c70SJohn Marino     MPFR_ASSERTD (inexact == 0);
894a238c70SJohn Marino     mpfr_const_log2 (bound, expo.saved_emin < 0 ? MPFR_RNDU : MPFR_RNDD);
904a238c70SJohn Marino     mpfr_mul (bound, bound, e, MPFR_RNDD);
914a238c70SJohn Marino     if (MPFR_UNLIKELY (mpfr_cmp (x, bound) <= 0))
924a238c70SJohn Marino       {
934a238c70SJohn Marino         /* x < log(2^(emin - 2)), thus exp(x) < 2^(emin - 2) */
944a238c70SJohn Marino         mpfr_clears (e, bound, (mpfr_ptr) 0);
954a238c70SJohn Marino         MPFR_SAVE_EXPO_FREE (expo);
964a238c70SJohn Marino         return mpfr_underflow (y, rnd_mode == MPFR_RNDN ? MPFR_RNDZ : rnd_mode,
974a238c70SJohn Marino                                1);
984a238c70SJohn Marino       }
994a238c70SJohn Marino 
1004a238c70SJohn Marino     /* Other overflow/underflow cases must be detected
1014a238c70SJohn Marino        by the generic routines. */
1024a238c70SJohn Marino     mpfr_clears (e, bound, (mpfr_ptr) 0);
1034a238c70SJohn Marino     MPFR_SAVE_EXPO_FREE (expo);
1044a238c70SJohn Marino   }
1054a238c70SJohn Marino 
1064a238c70SJohn Marino   expx  = MPFR_GET_EXP (x);
1074a238c70SJohn Marino   precy = MPFR_PREC (y);
1084a238c70SJohn Marino 
1094a238c70SJohn Marino   /* if x < 2^(-precy), then exp(x) i.e. gives 1 +/- 1 ulp(1) */
1104a238c70SJohn Marino   if (MPFR_UNLIKELY (expx < 0 && (mpfr_uexp_t) (-expx) > precy))
1114a238c70SJohn Marino     {
1124a238c70SJohn Marino       mpfr_exp_t emin = __gmpfr_emin;
1134a238c70SJohn Marino       mpfr_exp_t emax = __gmpfr_emax;
1144a238c70SJohn Marino       int signx = MPFR_SIGN (x);
1154a238c70SJohn Marino 
1164a238c70SJohn Marino       MPFR_SET_POS (y);
1174a238c70SJohn Marino       if (MPFR_IS_NEG_SIGN (signx) && (rnd_mode == MPFR_RNDD ||
1184a238c70SJohn Marino                                        rnd_mode == MPFR_RNDZ))
1194a238c70SJohn Marino         {
1204a238c70SJohn Marino           __gmpfr_emin = 0;
1214a238c70SJohn Marino           __gmpfr_emax = 0;
1224a238c70SJohn Marino           mpfr_setmax (y, 0);  /* y = 1 - epsilon */
1234a238c70SJohn Marino           inexact = -1;
1244a238c70SJohn Marino         }
1254a238c70SJohn Marino       else
1264a238c70SJohn Marino         {
1274a238c70SJohn Marino           __gmpfr_emin = 1;
1284a238c70SJohn Marino           __gmpfr_emax = 1;
1294a238c70SJohn Marino           mpfr_setmin (y, 1);  /* y = 1 */
1304a238c70SJohn Marino           if (MPFR_IS_POS_SIGN (signx) && (rnd_mode == MPFR_RNDU ||
1314a238c70SJohn Marino                                            rnd_mode == MPFR_RNDA))
1324a238c70SJohn Marino             {
1334a238c70SJohn Marino               mp_size_t yn;
1344a238c70SJohn Marino               int sh;
1354a238c70SJohn Marino 
136*ab6d115fSJohn Marino               yn = MPFR_LIMB_SIZE (y);
1374a238c70SJohn Marino               sh = (mpfr_prec_t) yn * GMP_NUMB_BITS - MPFR_PREC(y);
1384a238c70SJohn Marino               MPFR_MANT(y)[0] += MPFR_LIMB_ONE << sh;
1394a238c70SJohn Marino               inexact = 1;
1404a238c70SJohn Marino             }
1414a238c70SJohn Marino           else
1424a238c70SJohn Marino             inexact = -MPFR_FROM_SIGN_TO_INT(signx);
1434a238c70SJohn Marino         }
1444a238c70SJohn Marino 
1454a238c70SJohn Marino       __gmpfr_emin = emin;
1464a238c70SJohn Marino       __gmpfr_emax = emax;
1474a238c70SJohn Marino     }
1484a238c70SJohn Marino   else  /* General case */
1494a238c70SJohn Marino     {
1504a238c70SJohn Marino       if (MPFR_UNLIKELY (precy >= MPFR_EXP_THRESHOLD))
1514a238c70SJohn Marino         /* mpfr_exp_3 saves the exponent range and flags itself, otherwise
1524a238c70SJohn Marino            the flag changes in mpfr_exp_3 are lost */
1534a238c70SJohn Marino         inexact = mpfr_exp_3 (y, x, rnd_mode); /* O(M(n) log(n)^2) */
1544a238c70SJohn Marino       else
1554a238c70SJohn Marino         {
1564a238c70SJohn Marino           MPFR_SAVE_EXPO_MARK (expo);
1574a238c70SJohn Marino           inexact = mpfr_exp_2 (y, x, rnd_mode); /* O(n^(1/3) M(n)) */
1584a238c70SJohn Marino           MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
1594a238c70SJohn Marino           MPFR_SAVE_EXPO_FREE (expo);
1604a238c70SJohn Marino         }
1614a238c70SJohn Marino     }
1624a238c70SJohn Marino 
1634a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd_mode);
1644a238c70SJohn Marino }
165