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