From Beige Cheetah, 1 Year ago, written in C.
Embed
  1. one     = 1.0,
  2. halF[2] = {0.5,-0.5,},
  3. huge    = 1.0e+300,
  4. twom1000= 9.33263618503218878990e-302,     /* 2**-1000=0x01700000,0*/
  5. o_threshold=  7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
  6. u_threshold= -7.45133219101941108420e+02,  /* 0xc0874910, 0xD52D3051 */
  7. ln2HI[2]   ={ 6.93147180369123816490e-01,  /* 0x3fe62e42, 0xfee00000 */
  8.              -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
  9. ln2LO[2]   ={ 1.90821492927058770002e-10,  /* 0x3dea39ef, 0x35793c76 */
  10.              -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
  11. invln2 =  1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
  12. P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
  13. P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
  14. P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
  15. P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
  16. P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
  17.  
  18.  
  19. #ifdef __STDC__
  20.         double __ieee754_exp(double x)  /* default IEEE double exp */
  21. #else
  22.         double __ieee754_exp(x) /* default IEEE double exp */
  23.         double x;
  24. #endif
  25. {
  26.         double y,hi,lo,c,t;
  27.         int k,xsb;
  28.         unsigned hx;
  29.  
  30.         hx  = __HI(x);  /* high word of x */
  31.         xsb = (hx>>31)&1;               /* sign bit of x */
  32.         hx &= 0x7fffffff;               /* high word of |x| */
  33.  
  34.     /* filter out non-finite argument */
  35.         if(hx >= 0x40862E42) {                  /* if |x|>=709.78... */
  36.             if(hx>=0x7ff00000) {
  37.                 if(((hx&0xfffff)|__LO(x))!=0)
  38.                      return x+x;                /* NaN */
  39.                 else return (xsb==0)? x:0.0;    /* exp(+-inf)={inf,0} */
  40.             }
  41.             if(x > o_threshold) return huge*huge; /* overflow */
  42.             if(x < u_threshold) return twom1000*twom1000; /* underflow */
  43.         }
  44.  
  45.     /* argument reduction */
  46.         if(hx > 0x3fd62e42) {           /* if  |x| > 0.5 ln2 */
  47.             if(hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
  48.                 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
  49.             } else {
  50.                 k  = (int)(invln2*x+halF[xsb]);
  51.                 t  = k;
  52.                 hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
  53.                 lo = t*ln2LO[0];
  54.             }
  55.             x  = hi - lo;
  56.         }
  57.         else if(hx < 0x3e300000)  {     /* when |x|<2**-28 */
  58.             if(huge+x>one) return one+x;/* trigger inexact */
  59.         }
  60.         else k = 0;
  61.  
  62.     /* x is now in primary range */
  63.         t  = x*x;
  64.         c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  65.         if(k==0)        return one-((x*c)/(c-2.0)-x);
  66.         else            y = one-((lo-(x*c)/(2.0-c))-hi);
  67.         if(k >= -1021) {
  68.             __HI(y) += (k<<20); /* add k to y's exponent */
  69.             return y;
  70.         } else {
  71.             __HI(y) += ((k+1000)<<20);/* add k to y's exponent */
  72.             return y*twom1000;
  73.         }
  74. }