POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Fri Jan 30 14:41:34 2009 00015 */ 00016 00017 /* e_jnf.c -- float version of e_jn.c. 00018 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 00019 */ 00020 00021 /* 00022 * ==================================================== 00023 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00024 * 00025 * Developed at SunPro, a Sun Microsystems, Inc. business. 00026 * Permission to use, copy, modify, and distribute this 00027 * software is freely granted, provided that this notice 00028 * is preserved. 00029 * ==================================================== 00030 */ 00031 00032 #ifdef POK_NEEDS_LIBMATH 00033 00034 #include "math_private.h" 00035 #include <libm.h> 00036 00037 static const float 00038 #if 0 00039 invsqrtpi= 5.6418961287e-01, /* 0x3f106ebb */ 00040 #endif 00041 two = 2.0000000000e+00, /* 0x40000000 */ 00042 one = 1.0000000000e+00; /* 0x3F800000 */ 00043 00044 static const float zero = 0.0000000000e+00; 00045 00046 float 00047 __ieee754_jnf(int n, float x) 00048 { 00049 int32_t i,hx,ix, sgn; 00050 float a, b, temp, di; 00051 float z, w; 00052 00053 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) 00054 * Thus, J(-n,x) = J(n,-x) 00055 */ 00056 GET_FLOAT_WORD(hx,x); 00057 ix = 0x7fffffff&hx; 00058 /* if J(n,NaN) is NaN */ 00059 if(ix>0x7f800000) return x+x; 00060 if(n<0){ 00061 n = -n; 00062 x = -x; 00063 hx ^= 0x80000000; 00064 } 00065 if(n==0) return(__ieee754_j0f(x)); 00066 if(n==1) return(__ieee754_j1f(x)); 00067 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */ 00068 x = fabsf(x); 00069 if(ix==0||ix>=0x7f800000) /* if x is 0 or inf */ 00070 b = zero; 00071 else if((float)n<=x) { 00072 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ 00073 a = __ieee754_j0f(x); 00074 b = __ieee754_j1f(x); 00075 for(i=1;i<n;i++){ 00076 temp = b; 00077 b = b*((float)(i+i)/x) - a; /* avoid underflow */ 00078 a = temp; 00079 } 00080 } else { 00081 if(ix<0x30800000) { /* x < 2**-29 */ 00082 /* x is tiny, return the first Taylor expansion of J(n,x) 00083 * J(n,x) = 1/n!*(x/2)^n - ... 00084 */ 00085 if(n>33) /* underflow */ 00086 b = zero; 00087 else { 00088 temp = x*(float)0.5; b = temp; 00089 for (a=one,i=2;i<=n;i++) { 00090 a *= (float)i; /* a = n! */ 00091 b *= temp; /* b = (x/2)^n */ 00092 } 00093 b = b/a; 00094 } 00095 } else { 00096 /* use backward recurrence */ 00097 /* x x^2 x^2 00098 * J(n,x)/J(n-1,x) = ---- ------ ------ ..... 00099 * 2n - 2(n+1) - 2(n+2) 00100 * 00101 * 1 1 1 00102 * (for large x) = ---- ------ ------ ..... 00103 * 2n 2(n+1) 2(n+2) 00104 * -- - ------ - ------ - 00105 * x x x 00106 * 00107 * Let w = 2n/x and h=2/x, then the above quotient 00108 * is equal to the continued fraction: 00109 * 1 00110 * = ----------------------- 00111 * 1 00112 * w - ----------------- 00113 * 1 00114 * w+h - --------- 00115 * w+2h - ... 00116 * 00117 * To determine how many terms needed, let 00118 * Q(0) = w, Q(1) = w(w+h) - 1, 00119 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), 00120 * When Q(k) > 1e4 good for single 00121 * When Q(k) > 1e9 good for double 00122 * When Q(k) > 1e17 good for quadruple 00123 */ 00124 /* determine k */ 00125 float t,v; 00126 float q0,q1,h,tmp; int32_t k,m; 00127 w = (n+n)/(float)x; h = (float)2.0/(float)x; 00128 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1; 00129 while(q1<(float)1.0e9) { 00130 k += 1; z += h; 00131 tmp = z*q1 - q0; 00132 q0 = q1; 00133 q1 = tmp; 00134 } 00135 m = n+n; 00136 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); 00137 a = t; 00138 b = one; 00139 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) 00140 * Hence, if n*(log(2n/x)) > ... 00141 * single 8.8722839355e+01 00142 * double 7.09782712893383973096e+02 00143 * long double 1.1356523406294143949491931077970765006170e+04 00144 * then recurrent value may overflow and the result is 00145 * likely underflow to zero 00146 */ 00147 tmp = n; 00148 v = two/x; 00149 tmp = tmp*__ieee754_logf(fabsf(v*tmp)); 00150 if(tmp<(float)8.8721679688e+01) { 00151 for(i=n-1,di=(float)(i+i);i>0;i--){ 00152 temp = b; 00153 b *= di; 00154 b = b/x - a; 00155 a = temp; 00156 di -= two; 00157 } 00158 } else { 00159 for(i=n-1,di=(float)(i+i);i>0;i--){ 00160 temp = b; 00161 b *= di; 00162 b = b/x - a; 00163 a = temp; 00164 di -= two; 00165 /* scale b to avoid spurious overflow */ 00166 if(b>(float)1e10) { 00167 a /= b; 00168 t /= b; 00169 b = one; 00170 } 00171 } 00172 } 00173 b = (t*__ieee754_j0f(x)/b); 00174 } 00175 } 00176 if(sgn==1) return -b; else return b; 00177 } 00178 00179 float 00180 __ieee754_ynf(int n, float x) 00181 { 00182 int32_t i,hx,ix,ib; 00183 int32_t sign; 00184 float a, b, temp; 00185 00186 GET_FLOAT_WORD(hx,x); 00187 ix = 0x7fffffff&hx; 00188 /* if Y(n,NaN) is NaN */ 00189 if(ix>0x7f800000) return x+x; 00190 if(ix==0) return -one/zero; 00191 if(hx<0) return zero/zero; 00192 sign = 1; 00193 if(n<0){ 00194 n = -n; 00195 sign = 1 - ((n&1)<<1); 00196 } 00197 if(n==0) return(__ieee754_y0f(x)); 00198 if(n==1) return(sign*__ieee754_y1f(x)); 00199 if(ix==0x7f800000) return zero; 00200 00201 a = __ieee754_y0f(x); 00202 b = __ieee754_y1f(x); 00203 /* quit if b is -inf */ 00204 GET_FLOAT_WORD(ib,b); 00205 for(i=1;i<n&&(uint32_t)ib!=0xff800000;i++){ 00206 temp = b; 00207 b = ((float)(i+i)/x)*b - a; 00208 GET_FLOAT_WORD(ib,b); 00209 a = temp; 00210 } 00211 if(sign>0) return b; else return -b; 00212 } 00213 00214 #endif 00215