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_private.h> 26 27static long double c0 = 1.44268798828125L; 28static long double c1 = 7.05260771340735992468e-6L; 29 30long double 31__ieee754_expl (long double x) 32{ 33 long double res; 34 35/* I added the following ugly construct because expl(+-Inf) resulted 36 in NaN. The ugliness results from the bright minds at Intel. 37 For the i686 the code can be written better. 38 -- drepper@cygnus.com. */ 39 asm ("fxam\n\t" /* Is NaN or +-Inf? */ 40 "fstsw %%ax\n\t" 41 "movb $0x45, %%dh\n\t" 42 "andb %%ah, %%dh\n\t" 43 "cmpb $0x05, %%dh\n\t" 44 "je 1f\n\t" /* Is +-Inf, jump. */ 45 "fldl2e\n\t" /* 1 log2(e) */ 46 "fmul %%st(1),%%st\n\t" /* 1 x log2(e) */ 47 "frndint\n\t" /* 1 i */ 48 "fld %%st(1)\n\t" /* 2 x */ 49 "frndint\n\t" /* 2 xi */ 50 "fld %%st(1)\n\t" /* 3 i */ 51 "fldt %2\n\t" /* 4 c0 */ 52 "fld %%st(2)\n\t" /* 5 xi */ 53 "fmul %%st(1),%%st\n\t" /* 5 c0 xi */ 54 "fsubp %%st,%%st(2)\n\t" /* 4 f = c0 xi - i */ 55 "fld %%st(4)\n\t" /* 5 x */ 56 "fsub %%st(3),%%st\n\t" /* 5 xf = x - xi */ 57 "fmulp %%st,%%st(1)\n\t" /* 4 c0 xf */ 58 "faddp %%st,%%st(1)\n\t" /* 3 f = f + c0 xf */ 59 "fldt %3\n\t" /* 4 */ 60 "fmul %%st(4),%%st\n\t" /* 4 c1 * x */ 61 "faddp %%st,%%st(1)\n\t" /* 3 f = f + c1 * x */ 62 "f2xm1\n\t" /* 3 2^(fract(x * log2(e))) - 1 */ 63 "fld1\n\t" /* 4 1.0 */ 64 "faddp\n\t" /* 3 2^(fract(x * log2(e))) */ 65 "fstp %%st(1)\n\t" /* 2 */ 66 "fscale\n\t" /* 2 scale factor is st(1); e^x */ 67 "fstp %%st(1)\n\t" /* 1 */ 68 "fstp %%st(1)\n\t" /* 0 */ 69 "jmp 2f\n\t" 70 "1:\ttestl $0x200, %%eax\n\t" /* Test sign. */ 71 "jz 2f\n\t" /* If positive, jump. */ 72 "fstp %%st\n\t" 73 "fldz\n\t" /* Set result to 0. */ 74 "2:\t\n" 75 : "=t" (res) : "0" (x), "m" (c0), "m" (c1) : "ax", "dx"); 76 return res; 77} 78