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