11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* @(#)e_exp.c 1.6 04/04/22 */ 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ==================================================== 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Permission to use, copy, modify, and distribute this 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * software is freely granted, provided that this notice 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is preserved. 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ==================================================== 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h> 14a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$"); 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* __ieee754_exp(x) 171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the exponential of x. 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Method 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Argument reduction: 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Given x, find r and integer k such that 231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * x = k*ln2 + r, |r| <= 0.5*ln2. 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Here r will be represented as r = hi-lo for better 271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * accuracy. 281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Approximation of exp(r) by a special rational function on 301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the interval [0,0.34658]: 311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Write 321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * We use a special Remes algorithm on [0,0.34658] to generate 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * a polynomial of degree 5 to approximate R. The maximum error 351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * of this polynomial approximation is bounded by 2**-59. In 361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * other words, 371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (where z=r*r, and the values of P1 to P5 are listed below) 391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * and 401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * | 5 | -59 411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * | 2.0+P1*z+...+P5*z - R(z) | <= 2 421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * | | 431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The computation of exp(r) thus becomes 441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2*r 451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * exp(r) = 1 + ------- 461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * R - r 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * r*R1(r) 481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * = 1 + r + ----------- (for better accuracy) 491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2 - R1(r) 501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * where 511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2 4 10 521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Scale back to obtain exp(x): 551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * From step 1, we have 561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * exp(x) = 2^k * exp(r) 571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Special cases: 591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * exp(INF) is INF, exp(NaN) is NaN; 601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * exp(-INF) is 0, and 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for finite argument, only exp(0)=1 is exact. 621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Accuracy: 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * according to an error analysis, the error is always less than 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1 ulp (unit in the last place). 661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Misc. info. 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * For IEEE double 691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if x > 7.09782712893383973096e+02 then exp(x) overflow 701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * if x < -7.45133219101941108420e+02 then exp(x) underflow 711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Constants: 731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The hexadecimal values are the intended ones for the following 741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * constants. The decimal values may be used, provided that the 751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * compiler will convert from decimal to binary accurately enough 761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to produce the hexadecimal values shown. 771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 79a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <float.h> 80a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math.h" 821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math_private.h" 831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const double 851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectone = 1.0, 861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjecthalF[2] = {0.5,-0.5,}, 871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecto_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ 881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectu_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ 891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ 931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectinvln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectP1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectP2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectP3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectP4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source ProjectP5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 100a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesstatic volatile double 10178419467a2f88744ae2445fca5eb442877ebb1b0Elliott Hugheshuge = 1.0e+300, 102a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughestwom1000= 9.33263618503218878990e-302; /* 2**-1000=0x01700000,0*/ 1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectdouble 1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__ieee754_exp(double x) /* default IEEE double exp */ 1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 107a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes double y,hi=0.0,lo=0.0,c,t,twopk; 1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int32_t k=0,xsb; 1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_int32_t hx; 1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project GET_HIGH_WORD(hx,x); 1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project xsb = (hx>>31)&1; /* sign bit of x */ 1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hx &= 0x7fffffff; /* high word of |x| */ 1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* filter out non-finite argument */ 1161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx>=0x7ff00000) { 1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_int32_t lx; 1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project GET_LOW_WORD(lx,x); 1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(((hx&0xfffff)|lx)!=0) 1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return x+x; /* NaN */ 1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(x > o_threshold) return huge*huge; /* overflow */ 1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(x < u_threshold) return twom1000*twom1000; /* underflow */ 1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* argument reduction */ 1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project k = (int)(invln2*x+halF[xsb]); 1341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project t = k; 1351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project lo = t*ln2LO[0]; 1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 138a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes STRICT_ASSIGN(double, x, hi - lo); 1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else if(hx < 0x3e300000) { /* when |x|<2**-28 */ 1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(huge+x>one) return one+x;/* trigger inexact */ 1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else k = 0; 1441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* x is now in primary range */ 1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project t = x*x; 147a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if(k >= -1021) 148a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes INSERT_WORDS(twopk,0x3ff00000+(k<<20), 0); 149a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes else 150a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes INSERT_WORDS(twopk,0x3ff00000+((k+1000)<<20), 0); 1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(k==0) return one-((x*c)/(c-2.0)-x); 1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else y = one-((lo-(x*c)/(2.0-c))-hi); 1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(k >= -1021) { 155a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (k==1024) return y*2.0*0x1p1023; 156a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes return y*twopk; 1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 158a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes return y*twopk*twom1000; 1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 161a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 162a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#if (LDBL_MANT_DIG == 53) 163a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__weak_reference(exp, expl); 164a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#endif 165