Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Written by J.T. Conklin <jtc@netbsd.org>.
  3.  * Public domain.
  4.  *
  5.  * Adapted for `long double' by Ulrich Drepper <drepper@cygnus.com>.
  6.  */
  7.  
  8. /*
  9.  * The 8087 method for the exponential function is to calculate
  10.  *   exp(x) = 2^(x log2(e))
  11.  * after separating integer and fractional parts
  12.  *   x log2(e) = i + f, |f| <= .5
  13.  * 2^i is immediate but f needs to be precise for long double accuracy.
  14.  * Suppress range reduction error in computing f by the following.
  15.  * Separate x into integer and fractional parts
  16.  *   x = xi + xf, |xf| <= .5
  17.  * Separate log2(e) into the sum of an exact number c0 and small part c1.
  18.  *   c0 + c1 = log2(e) to extra precision
  19.  * Then
  20.  *   f = (c0 xi - i) + c0 xf + c1 x
  21.  * where c0 xi is exact and so also is (c0 xi - i).
  22.  * -- moshier@na-net.ornl.gov
  23.  */
  24.  
  25. #include <math.h>
  26. #include "cephes_mconf.h"  /* for max and min log thresholds */
  27.  
  28. static long double c0 = 1.44268798828125L;
  29. static long double c1 = 7.05260771340735992468e-6L;
  30.  
  31. static long double
  32. __expl (long double x)
  33. {
  34.   long double res;
  35.   asm ("fldl2e\n\t"             /* 1  log2(e)         */
  36.        "fmul %%st(1),%%st\n\t"  /* 1  x log2(e)       */
  37.        "frndint\n\t"            /* 1  i               */
  38.        "fld %%st(1)\n\t"        /* 2  x               */
  39.        "frndint\n\t"            /* 2  xi              */
  40.        "fld %%st(1)\n\t"        /* 3  i               */
  41.        "fldt %2\n\t"            /* 4  c0              */
  42.        "fld %%st(2)\n\t"        /* 5  xi              */
  43.        "fmul %%st(1),%%st\n\t"  /* 5  c0 xi           */
  44.        "fsubp %%st,%%st(2)\n\t" /* 4  f = c0 xi  - i  */
  45.        "fld %%st(4)\n\t"        /* 5  x               */
  46.        "fsub %%st(3),%%st\n\t"  /* 5  xf = x - xi     */
  47.        "fmulp %%st,%%st(1)\n\t" /* 4  c0 xf           */
  48.        "faddp %%st,%%st(1)\n\t" /* 3  f = f + c0 xf   */
  49.        "fldt %3\n\t"            /* 4                  */
  50.        "fmul %%st(4),%%st\n\t"  /* 4  c1 * x          */
  51.        "faddp %%st,%%st(1)\n\t" /* 3  f = f + c1 * x  */
  52.        "f2xm1\n\t"              /* 3 2^(fract(x * log2(e))) - 1 */
  53.        "fld1\n\t"               /* 4 1.0              */
  54.        "faddp\n\t"              /* 3 2^(fract(x * log2(e))) */
  55.        "fstp    %%st(1)\n\t"    /* 2  */
  56.        "fscale\n\t"             /* 2 scale factor is st(1); e^x */
  57.        "fstp    %%st(1)\n\t"    /* 1  */
  58.        "fstp    %%st(1)\n\t"    /* 0  */
  59.        : "=t" (res) : "0" (x), "m" (c0), "m" (c1) : "ax", "dx");
  60.   return res;
  61. }
  62.  
  63. long double expl (long double x)
  64. {
  65.   if (x > MAXLOGL)
  66.     return INFINITY;
  67.   else if (x < MINLOGL)
  68.     return 0.0L;
  69.   else
  70.     return __expl (x);
  71. }
  72.