FreeBSD and NetBSD both use derivates of Sun's math library. On FreeBSD,
[dragonfly.git] / lib / libm / 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
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  * $NetBSD: e_jnf.c,v 1.9 2002/05/26 22:01:50 wiz Exp $
16  * $DragonFly: src/lib/libm/src/e_jnf.c,v 1.1 2005/07/26 21:15:20 joerg Exp $
17  */
18
19 #include <math.h>
20 #include "math_private.h"
21
22 static const float
23 #if 0
24 invsqrtpi=  5.6418961287e-01, /* 0x3f106ebb */
25 #endif
26 two   =  2.0000000000e+00, /* 0x40000000 */
27 one   =  1.0000000000e+00; /* 0x3F800000 */
28
29 static const float zero  =  0.0000000000e+00;
30
31 float
32 jnf(int n, float x)
33 {
34         int32_t i,hx,ix, sgn;
35         float a, b, temp, di;
36         float z, w;
37
38     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
39      * Thus, J(-n,x) = J(n,-x)
40      */
41         GET_FLOAT_WORD(hx,x);
42         ix = 0x7fffffff&hx;
43     /* if J(n,NaN) is NaN */
44         if(ix>0x7f800000) return x+x;
45         if(n<0){
46                 n = -n;
47                 x = -x;
48                 hx ^= 0x80000000;
49         }
50         if(n==0) return(j0f(x));
51         if(n==1) return(j1f(x));
52         sgn = (n&1)&(hx>>31);   /* even n -- 0, odd n -- sign(x) */
53         x = fabsf(x);
54         if(ix==0||ix>=0x7f800000)       /* if x is 0 or inf */
55             b = zero;
56         else if((float)n<=x) {
57                 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
58             a = j0f(x);
59             b = j1f(x);
60             for(i=1;i<n;i++){
61                 temp = b;
62                 b = b*((float)(i+i)/x) - a; /* avoid underflow */
63                 a = temp;
64             }
65         } else {
66             if(ix<0x30800000) { /* x < 2**-29 */
67     /* x is tiny, return the first Taylor expansion of J(n,x)
68      * J(n,x) = 1/n!*(x/2)^n  - ...
69      */
70                 if(n>33)        /* underflow */
71                     b = zero;
72                 else {
73                     temp = x*(float)0.5; b = temp;
74                     for (a=one,i=2;i<=n;i++) {
75                         a *= (float)i;          /* a = n! */
76                         b *= temp;              /* b = (x/2)^n */
77                     }
78                     b = b/a;
79                 }
80             } else {
81                 /* use backward recurrence */
82                 /*                      x      x^2      x^2
83                  *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
84                  *                      2n  - 2(n+1) - 2(n+2)
85                  *
86                  *                      1      1        1
87                  *  (for large x)   =  ----  ------   ------   .....
88                  *                      2n   2(n+1)   2(n+2)
89                  *                      -- - ------ - ------ -
90                  *                       x     x         x
91                  *
92                  * Let w = 2n/x and h=2/x, then the above quotient
93                  * is equal to the continued fraction:
94                  *                  1
95                  *      = -----------------------
96                  *                     1
97                  *         w - -----------------
98                  *                        1
99                  *              w+h - ---------
100                  *                     w+2h - ...
101                  *
102                  * To determine how many terms needed, let
103                  * Q(0) = w, Q(1) = w(w+h) - 1,
104                  * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
105                  * When Q(k) > 1e4      good for single
106                  * When Q(k) > 1e9      good for double
107                  * When Q(k) > 1e17     good for quadruple
108                  */
109             /* determine k */
110                 float t,v;
111                 float q0,q1,h,tmp; int32_t k,m;
112                 w  = (n+n)/(float)x; h = (float)2.0/(float)x;
113                 q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
114                 while(q1<(float)1.0e9) {
115                         k += 1; z += h;
116                         tmp = z*q1 - q0;
117                         q0 = q1;
118                         q1 = tmp;
119                 }
120                 m = n+n;
121                 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
122                 a = t;
123                 b = one;
124                 /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
125                  *  Hence, if n*(log(2n/x)) > ...
126                  *  single 8.8722839355e+01
127                  *  double 7.09782712893383973096e+02
128                  *  long double 1.1356523406294143949491931077970765006170e+04
129                  *  then recurrent value may overflow and the result is
130                  *  likely underflow to zero
131                  */
132                 tmp = n;
133                 v = two/x;
134                 tmp = tmp*logf(fabsf(v*tmp));
135                 if(tmp<(float)8.8721679688e+01) {
136                     for(i=n-1,di=(float)(i+i);i>0;i--){
137                         temp = b;
138                         b *= di;
139                         b  = b/x - a;
140                         a = temp;
141                         di -= two;
142                     }
143                 } else {
144                     for(i=n-1,di=(float)(i+i);i>0;i--){
145                         temp = b;
146                         b *= di;
147                         b  = b/x - a;
148                         a = temp;
149                         di -= two;
150                     /* scale b to avoid spurious overflow */
151                         if(b>(float)1e10) {
152                             a /= b;
153                             t /= b;
154                             b  = one;
155                         }
156                     }
157                 }
158                 b = (t*j0f(x)/b);
159             }
160         }
161         if(sgn==1) return -b; else return b;
162 }
163
164 float
165 ynf(int n, float x)
166 {
167         int32_t i,hx,ix,ib;
168         int32_t sign;
169         float a, b, temp;
170
171         GET_FLOAT_WORD(hx,x);
172         ix = 0x7fffffff&hx;
173     /* if Y(n,NaN) is NaN */
174         if(ix>0x7f800000) return x+x;
175         if(ix==0) return -one/zero;
176         if(hx<0) return zero/zero;
177         sign = 1;
178         if(n<0){
179                 n = -n;
180                 sign = 1 - ((n&1)<<1);
181         }
182         if(n==0) return(y0f(x));
183         if(n==1) return(sign*y1f(x));
184         if(ix==0x7f800000) return zero;
185
186         a = y0f(x);
187         b = y1f(x);
188         /* quit if b is -inf */
189         GET_FLOAT_WORD(ib,b);
190         for(i=1;i<n&&ib!=0xff800000;i++){
191             temp = b;
192             b = ((float)(i+i)/x)*b - a;
193             GET_FLOAT_WORD(ib,b);
194             a = temp;
195         }
196         if(sign>0) return b; else return -b;
197 }