xref: /dflybsd-src/contrib/mpfr/src/const_pi.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_const_pi -- compute Pi
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 /* Declare the cache */
264a238c70SJohn Marino #ifndef MPFR_USE_LOGGING
274a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_pi, mpfr_const_pi_internal);
284a238c70SJohn Marino #else
294a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_normal_pi, mpfr_const_pi_internal);
304a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_logging_pi, mpfr_const_pi_internal);
314a238c70SJohn Marino mpfr_cache_ptr MPFR_THREAD_ATTR __gmpfr_cache_const_pi = __gmpfr_normal_pi;
324a238c70SJohn Marino #endif
334a238c70SJohn Marino 
344a238c70SJohn Marino /* Set User Interface */
354a238c70SJohn Marino #undef mpfr_const_pi
364a238c70SJohn Marino int
mpfr_const_pi(mpfr_ptr x,mpfr_rnd_t rnd_mode)374a238c70SJohn Marino mpfr_const_pi (mpfr_ptr x, mpfr_rnd_t rnd_mode) {
384a238c70SJohn Marino   return mpfr_cache (x, __gmpfr_cache_const_pi, rnd_mode);
394a238c70SJohn Marino }
404a238c70SJohn Marino 
414a238c70SJohn Marino /* Don't need to save/restore exponent range: the cache does it */
424a238c70SJohn Marino int
mpfr_const_pi_internal(mpfr_ptr x,mpfr_rnd_t rnd_mode)434a238c70SJohn Marino mpfr_const_pi_internal (mpfr_ptr x, mpfr_rnd_t rnd_mode)
444a238c70SJohn Marino {
454a238c70SJohn Marino   mpfr_t a, A, B, D, S;
464a238c70SJohn Marino   mpfr_prec_t px, p, cancel, k, kmax;
474a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
484a238c70SJohn Marino   int inex;
494a238c70SJohn Marino 
504a238c70SJohn Marino   MPFR_LOG_FUNC
514a238c70SJohn Marino     (("rnd_mode=%d", rnd_mode),
524a238c70SJohn Marino      ("x[%Pu]=%.*Rg inexact=%d", mpfr_get_prec(x), mpfr_log_prec, x, inex));
534a238c70SJohn Marino 
544a238c70SJohn Marino   px = MPFR_PREC (x);
554a238c70SJohn Marino 
564a238c70SJohn Marino   /* we need 9*2^kmax - 4 >= px+2*kmax+8 */
574a238c70SJohn Marino   for (kmax = 2; ((px + 2 * kmax + 12) / 9) >> kmax; kmax ++);
584a238c70SJohn Marino 
594a238c70SJohn Marino   p = px + 3 * kmax + 14; /* guarantees no recomputation for px <= 10000 */
604a238c70SJohn Marino 
614a238c70SJohn Marino   mpfr_init2 (a, p);
624a238c70SJohn Marino   mpfr_init2 (A, p);
634a238c70SJohn Marino   mpfr_init2 (B, p);
644a238c70SJohn Marino   mpfr_init2 (D, p);
654a238c70SJohn Marino   mpfr_init2 (S, p);
664a238c70SJohn Marino 
674a238c70SJohn Marino   MPFR_ZIV_INIT (loop, p);
684a238c70SJohn Marino   for (;;) {
694a238c70SJohn Marino     mpfr_set_ui (a, 1, MPFR_RNDN);          /* a = 1 */
704a238c70SJohn Marino     mpfr_set_ui (A, 1, MPFR_RNDN);          /* A = a^2 = 1 */
714a238c70SJohn Marino     mpfr_set_ui_2exp (B, 1, -1, MPFR_RNDN); /* B = b^2 = 1/2 */
724a238c70SJohn Marino     mpfr_set_ui_2exp (D, 1, -2, MPFR_RNDN); /* D = 1/4 */
734a238c70SJohn Marino 
744a238c70SJohn Marino #define b B
754a238c70SJohn Marino #define ap a
764a238c70SJohn Marino #define Ap A
774a238c70SJohn Marino #define Bp B
784a238c70SJohn Marino     for (k = 0; ; k++)
794a238c70SJohn Marino       {
804a238c70SJohn Marino         /* invariant: 1/2 <= B <= A <= a < 1 */
814a238c70SJohn Marino         mpfr_add (S, A, B, MPFR_RNDN); /* 1 <= S <= 2 */
824a238c70SJohn Marino         mpfr_div_2ui (S, S, 2, MPFR_RNDN); /* exact, 1/4 <= S <= 1/2 */
834a238c70SJohn Marino         mpfr_sqrt (b, B, MPFR_RNDN); /* 1/2 <= b <= 1 */
844a238c70SJohn Marino         mpfr_add (ap, a, b, MPFR_RNDN); /* 1 <= ap <= 2 */
854a238c70SJohn Marino         mpfr_div_2ui (ap, ap, 1, MPFR_RNDN); /* exact, 1/2 <= ap <= 1 */
864a238c70SJohn Marino         mpfr_mul (Ap, ap, ap, MPFR_RNDN); /* 1/4 <= Ap <= 1 */
874a238c70SJohn Marino         mpfr_sub (Bp, Ap, S, MPFR_RNDN); /* -1/4 <= Bp <= 3/4 */
884a238c70SJohn Marino         mpfr_mul_2ui (Bp, Bp, 1, MPFR_RNDN); /* -1/2 <= Bp <= 3/2 */
894a238c70SJohn Marino         mpfr_sub (S, Ap, Bp, MPFR_RNDN);
904a238c70SJohn Marino         MPFR_ASSERTN (mpfr_cmp_ui (S, 1) < 0);
914a238c70SJohn Marino         cancel = mpfr_cmp_ui (S, 0) ? (mpfr_uexp_t) -mpfr_get_exp(S) : p;
924a238c70SJohn Marino         /* MPFR_ASSERTN (cancel >= px || cancel >= 9 * (1 << k) - 4); */
934a238c70SJohn Marino         mpfr_mul_2ui (S, S, k, MPFR_RNDN);
944a238c70SJohn Marino         mpfr_sub (D, D, S, MPFR_RNDN);
954a238c70SJohn Marino         /* stop when |A_k - B_k| <= 2^(k-p) i.e. cancel >= p-k */
964a238c70SJohn Marino         if (cancel + k >= p)
974a238c70SJohn Marino           break;
984a238c70SJohn Marino       }
994a238c70SJohn Marino #undef b
1004a238c70SJohn Marino #undef ap
1014a238c70SJohn Marino #undef Ap
1024a238c70SJohn Marino #undef Bp
1034a238c70SJohn Marino 
1044a238c70SJohn Marino       mpfr_div (A, B, D, MPFR_RNDN);
1054a238c70SJohn Marino 
1064a238c70SJohn Marino       /* MPFR_ASSERTN(p >= 2 * k + 8); */
1074a238c70SJohn Marino       if (MPFR_LIKELY (MPFR_CAN_ROUND (A, p - 2 * k - 8, px, rnd_mode)))
1084a238c70SJohn Marino         break;
1094a238c70SJohn Marino 
1104a238c70SJohn Marino       p += kmax;
1114a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, p);
1124a238c70SJohn Marino       mpfr_set_prec (a, p);
1134a238c70SJohn Marino       mpfr_set_prec (A, p);
1144a238c70SJohn Marino       mpfr_set_prec (B, p);
1154a238c70SJohn Marino       mpfr_set_prec (D, p);
1164a238c70SJohn Marino       mpfr_set_prec (S, p);
1174a238c70SJohn Marino   }
1184a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
1194a238c70SJohn Marino   inex = mpfr_set (x, A, rnd_mode);
1204a238c70SJohn Marino 
1214a238c70SJohn Marino   mpfr_clear (a);
1224a238c70SJohn Marino   mpfr_clear (A);
1234a238c70SJohn Marino   mpfr_clear (B);
1244a238c70SJohn Marino   mpfr_clear (D);
1254a238c70SJohn Marino   mpfr_clear (S);
1264a238c70SJohn Marino 
1274a238c70SJohn Marino   return inex;
1284a238c70SJohn Marino }
129