xref: /dflybsd-src/contrib/mpfr/src/pow_si.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_pow_si -- power function x^y with y a signed int
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 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 #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino /* The computation of y = pow_si(x,n) is done by
274a238c70SJohn Marino  *    y = pow_ui(x,n)       if n >= 0
284a238c70SJohn Marino  *    y = 1 / pow_ui(x,-n)  if n < 0
294a238c70SJohn Marino  */
304a238c70SJohn Marino 
314a238c70SJohn Marino int
mpfr_pow_si(mpfr_ptr y,mpfr_srcptr x,long int n,mpfr_rnd_t rnd)324a238c70SJohn Marino mpfr_pow_si (mpfr_ptr y, mpfr_srcptr x, long int n, mpfr_rnd_t rnd)
334a238c70SJohn Marino {
344a238c70SJohn Marino   MPFR_LOG_FUNC
354a238c70SJohn Marino     (("x[%Pu]=%.*Rg n=%ld rnd=%d",
364a238c70SJohn Marino       mpfr_get_prec (x), mpfr_log_prec, x, n, rnd),
374a238c70SJohn Marino      ("y[%Pu]=%.*Rg", mpfr_get_prec (y), mpfr_log_prec, y));
384a238c70SJohn Marino 
394a238c70SJohn Marino   if (n >= 0)
404a238c70SJohn Marino     return mpfr_pow_ui (y, x, n, rnd);
414a238c70SJohn Marino   else
424a238c70SJohn Marino     {
434a238c70SJohn Marino       if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
444a238c70SJohn Marino         {
454a238c70SJohn Marino           if (MPFR_IS_NAN (x))
464a238c70SJohn Marino             {
474a238c70SJohn Marino               MPFR_SET_NAN (y);
484a238c70SJohn Marino               MPFR_RET_NAN;
494a238c70SJohn Marino             }
504a238c70SJohn Marino           else
514a238c70SJohn Marino             {
524a238c70SJohn Marino               int positive = MPFR_IS_POS (x) || ((unsigned long) n & 1) == 0;
534a238c70SJohn Marino               if (MPFR_IS_INF (x))
544a238c70SJohn Marino                 MPFR_SET_ZERO (y);
554a238c70SJohn Marino               else /* x is zero */
564a238c70SJohn Marino                 {
574a238c70SJohn Marino                   MPFR_ASSERTD (MPFR_IS_ZERO (x));
584a238c70SJohn Marino                   MPFR_SET_INF (y);
594a238c70SJohn Marino                   mpfr_set_divby0 ();
604a238c70SJohn Marino                 }
614a238c70SJohn Marino               if (positive)
624a238c70SJohn Marino                 MPFR_SET_POS (y);
634a238c70SJohn Marino               else
644a238c70SJohn Marino                 MPFR_SET_NEG (y);
654a238c70SJohn Marino               MPFR_RET (0);
664a238c70SJohn Marino             }
674a238c70SJohn Marino         }
684a238c70SJohn Marino 
694a238c70SJohn Marino       /* detect exact powers: x^(-n) is exact iff x is a power of 2 */
704a238c70SJohn Marino       if (mpfr_cmp_si_2exp (x, MPFR_SIGN(x), MPFR_EXP(x) - 1) == 0)
714a238c70SJohn Marino         {
724a238c70SJohn Marino           mpfr_exp_t expx = MPFR_EXP (x) - 1, expy;
734a238c70SJohn Marino           MPFR_ASSERTD (n < 0);
744a238c70SJohn Marino           /* Warning: n * expx may overflow!
754a238c70SJohn Marino            *
764a238c70SJohn Marino            * Some systems (apparently alpha-freebsd) abort with
774a238c70SJohn Marino            * LONG_MIN / 1, and LONG_MIN / -1 is undefined.
784a238c70SJohn Marino            * http://www.freebsd.org/cgi/query-pr.cgi?pr=72024
794a238c70SJohn Marino            *
804a238c70SJohn Marino            * Proof of the overflow checking. The expressions below are
814a238c70SJohn Marino            * assumed to be on the rational numbers, but the word "overflow"
824a238c70SJohn Marino            * still has its own meaning in the C context. / still denotes
834a238c70SJohn Marino            * the integer (truncated) division, and // denotes the exact
844a238c70SJohn Marino            * division.
854a238c70SJohn Marino            * - First, (__gmpfr_emin - 1) / n and (__gmpfr_emax - 1) / n
864a238c70SJohn Marino            *   cannot overflow due to the constraints on the exponents of
874a238c70SJohn Marino            *   MPFR numbers.
884a238c70SJohn Marino            * - If n = -1, then n * expx = - expx, which is representable
894a238c70SJohn Marino            *   because of the constraints on the exponents of MPFR numbers.
904a238c70SJohn Marino            * - If expx = 0, then n * expx = 0, which is representable.
914a238c70SJohn Marino            * - If n < -1 and expx > 0:
924a238c70SJohn Marino            *   + If expx > (__gmpfr_emin - 1) / n, then
934a238c70SJohn Marino            *           expx >= (__gmpfr_emin - 1) / n + 1
944a238c70SJohn Marino            *                > (__gmpfr_emin - 1) // n,
954a238c70SJohn Marino            *     and
964a238c70SJohn Marino            *           n * expx < __gmpfr_emin - 1,
974a238c70SJohn Marino            *     i.e.
984a238c70SJohn Marino            *           n * expx <= __gmpfr_emin - 2.
994a238c70SJohn Marino            *     This corresponds to an underflow, with a null result in
1004a238c70SJohn Marino            *     the rounding-to-nearest mode.
1014a238c70SJohn Marino            *   + If expx <= (__gmpfr_emin - 1) / n, then n * expx cannot
1024a238c70SJohn Marino            *     overflow since 0 < expx <= (__gmpfr_emin - 1) / n and
1034a238c70SJohn Marino            *           0 > n * expx >= n * ((__gmpfr_emin - 1) / n)
1044a238c70SJohn Marino            *                        >= __gmpfr_emin - 1.
1054a238c70SJohn Marino            * - If n < -1 and expx < 0:
1064a238c70SJohn Marino            *   + If expx < (__gmpfr_emax - 1) / n, then
1074a238c70SJohn Marino            *           expx <= (__gmpfr_emax - 1) / n - 1
1084a238c70SJohn Marino            *                < (__gmpfr_emax - 1) // n,
1094a238c70SJohn Marino            *     and
1104a238c70SJohn Marino            *           n * expx > __gmpfr_emax - 1,
1114a238c70SJohn Marino            *     i.e.
1124a238c70SJohn Marino            *           n * expx >= __gmpfr_emax.
1134a238c70SJohn Marino            *     This corresponds to an overflow (2^(n * expx) has an
1144a238c70SJohn Marino            *     exponent > __gmpfr_emax).
1154a238c70SJohn Marino            *   + If expx >= (__gmpfr_emax - 1) / n, then n * expx cannot
1164a238c70SJohn Marino            *     overflow since 0 > expx >= (__gmpfr_emax - 1) / n and
1174a238c70SJohn Marino            *           0 < n * expx <= n * ((__gmpfr_emax - 1) / n)
1184a238c70SJohn Marino            *                        <= __gmpfr_emax - 1.
1194a238c70SJohn Marino            * Note: one could use expx bounds based on MPFR_EXP_MIN and
1204a238c70SJohn Marino            * MPFR_EXP_MAX instead of __gmpfr_emin and __gmpfr_emax. The
1214a238c70SJohn Marino            * current bounds do not lead to noticeably slower code and
1224a238c70SJohn Marino            * allow us to avoid a bug in Sun's compiler for Solaris/x86
1234a238c70SJohn Marino            * (when optimizations are enabled); known affected versions:
1244a238c70SJohn Marino            *   cc: Sun C 5.8 2005/10/13
1254a238c70SJohn Marino            *   cc: Sun C 5.8 Patch 121016-02 2006/03/31
1264a238c70SJohn Marino            *   cc: Sun C 5.8 Patch 121016-04 2006/10/18
1274a238c70SJohn Marino            */
1284a238c70SJohn Marino           expy =
1294a238c70SJohn Marino             n != -1 && expx > 0 && expx > (__gmpfr_emin - 1) / n ?
1304a238c70SJohn Marino             MPFR_EMIN_MIN - 2 /* Underflow */ :
1314a238c70SJohn Marino             n != -1 && expx < 0 && expx < (__gmpfr_emax - 1) / n ?
1324a238c70SJohn Marino             MPFR_EMAX_MAX /* Overflow */ : n * expx;
1334a238c70SJohn Marino           return mpfr_set_si_2exp (y, n % 2 ? MPFR_INT_SIGN (x) : 1,
1344a238c70SJohn Marino                                    expy, rnd);
1354a238c70SJohn Marino         }
1364a238c70SJohn Marino 
1374a238c70SJohn Marino       /* General case */
1384a238c70SJohn Marino       {
1394a238c70SJohn Marino         /* Declaration of the intermediary variable */
1404a238c70SJohn Marino         mpfr_t t;
1414a238c70SJohn Marino         /* Declaration of the size variable */
1424a238c70SJohn Marino         mpfr_prec_t Ny;                              /* target precision */
1434a238c70SJohn Marino         mpfr_prec_t Nt;                              /* working precision */
1444a238c70SJohn Marino         mpfr_rnd_t rnd1;
1454a238c70SJohn Marino         int size_n;
1464a238c70SJohn Marino         int inexact;
1474a238c70SJohn Marino         unsigned long abs_n;
1484a238c70SJohn Marino         MPFR_SAVE_EXPO_DECL (expo);
1494a238c70SJohn Marino         MPFR_ZIV_DECL (loop);
1504a238c70SJohn Marino 
1514a238c70SJohn Marino         abs_n = - (unsigned long) n;
1524a238c70SJohn Marino         count_leading_zeros (size_n, (mp_limb_t) abs_n);
1534a238c70SJohn Marino         size_n = GMP_NUMB_BITS - size_n;
1544a238c70SJohn Marino 
1554a238c70SJohn Marino         /* initial working precision */
1564a238c70SJohn Marino         Ny = MPFR_PREC (y);
1574a238c70SJohn Marino         Nt = Ny + size_n + 3 + MPFR_INT_CEIL_LOG2 (Ny);
1584a238c70SJohn Marino 
1594a238c70SJohn Marino         MPFR_SAVE_EXPO_MARK (expo);
1604a238c70SJohn Marino 
1614a238c70SJohn Marino         /* initialise of intermediary   variable */
1624a238c70SJohn Marino         mpfr_init2 (t, Nt);
1634a238c70SJohn Marino 
1644a238c70SJohn Marino         /* We will compute rnd(rnd1(1/x) ^ |n|), where rnd1 is the rounding
1654a238c70SJohn Marino            toward sign(x), to avoid spurious overflow or underflow, as in
1664a238c70SJohn Marino            mpfr_pow_z. */
1674a238c70SJohn Marino         rnd1 = MPFR_EXP (x) < 1 ? MPFR_RNDZ :
1684a238c70SJohn Marino           (MPFR_SIGN (x) > 0 ? MPFR_RNDU : MPFR_RNDD);
1694a238c70SJohn Marino 
1704a238c70SJohn Marino         MPFR_ZIV_INIT (loop, Nt);
1714a238c70SJohn Marino         for (;;)
1724a238c70SJohn Marino           {
1734a238c70SJohn Marino             MPFR_BLOCK_DECL (flags);
1744a238c70SJohn Marino 
1754a238c70SJohn Marino             /* compute (1/x)^|n| */
1764a238c70SJohn Marino             MPFR_BLOCK (flags, mpfr_ui_div (t, 1, x, rnd1));
1774a238c70SJohn Marino             MPFR_ASSERTD (! MPFR_UNDERFLOW (flags));
1784a238c70SJohn Marino             /* t = (1/x)*(1+theta) where |theta| <= 2^(-Nt) */
1794a238c70SJohn Marino             if (MPFR_UNLIKELY (MPFR_OVERFLOW (flags)))
1804a238c70SJohn Marino               goto overflow;
1814a238c70SJohn Marino             MPFR_BLOCK (flags, mpfr_pow_ui (t, t, abs_n, rnd));
1824a238c70SJohn Marino             /* t = (1/x)^|n|*(1+theta')^(|n|+1) where |theta'| <= 2^(-Nt).
1834a238c70SJohn Marino                If (|n|+1)*2^(-Nt) <= 1/2, which is satisfied as soon as
1844a238c70SJohn Marino                Nt >= bits(n)+2, then we can use Lemma \ref{lemma_graillat}
1854a238c70SJohn Marino                from algorithms.tex, which yields x^n*(1+theta) with
1864a238c70SJohn Marino                |theta| <= 2(|n|+1)*2^(-Nt), thus the error is bounded by
1874a238c70SJohn Marino                2(|n|+1) ulps <= 2^(bits(n)+2) ulps. */
1884a238c70SJohn Marino             if (MPFR_UNLIKELY (MPFR_OVERFLOW (flags)))
1894a238c70SJohn Marino               {
1904a238c70SJohn Marino               overflow:
1914a238c70SJohn Marino                 MPFR_ZIV_FREE (loop);
1924a238c70SJohn Marino                 mpfr_clear (t);
1934a238c70SJohn Marino                 MPFR_SAVE_EXPO_FREE (expo);
1944a238c70SJohn Marino                 MPFR_LOG_MSG (("overflow\n", 0));
1954a238c70SJohn Marino                 return mpfr_overflow (y, rnd, abs_n & 1 ?
1964a238c70SJohn Marino                                       MPFR_SIGN (x) : MPFR_SIGN_POS);
1974a238c70SJohn Marino               }
1984a238c70SJohn Marino             if (MPFR_UNLIKELY (MPFR_UNDERFLOW (flags)))
1994a238c70SJohn Marino               {
2004a238c70SJohn Marino                 MPFR_ZIV_FREE (loop);
2014a238c70SJohn Marino                 mpfr_clear (t);
2024a238c70SJohn Marino                 MPFR_LOG_MSG (("underflow\n", 0));
2034a238c70SJohn Marino                 if (rnd == MPFR_RNDN)
2044a238c70SJohn Marino                   {
2054a238c70SJohn Marino                     mpfr_t y2, nn;
2064a238c70SJohn Marino 
2074a238c70SJohn Marino                     /* We cannot decide now whether the result should be
2084a238c70SJohn Marino                        rounded toward zero or away from zero. So, like
2094a238c70SJohn Marino                        in mpfr_pow_pos_z, let's use the general case of
2104a238c70SJohn Marino                        mpfr_pow in precision 2. */
2114a238c70SJohn Marino                     MPFR_ASSERTD (mpfr_cmp_si_2exp (x, MPFR_SIGN (x),
2124a238c70SJohn Marino                                                     MPFR_EXP (x) - 1) != 0);
2134a238c70SJohn Marino                     mpfr_init2 (y2, 2);
2144a238c70SJohn Marino                     mpfr_init2 (nn, sizeof (long) * CHAR_BIT);
2154a238c70SJohn Marino                     inexact = mpfr_set_si (nn, n, MPFR_RNDN);
2164a238c70SJohn Marino                     MPFR_ASSERTN (inexact == 0);
2174a238c70SJohn Marino                     inexact = mpfr_pow_general (y2, x, nn, rnd, 1,
2184a238c70SJohn Marino                                                 (mpfr_save_expo_t *) NULL);
2194a238c70SJohn Marino                     mpfr_clear (nn);
2204a238c70SJohn Marino                     mpfr_set (y, y2, MPFR_RNDN);
2214a238c70SJohn Marino                     mpfr_clear (y2);
2224a238c70SJohn Marino                     MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, MPFR_FLAGS_UNDERFLOW);
2234a238c70SJohn Marino                     goto end;
2244a238c70SJohn Marino                   }
2254a238c70SJohn Marino                 else
2264a238c70SJohn Marino                   {
2274a238c70SJohn Marino                     MPFR_SAVE_EXPO_FREE (expo);
2284a238c70SJohn Marino                     return mpfr_underflow (y, rnd, abs_n & 1 ?
2294a238c70SJohn Marino                                            MPFR_SIGN (x) : MPFR_SIGN_POS);
2304a238c70SJohn Marino                   }
2314a238c70SJohn Marino               }
2324a238c70SJohn Marino             /* error estimate -- see pow function in algorithms.ps */
2334a238c70SJohn Marino             if (MPFR_LIKELY (MPFR_CAN_ROUND (t, Nt - size_n - 2, Ny, rnd)))
2344a238c70SJohn Marino               break;
2354a238c70SJohn Marino 
2364a238c70SJohn Marino             /* actualisation of the precision */
2374a238c70SJohn Marino             MPFR_ZIV_NEXT (loop, Nt);
2384a238c70SJohn Marino             mpfr_set_prec (t, Nt);
2394a238c70SJohn Marino           }
2404a238c70SJohn Marino         MPFR_ZIV_FREE (loop);
2414a238c70SJohn Marino 
2424a238c70SJohn Marino         inexact = mpfr_set (y, t, rnd);
2434a238c70SJohn Marino         mpfr_clear (t);
2444a238c70SJohn Marino 
2454a238c70SJohn Marino       end:
2464a238c70SJohn Marino         MPFR_SAVE_EXPO_FREE (expo);
2474a238c70SJohn Marino         return mpfr_check_range (y, inexact, rnd);
2484a238c70SJohn Marino       }
2494a238c70SJohn Marino     }
2504a238c70SJohn Marino }
251