fix namespace issues for lgamma, etc.
[musl] / src / math / jnf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  */
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15
16 #define _GNU_SOURCE
17 #include "libm.h"
18
19 static const float
20 two = 2.0000000000e+00, /* 0x40000000 */
21 one = 1.0000000000e+00; /* 0x3F800000 */
22
23 static const float zero = 0.0000000000e+00;
24
25 float jnf(int n, float x)
26 {
27         int32_t i,hx,ix, sgn;
28         float a, b, temp, di;
29         float z, w;
30
31         /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
32          * Thus, J(-n,x) = J(n,-x)
33          */
34         GET_FLOAT_WORD(hx, x);
35         ix = 0x7fffffff & hx;
36         /* if J(n,NaN) is NaN */
37         if (ix > 0x7f800000)
38                 return x+x;
39         if (n < 0) {
40                 n = -n;
41                 x = -x;
42                 hx ^= 0x80000000;
43         }
44         if (n == 0) return j0f(x);
45         if (n == 1) return j1f(x);
46
47         sgn = (n&1)&(hx>>31);  /* even n -- 0, odd n -- sign(x) */
48         x = fabsf(x);
49         if (ix == 0 || ix >= 0x7f800000)  /* if x is 0 or inf */
50                 b = zero;
51         else if((float)n <= x) {
52                 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
53                 a = j0f(x);
54                 b = j1f(x);
55                 for (i=1; i<n; i++){
56                         temp = b;
57                         b = b*((float)(i+i)/x) - a; /* avoid underflow */
58                         a = temp;
59                 }
60         } else {
61                 if (ix < 0x30800000) { /* x < 2**-29 */
62                         /* x is tiny, return the first Taylor expansion of J(n,x)
63                          * J(n,x) = 1/n!*(x/2)^n  - ...
64                          */
65                         if (n > 33)  /* underflow */
66                                 b = zero;
67                         else {
68                                 temp = 0.5f * x;
69                                 b = temp;
70                                 for (a=one,i=2; i<=n; i++) {
71                                         a *= (float)i;    /* a = n! */
72                                         b *= temp;        /* b = (x/2)^n */
73                                 }
74                                 b = b/a;
75                         }
76                 } else {
77                         /* use backward recurrence */
78                         /*                      x      x^2      x^2
79                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
80                          *                      2n  - 2(n+1) - 2(n+2)
81                          *
82                          *                      1      1        1
83                          *  (for large x)   =  ----  ------   ------   .....
84                          *                      2n   2(n+1)   2(n+2)
85                          *                      -- - ------ - ------ -
86                          *                       x     x         x
87                          *
88                          * Let w = 2n/x and h=2/x, then the above quotient
89                          * is equal to the continued fraction:
90                          *                  1
91                          *      = -----------------------
92                          *                     1
93                          *         w - -----------------
94                          *                        1
95                          *              w+h - ---------
96                          *                     w+2h - ...
97                          *
98                          * To determine how many terms needed, let
99                          * Q(0) = w, Q(1) = w(w+h) - 1,
100                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
101                          * When Q(k) > 1e4      good for single
102                          * When Q(k) > 1e9      good for double
103                          * When Q(k) > 1e17     good for quadruple
104                          */
105                         /* determine k */
106                         float t,v;
107                         float q0,q1,h,tmp;
108                         int32_t k,m;
109
110                         w = (n+n)/x;
111                         h = 2.0f/x;
112                         z = w+h;
113                         q0 = w;
114                         q1 = w*z - 1.0f;
115                         k = 1;
116                         while (q1 < 1.0e9f) {
117                                 k += 1;
118                                 z += h;
119                                 tmp = z*q1 - q0;
120                                 q0 = q1;
121                                 q1 = tmp;
122                         }
123                         m = n+n;
124                         for (t=zero, i = 2*(n+k); i>=m; i -= 2)
125                                 t = one/(i/x-t);
126                         a = t;
127                         b = one;
128                         /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
129                          *  Hence, if n*(log(2n/x)) > ...
130                          *  single 8.8722839355e+01
131                          *  double 7.09782712893383973096e+02
132                          *  long double 1.1356523406294143949491931077970765006170e+04
133                          *  then recurrent value may overflow and the result is
134                          *  likely underflow to zero
135                          */
136                         tmp = n;
137                         v = two/x;
138                         tmp = tmp*logf(fabsf(v*tmp));
139                         if (tmp < 88.721679688f) {
140                                 for (i=n-1,di=(float)(i+i); i>0; i--) {
141                                         temp = b;
142                                         b *= di;
143                                         b = b/x - a;
144                                         a = temp;
145                                         di -= two;
146                                 }
147                         } else {
148                                 for (i=n-1,di=(float)(i+i); i>0; i--){
149                                         temp = b;
150                                         b *= di;
151                                         b = b/x - a;
152                                         a = temp;
153                                         di -= two;
154                                         /* scale b to avoid spurious overflow */
155                                         if (b > 1e10f) {
156                                                 a /= b;
157                                                 t /= b;
158                                                 b = one;
159                                         }
160                                 }
161                         }
162                         z = j0f(x);
163                         w = j1f(x);
164                         if (fabsf(z) >= fabsf(w))
165                                 b = t*z/b;
166                         else
167                                 b = t*w/a;
168                 }
169         }
170         if (sgn == 1) return -b;
171         return b;
172 }
173
174 float ynf(int n, float x)
175 {
176         int32_t i,hx,ix,ib;
177         int32_t sign;
178         float a, b, temp;
179
180         GET_FLOAT_WORD(hx, x);
181         ix = 0x7fffffff & hx;
182         /* if Y(n,NaN) is NaN */
183         if (ix > 0x7f800000)
184                 return x+x;
185         if (ix == 0)
186                 return -one/zero;
187         if (hx < 0)
188                 return zero/zero;
189         sign = 1;
190         if (n < 0) {
191                 n = -n;
192                 sign = 1 - ((n&1)<<1);
193         }
194         if (n == 0)
195                 return y0f(x);
196         if (n == 1)
197                 return sign*y1f(x);
198         if (ix == 0x7f800000)
199                 return zero;
200
201         a = y0f(x);
202         b = y1f(x);
203         /* quit if b is -inf */
204         GET_FLOAT_WORD(ib,b);
205         for (i = 1; i < n && ib != 0xff800000; i++){
206                 temp = b;
207                 b = ((float)(i+i)/x)*b - a;
208                 GET_FLOAT_WORD(ib, b);
209                 a = temp;
210         }
211         if (sign > 0)
212                 return b;
213         return -b;
214 }