revert between 56095 -> 55830 in arch
[AROS.git] / compiler / stdc / math / e_jnf.c
blob6230580aea50911064165fd91bbdebecde39c850
1 /* e_jnf.c -- float version of e_jn.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
16 #ifndef lint
17 static char rcsid[] = "$FreeBSD: src/lib/msun/src/e_jnf.c,v 1.11 2010/11/13 10:54:10 uqs Exp $";
18 #endif
21 * See e_jn.c for complete comments.
24 #include "math.h"
25 #include "math_private.h"
27 static const volatile float vone __attribute__ ((__section__(".rodata,\"a\" " SECTIONCOMMENT))) = 1.0, vzero __attribute__ ((__section__(".rodata,\"a\" " SECTIONCOMMENT))) = 0.0;
29 static const float
30 two = 2.0000000000e+00, /* 0x40000000 */
31 one = 1.0000000000e+00; /* 0x3F800000 */
33 static const float zero = 0.0000000000e+00;
35 float
36 __ieee754_jnf(int n, float x)
38 int32_t i,hx,ix, sgn;
39 float a, b, temp, di;
40 float z, w;
42 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
43 * Thus, J(-n,x) = J(n,-x)
45 GET_FLOAT_WORD(hx,x);
46 ix = 0x7fffffff&hx;
47 /* if J(n,NaN) is NaN */
48 if(ix>0x7f800000) return x+x;
49 if(n<0){
50 n = -n;
51 x = -x;
52 hx ^= 0x80000000;
54 if(n==0) return(__ieee754_j0f(x));
55 if(n==1) return(__ieee754_j1f(x));
56 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
57 x = fabsf(x);
58 if(ix==0||ix>=0x7f800000) /* if x is 0 or inf */
59 b = zero;
60 else if((float)n<=x) {
61 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
62 a = __ieee754_j0f(x);
63 b = __ieee754_j1f(x);
64 for(i=1;i<n;i++){
65 temp = b;
66 b = b*((float)(i+i)/x) - a; /* avoid underflow */
67 a = temp;
69 } else {
70 if(ix<0x30800000) { /* x < 2**-29 */
71 /* x is tiny, return the first Taylor expansion of J(n,x)
72 * J(n,x) = 1/n!*(x/2)^n - ...
74 if(n>33) /* underflow */
75 b = zero;
76 else {
77 temp = x*(float)0.5; b = temp;
78 for (a=one,i=2;i<=n;i++) {
79 a *= (float)i; /* a = n! */
80 b *= temp; /* b = (x/2)^n */
82 b = b/a;
84 } else {
85 /* use backward recurrence */
86 /* x x^2 x^2
87 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
88 * 2n - 2(n+1) - 2(n+2)
90 * 1 1 1
91 * (for large x) = ---- ------ ------ .....
92 * 2n 2(n+1) 2(n+2)
93 * -- - ------ - ------ -
94 * x x x
96 * Let w = 2n/x and h=2/x, then the above quotient
97 * is equal to the continued fraction:
98 * 1
99 * = -----------------------
101 * w - -----------------
103 * w+h - ---------
104 * w+2h - ...
106 * To determine how many terms needed, let
107 * Q(0) = w, Q(1) = w(w+h) - 1,
108 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
109 * When Q(k) > 1e4 good for single
110 * When Q(k) > 1e9 good for double
111 * When Q(k) > 1e17 good for quadruple
113 /* determine k */
114 float t,v;
115 float q0,q1,h,tmp; int32_t k,m;
116 w = (n+n)/(float)x; h = (float)2.0/(float)x;
117 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1;
118 while(q1<(float)1.0e9) {
119 k += 1; z += h;
120 tmp = z*q1 - q0;
121 q0 = q1;
122 q1 = tmp;
124 m = n+n;
125 for(t=zero, i = 2*(n+k); i>=m; i -= 2) 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
136 tmp = n;
137 v = two/x;
138 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
139 if(tmp<(float)8.8721679688e+01) {
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;
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>(float)1e10) {
156 a /= b;
157 t /= b;
158 b = one;
162 z = __ieee754_j0f(x);
163 w = __ieee754_j1f(x);
164 if (fabsf(z) >= fabsf(w))
165 b = (t*z/b);
166 else
167 b = (t*w/a);
170 if(sgn==1) return -b; else return b;
173 float
174 __ieee754_ynf(int n, float x)
176 int32_t i,hx,ix,ib;
177 int32_t sign;
178 float a, b, temp;
180 GET_FLOAT_WORD(hx,x);
181 ix = 0x7fffffff&hx;
182 if(ix>0x7f800000) return x+x;
183 if(ix==0) return -one/vzero;
184 if(hx<0) return vzero/vzero;
185 sign = 1;
186 if(n<0){
187 n = -n;
188 sign = 1 - ((n&1)<<1);
190 if(n==0) return(__ieee754_y0f(x));
191 if(n==1) return(sign*__ieee754_y1f(x));
192 if(ix==0x7f800000) return zero;
194 a = __ieee754_y0f(x);
195 b = __ieee754_y1f(x);
196 /* quit if b is -inf */
197 GET_FLOAT_WORD(ib,b);
198 for(i=1;i<n&&ib!=0xff800000;i++){
199 temp = b;
200 b = ((float)(i+i)/x)*b - a;
201 GET_FLOAT_WORD(ib,b);
202 a = temp;
204 if(sign>0) return b; else return -b;