xref: /dflybsd-src/contrib/mpfr/src/mpn_exp.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_mpn_exp -- auxiliary function for mpfr_get_str and mpfr_set_str
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 
244a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
254a238c70SJohn Marino #include "mpfr-impl.h"
264a238c70SJohn Marino 
274a238c70SJohn Marino /* this function computes an approximation of b^e in {a, n}, with exponent
284a238c70SJohn Marino    stored in exp_r. The computed value is rounded toward zero (truncated).
294a238c70SJohn Marino    It returns an integer f such that the final error is bounded by 2^f ulps,
304a238c70SJohn Marino    that is:
314a238c70SJohn Marino    a*2^exp_r <= b^e <= 2^exp_r (a + 2^f),
324a238c70SJohn Marino    where a represents {a, n}, i.e. the integer
334a238c70SJohn Marino    a[0] + a[1]*B + ... + a[n-1]*B^(n-1) where B=2^GMP_NUMB_BITS
344a238c70SJohn Marino 
354a238c70SJohn Marino    Return -1 is the result is exact.
364a238c70SJohn Marino    Return -2 if an overflow occurred in the computation of exp_r.
374a238c70SJohn Marino */
384a238c70SJohn Marino 
394a238c70SJohn Marino long
mpfr_mpn_exp(mp_limb_t * a,mpfr_exp_t * exp_r,int b,mpfr_exp_t e,size_t n)404a238c70SJohn Marino mpfr_mpn_exp (mp_limb_t *a, mpfr_exp_t *exp_r, int b, mpfr_exp_t e, size_t n)
414a238c70SJohn Marino {
424a238c70SJohn Marino   mp_limb_t *c, B;
434a238c70SJohn Marino   mpfr_exp_t f, h;
444a238c70SJohn Marino   int i;
454a238c70SJohn Marino   unsigned long t; /* number of bits in e */
464a238c70SJohn Marino   unsigned long bits;
474a238c70SJohn Marino   size_t n1;
484a238c70SJohn Marino   unsigned int error;           /* (number - 1) of loop a^2b inexact */
494a238c70SJohn Marino                                  /* error == t means no error */
504a238c70SJohn Marino   int err_s_a2 = 0;
514a238c70SJohn Marino   int err_s_ab = 0;              /* number of error when shift A^2, AB */
524a238c70SJohn Marino   MPFR_TMP_DECL(marker);
534a238c70SJohn Marino 
544a238c70SJohn Marino   MPFR_ASSERTN(e > 0);
554a238c70SJohn Marino   MPFR_ASSERTN((2 <= b) && (b <= 62));
564a238c70SJohn Marino 
574a238c70SJohn Marino   MPFR_TMP_MARK(marker);
584a238c70SJohn Marino 
594a238c70SJohn Marino   /* initialization of a, b, f, h */
604a238c70SJohn Marino 
614a238c70SJohn Marino   /* normalize the base */
624a238c70SJohn Marino   B = (mp_limb_t) b;
634a238c70SJohn Marino   count_leading_zeros (h, B);
644a238c70SJohn Marino 
654a238c70SJohn Marino   bits = GMP_NUMB_BITS - h;
664a238c70SJohn Marino 
674a238c70SJohn Marino   B = B << h;
684a238c70SJohn Marino   h = - h;
694a238c70SJohn Marino 
704a238c70SJohn Marino   /* allocate space for A and set it to B */
714a238c70SJohn Marino   c = MPFR_TMP_LIMBS_ALLOC (2 * n);
724a238c70SJohn Marino   a [n - 1] = B;
734a238c70SJohn Marino   MPN_ZERO (a, n - 1);
744a238c70SJohn Marino   /* initial exponent for A: invariant is A = {a, n} * 2^f */
754a238c70SJohn Marino   f = h - (n - 1) * GMP_NUMB_BITS;
764a238c70SJohn Marino 
774a238c70SJohn Marino   /* determine number of bits in e */
784a238c70SJohn Marino   count_leading_zeros (t, (mp_limb_t) e);
794a238c70SJohn Marino 
804a238c70SJohn Marino   t = GMP_NUMB_BITS - t; /* number of bits of exponent e */
814a238c70SJohn Marino 
824a238c70SJohn Marino   error = t; /* error <= GMP_NUMB_BITS */
834a238c70SJohn Marino 
844a238c70SJohn Marino   MPN_ZERO (c, 2 * n);
854a238c70SJohn Marino 
864a238c70SJohn Marino   for (i = t - 2; i >= 0; i--)
874a238c70SJohn Marino     {
884a238c70SJohn Marino 
894a238c70SJohn Marino       /* determine precision needed */
904a238c70SJohn Marino       bits = n * GMP_NUMB_BITS - mpn_scan1 (a, 0);
914a238c70SJohn Marino       n1 = (n * GMP_NUMB_BITS - bits) / GMP_NUMB_BITS;
924a238c70SJohn Marino 
934a238c70SJohn Marino       /* square of A : {c+2n1, 2(n-n1)} = {a+n1, n-n1}^2 */
944a238c70SJohn Marino       mpn_sqr_n (c + 2 * n1, a + n1, n - n1);
954a238c70SJohn Marino 
964a238c70SJohn Marino       /* set {c+n, 2n1-n} to 0 : {c, n} = {a, n}^2*K^n */
974a238c70SJohn Marino 
984a238c70SJohn Marino       /* check overflow on f */
994a238c70SJohn Marino       if (MPFR_UNLIKELY(f < MPFR_EXP_MIN/2 || f > MPFR_EXP_MAX/2))
1004a238c70SJohn Marino         {
1014a238c70SJohn Marino         overflow:
1024a238c70SJohn Marino           MPFR_TMP_FREE(marker);
1034a238c70SJohn Marino           return -2;
1044a238c70SJohn Marino         }
1054a238c70SJohn Marino       /* FIXME: Could f = 2*f + n * GMP_NUMB_BITS be used? */
1064a238c70SJohn Marino       f = 2*f;
1074a238c70SJohn Marino       MPFR_SADD_OVERFLOW (f, f, n * GMP_NUMB_BITS,
1084a238c70SJohn Marino                           mpfr_exp_t, mpfr_uexp_t,
1094a238c70SJohn Marino                           MPFR_EXP_MIN, MPFR_EXP_MAX,
1104a238c70SJohn Marino                           goto overflow, goto overflow);
1114a238c70SJohn Marino       if ((c[2*n - 1] & MPFR_LIMB_HIGHBIT) == 0)
1124a238c70SJohn Marino         {
1134a238c70SJohn Marino           /* shift A by one bit to the left */
1144a238c70SJohn Marino           mpn_lshift (a, c + n, n, 1);
1154a238c70SJohn Marino           a[0] |= mpn_lshift (c + n - 1, c + n - 1, 1, 1);
1164a238c70SJohn Marino           f --;
1174a238c70SJohn Marino           if (error != t)
1184a238c70SJohn Marino             err_s_a2 ++;
1194a238c70SJohn Marino         }
1204a238c70SJohn Marino       else
1214a238c70SJohn Marino         MPN_COPY (a, c + n, n);
1224a238c70SJohn Marino 
1234a238c70SJohn Marino       if ((error == t) && (2 * n1 <= n) &&
1244a238c70SJohn Marino           (mpn_scan1 (c + 2 * n1, 0) < (n - 2 * n1) * GMP_NUMB_BITS))
1254a238c70SJohn Marino         error = i;
1264a238c70SJohn Marino 
1274a238c70SJohn Marino       if (e & ((mpfr_exp_t) 1 << i))
1284a238c70SJohn Marino         {
1294a238c70SJohn Marino           /* multiply A by B */
1304a238c70SJohn Marino           c[2 * n - 1] = mpn_mul_1 (c + n - 1, a, n, B);
1314a238c70SJohn Marino           f += h + GMP_NUMB_BITS;
1324a238c70SJohn Marino           if ((c[2 * n - 1] & MPFR_LIMB_HIGHBIT) == 0)
1334a238c70SJohn Marino             { /* shift A by one bit to the left */
1344a238c70SJohn Marino               mpn_lshift (a, c + n, n, 1);
1354a238c70SJohn Marino               a[0] |= mpn_lshift (c + n - 1, c + n - 1, 1, 1);
1364a238c70SJohn Marino               f --;
1374a238c70SJohn Marino             }
1384a238c70SJohn Marino           else
1394a238c70SJohn Marino             {
1404a238c70SJohn Marino               MPN_COPY (a, c + n, n);
1414a238c70SJohn Marino               if (error != t)
1424a238c70SJohn Marino                 err_s_ab ++;
1434a238c70SJohn Marino             }
1444a238c70SJohn Marino           if ((error == t) && (c[n - 1] != 0))
1454a238c70SJohn Marino             error = i;
1464a238c70SJohn Marino         }
1474a238c70SJohn Marino     }
1484a238c70SJohn Marino 
1494a238c70SJohn Marino   MPFR_TMP_FREE(marker);
1504a238c70SJohn Marino 
1514a238c70SJohn Marino   *exp_r = f;
1524a238c70SJohn Marino 
1534a238c70SJohn Marino   if (error == t)
1544a238c70SJohn Marino     return -1; /* result is exact */
1554a238c70SJohn Marino   else /* error <= t-2 <= GMP_NUMB_BITS-2
1564a238c70SJohn Marino           err_s_ab, err_s_a2 <= t-1       */
1574a238c70SJohn Marino     {
1584a238c70SJohn Marino       /* if there are p loops after the first inexact result, with
1594a238c70SJohn Marino          j shifts in a^2 and l shifts in a*b, then the final error is
1604a238c70SJohn Marino          at most 2^(p+ceil((j+1)/2)+l+1)*ulp(res).
1614a238c70SJohn Marino          This is bounded by 2^(5/2*t-1/2) where t is the number of bits of e.
1624a238c70SJohn Marino       */
1634a238c70SJohn Marino       error = error + err_s_ab + err_s_a2 / 2 + 3; /* <= 5t/2-1/2 */
1644a238c70SJohn Marino #if 0
1654a238c70SJohn Marino       if ((error - 1) >= ((n * GMP_NUMB_BITS - 1) / 2))
1664a238c70SJohn Marino         error = n * GMP_NUMB_BITS; /* result is completely wrong:
1674a238c70SJohn Marino                                          this is very unlikely since error is
1684a238c70SJohn Marino                                          at most 5/2*log_2(e), and
1694a238c70SJohn Marino                                          n * GMP_NUMB_BITS is at least
1704a238c70SJohn Marino                                          3*log_2(e) */
1714a238c70SJohn Marino #endif
1724a238c70SJohn Marino       return error;
1734a238c70SJohn Marino     }
1744a238c70SJohn Marino }
175