xref: /netbsd-src/external/lgpl3/mpfr/dist/src/const_pi.c (revision ec6772edaf0cdcb5f52a48f4aca5e33a8fb8ecfd)
1 /* mpfr_const_pi -- compute Pi
2 
3 Copyright 1999-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #include "mpfr-impl.h"
24 
25 /* Declare the cache */
26 #ifndef MPFR_USE_LOGGING
MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_pi,mpfr_const_pi_internal)27 MPFR_DECL_INIT_CACHE (__gmpfr_cache_const_pi, mpfr_const_pi_internal)
28 #else
29 MPFR_DECL_INIT_CACHE (__gmpfr_normal_pi, mpfr_const_pi_internal)
30 MPFR_DECL_INIT_CACHE (__gmpfr_logging_pi, mpfr_const_pi_internal)
31 MPFR_THREAD_VAR (mpfr_cache_ptr, __gmpfr_cache_const_pi, __gmpfr_normal_pi)
32 #endif
33 
34 /* Set User Interface */
35 #undef mpfr_const_pi
36 int
37 mpfr_const_pi (mpfr_ptr x, mpfr_rnd_t rnd_mode) {
38   return mpfr_cache (x, __gmpfr_cache_const_pi, rnd_mode);
39 }
40 
41 /* The algorithm used here is taken from Section 8.2.5 of the book
42    "Fast Algorithms: A Multitape Turing Machine Implementation"
43    by A. Schönhage, A. F. W. Grotefeld and E. Vetter, 1994.
44    It is a clever form of Brent-Salamin formula. */
45 
46 /* Don't need to save/restore exponent range: the cache does it */
47 int
mpfr_const_pi_internal(mpfr_ptr x,mpfr_rnd_t rnd_mode)48 mpfr_const_pi_internal (mpfr_ptr x, mpfr_rnd_t rnd_mode)
49 {
50   mpfr_t a, A, B, D, S;
51   mpfr_prec_t px, p, cancel, k, kmax;
52   MPFR_GROUP_DECL (group);
53   MPFR_ZIV_DECL (loop);
54   int inex;
55 
56   MPFR_LOG_FUNC
57     (("rnd_mode=%d", rnd_mode),
58      ("x[%Pd]=%.*Rg inexact=%d", mpfr_get_prec(x), mpfr_log_prec, x, inex));
59 
60   px = MPFR_PREC (x);
61 
62   /* we need 9*2^kmax - 4 >= px+2*kmax+8 */
63   for (kmax = 2; ((px + 2 * kmax + 12) / 9) >> kmax; kmax ++);
64 
65   p = px + 3 * kmax + 14; /* guarantees no recomputation for px <= 10000 */
66 
67   MPFR_GROUP_INIT_5 (group, p, a, A, B, D, S);
68 
69   MPFR_ZIV_INIT (loop, p);
70   for (;;) {
71     mpfr_set_ui (a, 1, MPFR_RNDN);          /* a = 1 */
72     mpfr_set_ui (A, 1, MPFR_RNDN);          /* A = a^2 = 1 */
73     mpfr_set_ui_2exp (B, 1, -1, MPFR_RNDN); /* B = b^2 = 1/2 */
74     mpfr_set_ui_2exp (D, 1, -2, MPFR_RNDN); /* D = 1/4 */
75 
76 #define b B
77 #define ap a
78 #define Ap A
79 #define Bp B
80     for (k = 0; ; k++)
81       {
82         /* invariant: 1/2 <= B <= A <= a < 1 */
83         mpfr_add (S, A, B, MPFR_RNDN); /* 1 <= S <= 2 */
84         mpfr_div_2ui (S, S, 2, MPFR_RNDN); /* exact, 1/4 <= S <= 1/2 */
85         mpfr_sqrt (b, B, MPFR_RNDN); /* 1/2 <= b <= 1 */
86         mpfr_add (ap, a, b, MPFR_RNDN); /* 1 <= ap <= 2 */
87         mpfr_div_2ui (ap, ap, 1, MPFR_RNDN); /* exact, 1/2 <= ap <= 1 */
88         mpfr_sqr (Ap, ap, MPFR_RNDN); /* 1/4 <= Ap <= 1 */
89         mpfr_sub (Bp, Ap, S, MPFR_RNDN); /* -1/4 <= Bp <= 3/4 */
90         mpfr_mul_2ui (Bp, Bp, 1, MPFR_RNDN); /* -1/2 <= Bp <= 3/2 */
91         mpfr_sub (S, Ap, Bp, MPFR_RNDN);
92         MPFR_ASSERTD (mpfr_cmp_ui (S, 1) < 0);
93         cancel = MPFR_NOTZERO (S) ? (mpfr_uexp_t) -mpfr_get_exp(S) : p;
94         /* MPFR_ASSERTN (cancel >= px || cancel >= 9 * (1 << k) - 4); */
95         mpfr_mul_2ui (S, S, k, MPFR_RNDN);
96         mpfr_sub (D, D, S, MPFR_RNDN);
97         /* stop when |A_k - B_k| <= 2^(k-p) i.e. cancel >= p-k */
98         if (cancel >= p - k)
99           break;
100       }
101 #undef b
102 #undef ap
103 #undef Ap
104 #undef Bp
105 
106       mpfr_div (A, B, D, MPFR_RNDN);
107 
108       /* MPFR_ASSERTN(p >= 2 * k + 8); */
109       if (MPFR_LIKELY (MPFR_CAN_ROUND (A, p - 2 * k - 8, px, rnd_mode)))
110         break;
111 
112       p += kmax;
113       MPFR_ZIV_NEXT (loop, p);
114       MPFR_GROUP_REPREC_5 (group, p, a, A, B, D, S);
115   }
116   MPFR_ZIV_FREE (loop);
117   inex = mpfr_set (x, A, rnd_mode);
118 
119   MPFR_GROUP_CLEAR (group);
120 
121   return inex;
122 }
123