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
16#include <errno.h>
17#include <float.h>
18#include <math.h>
19#include <math_private.h>
20
21static const float
22two = 2.0000000000e+00, /* 0x40000000 */
23one = 1.0000000000e+00; /* 0x3F800000 */
24
25static const float zero = 0.0000000000e+00;
26
27float
28__ieee754_jnf(int n, float x)
29{
30 float ret;
31 {
32 int32_t i,hx,ix, sgn;
33 float a, b, temp, di;
34 float z, w;
35
36 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
37 * Thus, J(-n,x) = J(n,-x)
38 */
39 GET_FLOAT_WORD(hx,x);
40 ix = 0x7fffffff&hx;
41 /* if J(n,NaN) is NaN */
42 if(__builtin_expect(ix>0x7f800000, 0)) return x+x;
43 if(n<0){
44 n = -n;
45 x = -x;
46 hx ^= 0x80000000;
47 }
48 if(n==0) return(__ieee754_j0f(x));
49 if(n==1) return(__ieee754_j1f(x));
50 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
51 x = fabsf(x);
52 SET_RESTORE_ROUNDF (FE_TONEAREST);
53 if(__builtin_expect(ix==0||ix>=0x7f800000, 0)) /* if x is 0 or inf */
54 return sgn == 1 ? -zero : zero;
55 else if((float)n<=x) {
56 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
57 a = __ieee754_j0f(x);
58 b = __ieee754_j1f(x);
59 for(i=1;i<n;i++){
60 temp = b;
61 b = b*((double)(i+i)/x) - a; /* avoid underflow */
62 a = temp;
63 }
64 } else {
65 if(ix<0x30800000) { /* x < 2**-29 */
66 /* x is tiny, return the first Taylor expansion of J(n,x)
67 * J(n,x) = 1/n!*(x/2)^n - ...
68 */
69 if(n>33) /* underflow */
70 b = zero;
71 else {
72 temp = x*(float)0.5; b = temp;
73 for (a=one,i=2;i<=n;i++) {
74 a *= (float)i; /* a = n! */
75 b *= temp; /* b = (x/2)^n */
76 }
77 b = b/a;
78 }
79 } else {
80 /* use backward recurrence */
81 /* x x^2 x^2
82 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
83 * 2n - 2(n+1) - 2(n+2)
84 *
85 * 1 1 1
86 * (for large x) = ---- ------ ------ .....
87 * 2n 2(n+1) 2(n+2)
88 * -- - ------ - ------ -
89 * x x x
90 *
91 * Let w = 2n/x and h=2/x, then the above quotient
92 * is equal to the continued fraction:
93 * 1
94 * = -----------------------
95 * 1
96 * w - -----------------
97 * 1
98 * w+h - ---------
99 * w+2h - ...
100 *
101 * To determine how many terms needed, let
102 * Q(0) = w, Q(1) = w(w+h) - 1,
103 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
104 * When Q(k) > 1e4 good for single
105 * When Q(k) > 1e9 good for double
106 * When Q(k) > 1e17 good for quadruple
107 */
108 /* determine k */
109 float t,v;
110 float q0,q1,h,tmp; int32_t k,m;
111 w = (n+n)/(float)x; h = (float)2.0/(float)x;
112 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1;
113 while(q1<(float)1.0e9) {
114 k += 1; z += h;
115 tmp = z*q1 - q0;
116 q0 = q1;
117 q1 = tmp;
118 }
119 m = n+n;
120 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
121 a = t;
122 b = one;
123 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
124 * Hence, if n*(log(2n/x)) > ...
125 * single 8.8722839355e+01
126 * double 7.09782712893383973096e+02
127 * long double 1.1356523406294143949491931077970765006170e+04
128 * then recurrent value may overflow and the result is
129 * likely underflow to zero
130 */
131 tmp = n;
132 v = two/x;
133 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
134 if(tmp<(float)8.8721679688e+01) {
135 for(i=n-1,di=(float)(i+i);i>0;i--){
136 temp = b;
137 b *= di;
138 b = b/x - a;
139 a = temp;
140 di -= two;
141 }
142 } else {
143 for(i=n-1,di=(float)(i+i);i>0;i--){
144 temp = b;
145 b *= di;
146 b = b/x - a;
147 a = temp;
148 di -= two;
149 /* scale b to avoid spurious overflow */
150 if(b>(float)1e10) {
151 a /= b;
152 t /= b;
153 b = one;
154 }
155 }
156 }
157 /* j0() and j1() suffer enormous loss of precision at and
158 * near zero; however, we know that their zero points never
159 * coincide, so just choose the one further away from zero.
160 */
161 z = __ieee754_j0f (x);
162 w = __ieee754_j1f (x);
163 if (fabsf (z) >= fabsf (w))
164 b = (t * z / b);
165 else
166 b = (t * w / a);
167 }
168 }
169 if(sgn==1) ret = -b; else ret = b;
170 ret = math_narrow_eval (ret);
171 }
172 if (ret == 0)
173 {
174 ret = math_narrow_eval (__copysignf (FLT_MIN, ret) * FLT_MIN);
175 __set_errno (ERANGE);
176 }
177 else
178 math_check_force_underflow (ret);
179 return ret;
180}
181strong_alias (__ieee754_jnf, __jnf_finite)
182
183float
184__ieee754_ynf(int n, float x)
185{
186 float ret;
187 {
188 int32_t i,hx,ix;
189 uint32_t ib;
190 int32_t sign;
191 float a, b, temp;
192
193 GET_FLOAT_WORD(hx,x);
194 ix = 0x7fffffff&hx;
195 /* if Y(n,NaN) is NaN */
196 if(__builtin_expect(ix>0x7f800000, 0)) return x+x;
197 sign = 1;
198 if(n<0){
199 n = -n;
200 sign = 1 - ((n&1)<<1);
201 }
202 if(n==0) return(__ieee754_y0f(x));
203 if(__builtin_expect(ix==0, 0))
204 return -sign/zero;
205 if(__builtin_expect(hx<0, 0)) return zero/(zero*x);
206 SET_RESTORE_ROUNDF (FE_TONEAREST);
207 if(n==1) {
208 ret = sign*__ieee754_y1f(x);
209 goto out;
210 }
211 if(__builtin_expect(ix==0x7f800000, 0)) return zero;
212
213 a = __ieee754_y0f(x);
214 b = __ieee754_y1f(x);
215 /* quit if b is -inf */
216 GET_FLOAT_WORD(ib,b);
217 for(i=1;i<n&&ib!=0xff800000;i++){
218 temp = b;
219 b = ((double)(i+i)/x)*b - a;
220 GET_FLOAT_WORD(ib,b);
221 a = temp;
222 }
223 /* If B is +-Inf, set up errno accordingly. */
224 if (! isfinite (b))
225 __set_errno (ERANGE);
226 if(sign>0) ret = b; else ret = -b;
227 }
228 out:
229 if (isinf (ret))
230 ret = __copysignf (FLT_MAX, ret) * FLT_MAX;
231 return ret;
232}
233strong_alias (__ieee754_ynf, __ynf_finite)
234