Print this page
5261 libm should stop using synonyms.h
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/log1p.c
+++ new/usr/src/lib/libm/common/C/log1p.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 *
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 26 * Use is subject to license terms.
27 27 */
28 28
29 -#pragma weak log1p = __log1p
29 +#pragma weak __log1p = log1p
30 30
31 31 /* INDENT OFF */
32 32 /*
33 33 * Method :
34 34 * 1. Argument Reduction: find k and f such that
35 35 * 1+x = 2^k * (1+f),
36 36 * where sqrt(2)/2 < 1+f < sqrt(2) .
37 37 *
38 38 * Note. If k=0, then f=x is exact. However, if k != 0, then f
39 39 * may not be representable exactly. In that case, a correction
40 40 * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
41 41 * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
42 42 * and add back the correction term c/u.
43 43 * (Note: when x > 2**53, one can simply return log(x))
44 44 *
45 45 * 2. Approximation of log1p(f).
46 46 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
47 47 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
48 48 * = 2s + s*R
49 49 * We use a special Reme algorithm on [0,0.1716] to generate
50 50 * a polynomial of degree 14 to approximate R The maximum error
51 51 * of this polynomial approximation is bounded by 2**-58.45. In
52 52 * other words,
53 53 * 2 4 6 8 10 12 14
54 54 * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
55 55 * (the values of Lp1 to Lp7 are listed in the program)
56 56 * and
57 57 * | 2 14 | -58.45
58 58 * | Lp1*s +...+Lp7*s - R(z) | <= 2
59 59 * | |
60 60 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
61 61 * In order to guarantee error in log below 1ulp, we compute log
62 62 * by
63 63 * log1p(f) = f - (hfsq - s*(hfsq+R)).
64 64 *
65 65 * 3. Finally, log1p(x) = k*ln2 + log1p(f).
66 66 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
67 67 * Here ln2 is splitted into two floating point number:
68 68 * ln2_hi + ln2_lo,
69 69 * where n*ln2_hi is always exact for |n| < 2000.
70 70 *
71 71 * Special cases:
72 72 * log1p(x) is NaN with signal if x < -1 (including -INF) ;
73 73 * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
74 74 * log1p(NaN) is that NaN with no signal.
75 75 *
76 76 * Accuracy:
77 77 * according to an error analysis, the error is always less than
78 78 * 1 ulp (unit in the last place).
79 79 *
80 80 * Constants:
81 81 * The hexadecimal values are the intended ones for the following
82 82 * constants. The decimal values may be used, provided that the
83 83 * compiler will convert from decimal to binary accurately enough
84 84 * to produce the hexadecimal values shown.
85 85 *
86 86 * Note: Assuming log() return accurate answer, the following
87 87 * algorithm can be used to compute log1p(x) to within a few ULP:
88 88 *
89 89 * u = 1+x;
90 90 * if (u == 1.0) return x ; else
91 91 * return log(u)*(x/(u-1.0));
92 92 *
93 93 * See HP-15C Advanced Functions Handbook, p.193.
94 94 */
95 95 /* INDENT ON */
96 96
97 97 #include "libm.h"
98 98
99 99 static const double xxx[] = {
100 100 /* ln2_hi */ 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
101 101 /* ln2_lo */ 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
102 102 /* two54 */ 1.80143985094819840000e+16, /* 43500000 00000000 */
103 103 /* Lp1 */ 6.666666666666735130e-01, /* 3FE55555 55555593 */
104 104 /* Lp2 */ 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
105 105 /* Lp3 */ 2.857142874366239149e-01, /* 3FD24924 94229359 */
106 106 /* Lp4 */ 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
107 107 /* Lp5 */ 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
108 108 /* Lp6 */ 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
109 109 /* Lp7 */ 1.479819860511658591e-01, /* 3FC2F112 DF3E5244 */
110 110 /* zero */ 0.0
111 111 };
112 112 #define ln2_hi xxx[0]
113 113 #define ln2_lo xxx[1]
114 114 #define two54 xxx[2]
115 115 #define Lp1 xxx[3]
116 116 #define Lp2 xxx[4]
117 117 #define Lp3 xxx[5]
118 118 #define Lp4 xxx[6]
119 119 #define Lp5 xxx[7]
120 120 #define Lp6 xxx[8]
121 121 #define Lp7 xxx[9]
122 122 #define zero xxx[10]
123 123
124 124 double
125 125 log1p(double x) {
126 126 double hfsq, f, c = 0.0, s, z, R, u;
127 127 int k, hx, hu, ax;
128 128
129 129 hx = ((int *)&x)[HIWORD]; /* high word of x */
130 130 ax = hx & 0x7fffffff;
131 131
132 132 if (ax >= 0x7ff00000) { /* x is inf or nan */
133 133 if (((hx - 0xfff00000) | ((int *)&x)[LOWORD]) == 0) /* -inf */
134 134 return (_SVID_libm_err(x, x, 44));
135 135 return (x * x);
136 136 }
137 137
138 138 k = 1;
139 139 if (hx < 0x3FDA827A) { /* x < 0.41422 */
140 140 if (ax >= 0x3ff00000) /* x <= -1.0 */
141 141 return (_SVID_libm_err(x, x, x == -1.0 ? 43 : 44));
142 142 if (ax < 0x3e200000) { /* |x| < 2**-29 */
143 143 if (two54 + x > zero && /* raise inexact */
144 144 ax < 0x3c900000) /* |x| < 2**-54 */
145 145 return (x);
146 146 else
147 147 return (x - x * x * 0.5);
148 148 }
149 149 if (hx > 0 || hx <= (int)0xbfd2bec3) { /* -0.2929<x<0.41422 */
150 150 k = 0;
151 151 f = x;
152 152 hu = 1;
153 153 }
154 154 }
155 155 /* We will initialize 'c' here. */
156 156 if (k != 0) {
157 157 if (hx < 0x43400000) {
158 158 u = 1.0 + x;
159 159 hu = ((int *)&u)[HIWORD]; /* high word of u */
160 160 k = (hu >> 20) - 1023;
161 161 /*
162 162 * correction term
163 163 */
164 164 c = k > 0 ? 1.0 - (u - x) : x - (u - 1.0);
165 165 c /= u;
166 166 } else {
167 167 u = x;
168 168 hu = ((int *)&u)[HIWORD]; /* high word of u */
169 169 k = (hu >> 20) - 1023;
170 170 c = 0;
171 171 }
172 172 hu &= 0x000fffff;
173 173 if (hu < 0x6a09e) { /* normalize u */
174 174 ((int *)&u)[HIWORD] = hu | 0x3ff00000;
175 175 } else { /* normalize u/2 */
176 176 k += 1;
177 177 ((int *)&u)[HIWORD] = hu | 0x3fe00000;
178 178 hu = (0x00100000 - hu) >> 2;
179 179 }
180 180 f = u - 1.0;
181 181 }
182 182 hfsq = 0.5 * f * f;
183 183 if (hu == 0) { /* |f| < 2**-20 */
184 184 if (f == zero) {
185 185 if (k == 0)
186 186 return (zero);
187 187 /* We already initialized 'c' before, when (k != 0) */
188 188 c += k * ln2_lo;
189 189 return (k * ln2_hi + c);
190 190 }
191 191 R = hfsq * (1.0 - 0.66666666666666666 * f);
192 192 if (k == 0)
193 193 return (f - R);
194 194 return (k * ln2_hi - ((R - (k * ln2_lo + c)) - f));
195 195 }
196 196 s = f / (2.0 + f);
197 197 z = s * s;
198 198 R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 +
199 199 z * (Lp6 + z * Lp7))))));
200 200 if (k == 0)
201 201 return (f - (hfsq - s * (hfsq + R)));
202 202 return (k * ln2_hi - ((hfsq - (s * (hfsq + R) +
203 203 (k * ln2_lo + c))) - f));
204 204 }
↓ open down ↓ |
165 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX