• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * This file is based on work under the following copyright and permission
16  * notice:
17  *
18  *     Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19  *
20  *     Developed at SunSoft, a Sun Microsystems, Inc. business.
21  *     Permission to use, copy, modify, and distribute this
22  *     software is freely granted, provided that this notice
23  *     is preserved.
24  *
25  *     @(#)e_log10.c 1.3 95/01/18
26  */
27 
28 #include "jerry-libm-internal.h"
29 
30 /* log10(x)
31  * Return the base 10 logarithm of x
32  *
33  * Method :
34  *  Let log10_2hi = leading 40 bits of log10(2) and
35  *      log10_2lo = log10(2) - log10_2hi,
36  *      ivln10   = 1/log(10) rounded.
37  *  Then
38  *    n = ilogb(x),
39  *    if(n<0)  n = n+1;
40  *    x = scalbn(x,-n);
41  *    log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
42  *
43  * Note 1:
44  *  To guarantee log10(10**n)=n, where 10**n is normal, the rounding
45  *  mode must set to Round-to-Nearest.
46  * Note 2:
47  *  [1/log(10)] rounded to 53 bits has error  .198   ulps;
48  *  log10 is monotonic at all binary break points.
49  *
50  * Special cases:
51  *  log10(x) is NaN with signal if x < 0;
52  *  log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
53  *  log10(NaN) is that NaN with no signal;
54  *  log10(10**N) = N  for N=0,1,...,22.
55  *
56  * Constants:
57  * The hexadecimal values are the intended ones for the following constants.
58  * The decimal values may be used, provided that the compiler will convert
59  * from decimal to binary accurately enough to produce the hexadecimal values
60  * shown.
61  */
62 
63 #define zero 0.0
64 #define two54 1.80143985094819840000e+16     /* 0x43500000, 0x00000000 */
65 #define ivln10 4.34294481903251816668e-01    /* 0x3FDBCB7B, 0x1526E50E */
66 #define log10_2hi 3.01029995663611771306e-01 /* 0x3FD34413, 0x509F6000 */
67 #define log10_2lo 3.69423907715893078616e-13 /* 0x3D59FEF3, 0x11F12B36 */
68 
69 double
log10(double x)70 log10 (double x)
71 {
72   double y, z;
73   int i, k, hx;
74   unsigned lx;
75   double_accessor temp;
76 
77   hx = __HI (x); /* high word of x */
78   lx = __LO (x); /* low word of x */
79 
80   k = 0;
81   if (hx < 0x00100000)
82   {
83     /* x < 2**-1022  */
84     if (((hx & 0x7fffffff) | lx) == 0)
85     {
86       /* log(+-0)=-inf */
87       return -two54 / zero;
88     }
89     if (hx < 0)
90     {
91       /* log(-#) = NaN */
92       return (x - x) / zero;
93     }
94     k -= 54;
95     x *= two54;    /* subnormal number, scale up x */
96     hx = __HI (x); /* high word of x */
97   }
98   if (hx >= 0x7ff00000)
99   {
100     return x + x;
101   }
102   k += (hx >> 20) - 1023;
103   i = ((unsigned) k & 0x80000000) >> 31;
104   hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
105   y = (double) (k + i);
106   temp.dbl = x;
107   temp.as_int.hi = hx;
108   z = y * log10_2lo + ivln10 * log (temp.dbl);
109   return z + y * log10_2hi;
110 } /* log10 */
111 
112 #undef zero
113 #undef two54
114 #undef ivln10
115 #undef log10_2hi
116 #undef log10_2lo
117