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_log10(x) |
27 | * Return the base 10 logarithm of x |
28 | * |
29 | * Method : |
30 | * Let log10_2hi = leading 40 bits of log10(2) and |
31 | * log10_2lo = log10(2) - log10_2hi, |
32 | * ivln10 = 1/log(10) rounded. |
33 | * Then |
34 | * n = ilogb(x), |
35 | * if(n<0) n = n+1; |
36 | * x = scalbn(x,-n); |
37 | * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x)) |
38 | * |
39 | * Note 1: |
40 | * To guarantee log10(10**n)=n, where 10**n is normal, the rounding |
41 | * mode must set to Round-to-Nearest. |
42 | * Note 2: |
43 | * [1/log(10)] rounded to 53 bits has error .198 ulps; |
44 | * log10 is monotonic at all binary break points. |
45 | * |
46 | * Special cases: |
47 | * log10(x) is NaN with signal if x < 0; |
48 | * log10(+INF) is +INF with no signal; log10(0) is -INF with signal; |
49 | * log10(NaN) is that NaN with no signal; |
50 | * log10(10**N) = N for N=0,1,...,22. |
51 | * |
52 | * Constants: |
53 | * The hexadecimal values are the intended ones for the following constants. |
54 | * The decimal values may be used, provided that the compiler will convert |
55 | * from decimal to binary accurately enough to produce the hexadecimal values |
56 | * shown. |
57 | */ |
58 | |
59 | #include "fdlibm.h" |
60 | |
61 | #ifdef __STDC__ |
62 | static const double |
63 | #else |
64 | static double |
65 | #endif |
66 | two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ |
67 | ivln10 = 4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */ |
68 | log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */ |
69 | log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */ |
70 | |
71 | static double zero = 0.0; |
72 | |
73 | #ifdef __STDC__ |
74 | double __ieee754_log10(double x) |
75 | #else |
76 | double __ieee754_log10(x) |
77 | double x; |
78 | #endif |
79 | { |
80 | double y,z; |
81 | int i,k,hx; |
82 | unsigned lx; |
83 | |
84 | hx = __HI(x); /* high word of x */ |
85 | lx = __LO(x); /* low word of x */ |
86 | |
87 | k=0; |
88 | if (hx < 0x00100000) { /* x < 2**-1022 */ |
89 | if (((hx&0x7fffffff)|lx)==0) |
90 | return -two54/zero; /* log(+-0)=-inf */ |
91 | if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ |
92 | k -= 54; x *= two54; /* subnormal number, scale up x */ |
93 | hx = __HI(x); /* high word of x */ |
94 | } |
95 | if (hx >= 0x7ff00000) return x+x; |
96 | k += (hx>>20)-1023; |
97 | i = ((unsigned)k&0x80000000)>>31; |
98 | hx = (hx&0x000fffff)|((0x3ff-i)<<20); |
99 | y = (double)(k+i); |
100 | __HI(x) = hx; |
101 | z = y*log10_2lo + ivln10*__ieee754_log(x); |
102 | return z+y*log10_2hi; |
103 | } |
104 | |