summaryrefslogtreecommitdiff
path: root/generic.c
blob: f578f134989d51a43dc763d900288c659e3ad9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* generic file for evaluation of hypergeometric series using binary splitting

Copyright 1999, 2000, 2001 Free Software Foundation.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPdFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#ifndef GENERIC
 # error You should specify a name 
#endif

#ifdef B
#  ifndef A
 #   error B cannot be used without A
#  endif
#endif

/* Compute the first 2^m terms from the hypergeometric series
   with x = p / 2^r */
static int
GENERIC (mpfr_ptr y, mpz_srcptr p, int r, int m)
{
  int n,i,k,j,l;
  int is_p_one = 0;
  mpz_t* P,*S;
#ifdef A
  mpz_t *T;
#endif
  mpz_t* ptoj;
#ifdef R_IS_RATIONAL
  mpz_t* qtoj;
  mpfr_t tmp;
#endif
  int diff, expo;
  int precy = MPFR_PREC(y);
  TMP_DECL(marker);

  TMP_MARK(marker);
  MPFR_CLEAR_FLAGS(y); 
  n = 1 << m;
  P = (mpz_t*) TMP_ALLOC ((m+1) * sizeof(mpz_t));
  S = (mpz_t*) TMP_ALLOC ((m+1) * sizeof(mpz_t));
  ptoj = (mpz_t*) TMP_ALLOC ((m+1) * sizeof(mpz_t)); /* ptoj[i] = mantissa^(2^i) */
#ifdef A
  T = (mpz_t*) TMP_ALLOC ((m+1) * sizeof(mpz_t));
#endif
#ifdef R_IS_RATIONAL
  qtoj = (mpz_t*) TMP_ALLOC ((m+1) * sizeof(mpz_t));
#endif
  for (i=0;i<=m;i++)
    {
      mpz_init (P[i]);
      mpz_init (S[i]);
      mpz_init (ptoj[i]);
#ifdef R_IS_RATIONAL
      mpz_init (qtoj[i]);
#endif
#ifdef A
      mpz_init (T[i]);
#endif
    }
  mpz_set (ptoj[0], p);
#ifdef C
#  if C2 != 1
  mpz_mul_ui(ptoj[0], ptoj[0], C2);
#  endif
#endif
  is_p_one = !mpz_cmp_si(ptoj[0], 1);
#ifdef A
#  ifdef B
  mpz_set_ui(T[0], A1 * B1);
#  else
  mpz_set_ui(T[0], A1);
#  endif
#endif
  if (!is_p_one) 
  for (i=1;i<m;i++) mpz_mul(ptoj[i], ptoj[i-1], ptoj[i-1]);
#ifdef R_IS_RATIONAL
  mpz_set_si(qtoj[0], r);
  for (i=1;i<=m;i++) 
    {
      mpz_mul(qtoj[i], qtoj[i-1], qtoj[i-1]);
    }
#endif

  mpz_set_ui(P[0], 1);
  mpz_set_ui(S[0], 1);
  k = 0;
  for (i=1;(i < n) ;i++) {
    k++;
    
#ifdef A
#  ifdef B 
    mpz_set_ui(T[k], (A1 + A2*i)*(B1+B2*i));
#  else
    mpz_set_ui(T[k], A1 + A2*i);
#  endif
#endif
    
#ifdef C
#  ifdef NO_FACTORIAL
    mpz_set_ui(P[k], (C1 + C2 * (i-1)));
    mpz_set_ui(S[k], 1);
#  else
    mpz_set_ui(P[k], (i+1) * (C1 + C2 * (i-1)));
    mpz_set_ui(S[k], i+1);
#  endif
#else
#  ifdef NO_FACTORIAL
    mpz_set_ui(P[k], 1);
#  else
    mpz_set_ui(P[k], i+1);
#  endif
    mpz_set(S[k], P[k]);
#endif
    j=i+1; l=0; while ((j & 1) == 0) {      
      if (!is_p_one) 
	mpz_mul(S[k], S[k], ptoj[l]);
#ifdef A
#  ifdef B
#    if (A2*B2) != 1
      mpz_mul_ui(P[k], P[k], A2*B2);
#    endif
#  else
#    if A2 != 1 
      mpz_mul_ui(P[k], P[k], A2);
#  endif
#endif
      mpz_mul(S[k], S[k], T[k-1]);
#endif
      mpz_mul(S[k-1], S[k-1], P[k]);
#ifdef R_IS_RATIONAL
      mpz_mul(S[k-1], S[k-1], qtoj[l]);
#else
      mpz_mul_2exp(S[k-1], S[k-1], r*(1<<l));
#endif
      mpz_add(S[k-1], S[k-1], S[k]);
      mpz_mul(P[k-1], P[k-1], P[k]);
#ifdef A
      mpz_mul(T[k-1], T[k-1], T[k]);
#endif
      l++; j>>=1; k--;
    }
  }

  diff = mpz_sizeinbase(S[0],2) - 2*precy;
  expo = diff;
  if (diff >=0)
    {
      mpz_div_2exp(S[0],S[0],diff);
    } else 
      {
	mpz_mul_2exp(S[0],S[0],-diff);
      }
  diff = mpz_sizeinbase(P[0],2) - precy;
  expo -= diff;
  if (diff >=0)
    {
      mpz_div_2exp(P[0],P[0],diff);
    } else
      {
	mpz_mul_2exp(P[0],P[0],-diff);
	}

  mpz_tdiv_q(S[0], S[0], P[0]);
  mpfr_set_z(y, S[0], GMP_RNDD);
  MPFR_EXP(y) += expo; 

#ifdef R_IS_RATIONAL
  /* exact division */
  mpz_div_ui (qtoj[m], qtoj[m], r);
  mpfr_init2 (tmp, MPFR_PREC(y));
  mpfr_set_z (tmp, qtoj[m] , GMP_RNDD);
  mpfr_div (y, y, tmp, GMP_RNDD);
  mpfr_clear (tmp);
#else
  mpfr_div_2ui(y, y, r*(i-1), GMP_RNDN);
#endif
  for (i=0;i<=m;i++)
    {
      mpz_clear (P[i]);
      mpz_clear (S[i]);
      mpz_clear (ptoj[i]); 
#ifdef R_IS_RATIONAL
      mpz_clear (qtoj[i]);
#endif
#ifdef A
      mpz_clear (T[i]);
#endif
    }
  TMP_FREE(marker);
  return 0;
}