e_exp.c revision 97407
1169691Skan/* @(#)e_exp.c 5.1 93/09/24 */ 2169691Skan/* 3169691Skan * ==================================================== 4169691Skan * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5169691Skan * 6169691Skan * Developed at SunPro, a Sun Microsystems, Inc. business. 7169691Skan * Permission to use, copy, modify, and distribute this 8169691Skan * software is freely granted, provided that this notice 9169691Skan * is preserved. 10169691Skan * ==================================================== 11169691Skan */ 12169691Skan 13169691Skan#ifndef lint 14169691Skanstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/e_exp.c 97407 2002-05-28 17:03:12Z alfred $"; 15169691Skan#endif 16169691Skan 17169691Skan/* __ieee754_exp(x) 18169691Skan * Returns the exponential of x. 19169691Skan * 20169691Skan * Method 21169691Skan * 1. Argument reduction: 22169691Skan * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 23169691Skan * Given x, find r and integer k such that 24169691Skan * 25169691Skan * x = k*ln2 + r, |r| <= 0.5*ln2. 26169691Skan * 27169691Skan * Here r will be represented as r = hi-lo for better 28169691Skan * accuracy. 29169691Skan * 30169691Skan * 2. Approximation of exp(r) by a special rational function on 31169691Skan * the interval [0,0.34658]: 32169691Skan * Write 33169691Skan * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 34169691Skan * We use a special Reme algorithm on [0,0.34658] to generate 35169691Skan * a polynomial of degree 5 to approximate R. The maximum error 36169691Skan * of this polynomial approximation is bounded by 2**-59. In 37169691Skan * other words, 38169691Skan * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 39169691Skan * (where z=r*r, and the values of P1 to P5 are listed below) 40169691Skan * and 41169691Skan * | 5 | -59 42169691Skan * | 2.0+P1*z+...+P5*z - R(z) | <= 2 43169691Skan * | | 44169691Skan * The computation of exp(r) thus becomes 45169691Skan * 2*r 46169691Skan * exp(r) = 1 + ------- 47169691Skan * R - r 48169691Skan * r*R1(r) 49169691Skan * = 1 + r + ----------- (for better accuracy) 50169691Skan * 2 - R1(r) 51169691Skan * where 52169691Skan * 2 4 10 53169691Skan * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 54169691Skan * 55169691Skan * 3. Scale back to obtain exp(x): 56169691Skan * From step 1, we have 57169691Skan * exp(x) = 2^k * exp(r) 58169691Skan * 59169691Skan * Special cases: 60169691Skan * exp(INF) is INF, exp(NaN) is NaN; 61169691Skan * exp(-INF) is 0, and 62169691Skan * for finite argument, only exp(0)=1 is exact. 63169691Skan * 64169691Skan * Accuracy: 65169691Skan * according to an error analysis, the error is always less than 66169691Skan * 1 ulp (unit in the last place). 67169691Skan * 68169691Skan * Misc. info. 69169691Skan * For IEEE double 70169691Skan * if x > 7.09782712893383973096e+02 then exp(x) overflow 71169691Skan * if x < -7.45133219101941108420e+02 then exp(x) underflow 72169691Skan * 73169691Skan * Constants: 74169691Skan * The hexadecimal values are the intended ones for the following 75169691Skan * constants. The decimal values may be used, provided that the 76169691Skan * compiler will convert from decimal to binary accurately enough 77169691Skan * to produce the hexadecimal values shown. 78169691Skan */ 79169691Skan 80169691Skan#include "math.h" 81169691Skan#include "math_private.h" 82169691Skan 83169691Skanstatic const double 84169691Skanone = 1.0, 85169691SkanhalF[2] = {0.5,-0.5,}, 86169691Skanhuge = 1.0e+300, 87169691Skantwom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/ 88169691Skano_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ 89169691Skanu_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ 90169691Skanln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 91169691Skan -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ 92169691Skanln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 93169691Skan -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ 94169691Skaninvln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 95169691SkanP1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 96169691SkanP2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 97169691SkanP3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 98169691SkanP4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 99169691SkanP5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 100169691Skan 101169691Skan 102169691Skandouble 103169691Skan__generic___ieee754_exp(double x) /* default IEEE double exp */ 104169691Skan{ 105169691Skan double y,hi=0.0,lo=0.0,c,t; 106169691Skan int32_t k=0,xsb; 107169691Skan u_int32_t hx; 108169691Skan 109169691Skan GET_HIGH_WORD(hx,x); 110169691Skan xsb = (hx>>31)&1; /* sign bit of x */ 111169691Skan hx &= 0x7fffffff; /* high word of |x| */ 112169691Skan 113169691Skan /* filter out non-finite argument */ 114169691Skan if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 115169691Skan if(hx>=0x7ff00000) { 116169691Skan u_int32_t lx; 117169691Skan GET_LOW_WORD(lx,x); 118169691Skan if(((hx&0xfffff)|lx)!=0) 119169691Skan return x+x; /* NaN */ 120169691Skan else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 121169691Skan } 122169691Skan if(x > o_threshold) return huge*huge; /* overflow */ 123169691Skan if(x < u_threshold) return twom1000*twom1000; /* underflow */ 124169691Skan } 125169691Skan 126169691Skan /* argument reduction */ 127169691Skan if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 128169691Skan if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 129169691Skan hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 130169691Skan } else { 131169691Skan k = invln2*x+halF[xsb]; 132169691Skan t = k; 133169691Skan hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 134169691Skan lo = t*ln2LO[0]; 135169691Skan } 136169691Skan x = hi - lo; 137169691Skan } 138169691Skan else if(hx < 0x3e300000) { /* when |x|<2**-28 */ 139169691Skan if(huge+x>one) return one+x;/* trigger inexact */ 140169691Skan } 141169691Skan else k = 0; 142169691Skan 143169691Skan /* x is now in primary range */ 144169691Skan t = x*x; 145169691Skan c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 146169691Skan if(k==0) return one-((x*c)/(c-2.0)-x); 147169691Skan else y = one-((lo-(x*c)/(2.0-c))-hi); 148169691Skan if(k >= -1021) { 149169691Skan u_int32_t hy; 150169691Skan GET_HIGH_WORD(hy,y); 151169691Skan SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */ 152169691Skan return y; 153169691Skan } else { 154169691Skan u_int32_t hy; 155169691Skan GET_HIGH_WORD(hy,y); 156169691Skan SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */ 157169691Skan return y*twom1000; 158169691Skan } 159169691Skan} 160169691Skan