1 /* 2 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* __ieee754_exp(x) 27 * Returns the exponential of x. 28 * 29 * Method 30 * 1. Argument reduction: 31 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 32 * Given x, find r and integer k such that 33 * 34 * x = k*ln2 + r, |r| <= 0.5*ln2. 35 * 36 * Here r will be represented as r = hi-lo for better 37 * accuracy. 38 * 39 * 2. Approximation of exp(r) by a special rational function on 40 * the interval [0,0.34658]: 41 * Write 42 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 43 * We use a special Reme algorithm on [0,0.34658] to generate 44 * a polynomial of degree 5 to approximate R. The maximum error 45 * of this polynomial approximation is bounded by 2**-59. In 46 * other words, 47 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 48 * (where z=r*r, and the values of P1 to P5 are listed below) 49 * and 50 * | 5 | -59 51 * | 2.0+P1*z+...+P5*z - R(z) | <= 2 52 * | | 53 * The computation of exp(r) thus becomes 54 * 2*r 55 * exp(r) = 1 + ------- 56 * R - r 57 * r*R1(r) 58 * = 1 + r + ----------- (for better accuracy) 59 * 2 - R1(r) 60 * where 61 * 2 4 10 62 * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 63 * 64 * 3. Scale back to obtain exp(x): 65 * From step 1, we have 66 * exp(x) = 2^k * exp(r) 67 * 68 * Special cases: 69 * exp(INF) is INF, exp(NaN) is NaN; 70 * exp(-INF) is 0, and 71 * for finite argument, only exp(0)=1 is exact. 72 * 73 * Accuracy: 74 * according to an error analysis, the error is always less than 75 * 1 ulp (unit in the last place). 76 * 77 * Misc. info. 78 * For IEEE double 79 * if x > 7.09782712893383973096e+02 then exp(x) overflow 80 * if x < -7.45133219101941108420e+02 then exp(x) underflow 81 * 82 * Constants: 83 * The hexadecimal values are the intended ones for the following 84 * constants. The decimal values may be used, provided that the 85 * compiler will convert from decimal to binary accurately enough 86 * to produce the hexadecimal values shown. 87 */ 88 89 #include "fdlibm.h" 90 91 #ifdef __STDC__ 92 static const double 93 #else 94 static double 95 #endif 96 one = 1.0, 97 halF[2] = {0.5,-0.5,}, 98 huge = 1.0e+300, 99 twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/ 100 o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ 101 u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ 102 ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 103 -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ 104 ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 105 -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ 106 invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 107 P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 108 P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 109 P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 110 P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 111 P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 112 113 114 #ifdef __STDC__ __ieee754_exp(double x)115 double __ieee754_exp(double x) /* default IEEE double exp */ 116 #else 117 double __ieee754_exp(x) /* default IEEE double exp */ 118 double x; 119 #endif 120 { 121 double y,hi=0,lo=0,c,t; 122 int k=0,xsb; 123 unsigned hx; 124 125 hx = __HI(x); /* high word of x */ 126 xsb = (hx>>31)&1; /* sign bit of x */ 127 hx &= 0x7fffffff; /* high word of |x| */ 128 129 /* filter out non-finite argument */ 130 if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 131 if(hx>=0x7ff00000) { 132 if(((hx&0xfffff)|__LO(x))!=0) 133 return x+x; /* NaN */ 134 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 135 } 136 if(x > o_threshold) return huge*huge; /* overflow */ 137 if(x < u_threshold) return twom1000*twom1000; /* underflow */ 138 } 139 140 /* argument reduction */ 141 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 142 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 143 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 144 } else { 145 k = invln2*x+halF[xsb]; 146 t = k; 147 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 148 lo = t*ln2LO[0]; 149 } 150 x = hi - lo; 151 } 152 else if(hx < 0x3e300000) { /* when |x|<2**-28 */ 153 if(huge+x>one) return one+x;/* trigger inexact */ 154 } 155 else k = 0; 156 157 /* x is now in primary range */ 158 t = x*x; 159 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 160 if(k==0) return one-((x*c)/(c-2.0)-x); 161 else y = one-((lo-(x*c)/(2.0-c))-hi); 162 if(k >= -1021) { 163 __HI(y) += (k<<20); /* add k to y's exponent */ 164 return y; 165 } else { 166 __HI(y) += ((k+1000)<<20);/* add k to y's exponent */ 167 return y*twom1000; 168 } 169 } 170