Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: src/base/ieee754.cc

Issue 2053893003: [builtins] Introduce proper base::ieee754::log. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: BUILD.gn Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/base/ieee754.h ('k') | src/compiler/arm/code-generator-arm.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm).
2 //
3 // ====================================================
4 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 //
6 // Developed at SunSoft, a Sun Microsystems, Inc. business.
7 // Permission to use, copy, modify, and distribute this
8 // software is freely granted, provided that this notice
9 // is preserved.
10 // ====================================================
11 //
12 // The original source code covered by the above license above has been
13 // modified significantly by Google Inc.
14 // Copyright 2016 the V8 project authors. All rights reserved.
15
16 #include "src/base/ieee754.h"
17
18 #include <limits>
19
20 #include "src/base/build_config.h"
21 #include "src/base/macros.h"
22
23 namespace v8 {
24 namespace base {
25 namespace ieee754 {
26
27 namespace {
28
29 union Float64 {
30 double v;
31 uint64_t w;
32 struct {
33 #if V8_TARGET_LITTLE_ENDIAN
34 uint32_t lw;
35 uint32_t hw;
36 #else
37 uint32_t hw;
38 uint32_t lw;
39 #endif
40 } words;
41 };
42
43 // Extract the less significant 32-bit word from a double.
44 V8_INLINE uint32_t extractLowWord32(double v) {
45 Float64 f;
46 f.v = v;
47 return f.words.lw;
48 }
49
50 // Extract the most significant 32-bit word from a double.
51 V8_INLINE uint32_t extractHighWord32(double v) {
52 Float64 f;
53 f.v = v;
54 return f.words.hw;
55 }
56
57 // Insert the most significant 32-bit word into a double.
58 V8_INLINE double insertHighWord32(double v, uint32_t hw) {
59 Float64 f;
60 f.v = v;
61 f.words.hw = hw;
62 return f.v;
63 }
64
65 double const kLn2Hi = 6.93147180369123816490e-01; // 3fe62e42 fee00000
66 double const kLn2Lo = 1.90821492927058770002e-10; // 3dea39ef 35793c76
67 double const kTwo54 = 1.80143985094819840000e+16; // 43500000 00000000
68 double const kLg1 = 6.666666666666735130e-01; // 3FE55555 55555593
69 double const kLg2 = 3.999999999940941908e-01; // 3FD99999 9997FA04
70 double const kLg3 = 2.857142874366239149e-01; // 3FD24924 94229359
71 double const kLg4 = 2.222219843214978396e-01; // 3FCC71C5 1D8E78AF
72 double const kLg5 = 1.818357216161805012e-01; // 3FC74664 96CB03DE
73 double const kLg6 = 1.531383769920937332e-01; // 3FC39A09 D078C69F
74 double const kLg7 = 1.479819860511658591e-01; // 3FC2F112 DF3E5244
75
76 } // namespace
77
78 /* log(x)
79 * Return the logrithm of x
80 *
81 * Method :
82 * 1. Argument Reduction: find k and f such that
83 * x = 2^k * (1+f),
84 * where sqrt(2)/2 < 1+f < sqrt(2) .
85 *
86 * 2. Approximation of log(1+f).
87 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
88 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
89 * = 2s + s*R
90 * We use a special Reme algorithm on [0,0.1716] to generate
91 * a polynomial of degree 14 to approximate R The maximum error
92 * of this polynomial approximation is bounded by 2**-58.45. In
93 * other words,
94 * 2 4 6 8 10 12 14
95 * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
96 * (the values of Lg1 to Lg7 are listed in the program)
97 * and
98 * | 2 14 | -58.45
99 * | Lg1*s +...+Lg7*s - R(z) | <= 2
100 * | |
101 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
102 * In order to guarantee error in log below 1ulp, we compute log
103 * by
104 * log(1+f) = f - s*(f - R) (if f is not too large)
105 * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
106 *
107 * 3. Finally, log(x) = k*ln2 + log(1+f).
108 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
109 * Here ln2 is split into two floating point number:
110 * ln2_hi + ln2_lo,
111 * where n*ln2_hi is always exact for |n| < 2000.
112 *
113 * Special cases:
114 * log(x) is NaN with signal if x < 0 (including -INF) ;
115 * log(+INF) is +INF; log(0) is -INF with signal;
116 * log(NaN) is that NaN with no signal.
117 *
118 * Accuracy:
119 * according to an error analysis, the error is always less than
120 * 1 ulp (unit in the last place).
121 *
122 * Constants:
123 * The hexadecimal values are the intended ones for the following
124 * constants. The decimal values may be used, provided that the
125 * compiler will convert from decimal to binary accurately enough
126 * to produce the hexadecimal values shown.
127 */
128 double log(double x) {
129 double hfsq, f, s, z, r, w, t1, t2, dk;
130 int32_t k = 0, i, j;
131 int32_t hx = extractHighWord32(x);
132 uint32_t lx = extractLowWord32(x);
133
134 if (hx < 0x00100000) { /* x < 2**-1022 */
135 if (((hx & 0x7fffffff) | lx) == 0) {
136 return -std::numeric_limits<double>::infinity();
137 }
138 if (hx < 0) {
139 return std::numeric_limits<double>::quiet_NaN();
140 }
141 k -= 54;
142 x *= kTwo54; /* subnormal number, scale up x */
143 hx = extractHighWord32(x);
144 }
145 if (hx >= 0x7ff00000) return x + x;
146 k += (hx >> 20) - 1023;
147 hx &= 0x000fffff;
148 i = (hx + 0x95f64) & 0x100000;
149 x = insertHighWord32(x, hx | (i ^ 0x3ff00000)); /* normalize x or x/2 */
150 k += (i >> 20);
151 f = x - 1.0;
152 if ((0x000fffff & (2 + hx)) < 3) { /* -2**-20 <= f < 2**-20 */
153 if (f == 0.0) {
154 if (k == 0) {
155 return 0.0;
156 } else {
157 dk = static_cast<double>(k);
158 return dk * kLn2Hi + dk * kLn2Lo;
159 }
160 }
161 r = f * f * (0.5 - 0.33333333333333333 * f);
162 if (k == 0) {
163 return f - r;
164 } else {
165 dk = static_cast<double>(k);
166 return dk * kLn2Hi - ((r - dk * kLn2Lo) - f);
167 }
168 }
169 s = f / (2.0 + f);
170 dk = static_cast<double>(k);
171 z = s * s;
172 i = hx - 0x6147a;
173 w = z * z;
174 j = 0x6b851 - hx;
175 t1 = w * (kLg2 + w * (kLg4 + w * kLg6));
176 t2 = z * (kLg1 + w * (kLg3 + w * (kLg5 + w * kLg7)));
177 i |= j;
178 r = t2 + t1;
179 if (i > 0) {
180 hfsq = 0.5 * f * f;
181 if (k == 0) {
182 return f - (hfsq - s * (hfsq + r));
183 } else {
184 return dk * kLn2Hi - ((hfsq - (s * (hfsq + r) + dk * kLn2Lo)) - f);
185 }
186 } else {
187 if (k == 0) {
188 return f - s * (f - r);
189 } else {
190 return dk * kLn2Hi - ((s * (f - r) - dk * kLn2Lo) - f);
191 }
192 }
193 }
194
195 } // namespace ieee754
196 } // namespace base
197 } // namespace v8
OLDNEW
« no previous file with comments | « src/base/ieee754.h ('k') | src/compiler/arm/code-generator-arm.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698