Back to index

glibc  2.9
e_expl.c
Go to the documentation of this file.
00001 /*
00002  * Written by J.T. Conklin <jtc@netbsd.org>.
00003  * Public domain.
00004  *
00005  * Adapted for `long double' by Ulrich Drepper <drepper@cygnus.com>.
00006  */
00007 
00008 /*
00009  * The 8087 method for the exponential function is to calculate
00010  *   exp(x) = 2^(x log2(e))
00011  * after separating integer and fractional parts
00012  *   x log2(e) = i + f, |f| <= .5
00013  * 2^i is immediate but f needs to be precise for long double accuracy.
00014  * Suppress range reduction error in computing f by the following.
00015  * Separate x into integer and fractional parts
00016  *   x = xi + xf, |xf| <= .5
00017  * Separate log2(e) into the sum of an exact number c0 and small part c1.
00018  *   c0 + c1 = log2(e) to extra precision
00019  * Then
00020  *   f = (c0 xi - i) + c0 xf + c1 x
00021  * where c0 xi is exact and so also is (c0 xi - i).
00022  * -- moshier@na-net.ornl.gov
00023  */
00024 
00025 #include <math_private.h>
00026 
00027 static const long double c0 = 1.44268798828125L;
00028 static const long double c1 = 7.05260771340735992468e-6L;
00029 
00030 long double
00031 __ieee754_expl (long double x)
00032 {
00033   long double res;
00034 
00035 /* I added the following ugly construct because expl(+-Inf) resulted
00036    in NaN.  The ugliness results from the bright minds at Intel.
00037    For the i686 the code can be written better.
00038    -- drepper@cygnus.com.  */
00039   asm ("fxam\n\t"           /* Is NaN or +-Inf?  */
00040        "fstsw %%ax\n\t"
00041        "movb  $0x45, %%dh\n\t"
00042        "andb  %%ah, %%dh\n\t"
00043        "cmpb  $0x05, %%dh\n\t"
00044        "je    1f\n\t"              /* Is +-Inf, jump.    */
00045        "fldl2e\n\t"             /* 1  log2(e)         */
00046        "fmul %%st(1),%%st\n\t"  /* 1  x log2(e)       */
00047        "frndint\n\t"            /* 1  i               */
00048        "fld %%st(1)\n\t"        /* 2  x               */
00049        "frndint\n\t"            /* 2  xi              */
00050        "fld %%st(1)\n\t"        /* 3  i               */
00051        "fldt %2\n\t"            /* 4  c0              */
00052        "fld %%st(2)\n\t"        /* 5  xi              */
00053        "fmul %%st(1),%%st\n\t"  /* 5  c0 xi           */
00054        "fsubp %%st,%%st(2)\n\t" /* 4  f = c0 xi  - i  */
00055        "fld %%st(4)\n\t"        /* 5  x               */
00056        "fsub %%st(3),%%st\n\t"  /* 5  xf = x - xi     */
00057        "fmulp %%st,%%st(1)\n\t" /* 4  c0 xf           */
00058        "faddp %%st,%%st(1)\n\t" /* 3  f = f + c0 xf   */
00059        "fldt %3\n\t"            /* 4                  */
00060        "fmul %%st(4),%%st\n\t"  /* 4  c1 * x          */
00061        "faddp %%st,%%st(1)\n\t" /* 3  f = f + c1 * x  */
00062        "f2xm1\n\t"          /* 3 2^(fract(x * log2(e))) - 1 */
00063        "fld1\n\t"               /* 4 1.0              */
00064        "faddp\n\t"          /* 3 2^(fract(x * log2(e))) */
00065        "fstp  %%st(1)\n\t"    /* 2  */
00066        "fscale\n\t"          /* 2 scale factor is st(1); e^x */
00067        "fstp  %%st(1)\n\t"    /* 1  */
00068        "fstp  %%st(1)\n\t"    /* 0  */
00069        "jmp 2f\n\t"
00070        "1:\ttestl    $0x200, %%eax\n\t"   /* Test sign.  */
00071        "jz    2f\n\t"              /* If positive, jump.  */
00072        "fstp  %%st\n\t"
00073        "fldz\n\t"           /* Set result to 0.  */
00074        "2:\t\n"
00075        : "=t" (res) : "0" (x), "m" (c0), "m" (c1) : "ax", "dx");
00076   return res;
00077 }