xref: /minix3/lib/libm/src/s_expm1f.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* s_expm1f.c -- float version of s_expm1.c.
2*2fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras  */
4*2fe8fb19SBen Gras 
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras  * ====================================================
7*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras  * is preserved.
13*2fe8fb19SBen Gras  * ====================================================
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: s_expm1f.c,v 1.10 2002/05/26 22:01:55 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras 
24*2fe8fb19SBen Gras static const float huge = 1.0e+30, tiny = 1.0e-30;
25*2fe8fb19SBen Gras 
26*2fe8fb19SBen Gras static const float
27*2fe8fb19SBen Gras one		= 1.0,
28*2fe8fb19SBen Gras o_threshold	= 8.8721679688e+01,/* 0x42b17180 */
29*2fe8fb19SBen Gras ln2_hi		= 6.9313812256e-01,/* 0x3f317180 */
30*2fe8fb19SBen Gras ln2_lo		= 9.0580006145e-06,/* 0x3717f7d1 */
31*2fe8fb19SBen Gras invln2		= 1.4426950216e+00,/* 0x3fb8aa3b */
32*2fe8fb19SBen Gras 	/* scaled coefficients related to expm1 */
33*2fe8fb19SBen Gras Q1  =  -3.3333335072e-02, /* 0xbd088889 */
34*2fe8fb19SBen Gras Q2  =   1.5873016091e-03, /* 0x3ad00d01 */
35*2fe8fb19SBen Gras Q3  =  -7.9365076090e-05, /* 0xb8a670cd */
36*2fe8fb19SBen Gras Q4  =   4.0082177293e-06, /* 0x36867e54 */
37*2fe8fb19SBen Gras Q5  =  -2.0109921195e-07; /* 0xb457edbb */
38*2fe8fb19SBen Gras 
39*2fe8fb19SBen Gras float
expm1f(float x)40*2fe8fb19SBen Gras expm1f(float x)
41*2fe8fb19SBen Gras {
42*2fe8fb19SBen Gras 	float y,hi,lo,c,t,e,hxs,hfx,r1;
43*2fe8fb19SBen Gras 	int32_t k,xsb;
44*2fe8fb19SBen Gras 	u_int32_t hx;
45*2fe8fb19SBen Gras 
46*2fe8fb19SBen Gras 	c = 0;
47*2fe8fb19SBen Gras 	GET_FLOAT_WORD(hx,x);
48*2fe8fb19SBen Gras 	xsb = hx&0x80000000;		/* sign bit of x */
49*2fe8fb19SBen Gras 	if(xsb==0) y=x; else y= -x;	/* y = |x| */
50*2fe8fb19SBen Gras 	hx &= 0x7fffffff;		/* high word of |x| */
51*2fe8fb19SBen Gras 
52*2fe8fb19SBen Gras     /* filter out huge and non-finite argument */
53*2fe8fb19SBen Gras 	if(hx >= 0x4195b844) {			/* if |x|>=27*ln2 */
54*2fe8fb19SBen Gras 	    if(hx >= 0x42b17218) {		/* if |x|>=88.721... */
55*2fe8fb19SBen Gras                 if(hx>0x7f800000)
56*2fe8fb19SBen Gras 		    return x+x; 	 /* NaN */
57*2fe8fb19SBen Gras 		if(hx==0x7f800000)
58*2fe8fb19SBen Gras 		    return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
59*2fe8fb19SBen Gras 	        if(x > o_threshold) return huge*huge; /* overflow */
60*2fe8fb19SBen Gras 	    }
61*2fe8fb19SBen Gras 	    if(xsb!=0) { /* x < -27*ln2, return -1.0 with inexact */
62*2fe8fb19SBen Gras 		if(x+tiny<(float)0.0)	/* raise inexact */
63*2fe8fb19SBen Gras 		return tiny-one;	/* return -1 */
64*2fe8fb19SBen Gras 	    }
65*2fe8fb19SBen Gras 	}
66*2fe8fb19SBen Gras 
67*2fe8fb19SBen Gras     /* argument reduction */
68*2fe8fb19SBen Gras 	if(hx > 0x3eb17218) {		/* if  |x| > 0.5 ln2 */
69*2fe8fb19SBen Gras 	    if(hx < 0x3F851592) {	/* and |x| < 1.5 ln2 */
70*2fe8fb19SBen Gras 		if(xsb==0)
71*2fe8fb19SBen Gras 		    {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
72*2fe8fb19SBen Gras 		else
73*2fe8fb19SBen Gras 		    {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
74*2fe8fb19SBen Gras 	    } else {
75*2fe8fb19SBen Gras 		k  = invln2*x+((xsb==0)?(float)0.5:(float)-0.5);
76*2fe8fb19SBen Gras 		t  = k;
77*2fe8fb19SBen Gras 		hi = x - t*ln2_hi;	/* t*ln2_hi is exact here */
78*2fe8fb19SBen Gras 		lo = t*ln2_lo;
79*2fe8fb19SBen Gras 	    }
80*2fe8fb19SBen Gras 	    x  = hi - lo;
81*2fe8fb19SBen Gras 	    c  = (hi-x)-lo;
82*2fe8fb19SBen Gras 	}
83*2fe8fb19SBen Gras 	else if(hx < 0x33000000) {  	/* when |x|<2**-25, return x */
84*2fe8fb19SBen Gras 	    t = huge+x;	/* return x with inexact flags when x!=0 */
85*2fe8fb19SBen Gras 	    return x - (t-(huge+x));
86*2fe8fb19SBen Gras 	}
87*2fe8fb19SBen Gras 	else k = 0;
88*2fe8fb19SBen Gras 
89*2fe8fb19SBen Gras     /* x is now in primary range */
90*2fe8fb19SBen Gras 	hfx = (float)0.5*x;
91*2fe8fb19SBen Gras 	hxs = x*hfx;
92*2fe8fb19SBen Gras 	r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
93*2fe8fb19SBen Gras 	t  = (float)3.0-r1*hfx;
94*2fe8fb19SBen Gras 	e  = hxs*((r1-t)/((float)6.0 - x*t));
95*2fe8fb19SBen Gras 	if(k==0) return x - (x*e-hxs);		/* c is 0 */
96*2fe8fb19SBen Gras 	else {
97*2fe8fb19SBen Gras 	    e  = (x*(e-c)-c);
98*2fe8fb19SBen Gras 	    e -= hxs;
99*2fe8fb19SBen Gras 	    if(k== -1) return (float)0.5*(x-e)-(float)0.5;
100*2fe8fb19SBen Gras 	    if(k==1)  {
101*2fe8fb19SBen Gras 	       	if(x < (float)-0.25) return -(float)2.0*(e-(x+(float)0.5));
102*2fe8fb19SBen Gras 	       	else 	      return  one+(float)2.0*(x-e);
103*2fe8fb19SBen Gras 	    }
104*2fe8fb19SBen Gras 	    if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
105*2fe8fb19SBen Gras 	        int32_t i;
106*2fe8fb19SBen Gras 	        y = one-(e-x);
107*2fe8fb19SBen Gras 		GET_FLOAT_WORD(i,y);
108*2fe8fb19SBen Gras 		SET_FLOAT_WORD(y,i+(k<<23));	/* add k to y's exponent */
109*2fe8fb19SBen Gras 	        return y-one;
110*2fe8fb19SBen Gras 	    }
111*2fe8fb19SBen Gras 	    t = one;
112*2fe8fb19SBen Gras 	    if(k<23) {
113*2fe8fb19SBen Gras 	        int32_t i;
114*2fe8fb19SBen Gras 	        SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */
115*2fe8fb19SBen Gras 	       	y = t-(e-x);
116*2fe8fb19SBen Gras 		GET_FLOAT_WORD(i,y);
117*2fe8fb19SBen Gras 		SET_FLOAT_WORD(y,i+(k<<23));	/* add k to y's exponent */
118*2fe8fb19SBen Gras 	   } else {
119*2fe8fb19SBen Gras 	        int32_t i;
120*2fe8fb19SBen Gras 		SET_FLOAT_WORD(t,((0x7f-k)<<23));	/* 2^-k */
121*2fe8fb19SBen Gras 	       	y = x-(e+t);
122*2fe8fb19SBen Gras 	       	y += one;
123*2fe8fb19SBen Gras 		GET_FLOAT_WORD(i,y);
124*2fe8fb19SBen Gras 		SET_FLOAT_WORD(y,i+(k<<23));	/* add k to y's exponent */
125*2fe8fb19SBen Gras 	    }
126*2fe8fb19SBen Gras 	}
127*2fe8fb19SBen Gras 	return y;
128*2fe8fb19SBen Gras }
129