1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 |
* |
3 |
* ***** BEGIN LICENSE BLOCK ***** |
4 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
5 |
* |
6 |
* The contents of this file are subject to the Mozilla Public License Version |
7 |
* 1.1 (the "License"); you may not use this file except in compliance with |
8 |
* the License. You may obtain a copy of the License at |
9 |
* http://www.mozilla.org/MPL/ |
10 |
* |
11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 |
* for the specific language governing rights and limitations under the |
14 |
* License. |
15 |
* |
16 |
* The Original Code is Mozilla Communicator client code, released |
17 |
* March 31, 1998. |
18 |
* |
19 |
* The Initial Developer of the Original Code is |
20 |
* Sun Microsystems, Inc. |
21 |
* Portions created by the Initial Developer are Copyright (C) 1998 |
22 |
* the Initial Developer. All Rights Reserved. |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* Alternatively, the contents of this file may be used under the terms of |
27 |
* either of the GNU General Public License Version 2 or later (the "GPL"), |
28 |
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
29 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
30 |
* of those above. If you wish to allow use of your version of this file only |
31 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
32 |
* use your version of this file under the terms of the MPL, indicate your |
33 |
* decision by deleting the provisions above and replace them with the notice |
34 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
35 |
* the provisions above, a recipient may use your version of this file under |
36 |
* the terms of any one of the MPL, the GPL or the LGPL. |
37 |
* |
38 |
* ***** END LICENSE BLOCK ***** */ |
39 |
|
40 |
/* @(#)e_log.c 1.3 95/01/18 */ |
41 |
/* |
42 |
* ==================================================== |
43 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
44 |
* |
45 |
* Developed at SunSoft, a Sun Microsystems, Inc. business. |
46 |
* Permission to use, copy, modify, and distribute this |
47 |
* software is freely granted, provided that this notice |
48 |
* is preserved. |
49 |
* ==================================================== |
50 |
*/ |
51 |
|
52 |
/* __ieee754_log(x) |
53 |
* Return the logrithm of x |
54 |
* |
55 |
* Method : |
56 |
* 1. Argument Reduction: find k and f such that |
57 |
* x = 2^k * (1+f), |
58 |
* where sqrt(2)/2 < 1+f < sqrt(2) . |
59 |
* |
60 |
* 2. Approximation of log(1+f). |
61 |
* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) |
62 |
* = 2s + 2/3 s**3 + 2/5 s**5 + ....., |
63 |
* = 2s + s*R |
64 |
* We use a special Reme algorithm on [0,0.1716] to generate |
65 |
* a polynomial of degree 14 to approximate R The maximum error |
66 |
* of this polynomial approximation is bounded by 2**-58.45. In |
67 |
* other words, |
68 |
* 2 4 6 8 10 12 14 |
69 |
* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s |
70 |
* (the values of Lg1 to Lg7 are listed in the program) |
71 |
* and |
72 |
* | 2 14 | -58.45 |
73 |
* | Lg1*s +...+Lg7*s - R(z) | <= 2 |
74 |
* | | |
75 |
* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. |
76 |
* In order to guarantee error in log below 1ulp, we compute log |
77 |
* by |
78 |
* log(1+f) = f - s*(f - R) (if f is not too large) |
79 |
* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) |
80 |
* |
81 |
* 3. Finally, log(x) = k*ln2 + log(1+f). |
82 |
* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) |
83 |
* Here ln2 is split into two floating point number: |
84 |
* ln2_hi + ln2_lo, |
85 |
* where n*ln2_hi is always exact for |n| < 2000. |
86 |
* |
87 |
* Special cases: |
88 |
* log(x) is NaN with signal if x < 0 (including -INF) ; |
89 |
* log(+INF) is +INF; log(0) is -INF with signal; |
90 |
* log(NaN) is that NaN with no signal. |
91 |
* |
92 |
* Accuracy: |
93 |
* according to an error analysis, the error is always less than |
94 |
* 1 ulp (unit in the last place). |
95 |
* |
96 |
* Constants: |
97 |
* The hexadecimal values are the intended ones for the following |
98 |
* constants. The decimal values may be used, provided that the |
99 |
* compiler will convert from decimal to binary accurately enough |
100 |
* to produce the hexadecimal values shown. |
101 |
*/ |
102 |
|
103 |
#include "fdlibm.h" |
104 |
|
105 |
#ifdef __STDC__ |
106 |
static const double |
107 |
#else |
108 |
static double |
109 |
#endif |
110 |
ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ |
111 |
ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ |
112 |
two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ |
113 |
Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ |
114 |
Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ |
115 |
Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ |
116 |
Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ |
117 |
Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ |
118 |
Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ |
119 |
Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ |
120 |
|
121 |
static double zero = 0.0; |
122 |
|
123 |
#ifdef __STDC__ |
124 |
double __ieee754_log(double x) |
125 |
#else |
126 |
double __ieee754_log(x) |
127 |
double x; |
128 |
#endif |
129 |
{ |
130 |
fd_twoints u; |
131 |
double hfsq,f,s,z,R,w,t1,t2,dk; |
132 |
int k,hx,i,j; |
133 |
unsigned lx; |
134 |
|
135 |
u.d = x; |
136 |
hx = __HI(u); /* high word of x */ |
137 |
lx = __LO(u); /* low word of x */ |
138 |
|
139 |
k=0; |
140 |
if (hx < 0x00100000) { /* x < 2**-1022 */ |
141 |
if (((hx&0x7fffffff)|lx)==0) |
142 |
return -two54/zero; /* log(+-0)=-inf */ |
143 |
if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ |
144 |
k -= 54; x *= two54; /* subnormal number, scale up x */ |
145 |
u.d = x; |
146 |
hx = __HI(u); /* high word of x */ |
147 |
} |
148 |
if (hx >= 0x7ff00000) return x+x; |
149 |
k += (hx>>20)-1023; |
150 |
hx &= 0x000fffff; |
151 |
i = (hx+0x95f64)&0x100000; |
152 |
u.d = x; |
153 |
__HI(u) = hx|(i^0x3ff00000); /* normalize x or x/2 */ |
154 |
x = u.d; |
155 |
k += (i>>20); |
156 |
f = x-1.0; |
157 |
if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */ |
158 |
if(f==zero) { |
159 |
if(k==0) return zero; else {dk=(double)k; |
160 |
return dk*ln2_hi+dk*ln2_lo;} |
161 |
} |
162 |
R = f*f*(0.5-0.33333333333333333*f); |
163 |
if(k==0) return f-R; else {dk=(double)k; |
164 |
return dk*ln2_hi-((R-dk*ln2_lo)-f);} |
165 |
} |
166 |
s = f/(2.0+f); |
167 |
dk = (double)k; |
168 |
z = s*s; |
169 |
i = hx-0x6147a; |
170 |
w = z*z; |
171 |
j = 0x6b851-hx; |
172 |
t1= w*(Lg2+w*(Lg4+w*Lg6)); |
173 |
t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); |
174 |
i |= j; |
175 |
R = t2+t1; |
176 |
if(i>0) { |
177 |
hfsq=0.5*f*f; |
178 |
if(k==0) return f-(hfsq-s*(hfsq+R)); else |
179 |
return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); |
180 |
} else { |
181 |
if(k==0) return f-s*(f-R); else |
182 |
return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f); |
183 |
} |
184 |
} |