xref: /freebsd/lib/msun/src/e_expf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13a8617a8SJordan K. Hubbard /* e_expf.c -- float version of e_exp.c.
23a8617a8SJordan K. Hubbard  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
33a8617a8SJordan K. Hubbard  */
43a8617a8SJordan K. Hubbard 
53a8617a8SJordan K. Hubbard /*
63a8617a8SJordan K. Hubbard  * ====================================================
73a8617a8SJordan K. Hubbard  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
83a8617a8SJordan K. Hubbard  *
93a8617a8SJordan K. Hubbard  * Developed at SunPro, a Sun Microsystems, Inc. business.
103a8617a8SJordan K. Hubbard  * Permission to use, copy, modify, and distribute this
113a8617a8SJordan K. Hubbard  * software is freely granted, provided that this notice
123a8617a8SJordan K. Hubbard  * is preserved.
133a8617a8SJordan K. Hubbard  * ====================================================
143a8617a8SJordan K. Hubbard  */
153a8617a8SJordan K. Hubbard 
16f2ea2b9dSDavid Schultz #include <float.h>
17f2ea2b9dSDavid Schultz 
183a8617a8SJordan K. Hubbard #include "math.h"
193a8617a8SJordan K. Hubbard #include "math_private.h"
203a8617a8SJordan K. Hubbard 
213a8617a8SJordan K. Hubbard static const float
223a8617a8SJordan K. Hubbard one	= 1.0,
233a8617a8SJordan K. Hubbard halF[2]	= {0.5,-0.5,},
243a8617a8SJordan K. Hubbard o_threshold=  8.8721679688e+01,  /* 0x42b17180 */
253a8617a8SJordan K. Hubbard u_threshold= -1.0397208405e+02,  /* 0xc2cff1b5 */
2690880193SBruce Evans ln2HI[2]   ={ 6.9314575195e-01,		/* 0x3f317200 */
2790880193SBruce Evans 	     -6.9314575195e-01,},	/* 0xbf317200 */
2890880193SBruce Evans ln2LO[2]   ={ 1.4286067653e-06,  	/* 0x35bfbe8e */
2990880193SBruce Evans 	     -1.4286067653e-06,},	/* 0xb5bfbe8e */
303a8617a8SJordan K. Hubbard invln2 =  1.4426950216e+00, 		/* 0x3fb8aa3b */
31ce56838fSBruce Evans /*
32ce56838fSBruce Evans  * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
33ce56838fSBruce Evans  * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
34ce56838fSBruce Evans  */
35ce56838fSBruce Evans P1 =  1.6666625440e-1,		/*  0xaaaa8f.0p-26 */
36ce56838fSBruce Evans P2 = -2.7667332906e-3;		/* -0xb55215.0p-32 */
373a8617a8SJordan K. Hubbard 
387dbbb6ddSDavid Schultz static volatile float
397dbbb6ddSDavid Schultz huge	= 1.0e+30,
407dbbb6ddSDavid Schultz twom100 = 7.8886090522e-31;      /* 2**-100=0x0d800000 */
4186c2e0c0SDavid Schultz 
42a82bbc73SAlfred Perlstein float
expf(float x)43*99843eb8SSteve Kargl expf(float x)
443a8617a8SJordan K. Hubbard {
45a373e66bSBruce Evans 	float y,hi=0.0,lo=0.0,c,t,twopk;
4651295a4dSJordan K. Hubbard 	int32_t k=0,xsb;
473a8617a8SJordan K. Hubbard 	u_int32_t hx;
483a8617a8SJordan K. Hubbard 
493a8617a8SJordan K. Hubbard 	GET_FLOAT_WORD(hx,x);
503a8617a8SJordan K. Hubbard 	xsb = (hx>>31)&1;		/* sign bit of x */
51aa28340dSDavid Schultz 	hx &= 0x7fffffff;		/* high word of |x| */
523a8617a8SJordan K. Hubbard 
533a8617a8SJordan K. Hubbard     /* filter out non-finite argument */
543a8617a8SJordan K. Hubbard 	if(hx >= 0x42b17218) {			/* if |x|>=88.721... */
553a8617a8SJordan K. Hubbard 	    if(hx>0x7f800000)
563a8617a8SJordan K. Hubbard 		 return x+x;	 		/* NaN */
573a8617a8SJordan K. Hubbard             if(hx==0x7f800000)
583a8617a8SJordan K. Hubbard 		return (xsb==0)? x:0.0;		/* exp(+-inf)={inf,0} */
593a8617a8SJordan K. Hubbard 	    if(x > o_threshold) return huge*huge; /* overflow */
603a8617a8SJordan K. Hubbard 	    if(x < u_threshold) return twom100*twom100; /* underflow */
613a8617a8SJordan K. Hubbard 	}
623a8617a8SJordan K. Hubbard 
633a8617a8SJordan K. Hubbard     /* argument reduction */
643a8617a8SJordan K. Hubbard 	if(hx > 0x3eb17218) {		/* if  |x| > 0.5 ln2 */
653a8617a8SJordan K. Hubbard 	    if(hx < 0x3F851592) {	/* and |x| < 1.5 ln2 */
66aa28340dSDavid Schultz 		hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
673a8617a8SJordan K. Hubbard 	    } else {
683a8617a8SJordan K. Hubbard 		k  = invln2*x+halF[xsb];
693a8617a8SJordan K. Hubbard 		t  = k;
70aa28340dSDavid Schultz 		hi = x - t*ln2HI[0];	/* t*ln2HI is exact here */
71aa28340dSDavid Schultz 		lo = t*ln2LO[0];
723a8617a8SJordan K. Hubbard 	    }
73f2ea2b9dSDavid Schultz 	    STRICT_ASSIGN(float, x, hi - lo);
743a8617a8SJordan K. Hubbard 	}
75b775d187SDavid Schultz 	else if(hx < 0x39000000)  {	/* when |x|<2**-14 */
763a8617a8SJordan K. Hubbard 	    if(huge+x>one) return one+x;/* trigger inexact */
773a8617a8SJordan K. Hubbard 	}
783a8617a8SJordan K. Hubbard 	else k = 0;
793a8617a8SJordan K. Hubbard 
803a8617a8SJordan K. Hubbard     /* x is now in primary range */
813a8617a8SJordan K. Hubbard 	t  = x*x;
82a373e66bSBruce Evans 	if(k >= -125)
835763a8cfSDimitry Andric 	    SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+k))<<23);
84a373e66bSBruce Evans 	else
855763a8cfSDimitry Andric 	    SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+(k+100)))<<23);
86ce56838fSBruce Evans 	c  = x - t*(P1+t*P2);
87aa28340dSDavid Schultz 	if(k==0) 	return one-((x*c)/(c-(float)2.0)-x);
88aa28340dSDavid Schultz 	else 		y = one-((lo-(x*c)/((float)2.0-c))-hi);
893a8617a8SJordan K. Hubbard 	if(k >= -125) {
90a373e66bSBruce Evans 	    if(k==128) return y*2.0F*0x1p127F;
91a373e66bSBruce Evans 	    return y*twopk;
923a8617a8SJordan K. Hubbard 	} else {
93a373e66bSBruce Evans 	    return y*twopk*twom100;
943a8617a8SJordan K. Hubbard 	}
953a8617a8SJordan K. Hubbard }
96