use #if LDBL_MANT_DIG == ... instead of custom LD80 etc macros
[libm] / src / math / expm1f.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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 "libm.h"
17
18 static const float
19 one         = 1.0,
20 huge        = 1.0e+30,
21 tiny        = 1.0e-30,
22 o_threshold = 8.8721679688e+01, /* 0x42b17180 */
23 ln2_hi      = 6.9313812256e-01, /* 0x3f317180 */
24 ln2_lo      = 9.0580006145e-06, /* 0x3717f7d1 */
25 invln2      = 1.4426950216e+00, /* 0x3fb8aa3b */
26 /*
27  * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
28  * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
29  * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
30  */
31 Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
32 Q2 =  1.5807170421e-3; /*  0xcf3010.0p-33 */
33
34 float expm1f(float x)
35 {
36         float y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
37         int32_t k,xsb;
38         uint32_t hx;
39
40         GET_FLOAT_WORD(hx, x);
41         xsb = hx&0x80000000;  /* sign bit of x */
42         hx &= 0x7fffffff;     /* high word of |x| */
43
44         /* filter out huge and non-finite argument */
45         if (hx >= 0x4195b844) {  /* if |x|>=27*ln2 */
46                 if (hx >= 0x42b17218) {  /* if |x|>=88.721... */
47                         if (hx > 0x7f800000)  /* NaN */
48                                 return x+x;
49                         if (hx == 0x7f800000) /* exp(+-inf)={inf,-1} */
50                                 return xsb==0 ? x : -1.0;
51                         if (x > o_threshold)
52                                 return huge*huge; /* overflow */
53                 }
54                 if (xsb != 0) {  /* x < -27*ln2 */
55                         /* raise inexact */
56                         if (x+tiny < (float)0.0)
57                                 return tiny-one;  /* return -1 */
58                 }
59         }
60
61         /* argument reduction */
62         if (hx > 0x3eb17218) {           /* if  |x| > 0.5 ln2 */
63                 if (hx < 0x3F851592) {       /* and |x| < 1.5 ln2 */
64                         if (xsb == 0) {
65                                 hi = x - ln2_hi;
66                                 lo = ln2_lo;
67                                 k =  1;
68                         } else {
69                                 hi = x + ln2_hi;
70                                 lo = -ln2_lo;
71                                 k = -1;
72                         }
73                 } else {
74                         k  = invln2*x+((xsb==0)?(float)0.5:(float)-0.5);
75                         t  = k;
76                         hi = x - t*ln2_hi;      /* t*ln2_hi is exact here */
77                         lo = t*ln2_lo;
78                 }
79                 STRICT_ASSIGN(float, x, hi - lo);
80                 c = (hi-x)-lo;
81         } else if (hx < 0x33000000) {  /* when |x|<2**-25, return x */
82                 t = huge+x; /* return x with inexact flags when x!=0 */
83                 return x - (t-(huge+x));
84         } else
85                 k = 0;
86
87         /* x is now in primary range */
88         hfx = (float)0.5*x;
89         hxs = x*hfx;
90         r1 = one+hxs*(Q1+hxs*Q2);
91         t  = (float)3.0 - r1*hfx;
92         e  = hxs*((r1-t)/((float)6.0 - x*t));
93         if (k == 0)  /* c is 0 */
94                 return x - (x*e-hxs);
95         SET_FLOAT_WORD(twopk, 0x3f800000+(k<<23));   /* 2^k */
96         e  = x*(e-c) - c;
97         e -= hxs;
98         if (k == -1)
99                 return (float)0.5*(x-e) - (float)0.5;
100         if (k == 1) {
101                 if (x < (float)-0.25)
102                         return -(float)2.0*(e-(x+(float)0.5));
103                 return one+(float)2.0*(x-e);
104         }
105         if (k <= -2 || k > 56) {   /* suffice to return exp(x)-1 */
106                 y = one - (e - x);
107                 if (k == 128)
108                         y = y*2.0F*0x1p127F;
109                 else
110                         y = y*twopk;
111                 return y - one;
112         }
113         t = one;
114         if (k < 23) {
115                 SET_FLOAT_WORD(t, 0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */
116                 y = t - (e - x);
117                 y = y*twopk;
118         } else {
119                 SET_FLOAT_WORD(t, ((0x7f-k)<<23));  /* 2^-k */
120                 y = x - (e + t);
121                 y += one;
122                 y = y*twopk;
123         }
124         return y;
125 }