VM: full munmap
[minix.git] / lib / libm / src / e_jnf.c
blob9263cdfb42ce89a65f11aca86b5fd5d237806315
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 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: e_jnf.c,v 1.11 2010/11/29 15:10:06 drochner Exp $");
19 #endif
21 #include "math.h"
22 #include "math_private.h"
24 static const float
25 #if 0
26 invsqrtpi= 5.6418961287e-01, /* 0x3f106ebb */
27 #endif
28 two = 2.0000000000e+00, /* 0x40000000 */
29 one = 1.0000000000e+00; /* 0x3F800000 */
31 static const float zero = 0.0000000000e+00;
33 float
34 __ieee754_jnf(int n, float x)
36 int32_t i,hx,ix, sgn;
37 float a, b, temp, di;
38 float z, w;
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)
43 GET_FLOAT_WORD(hx,x);
44 ix = 0x7fffffff&hx;
45 /* if J(n,NaN) is NaN */
46 if(ix>0x7f800000) return x+x;
47 if(n<0){
48 n = -n;
49 x = -x;
50 hx ^= 0x80000000;
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 if(ix==0||ix>=0x7f800000) /* if x is 0 or inf */
57 b = zero;
58 else if((float)n<=x) {
59 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
60 a = __ieee754_j0f(x);
61 b = __ieee754_j1f(x);
62 for(i=1;i<n;i++){
63 temp = b;
64 b = b*((float)(i+i)/x) - a; /* avoid underflow */
65 a = temp;
67 } else {
68 if(ix<0x30800000) { /* x < 2**-29 */
69 /* x is tiny, return the first Taylor expansion of J(n,x)
70 * J(n,x) = 1/n!*(x/2)^n - ...
72 if(n>33) /* underflow */
73 b = zero;
74 else {
75 temp = x*(float)0.5; b = temp;
76 for (a=one,i=2;i<=n;i++) {
77 a *= (float)i; /* a = n! */
78 b *= temp; /* b = (x/2)^n */
80 b = b/a;
82 } else {
83 /* use backward recurrence */
84 /* x x^2 x^2
85 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
86 * 2n - 2(n+1) - 2(n+2)
88 * 1 1 1
89 * (for large x) = ---- ------ ------ .....
90 * 2n 2(n+1) 2(n+2)
91 * -- - ------ - ------ -
92 * x x x
94 * Let w = 2n/x and h=2/x, then the above quotient
95 * is equal to the continued fraction:
96 * 1
97 * = -----------------------
98 * 1
99 * w - -----------------
101 * w+h - ---------
102 * w+2h - ...
104 * To determine how many terms needed, let
105 * Q(0) = w, Q(1) = w(w+h) - 1,
106 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
107 * When Q(k) > 1e4 good for single
108 * When Q(k) > 1e9 good for double
109 * When Q(k) > 1e17 good for quadruple
111 /* determine k */
112 float t,v;
113 float q0,q1,h,tmp; int32_t k,m;
114 w = (n+n)/(float)x; h = (float)2.0/(float)x;
115 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1;
116 while(q1<(float)1.0e9) {
117 k += 1; z += h;
118 tmp = z*q1 - q0;
119 q0 = q1;
120 q1 = tmp;
122 m = n+n;
123 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
124 a = t;
125 b = one;
126 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
127 * Hence, if n*(log(2n/x)) > ...
128 * single 8.8722839355e+01
129 * double 7.09782712893383973096e+02
130 * long double 1.1356523406294143949491931077970765006170e+04
131 * then recurrent value may overflow and the result is
132 * likely underflow to zero
134 tmp = n;
135 v = two/x;
136 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
137 if(tmp<(float)8.8721679688e+01) {
138 for(i=n-1,di=(float)(i+i);i>0;i--){
139 temp = b;
140 b *= di;
141 b = b/x - a;
142 a = temp;
143 di -= two;
145 } else {
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 /* scale b to avoid spurious overflow */
153 if(b>(float)1e10) {
154 a /= b;
155 t /= b;
156 b = one;
160 z = __ieee754_j0f(x);
161 w = __ieee754_j1f(x);
162 if (fabsf(z) >= fabsf(w))
163 b = (t*z/b);
164 else
165 b = (t*w/a);
168 if(sgn==1) return -b; else return b;
171 float
172 __ieee754_ynf(int n, float x)
174 int32_t i,hx,ix,ib;
175 int32_t sign;
176 float a, b, temp;
178 GET_FLOAT_WORD(hx,x);
179 ix = 0x7fffffff&hx;
180 /* if Y(n,NaN) is NaN */
181 if(ix>0x7f800000) return x+x;
182 if(ix==0) return -one/zero;
183 if(hx<0) return zero/zero;
184 sign = 1;
185 if(n<0){
186 n = -n;
187 sign = 1 - ((n&1)<<1);
189 if(n==0) return(__ieee754_y0f(x));
190 if(n==1) return(sign*__ieee754_y1f(x));
191 if(ix==0x7f800000) return zero;
193 a = __ieee754_y0f(x);
194 b = __ieee754_y1f(x);
195 /* quit if b is -inf */
196 GET_FLOAT_WORD(ib,b);
197 for(i=1;i<n&&(uint32_t)ib!=0xff800000;i++){
198 temp = b;
199 b = ((float)(i+i)/x)*b - a;
200 GET_FLOAT_WORD(ib,b);
201 a = temp;
203 if(sign>0) return b; else return -b;