Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/log.c
+++ new/usr/src/lib/libm/common/C/log.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 log = __log
29 +#pragma weak __log = log
30 30
31 31 /* INDENT OFF */
32 32 /*
33 33 * log(x)
34 34 * Table look-up algorithm with product polynomial approximation.
35 35 * By K.C. Ng, Oct 23, 2004. Updated Oct 18, 2005.
36 36 *
37 37 * (a). For x in [1-0.125, 1+0.1328125], using a special approximation:
38 38 * Let f = x - 1 and z = f*f.
39 39 * return f + ((a1*z) *
40 40 * ((a2 + (a3*f)*(a4+f)) + (f*z)*(a5+f))) *
41 41 * (((a6 + f*(a7+f)) + (f*z)*(a8+f)) *
42 42 * ((a9 + (a10*f)*(a11+f)) + (f*z)*(a12+f)))
43 43 * a1 -6.88821452420390473170286327331268694251775741577e-0002,
44 44 * a2 1.97493380704769294631262255279580131173133850098e+0000,
45 45 * a3 2.24963218866067560242072431719861924648284912109e+0000,
46 46 * a4 -9.02975906958474405783476868236903101205825805664e-0001,
47 47 * a5 -1.47391630715542865104339398385491222143173217773e+0000,
48 48 * a6 1.86846544648220058704168877738993614912033081055e+0000,
49 49 * a7 1.82277370459347465292410106485476717352867126465e+0000,
50 50 * a8 1.25295479915214102994980294170090928673744201660e+0000,
51 51 * a9 1.96709676945198275177517643896862864494323730469e+0000,
52 52 * a10 -4.00127989749189894030934055990655906498432159424e-0001,
53 53 * a11 3.01675528558798333733648178167641162872314453125e+0000,
54 54 * a12 -9.52325445049240770778453679668018594384193420410e-0001,
55 55 *
56 56 * with remez error |(log(1+f) - P(f))/f| <= 2**-56.81 and
57 57 *
58 58 * (b). For 0.09375 <= x < 24
59 59 * Use an 8-bit table look-up (3-bit for exponent and 5 bit for
60 60 * significand):
61 61 * Let ix stands for the high part of x in IEEE double format.
62 62 * Since 0.09375 <= x < 24, we have
63 63 * 0x3fb80000 <= ix < 0x40380000.
64 64 * Let j = (ix - 0x3fb80000) >> 15. Then 0 <= j < 256. Choose
65 65 * a Y[j] such that HIWORD(Y[j]) ~ 0x3fb8400 + (j<<15) (the middle
66 66 * number between 0x3fb80000 + (j<<15) and 3fb80000 + ((j+1)<<15)),
67 67 * and at the same time 1/Y[j] as well as log(Y[j]) are very close
68 68 * to 53-bits floating point numbers.
69 69 * A table of Y[j], 1/Y[j], and log(Y[j]) are pre-computed and thus
70 70 * log(x) = log(Y[j]) + log(1 + (x-Y[j])*(1/Y[j]))
71 71 * = log(Y[j]) + log(1 + s)
72 72 * where
73 73 * s = (x-Y[j])*(1/Y[j])
74 74 * We compute max (x-Y[j])*(1/Y[j]) for the chosen Y[j] and obtain
75 75 * |s| < 0.0154. By applying remez algorithm with Product Polynomial
76 76 * Approximiation, we find the following approximated of log(1+s)
77 77 * (b1*s)*(b2+s*(b3+s))*((b4+s*b5)+(s*s)*(b6+s))*(b7+s*(b8+s))
78 78 * with remez error |log(1+s) - P(s)| <= 2**-63.5
79 79 *
80 80 * (c). Otherwise, get "n", the exponent of x, and then normalize x to
81 81 * z in [1,2). Then similar to (b) find a Y[i] that matches z to 5.5
82 82 * significant bits. Then
83 83 * log(x) = n*ln2 + log(Y[i]) + log(z/Y[i]).
84 84 *
85 85 * Special cases:
86 86 * log(x) is NaN with signal if x < 0 (including -INF) ;
87 87 * log(+INF) is +INF; log(0) is -INF with signal;
88 88 * log(NaN) is that NaN with no signal.
89 89 *
90 90 * Maximum error observed: less than 0.90 ulp
91 91 *
92 92 * Constants:
93 93 * The hexadecimal values are the intended ones for the following constants.
94 94 * The decimal values may be used, provided that the compiler will convert
95 95 * from decimal to binary accurately enough to produce the hexadecimal values
96 96 * shown.
97 97 */
98 98 /* INDENT ON */
99 99
100 100 #include "libm.h"
101 101
102 102 extern const double _TBL_log[];
103 103
104 104 static const double P[] = {
105 105 /* ONE */ 1.0,
106 106 /* TWO52 */ 4503599627370496.0,
107 107 /* LN2HI */ 6.93147180369123816490e-01, /* 3fe62e42, fee00000 */
108 108 /* LN2LO */ 1.90821492927058770002e-10, /* 3dea39ef, 35793c76 */
109 109 /* A1 */ -6.88821452420390473170286327331268694251775741577e-0002,
110 110 /* A2 */ 1.97493380704769294631262255279580131173133850098e+0000,
111 111 /* A3 */ 2.24963218866067560242072431719861924648284912109e+0000,
112 112 /* A4 */ -9.02975906958474405783476868236903101205825805664e-0001,
113 113 /* A5 */ -1.47391630715542865104339398385491222143173217773e+0000,
114 114 /* A6 */ 1.86846544648220058704168877738993614912033081055e+0000,
115 115 /* A7 */ 1.82277370459347465292410106485476717352867126465e+0000,
116 116 /* A8 */ 1.25295479915214102994980294170090928673744201660e+0000,
117 117 /* A9 */ 1.96709676945198275177517643896862864494323730469e+0000,
118 118 /* A10 */ -4.00127989749189894030934055990655906498432159424e-0001,
119 119 /* A11 */ 3.01675528558798333733648178167641162872314453125e+0000,
120 120 /* A12 */ -9.52325445049240770778453679668018594384193420410e-0001,
121 121 /* B1 */ -1.25041641589283658575482149899471551179885864258e-0001,
122 122 /* B2 */ 1.87161713283355151891381127914642725337613123482e+0000,
123 123 /* B3 */ -1.89082956295731507978530316904652863740921020508e+0000,
124 124 /* B4 */ -2.50562891673640253387134180229622870683670043945e+0000,
125 125 /* B5 */ 1.64822828085258366037635369139024987816810607910e+0000,
126 126 /* B6 */ -1.24409107065868340669112512841820716857910156250e+0000,
127 127 /* B7 */ 1.70534231658220414296067701798165217041969299316e+0000,
128 128 /* B8 */ 1.99196833784655646937267192697618156671524047852e+0000,
129 129 };
130 130
131 131 #define ONE P[0]
132 132 #define TWO52 P[1]
133 133 #define LN2HI P[2]
134 134 #define LN2LO P[3]
135 135 #define A1 P[4]
136 136 #define A2 P[5]
137 137 #define A3 P[6]
138 138 #define A4 P[7]
139 139 #define A5 P[8]
140 140 #define A6 P[9]
141 141 #define A7 P[10]
142 142 #define A8 P[11]
143 143 #define A9 P[12]
144 144 #define A10 P[13]
145 145 #define A11 P[14]
146 146 #define A12 P[15]
147 147 #define B1 P[16]
148 148 #define B2 P[17]
149 149 #define B3 P[18]
150 150 #define B4 P[19]
151 151 #define B5 P[20]
152 152 #define B6 P[21]
153 153 #define B7 P[22]
154 154 #define B8 P[23]
155 155
156 156 double
157 157 log(double x) {
158 158 double *tb, dn, dn1, s, z, r, w;
159 159 int i, hx, ix, n, lx;
160 160
161 161 n = 0;
162 162 hx = ((int *)&x)[HIWORD];
163 163 ix = hx & 0x7fffffff;
164 164 lx = ((int *)&x)[LOWORD];
165 165
166 166 /* subnormal,0,negative,inf,nan */
167 167 if ((hx + 0x100000) < 0x200000) {
168 168 if (ix > 0x7ff00000 || (ix == 0x7ff00000 && lx != 0)) /* nan */
169 169 return (x * x);
170 170 if (((hx << 1) | lx) == 0) /* zero */
171 171 return (_SVID_libm_err(x, x, 16));
172 172 if (hx < 0) /* negative */
173 173 return (_SVID_libm_err(x, x, 17));
174 174 if (((hx - 0x7ff00000) | lx) == 0) /* +inf */
175 175 return (x);
176 176
177 177 /* x must be positive and subnormal */
178 178 x *= TWO52;
179 179 n = -52;
180 180 ix = ((int *)&x)[HIWORD];
181 181 lx = ((int *)&x)[LOWORD];
182 182 }
183 183
184 184 i = ix >> 19;
185 185 if (i >= 0x7f7 && i <= 0x806) {
186 186 /* 0.09375 (0x3fb80000) <= x < 24 (0x40380000) */
187 187 if (ix >= 0x3fec0000 && ix < 0x3ff22000) {
188 188 /* 0.875 <= x < 1.125 */
189 189 s = x - ONE;
190 190 z = s * s;
191 191 if (((ix - 0x3ff00000) | lx) == 0) /* x = 1 */
192 192 return (z);
193 193 r = (A10 * s) * (A11 + s);
194 194 w = z * s;
195 195 return (s + ((A1 * z) *
196 196 (A2 + ((A3 * s) * (A4 + s) + w * (A5 + s)))) *
197 197 ((A6 + (s * (A7 + s) + w * (A8 + s))) *
198 198 (A9 + (r + w * (A12 + s)))));
199 199 } else {
200 200 i = (ix - 0x3fb80000) >> 15;
201 201 tb = (double *)_TBL_log + (i + i + i);
202 202 s = (x - tb[0]) * tb[1];
203 203 return (tb[2] + ((B1 * s) * (B2 + s * (B3 + s))) *
204 204 (((B4 + s * B5) + (s * s) * (B6 + s)) *
205 205 (B7 + s * (B8 + s))));
206 206 }
207 207 } else {
208 208 dn = (double)(n + ((ix >> 20) - 0x3ff));
209 209 dn1 = dn * LN2HI;
210 210 i = (ix & 0x000fffff) | 0x3ff00000; /* scale x to [1,2] */
211 211 ((int *)&x)[HIWORD] = i;
212 212 i = (i - 0x3fb80000) >> 15;
213 213 tb = (double *)_TBL_log + (i + i + i);
214 214 s = (x - tb[0]) * tb[1];
215 215 dn = dn * LN2LO + tb[2];
216 216 return (dn1 + (dn + ((B1 * s) * (B2 + s * (B3 + s))) *
217 217 (((B4 + s * B5) + (s * s) * (B6 + s)) *
218 218 (B7 + s * (B8 + s)))));
219 219 }
220 220 }
↓ open down ↓ |
181 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX