12116Sjkh/* e_expf.c -- float version of e_exp.c.
22116Sjkh * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
32116Sjkh */
42116Sjkh
52116Sjkh/*
62116Sjkh * ====================================================
72116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
82116Sjkh *
92116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
102116Sjkh * Permission to use, copy, modify, and distribute this
118870Srgrimes * software is freely granted, provided that this notice
122116Sjkh * is preserved.
132116Sjkh * ====================================================
142116Sjkh */
152116Sjkh
16176451Sdas#include <sys/cdefs.h>
17176451Sdas__FBSDID("$FreeBSD$");
182116Sjkh
19226596Sdas#include <float.h>
20226596Sdas
212116Sjkh#include "math.h"
222116Sjkh#include "math_private.h"
232116Sjkh
242116Sjkhstatic const float
252116Sjkhone	= 1.0,
262116SjkhhalF[2]	= {0.5,-0.5,},
272116Sjkho_threshold=  8.8721679688e+01,  /* 0x42b17180 */
282116Sjkhu_threshold= -1.0397208405e+02,  /* 0xc2cff1b5 */
29152947Sbdeln2HI[2]   ={ 6.9314575195e-01,		/* 0x3f317200 */
30152947Sbde	     -6.9314575195e-01,},	/* 0xbf317200 */
31152947Sbdeln2LO[2]   ={ 1.4286067653e-06,  	/* 0x35bfbe8e */
32152947Sbde	     -1.4286067653e-06,},	/* 0xb5bfbe8e */
332116Sjkhinvln2 =  1.4426950216e+00, 		/* 0x3fb8aa3b */
34176032Sbde/*
35176032Sbde * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
36176032Sbde * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
37176032Sbde */
38176032SbdeP1 =  1.6666625440e-1,		/*  0xaaaa8f.0p-26 */
39176032SbdeP2 = -2.7667332906e-3;		/* -0xb55215.0p-32 */
402116Sjkh
41251024Sdasstatic volatile float
42251024Sdashuge	= 1.0e+30,
43251024Sdastwom100 = 7.8886090522e-31;      /* 2**-100=0x0d800000 */
44175468Sdas
4597407Salfredfloat
46226596Sdas__ieee754_expf(float x)
472116Sjkh{
48176074Sbde	float y,hi=0.0,lo=0.0,c,t,twopk;
4917141Sjkh	int32_t k=0,xsb;
502116Sjkh	u_int32_t hx;
512116Sjkh
522116Sjkh	GET_FLOAT_WORD(hx,x);
532116Sjkh	xsb = (hx>>31)&1;		/* sign bit of x */
54142369Sdas	hx &= 0x7fffffff;		/* high word of |x| */
552116Sjkh
562116Sjkh    /* filter out non-finite argument */
572116Sjkh	if(hx >= 0x42b17218) {			/* if |x|>=88.721... */
582116Sjkh	    if(hx>0x7f800000)
592116Sjkh		 return x+x;	 		/* NaN */
602116Sjkh            if(hx==0x7f800000)
612116Sjkh		return (xsb==0)? x:0.0;		/* exp(+-inf)={inf,0} */
622116Sjkh	    if(x > o_threshold) return huge*huge; /* overflow */
632116Sjkh	    if(x < u_threshold) return twom100*twom100; /* underflow */
642116Sjkh	}
652116Sjkh
662116Sjkh    /* argument reduction */
678870Srgrimes	if(hx > 0x3eb17218) {		/* if  |x| > 0.5 ln2 */
682116Sjkh	    if(hx < 0x3F851592) {	/* and |x| < 1.5 ln2 */
69142369Sdas		hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
702116Sjkh	    } else {
712116Sjkh		k  = invln2*x+halF[xsb];
722116Sjkh		t  = k;
73142369Sdas		hi = x - t*ln2HI[0];	/* t*ln2HI is exact here */
74142369Sdas		lo = t*ln2LO[0];
752116Sjkh	    }
76226596Sdas	    STRICT_ASSIGN(float, x, hi - lo);
778870Srgrimes	}
78218508Sdas	else if(hx < 0x39000000)  {	/* when |x|<2**-14 */
792116Sjkh	    if(huge+x>one) return one+x;/* trigger inexact */
802116Sjkh	}
812116Sjkh	else k = 0;
822116Sjkh
832116Sjkh    /* x is now in primary range */
842116Sjkh	t  = x*x;
85176074Sbde	if(k >= -125)
86176074Sbde	    SET_FLOAT_WORD(twopk,0x3f800000+(k<<23));
87176074Sbde	else
88176074Sbde	    SET_FLOAT_WORD(twopk,0x3f800000+((k+100)<<23));
89176032Sbde	c  = x - t*(P1+t*P2);
90142369Sdas	if(k==0) 	return one-((x*c)/(c-(float)2.0)-x);
91142369Sdas	else 		y = one-((lo-(x*c)/((float)2.0-c))-hi);
922116Sjkh	if(k >= -125) {
93176074Sbde	    if(k==128) return y*2.0F*0x1p127F;
94176074Sbde	    return y*twopk;
952116Sjkh	} else {
96176074Sbde	    return y*twopk*twom100;
972116Sjkh	}
982116Sjkh}
99