| 1 |
/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */ |
| 2 |
/* |
| 3 |
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. |
| 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 |
ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ |
| 20 |
ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ |
| 21 |
invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ |
| 22 |
/* |
| 23 |
* Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]: |
| 24 |
* |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04 |
| 25 |
* Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c): |
| 26 |
*/ |
| 27 |
Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */ |
| 28 |
Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */ |
| 29 |
|
| 30 |
float expm1f(float x) |
| 31 |
{ |
| 32 |
float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk; |
| 33 |
union {float f; uint32_t i;} u = {x}; |
| 34 |
uint32_t hx = u.i & 0x7fffffff; |
| 35 |
int k, sign = u.i >> 31; |
| 36 |
|
| 37 |
/* filter out huge and non-finite argument */ |
| 38 |
if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */ |
| 39 |
if (hx > 0x7f800000) /* NaN */ |
| 40 |
return x; |
| 41 |
if (sign) |
| 42 |
return -1; |
| 43 |
if (hx > 0x42b17217) { /* x > log(FLT_MAX) */ |
| 44 |
x *= 0x1p127f; |
| 45 |
return x; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/* argument reduction */ |
| 50 |
if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ |
| 51 |
if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ |
| 52 |
if (!sign) { |
| 53 |
hi = x - ln2_hi; |
| 54 |
lo = ln2_lo; |
| 55 |
k = 1; |
| 56 |
} else { |
| 57 |
hi = x + ln2_hi; |
| 58 |
lo = -ln2_lo; |
| 59 |
k = -1; |
| 60 |
} |
| 61 |
} else { |
| 62 |
k = invln2*x + (sign ? -0.5f : 0.5f); |
| 63 |
t = k; |
| 64 |
hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ |
| 65 |
lo = t*ln2_lo; |
| 66 |
} |
| 67 |
x = hi-lo; |
| 68 |
c = (hi-x)-lo; |
| 69 |
} else if (hx < 0x33000000) { /* when |x|<2**-25, return x */ |
| 70 |
if (hx < 0x00800000) |
| 71 |
FORCE_EVAL(x*x); |
| 72 |
return x; |
| 73 |
} else |
| 74 |
k = 0; |
| 75 |
|
| 76 |
/* x is now in primary range */ |
| 77 |
hfx = 0.5f*x; |
| 78 |
hxs = x*hfx; |
| 79 |
r1 = 1.0f+hxs*(Q1+hxs*Q2); |
| 80 |
t = 3.0f - r1*hfx; |
| 81 |
e = hxs*((r1-t)/(6.0f - x*t)); |
| 82 |
if (k == 0) /* c is 0 */ |
| 83 |
return x - (x*e-hxs); |
| 84 |
e = x*(e-c) - c; |
| 85 |
e -= hxs; |
| 86 |
/* exp(x) ~ 2^k (x_reduced - e + 1) */ |
| 87 |
if (k == -1) |
| 88 |
return 0.5f*(x-e) - 0.5f; |
| 89 |
if (k == 1) { |
| 90 |
if (x < -0.25f) |
| 91 |
return -2.0f*(e-(x+0.5f)); |
| 92 |
return 1.0f + 2.0f*(x-e); |
| 93 |
} |
| 94 |
u.i = (0x7f+k)<<23; /* 2^k */ |
| 95 |
twopk = u.f; |
| 96 |
if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */ |
| 97 |
y = x - e + 1.0f; |
| 98 |
if (k == 128) |
| 99 |
y = y*2.0f*0x1p127f; |
| 100 |
else |
| 101 |
y = y*twopk; |
| 102 |
return y - 1.0f; |
| 103 |
} |
| 104 |
u.i = (0x7f-k)<<23; /* 2^-k */ |
| 105 |
if (k < 23) |
| 106 |
y = (x-e+(1-u.f))*twopk; |
| 107 |
else |
| 108 |
y = (x-(e+u.f)+1)*twopk; |
| 109 |
return y; |
| 110 |
} |