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_exp.c 5.1 93/09/24 */ 00018 /* 00019 * ==================================================== 00020 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00021 * 00022 * Developed at SunPro, a Sun Microsystems, Inc. business. 00023 * Permission to use, copy, modify, and distribute this 00024 * software is freely granted, provided that this notice 00025 * is preserved. 00026 * ==================================================== 00027 */ 00028 00029 /* __ieee754_exp(x) 00030 * Returns the exponential of x. 00031 * 00032 * Method 00033 * 1. Argument reduction: 00034 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 00035 * Given x, find r and integer k such that 00036 * 00037 * x = k*ln2 + r, |r| <= 0.5*ln2. 00038 * 00039 * Here r will be represented as r = hi-lo for better 00040 * accuracy. 00041 * 00042 * 2. Approximation of exp(r) by a special rational function on 00043 * the interval [0,0.34658]: 00044 * Write 00045 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 00046 * We use a special Reme algorithm on [0,0.34658] to generate 00047 * a polynomial of degree 5 to approximate R. The maximum error 00048 * of this polynomial approximation is bounded by 2**-59. In 00049 * other words, 00050 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 00051 * (where z=r*r, and the values of P1 to P5 are listed below) 00052 * and 00053 * | 5 | -59 00054 * | 2.0+P1*z+...+P5*z - R(z) | <= 2 00055 * | | 00056 * The computation of exp(r) thus becomes 00057 * 2*r 00058 * exp(r) = 1 + ------- 00059 * R - r 00060 * r*R1(r) 00061 * = 1 + r + ----------- (for better accuracy) 00062 * 2 - R1(r) 00063 * where 00064 * 2 4 10 00065 * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 00066 * 00067 * 3. Scale back to obtain exp(x): 00068 * From step 1, we have 00069 * exp(x) = 2^k * exp(r) 00070 * 00071 * Special cases: 00072 * exp(INF) is INF, exp(NaN) is NaN; 00073 * exp(-INF) is 0, and 00074 * for finite argument, only exp(0)=1 is exact. 00075 * 00076 * Accuracy: 00077 * according to an error analysis, the error is always less than 00078 * 1 ulp (unit in the last place). 00079 * 00080 * Misc. info. 00081 * For IEEE double 00082 * if x > 7.09782712893383973096e+02 then exp(x) overflow 00083 * if x < -7.45133219101941108420e+02 then exp(x) underflow 00084 * 00085 * Constants: 00086 * The hexadecimal values are the intended ones for the following 00087 * constants. The decimal values may be used, provided that the 00088 * compiler will convert from decimal to binary accurately enough 00089 * to produce the hexadecimal values shown. 00090 */ 00091 00092 #ifdef POK_NEEDS_LIBMATH 00093 00094 #include "math_private.h" 00095 00096 static const double 00097 one = 1.0, 00098 halF[2] = {0.5,-0.5,}, 00099 huge = 1.0e+300, 00100 twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/ 00101 o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ 00102 u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ 00103 ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 00104 -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ 00105 ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 00106 -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ 00107 invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 00108 P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 00109 P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 00110 P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 00111 P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 00112 P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 00113 00114 00115 double 00116 __ieee754_exp(double x) /* default IEEE double exp */ 00117 { 00118 double y,hi,lo,c,t; 00119 int32_t k,xsb; 00120 uint32_t hx; 00121 00122 hi = lo = 0; 00123 k = 0; 00124 GET_HIGH_WORD(hx,x); 00125 xsb = (hx>>31)&1; /* sign bit of x */ 00126 hx &= 0x7fffffff; /* high word of |x| */ 00127 00128 /* filter out non-finite argument */ 00129 if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 00130 if(hx>=0x7ff00000) { 00131 uint32_t lx; 00132 GET_LOW_WORD(lx,x); 00133 if(((hx&0xfffff)|lx)!=0) 00134 return x+x; /* NaN */ 00135 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 00136 } 00137 if(x > o_threshold) return huge*huge; /* overflow */ 00138 if(x < u_threshold) return twom1000*twom1000; /* underflow */ 00139 } 00140 00141 /* argument reduction */ 00142 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 00143 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 00144 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 00145 } else { 00146 k = invln2*x+halF[xsb]; 00147 t = k; 00148 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 00149 lo = t*ln2LO[0]; 00150 } 00151 x = hi - lo; 00152 } 00153 else if(hx < 0x3e300000) { /* when |x|<2**-28 */ 00154 if(huge+x>one) return one+x;/* trigger inexact */ 00155 } 00156 else k = 0; 00157 00158 /* x is now in primary range */ 00159 t = x*x; 00160 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 00161 if(k==0) return one-((x*c)/(c-2.0)-x); 00162 else y = one-((lo-(x*c)/(2.0-c))-hi); 00163 if(k >= -1021) { 00164 uint32_t hy; 00165 GET_HIGH_WORD(hy,y); 00166 SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */ 00167 return y; 00168 } else { 00169 uint32_t hy; 00170 GET_HIGH_WORD(hy,y); 00171 SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */ 00172 return y*twom1000; 00173 } 00174 } 00175 #endif