Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/expm1.c
+++ new/usr/src/lib/libm/common/C/expm1.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #pragma weak expm1 = __expm1
31 31
32 32 /* INDENT OFF */
33 33 /* expm1(x)
34 34 * Returns exp(x)-1, the exponential of x minus 1.
35 35 *
36 36 * Method
37 37 * 1. Arugment reduction:
38 38 * Given x, find r and integer k such that
39 39 *
40 40 * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
41 41 *
42 42 * Here a correction term c will be computed to compensate
43 43 * the error in r when rounded to a floating-point number.
44 44 *
45 45 * 2. Approximating expm1(r) by a special rational function on
46 46 * the interval [0,0.34658]:
47 47 * Since
48 48 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
49 49 * we define R1(r*r) by
50 50 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
51 51 * That is,
52 52 * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
53 53 * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
54 54 * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
55 55 * We use a special Reme algorithm on [0,0.347] to generate
56 56 * a polynomial of degree 5 in r*r to approximate R1. The
57 57 * maximum error of this polynomial approximation is bounded
58 58 * by 2**-61. In other words,
59 59 * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
60 60 * where Q1 = -1.6666666666666567384E-2,
61 61 * Q2 = 3.9682539681370365873E-4,
62 62 * Q3 = -9.9206344733435987357E-6,
63 63 * Q4 = 2.5051361420808517002E-7,
64 64 * Q5 = -6.2843505682382617102E-9;
65 65 * (where z=r*r, and the values of Q1 to Q5 are listed below)
66 66 * with error bounded by
67 67 * | 5 | -61
68 68 * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
69 69 * | |
70 70 *
71 71 * expm1(r) = exp(r)-1 is then computed by the following
72 72 * specific way which minimize the accumulation rounding error:
73 73 * 2 3
74 74 * r r [ 3 - (R1 + R1*r/2) ]
75 75 * expm1(r) = r + --- + --- * [--------------------]
76 76 * 2 2 [ 6 - r*(3 - R1*r/2) ]
77 77 *
78 78 * To compensate the error in the argument reduction, we use
79 79 * expm1(r+c) = expm1(r) + c + expm1(r)*c
80 80 * ~ expm1(r) + c + r*c
81 81 * Thus c+r*c will be added in as the correction terms for
82 82 * expm1(r+c). Now rearrange the term to avoid optimization
83 83 * screw up:
84 84 * ( 2 2 )
85 85 * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
86 86 * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
87 87 * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
88 88 * ( )
89 89 *
90 90 * = r - E
91 91 * 3. Scale back to obtain expm1(x):
92 92 * From step 1, we have
93 93 * expm1(x) = either 2^k*[expm1(r)+1] - 1
94 94 * = or 2^k*[expm1(r) + (1-2^-k)]
95 95 * 4. Implementation notes:
96 96 * (A). To save one multiplication, we scale the coefficient Qi
97 97 * to Qi*2^i, and replace z by (x^2)/2.
98 98 * (B). To achieve maximum accuracy, we compute expm1(x) by
99 99 * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
100 100 * (ii) if k=0, return r-E
101 101 * (iii) if k=-1, return 0.5*(r-E)-0.5
102 102 * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
103 103 * else return 1.0+2.0*(r-E);
104 104 * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
105 105 * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
106 106 * (vii) return 2^k(1-((E+2^-k)-r))
107 107 *
108 108 * Special cases:
109 109 * expm1(INF) is INF, expm1(NaN) is NaN;
110 110 * expm1(-INF) is -1, and
111 111 * for finite argument, only expm1(0)=0 is exact.
112 112 *
113 113 * Accuracy:
114 114 * according to an error analysis, the error is always less than
115 115 * 1 ulp (unit in the last place).
116 116 *
117 117 * Misc. info.
118 118 * For IEEE double
119 119 * if x > 7.09782712893383973096e+02 then expm1(x) overflow
120 120 *
121 121 * Constants:
122 122 * The hexadecimal values are the intended ones for the following
123 123 * constants. The decimal values may be used, provided that the
124 124 * compiler will convert from decimal to binary accurately enough
125 125 * to produce the hexadecimal values shown.
126 126 */
127 127 /* INDENT ON */
128 128
129 129 #include "libm_synonyms.h" /* __expm1 */
130 130 #include "libm_macros.h"
131 131 #include <math.h>
132 132
133 133 static const double xxx[] = {
134 134 /* one */ 1.0,
135 135 /* huge */ 1.0e+300,
136 136 /* tiny */ 1.0e-300,
137 137 /* o_threshold */ 7.09782712893383973096e+02, /* 40862E42 FEFA39EF */
138 138 /* ln2_hi */ 6.93147180369123816490e-01, /* 3FE62E42 FEE00000 */
139 139 /* ln2_lo */ 1.90821492927058770002e-10, /* 3DEA39EF 35793C76 */
140 140 /* invln2 */ 1.44269504088896338700e+00, /* 3FF71547 652B82FE */
141 141 /* scaled coefficients related to expm1 */
142 142 /* Q1 */ -3.33333333333331316428e-02, /* BFA11111 111110F4 */
143 143 /* Q2 */ 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
144 144 /* Q3 */ -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
145 145 /* Q4 */ 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
146 146 /* Q5 */ -2.01099218183624371326e-07 /* BE8AFDB7 6E09C32D */
147 147 };
148 148 #define one xxx[0]
149 149 #define huge xxx[1]
150 150 #define tiny xxx[2]
151 151 #define o_threshold xxx[3]
152 152 #define ln2_hi xxx[4]
↓ open down ↓ |
152 lines elided |
↑ open up ↑ |
153 153 #define ln2_lo xxx[5]
154 154 #define invln2 xxx[6]
155 155 #define Q1 xxx[7]
156 156 #define Q2 xxx[8]
157 157 #define Q3 xxx[9]
158 158 #define Q4 xxx[10]
159 159 #define Q5 xxx[11]
160 160
161 161 double
162 162 expm1(double x) {
163 - double y, hi, lo, c, t, e, hxs, hfx, r1;
163 + double y, hi, lo, c = 0.0L, t, e, hxs, hfx, r1;
164 164 int k, xsb;
165 165 unsigned hx;
166 166
167 167 hx = ((unsigned *) &x)[HIWORD]; /* high word of x */
168 168 xsb = hx & 0x80000000; /* sign bit of x */
169 169 if (xsb == 0)
170 170 y = x;
171 171 else
172 172 y = -x; /* y = |x| */
173 173 hx &= 0x7fffffff; /* high word of |x| */
174 174
175 - /* filter out huge and non-finite arugment */
176 - if (hx >= 0x4043687A) { /* if |x|>=56*ln2 */
177 - if (hx >= 0x40862E42) { /* if |x|>=709.78... */
175 + /* filter out huge and non-finite argument */
176 + /* for example exp(38)-1 is approximately 3.1855932e+16 */
177 + if (hx >= 0x4043687A) { /* if |x|>=56*ln2 (~38.8162...)*/
178 + if (hx >= 0x40862E42) { /* if |x|>=709.78... -> inf */
178 179 if (hx >= 0x7ff00000) {
179 180 if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
180 181 != 0)
181 182 return x * x; /* + -> * for Cheetah */
182 183 else
183 184 return xsb == 0 ? x : -1.0; /* exp(+-inf)={inf,-1} */
184 185 }
185 186 if (x > o_threshold)
186 187 return huge * huge; /* overflow */
187 188 }
188 189 if (xsb != 0) { /* x < -56*ln2, return -1.0 w/inexact */
189 190 if (x + tiny < 0.0) /* raise inexact */
190 191 return tiny - one; /* return -1 */
191 192 }
192 193 }
193 194
194 195 /* argument reduction */
195 196 if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
196 197 if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
197 - if (xsb == 0) {
198 + if (xsb == 0) { /* positive number */
198 199 hi = x - ln2_hi;
199 200 lo = ln2_lo;
200 201 k = 1;
201 202 }
202 - else {
203 + else { /* negative number */
203 204 hi = x + ln2_hi;
204 205 lo = -ln2_lo;
205 206 k = -1;
206 207 }
207 208 }
208 - else {
209 + else { /* |x| > 1.5 ln2 */
209 210 k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
210 211 t = k;
211 212 hi = x - t * ln2_hi; /* t*ln2_hi is exact here */
212 213 lo = t * ln2_lo;
213 214 }
214 215 x = hi - lo;
215 - c = (hi - x) - lo;
216 + c = (hi - x) - lo; /* still at |x| > 0.5 ln2 */
216 217 }
217 218 else if (hx < 0x3c900000) { /* when |x|<2**-54, return x */
218 219 t = huge + x; /* return x w/inexact when x != 0 */
219 220 return x - (t - (huge + x));
220 221 }
221 - else
222 + else /* |x| <= 0.5 ln2 */
222 223 k = 0;
223 224
224 225 /* x is now in primary range */
225 226 hfx = 0.5 * x;
226 227 hxs = x * hfx;
227 228 r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
228 229 t = 3.0 - r1 * hfx;
229 230 e = hxs * ((r1 - t) / (6.0 - x * t));
230 - if (k == 0)
231 - return x - (x * e - hxs); /* c is 0 */
232 - else {
231 + if (k == 0) /* |x| <= 0.5 ln2 */
232 + return x - (x * e - hxs);
233 + else { /* |x| > 0.5 ln2 */
233 234 e = (x * (e - c) - c);
234 235 e -= hxs;
235 236 if (k == -1)
236 237 return 0.5 * (x - e) - 0.5;
237 - if (k == 1)
238 + if (k == 1) {
238 239 if (x < -0.25)
239 240 return -2.0 * (e - (x + 0.5));
240 241 else
241 242 return one + 2.0 * (x - e);
243 + }
242 244 if (k <= -2 || k > 56) { /* suffice to return exp(x)-1 */
243 245 y = one - (e - x);
244 246 ((int *) &y)[HIWORD] += k << 20;
245 247 return y - one;
246 248 }
247 249 t = one;
248 250 if (k < 20) {
249 251 ((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
250 252 /* t = 1 - 2^-k */
251 253 y = t - (e - x);
252 254 ((int *) &y)[HIWORD] += k << 20;
253 255 }
254 256 else {
255 257 ((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
256 258 y = x - (e + t);
257 259 y += one;
258 260 ((int *) &y)[HIWORD] += k << 20;
259 261 }
260 262 }
261 263 return y;
262 264 }
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX