xref: /dflybsd-src/contrib/mpfr/src/pow_ui.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_pow_ui-- compute the power of a floating-point
24a238c70SJohn Marino                                   by a machine integer
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 #define MPFR_NEED_LONGLONG_H
254a238c70SJohn Marino #include "mpfr-impl.h"
264a238c70SJohn Marino 
274a238c70SJohn Marino /* sets y to x^n, and return 0 if exact, non-zero otherwise */
284a238c70SJohn Marino int
mpfr_pow_ui(mpfr_ptr y,mpfr_srcptr x,unsigned long int n,mpfr_rnd_t rnd)294a238c70SJohn Marino mpfr_pow_ui (mpfr_ptr y, mpfr_srcptr x, unsigned long int n, mpfr_rnd_t rnd)
304a238c70SJohn Marino {
314a238c70SJohn Marino   unsigned long m;
324a238c70SJohn Marino   mpfr_t res;
334a238c70SJohn Marino   mpfr_prec_t prec, err;
344a238c70SJohn Marino   int inexact;
354a238c70SJohn Marino   mpfr_rnd_t rnd1;
364a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
374a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
384a238c70SJohn Marino   MPFR_BLOCK_DECL (flags);
394a238c70SJohn Marino 
404a238c70SJohn Marino   MPFR_LOG_FUNC
414a238c70SJohn Marino     (("x[%Pu]=%.*Rg n=%lu rnd=%d",
424a238c70SJohn Marino       mpfr_get_prec (x), mpfr_log_prec, x, n, rnd),
434a238c70SJohn Marino      ("y[%Pu]=%.*Rg inexact=%d",
444a238c70SJohn Marino       mpfr_get_prec (y), mpfr_log_prec, y, inexact));
454a238c70SJohn Marino 
464a238c70SJohn Marino   /* x^0 = 1 for any x, even a NaN */
474a238c70SJohn Marino   if (MPFR_UNLIKELY (n == 0))
484a238c70SJohn Marino     return mpfr_set_ui (y, 1, rnd);
494a238c70SJohn Marino 
504a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
514a238c70SJohn Marino     {
524a238c70SJohn Marino       if (MPFR_IS_NAN (x))
534a238c70SJohn Marino         {
544a238c70SJohn Marino           MPFR_SET_NAN (y);
554a238c70SJohn Marino           MPFR_RET_NAN;
564a238c70SJohn Marino         }
574a238c70SJohn Marino       else if (MPFR_IS_INF (x))
584a238c70SJohn Marino         {
594a238c70SJohn Marino           /* Inf^n = Inf, (-Inf)^n = Inf for n even, -Inf for n odd */
604a238c70SJohn Marino           if (MPFR_IS_NEG (x) && (n & 1) == 1)
614a238c70SJohn Marino             MPFR_SET_NEG (y);
624a238c70SJohn Marino           else
634a238c70SJohn Marino             MPFR_SET_POS (y);
644a238c70SJohn Marino           MPFR_SET_INF (y);
654a238c70SJohn Marino           MPFR_RET (0);
664a238c70SJohn Marino         }
674a238c70SJohn Marino       else /* x is zero */
684a238c70SJohn Marino         {
694a238c70SJohn Marino           MPFR_ASSERTD (MPFR_IS_ZERO (x));
704a238c70SJohn Marino           /* 0^n = 0 for any n */
714a238c70SJohn Marino           MPFR_SET_ZERO (y);
724a238c70SJohn Marino           if (MPFR_IS_POS (x) || (n & 1) == 0)
734a238c70SJohn Marino             MPFR_SET_POS (y);
744a238c70SJohn Marino           else
754a238c70SJohn Marino             MPFR_SET_NEG (y);
764a238c70SJohn Marino           MPFR_RET (0);
774a238c70SJohn Marino         }
784a238c70SJohn Marino     }
794a238c70SJohn Marino   else if (MPFR_UNLIKELY (n <= 2))
804a238c70SJohn Marino     {
814a238c70SJohn Marino       if (n < 2)
824a238c70SJohn Marino         /* x^1 = x */
834a238c70SJohn Marino         return mpfr_set (y, x, rnd);
844a238c70SJohn Marino       else
854a238c70SJohn Marino         /* x^2 = sqr(x) */
864a238c70SJohn Marino         return mpfr_sqr (y, x, rnd);
874a238c70SJohn Marino     }
884a238c70SJohn Marino 
894a238c70SJohn Marino   /* Augment exponent range */
904a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
914a238c70SJohn Marino 
924a238c70SJohn Marino   /* setup initial precision */
934a238c70SJohn Marino   prec = MPFR_PREC (y) + 3 + GMP_NUMB_BITS
944a238c70SJohn Marino     + MPFR_INT_CEIL_LOG2 (MPFR_PREC (y));
954a238c70SJohn Marino   mpfr_init2 (res, prec);
964a238c70SJohn Marino 
974a238c70SJohn Marino   rnd1 = MPFR_IS_POS (x) ? MPFR_RNDU : MPFR_RNDD; /* away */
984a238c70SJohn Marino 
994a238c70SJohn Marino   MPFR_ZIV_INIT (loop, prec);
1004a238c70SJohn Marino   for (;;)
1014a238c70SJohn Marino     {
1024a238c70SJohn Marino       int i;
1034a238c70SJohn Marino 
1044a238c70SJohn Marino       for (m = n, i = 0; m; i++, m >>= 1)
1054a238c70SJohn Marino         ;
1064a238c70SJohn Marino       /* now 2^(i-1) <= n < 2^i */
1074a238c70SJohn Marino       MPFR_ASSERTD (prec > (mpfr_prec_t) i);
1084a238c70SJohn Marino       err = prec - 1 - (mpfr_prec_t) i;
1094a238c70SJohn Marino       /* First step: compute square from x */
1104a238c70SJohn Marino       MPFR_BLOCK (flags,
1114a238c70SJohn Marino                   inexact = mpfr_mul (res, x, x, MPFR_RNDU);
1124a238c70SJohn Marino                   MPFR_ASSERTD (i >= 2);
1134a238c70SJohn Marino                   if (n & (1UL << (i-2)))
1144a238c70SJohn Marino                     inexact |= mpfr_mul (res, res, x, rnd1);
1154a238c70SJohn Marino                   for (i -= 3; i >= 0 && !MPFR_BLOCK_EXCEP; i--)
1164a238c70SJohn Marino                     {
1174a238c70SJohn Marino                       inexact |= mpfr_mul (res, res, res, MPFR_RNDU);
1184a238c70SJohn Marino                       if (n & (1UL << i))
1194a238c70SJohn Marino                         inexact |= mpfr_mul (res, res, x, rnd1);
1204a238c70SJohn Marino                     });
1214a238c70SJohn Marino       /* let r(n) be the number of roundings: we have r(2)=1, r(3)=2,
1224a238c70SJohn Marino          and r(2n)=2r(n)+1, r(2n+1)=2r(n)+2, thus r(n)=n-1.
1234a238c70SJohn Marino          Using Higham's method, to each rounding corresponds a factor
1244a238c70SJohn Marino          (1-theta) with 0 <= theta <= 2^(1-p), thus at the end the
1254a238c70SJohn Marino          absolute error is bounded by (n-1)*2^(1-p)*res <= 2*(n-1)*ulp(res)
1264a238c70SJohn Marino          since 2^(-p)*x <= ulp(x). Since n < 2^i, this gives a maximal
1274a238c70SJohn Marino          error of 2^(1+i)*ulp(res).
1284a238c70SJohn Marino       */
1294a238c70SJohn Marino       if (MPFR_LIKELY (inexact == 0
1304a238c70SJohn Marino                        || MPFR_OVERFLOW (flags) || MPFR_UNDERFLOW (flags)
1314a238c70SJohn Marino                        || MPFR_CAN_ROUND (res, err, MPFR_PREC (y), rnd)))
1324a238c70SJohn Marino         break;
1334a238c70SJohn Marino       /* Actualisation of the precision */
1344a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, prec);
1354a238c70SJohn Marino       mpfr_set_prec (res, prec);
1364a238c70SJohn Marino     }
1374a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
1384a238c70SJohn Marino 
1394a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_OVERFLOW (flags) || MPFR_UNDERFLOW (flags)))
1404a238c70SJohn Marino     {
1414a238c70SJohn Marino       mpz_t z;
1424a238c70SJohn Marino 
1434a238c70SJohn Marino       /* Internal overflow or underflow. However the approximation error has
1444a238c70SJohn Marino        * not been taken into account. So, let's solve this problem by using
1454a238c70SJohn Marino        * mpfr_pow_z, which can handle it. This case could be improved in the
1464a238c70SJohn Marino        * future, without having to use mpfr_pow_z.
1474a238c70SJohn Marino        */
1484a238c70SJohn Marino       MPFR_LOG_MSG (("Internal overflow or underflow,"
1494a238c70SJohn Marino                      " let's use mpfr_pow_z.\n", 0));
1504a238c70SJohn Marino       mpfr_clear (res);
1514a238c70SJohn Marino       MPFR_SAVE_EXPO_FREE (expo);
1524a238c70SJohn Marino       mpz_init (z);
1534a238c70SJohn Marino       mpz_set_ui (z, n);
1544a238c70SJohn Marino       inexact = mpfr_pow_z (y, x, z, rnd);
1554a238c70SJohn Marino       mpz_clear (z);
1564a238c70SJohn Marino       return inexact;
1574a238c70SJohn Marino     }
1584a238c70SJohn Marino 
1594a238c70SJohn Marino   inexact = mpfr_set (y, res, rnd);
1604a238c70SJohn Marino   mpfr_clear (res);
1614a238c70SJohn Marino 
1624a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
1634a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd);
1644a238c70SJohn Marino }
165