xref: /dflybsd-src/contrib/mpfr/src/const_log2.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_const_log2 -- compute natural logarithm of 2
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 1999, 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 /* Declare the cache */
274a238c70SJohn Marino #ifndef MPFR_USE_LOGGING
284a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_log2, mpfr_const_log2_internal);
294a238c70SJohn Marino #else
304a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_normal_log2, mpfr_const_log2_internal);
314a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_logging_log2, mpfr_const_log2_internal);
324a238c70SJohn Marino mpfr_cache_ptr MPFR_THREAD_ATTR __gmpfr_cache_const_log2 = __gmpfr_normal_log2;
334a238c70SJohn Marino #endif
344a238c70SJohn Marino 
354a238c70SJohn Marino /* Set User interface */
364a238c70SJohn Marino #undef mpfr_const_log2
374a238c70SJohn Marino int
mpfr_const_log2(mpfr_ptr x,mpfr_rnd_t rnd_mode)384a238c70SJohn Marino mpfr_const_log2 (mpfr_ptr x, mpfr_rnd_t rnd_mode) {
394a238c70SJohn Marino   return mpfr_cache (x, __gmpfr_cache_const_log2, rnd_mode);
404a238c70SJohn Marino }
414a238c70SJohn Marino 
424a238c70SJohn Marino /* Auxiliary function: Compute the terms from n1 to n2 (excluded)
434a238c70SJohn Marino    3/4*sum((-1)^n*n!^2/2^n/(2*n+1)!, n = n1..n2-1).
444a238c70SJohn Marino    Numerator is T[0], denominator is Q[0],
454a238c70SJohn Marino    Compute P[0] only when need_P is non-zero.
464a238c70SJohn Marino    Need 1+ceil(log(n2-n1)/log(2)) cells in T[],P[],Q[].
474a238c70SJohn Marino */
484a238c70SJohn Marino static void
S(mpz_t * T,mpz_t * P,mpz_t * Q,unsigned long n1,unsigned long n2,int need_P)494a238c70SJohn Marino S (mpz_t *T, mpz_t *P, mpz_t *Q, unsigned long n1, unsigned long n2, int need_P)
504a238c70SJohn Marino {
514a238c70SJohn Marino   if (n2 == n1 + 1)
524a238c70SJohn Marino     {
534a238c70SJohn Marino       if (n1 == 0)
544a238c70SJohn Marino         mpz_set_ui (P[0], 3);
554a238c70SJohn Marino       else
564a238c70SJohn Marino         {
574a238c70SJohn Marino           mpz_set_ui (P[0], n1);
584a238c70SJohn Marino           mpz_neg (P[0], P[0]);
594a238c70SJohn Marino         }
604a238c70SJohn Marino       if (n1 <= (ULONG_MAX / 4 - 1) / 2)
614a238c70SJohn Marino         mpz_set_ui (Q[0], 4 * (2 * n1 + 1));
624a238c70SJohn Marino       else /* to avoid overflow in 4 * (2 * n1 + 1) */
634a238c70SJohn Marino         {
644a238c70SJohn Marino           mpz_set_ui (Q[0], n1);
654a238c70SJohn Marino           mpz_mul_2exp (Q[0], Q[0], 1);
664a238c70SJohn Marino           mpz_add_ui (Q[0], Q[0], 1);
674a238c70SJohn Marino           mpz_mul_2exp (Q[0], Q[0], 2);
684a238c70SJohn Marino         }
694a238c70SJohn Marino       mpz_set (T[0], P[0]);
704a238c70SJohn Marino     }
714a238c70SJohn Marino   else
724a238c70SJohn Marino     {
734a238c70SJohn Marino       unsigned long m = (n1 / 2) + (n2 / 2) + (n1 & 1UL & n2);
744a238c70SJohn Marino       unsigned long v, w;
754a238c70SJohn Marino 
764a238c70SJohn Marino       S (T, P, Q, n1, m, 1);
774a238c70SJohn Marino       S (T + 1, P + 1, Q + 1, m, n2, need_P);
784a238c70SJohn Marino       mpz_mul (T[0], T[0], Q[1]);
794a238c70SJohn Marino       mpz_mul (T[1], T[1], P[0]);
804a238c70SJohn Marino       mpz_add (T[0], T[0], T[1]);
814a238c70SJohn Marino       if (need_P)
824a238c70SJohn Marino         mpz_mul (P[0], P[0], P[1]);
834a238c70SJohn Marino       mpz_mul (Q[0], Q[0], Q[1]);
844a238c70SJohn Marino 
854a238c70SJohn Marino       /* remove common trailing zeroes if any */
864a238c70SJohn Marino       v = mpz_scan1 (T[0], 0);
874a238c70SJohn Marino       if (v > 0)
884a238c70SJohn Marino         {
894a238c70SJohn Marino           w = mpz_scan1 (Q[0], 0);
904a238c70SJohn Marino           if (w < v)
914a238c70SJohn Marino             v = w;
924a238c70SJohn Marino           if (need_P)
934a238c70SJohn Marino             {
944a238c70SJohn Marino               w = mpz_scan1 (P[0], 0);
954a238c70SJohn Marino               if (w < v)
964a238c70SJohn Marino                 v = w;
974a238c70SJohn Marino             }
984a238c70SJohn Marino           /* now v = min(val(T), val(Q), val(P)) */
994a238c70SJohn Marino           if (v > 0)
1004a238c70SJohn Marino             {
1014a238c70SJohn Marino               mpz_fdiv_q_2exp (T[0], T[0], v);
1024a238c70SJohn Marino               mpz_fdiv_q_2exp (Q[0], Q[0], v);
1034a238c70SJohn Marino               if (need_P)
1044a238c70SJohn Marino                 mpz_fdiv_q_2exp (P[0], P[0], v);
1054a238c70SJohn Marino             }
1064a238c70SJohn Marino         }
1074a238c70SJohn Marino     }
1084a238c70SJohn Marino }
1094a238c70SJohn Marino 
1104a238c70SJohn Marino /* Don't need to save / restore exponent range: the cache does it */
1114a238c70SJohn Marino int
mpfr_const_log2_internal(mpfr_ptr x,mpfr_rnd_t rnd_mode)1124a238c70SJohn Marino mpfr_const_log2_internal (mpfr_ptr x, mpfr_rnd_t rnd_mode)
1134a238c70SJohn Marino {
1144a238c70SJohn Marino   unsigned long n = MPFR_PREC (x);
1154a238c70SJohn Marino   mpfr_prec_t w; /* working precision */
1164a238c70SJohn Marino   unsigned long N;
1174a238c70SJohn Marino   mpz_t *T, *P, *Q;
1184a238c70SJohn Marino   mpfr_t t, q;
1194a238c70SJohn Marino   int inexact;
1204a238c70SJohn Marino   int ok = 1; /* ensures that the 1st try will give correct rounding */
1214a238c70SJohn Marino   unsigned long lgN, i;
1224a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
1234a238c70SJohn Marino 
1244a238c70SJohn Marino   MPFR_LOG_FUNC (
1254a238c70SJohn Marino     ("rnd_mode=%d", rnd_mode),
1264a238c70SJohn Marino     ("x[%Pu]=%.*Rg inex=%d", mpfr_get_prec(x), mpfr_log_prec, x, inexact));
1274a238c70SJohn Marino 
1284a238c70SJohn Marino   mpfr_init2 (t, MPFR_PREC_MIN);
1294a238c70SJohn Marino   mpfr_init2 (q, MPFR_PREC_MIN);
1304a238c70SJohn Marino 
1314a238c70SJohn Marino   if (n < 1253)
1324a238c70SJohn Marino     w = n + 10; /* ensures correct rounding for the four rounding modes,
1334a238c70SJohn Marino                    together with N = w / 3 + 1 (see below). */
1344a238c70SJohn Marino   else if (n < 2571)
1354a238c70SJohn Marino     w = n + 11; /* idem */
1364a238c70SJohn Marino   else if (n < 3983)
1374a238c70SJohn Marino     w = n + 12;
1384a238c70SJohn Marino   else if (n < 4854)
1394a238c70SJohn Marino     w = n + 13;
1404a238c70SJohn Marino   else if (n < 26248)
1414a238c70SJohn Marino     w = n + 14;
1424a238c70SJohn Marino   else
1434a238c70SJohn Marino     {
1444a238c70SJohn Marino       w = n + 15;
1454a238c70SJohn Marino       ok = 0;
1464a238c70SJohn Marino     }
1474a238c70SJohn Marino 
1484a238c70SJohn Marino   MPFR_ZIV_INIT (loop, w);
1494a238c70SJohn Marino   for (;;)
1504a238c70SJohn Marino     {
1514a238c70SJohn Marino       N = w / 3 + 1; /* Warning: do not change that (even increasing N!)
1524a238c70SJohn Marino                         without checking correct rounding in the above
1534a238c70SJohn Marino                         ranges for n. */
1544a238c70SJohn Marino 
1554a238c70SJohn Marino       /* the following are needed for error analysis (see algorithms.tex) */
1564a238c70SJohn Marino       MPFR_ASSERTD(w >= 3 && N >= 2);
1574a238c70SJohn Marino 
1584a238c70SJohn Marino       lgN = MPFR_INT_CEIL_LOG2 (N) + 1;
1594a238c70SJohn Marino       T  = (mpz_t *) (*__gmp_allocate_func) (3 * lgN * sizeof (mpz_t));
1604a238c70SJohn Marino       P  = T + lgN;
1614a238c70SJohn Marino       Q  = T + 2*lgN;
1624a238c70SJohn Marino       for (i = 0; i < lgN; i++)
1634a238c70SJohn Marino         {
1644a238c70SJohn Marino           mpz_init (T[i]);
1654a238c70SJohn Marino           mpz_init (P[i]);
1664a238c70SJohn Marino           mpz_init (Q[i]);
1674a238c70SJohn Marino         }
1684a238c70SJohn Marino 
1694a238c70SJohn Marino       S (T, P, Q, 0, N, 0);
1704a238c70SJohn Marino 
1714a238c70SJohn Marino       mpfr_set_prec (t, w);
1724a238c70SJohn Marino       mpfr_set_prec (q, w);
1734a238c70SJohn Marino 
1744a238c70SJohn Marino       mpfr_set_z (t, T[0], MPFR_RNDN);
1754a238c70SJohn Marino       mpfr_set_z (q, Q[0], MPFR_RNDN);
1764a238c70SJohn Marino       mpfr_div (t, t, q, MPFR_RNDN);
1774a238c70SJohn Marino 
1784a238c70SJohn Marino       for (i = 0; i < lgN; i++)
1794a238c70SJohn Marino         {
1804a238c70SJohn Marino           mpz_clear (T[i]);
1814a238c70SJohn Marino           mpz_clear (P[i]);
1824a238c70SJohn Marino           mpz_clear (Q[i]);
1834a238c70SJohn Marino         }
1844a238c70SJohn Marino       (*__gmp_free_func) (T, 3 * lgN * sizeof (mpz_t));
1854a238c70SJohn Marino 
1864a238c70SJohn Marino       if (MPFR_LIKELY (ok != 0
1874a238c70SJohn Marino                        || mpfr_can_round (t, w - 2, MPFR_RNDN, rnd_mode, n)))
1884a238c70SJohn Marino         break;
1894a238c70SJohn Marino 
1904a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, w);
1914a238c70SJohn Marino     }
1924a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
1934a238c70SJohn Marino 
1944a238c70SJohn Marino   inexact = mpfr_set (x, t, rnd_mode);
1954a238c70SJohn Marino 
1964a238c70SJohn Marino   mpfr_clear (t);
1974a238c70SJohn Marino   mpfr_clear (q);
1984a238c70SJohn Marino 
1994a238c70SJohn Marino   return inexact;
2004a238c70SJohn Marino }
201