1 | /* |
2 | * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Oracle designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Oracle in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 | * or visit www.oracle.com if you need additional information or have any |
23 | * questions. |
24 | */ |
25 | |
26 | /* __ieee754_log(x) |
27 | * Return the logrithm of x |
28 | * |
29 | * Method : |
30 | * 1. Argument Reduction: find k and f such that |
31 | * x = 2^k * (1+f), |
32 | * where sqrt(2)/2 < 1+f < sqrt(2) . |
33 | * |
34 | * 2. Approximation of log(1+f). |
35 | * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) |
36 | * = 2s + 2/3 s**3 + 2/5 s**5 + ....., |
37 | * = 2s + s*R |
38 | * We use a special Reme algorithm on [0,0.1716] to generate |
39 | * a polynomial of degree 14 to approximate R The maximum error |
40 | * of this polynomial approximation is bounded by 2**-58.45. In |
41 | * other words, |
42 | * 2 4 6 8 10 12 14 |
43 | * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s |
44 | * (the values of Lg1 to Lg7 are listed in the program) |
45 | * and |
46 | * | 2 14 | -58.45 |
47 | * | Lg1*s +...+Lg7*s - R(z) | <= 2 |
48 | * | | |
49 | * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. |
50 | * In order to guarantee error in log below 1ulp, we compute log |
51 | * by |
52 | * log(1+f) = f - s*(f - R) (if f is not too large) |
53 | * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) |
54 | * |
55 | * 3. Finally, log(x) = k*ln2 + log(1+f). |
56 | * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) |
57 | * Here ln2 is split into two floating point number: |
58 | * ln2_hi + ln2_lo, |
59 | * where n*ln2_hi is always exact for |n| < 2000. |
60 | * |
61 | * Special cases: |
62 | * log(x) is NaN with signal if x < 0 (including -INF) ; |
63 | * log(+INF) is +INF; log(0) is -INF with signal; |
64 | * log(NaN) is that NaN with no signal. |
65 | * |
66 | * Accuracy: |
67 | * according to an error analysis, the error is always less than |
68 | * 1 ulp (unit in the last place). |
69 | * |
70 | * Constants: |
71 | * The hexadecimal values are the intended ones for the following |
72 | * constants. The decimal values may be used, provided that the |
73 | * compiler will convert from decimal to binary accurately enough |
74 | * to produce the hexadecimal values shown. |
75 | */ |
76 | |
77 | #include "fdlibm.h" |
78 | |
79 | #ifdef __STDC__ |
80 | static const double |
81 | #else |
82 | static double |
83 | #endif |
84 | ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ |
85 | ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ |
86 | two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ |
87 | Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ |
88 | Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ |
89 | Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ |
90 | Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ |
91 | Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ |
92 | Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ |
93 | Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ |
94 | |
95 | static double zero = 0.0; |
96 | |
97 | #ifdef __STDC__ |
98 | double __ieee754_log(double x) |
99 | #else |
100 | double __ieee754_log(x) |
101 | double x; |
102 | #endif |
103 | { |
104 | double hfsq,f,s,z,R,w,t1,t2,dk; |
105 | int k,hx,i,j; |
106 | unsigned lx; |
107 | |
108 | hx = __HI(x); /* high word of x */ |
109 | lx = __LO(x); /* low word of x */ |
110 | |
111 | k=0; |
112 | if (hx < 0x00100000) { /* x < 2**-1022 */ |
113 | if (((hx&0x7fffffff)|lx)==0) |
114 | return -two54/zero; /* log(+-0)=-inf */ |
115 | if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ |
116 | k -= 54; x *= two54; /* subnormal number, scale up x */ |
117 | hx = __HI(x); /* high word of x */ |
118 | } |
119 | if (hx >= 0x7ff00000) return x+x; |
120 | k += (hx>>20)-1023; |
121 | hx &= 0x000fffff; |
122 | i = (hx+0x95f64)&0x100000; |
123 | __HI(x) = hx|(i^0x3ff00000); /* normalize x or x/2 */ |
124 | k += (i>>20); |
125 | f = x-1.0; |
126 | if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */ |
127 | if(f==zero) { |
128 | if (k==0) return zero; |
129 | else {dk=(double)k; return dk*ln2_hi+dk*ln2_lo;} |
130 | } |
131 | R = f*f*(0.5-0.33333333333333333*f); |
132 | if(k==0) return f-R; else {dk=(double)k; |
133 | return dk*ln2_hi-((R-dk*ln2_lo)-f);} |
134 | } |
135 | s = f/(2.0+f); |
136 | dk = (double)k; |
137 | z = s*s; |
138 | i = hx-0x6147a; |
139 | w = z*z; |
140 | j = 0x6b851-hx; |
141 | t1= w*(Lg2+w*(Lg4+w*Lg6)); |
142 | t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); |
143 | i |= j; |
144 | R = t2+t1; |
145 | if(i>0) { |
146 | hfsq=0.5*f*f; |
147 | if(k==0) return f-(hfsq-s*(hfsq+R)); else |
148 | return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); |
149 | } else { |
150 | if(k==0) return f-s*(f-R); else |
151 | return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f); |
152 | } |
153 | } |
154 | |