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