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

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa/strtod.cc

Issue 2700123003: DO NOT COMMIT: Results of running old (current) clang-format on Blink (Closed)
Patch Set: Created 3 years, 10 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
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "strtod.h" 28 #include "strtod.h"
29 29
30 #include <limits.h>
31 #include <stdarg.h>
30 #include "bignum.h" 32 #include "bignum.h"
31 #include "cached-powers.h" 33 #include "cached-powers.h"
32 #include "double.h" 34 #include "double.h"
33 #include <stdarg.h>
34 #include <limits.h>
35 35
36 namespace WTF { 36 namespace WTF {
37 37
38 namespace double_conversion { 38 namespace double_conversion {
39 39
40 // 2^53 = 9007199254740992. 40 // 2^53 = 9007199254740992.
41 // Any integer with at most 15 decimal digits will hence fit into a double 41 // Any integer with at most 15 decimal digits will hence fit into a double
42 // (which has a 53bit significand) without loss of precision. 42 // (which has a 53bit significand) without loss of precision.
43 static const int kMaxExactDoubleIntegerDecimalDigits = 15; 43 static const int kMaxExactDoubleIntegerDecimalDigits = 15;
44 // 2^64 = 18446744073709551616 > 10^19 44 // 2^64 = 18446744073709551616 > 10^19
45 static const int kMaxUint64DecimalDigits = 19; 45 static const int kMaxUint64DecimalDigits = 19;
46 46
47 // Max double: 1.7976931348623157 x 10^308 47 // Max double: 1.7976931348623157 x 10^308
48 // Min non-zero double: 4.9406564584124654 x 10^-324 48 // Min non-zero double: 4.9406564584124654 x 10^-324
49 // Any x >= 10^309 is interpreted as +infinity. 49 // Any x >= 10^309 is interpreted as +infinity.
50 // Any x <= 10^-324 is interpreted as 0. 50 // Any x <= 10^-324 is interpreted as 0.
51 // Note that 2.5e-324 (despite being smaller than the min double) will be re ad 51 // Note that 2.5e-324 (despite being smaller than the min double) will be read
52 // as non-zero (equal to the min non-zero double). 52 // as non-zero (equal to the min non-zero double).
53 static const int kMaxDecimalPower = 309; 53 static const int kMaxDecimalPower = 309;
54 static const int kMinDecimalPower = -324; 54 static const int kMinDecimalPower = -324;
55 55
56 // 2^64 = 18446744073709551616 56 // 2^64 = 18446744073709551616
57 static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); 57 static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
58 58
59 59 static const double exact_powers_of_ten[] = {
60 static const double exact_powers_of_ten[] = { 60 1.0, // 10^0
61 1.0, // 10^0 61 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0,
62 10.0, 62 1000000000.0,
63 100.0, 63 10000000000.0, // 10^10
64 1000.0, 64 100000000000.0, 1000000000000.0, 10000000000000.0, 100000000000000.0,
65 10000.0, 65 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
66 100000.0, 66 1000000000000000000.0, 10000000000000000000.0,
67 1000000.0, 67 100000000000000000000.0, // 10^20
68 10000000.0, 68 1000000000000000000000.0,
69 100000000.0, 69 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
70 1000000000.0, 70 10000000000000000000000.0};
71 10000000000.0, // 10^10 71 static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
72 100000000000.0, 72
73 1000000000000.0, 73 // Maximum number of significant digits in the decimal representation.
74 10000000000000.0, 74 // In fact the value is 772 (see conversions.cc), but to give us some margin
75 100000000000000.0, 75 // we round up to 780.
76 1000000000000000.0, 76 static const int kMaxSignificantDecimalDigits = 780;
77 10000000000000000.0, 77
78 100000000000000000.0, 78 static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
79 1000000000000000000.0, 79 for (int i = 0; i < buffer.length(); i++) {
80 10000000000000000000.0, 80 if (buffer[i] != '0') {
81 100000000000000000000.0, // 10^20 81 return buffer.SubVector(i, buffer.length());
82 1000000000000000000000.0, 82 }
83 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 83 }
84 10000000000000000000000.0 84 return Vector<const char>(buffer.start(), 0);
85 }; 85 }
86 static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); 86
87 87 static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
88 // Maximum number of significant digits in the decimal representation. 88 for (int i = buffer.length() - 1; i >= 0; --i) {
89 // In fact the value is 772 (see conversions.cc), but to give us some margin 89 if (buffer[i] != '0') {
90 // we round up to 780. 90 return buffer.SubVector(0, i + 1);
91 static const int kMaxSignificantDecimalDigits = 780; 91 }
92 92 }
93 static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { 93 return Vector<const char>(buffer.start(), 0);
94 for (int i = 0; i < buffer.length(); i++) { 94 }
95 if (buffer[i] != '0') { 95
96 return buffer.SubVector(i, buffer.length()); 96 static void TrimToMaxSignificantDigits(Vector<const char> buffer,
97 } 97 int exponent,
98 } 98 char* significant_buffer,
99 return Vector<const char>(buffer.start(), 0); 99 int* significant_exponent) {
100 } 100 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
101 101 significant_buffer[i] = buffer[i];
102 102 }
103 static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { 103 // The input buffer has been trimmed. Therefore the last digit must be
104 for (int i = buffer.length() - 1; i >= 0; --i) { 104 // different from '0'.
105 if (buffer[i] != '0') { 105 ASSERT(buffer[buffer.length() - 1] != '0');
106 return buffer.SubVector(0, i + 1); 106 // Set the last digit to be non-zero. This is sufficient to guarantee
107 } 107 // correct rounding.
108 } 108 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
109 return Vector<const char>(buffer.start(), 0); 109 *significant_exponent =
110 } 110 exponent + (buffer.length() - kMaxSignificantDecimalDigits);
111 111 }
112 112
113 static void TrimToMaxSignificantDigits(Vector<const char> buffer, 113 // Reads digits from the buffer and converts them to a uint64.
114 int exponent, 114 // Reads in as many digits as fit into a uint64.
115 char* significant_buffer, 115 // When the string starts with "1844674407370955161" no further digit is read.
116 int* significant_exponent) { 116 // Since 2^64 = 18446744073709551616 it would still be possible read another
117 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { 117 // digit if it was less or equal than 6, but this would complicate the code.
118 significant_buffer[i] = buffer[i]; 118 static uint64_t ReadUint64(Vector<const char> buffer,
119 } 119 int* number_of_read_digits) {
120 // The input buffer has been trimmed. Therefore the last digit must be 120 uint64_t result = 0;
121 // different from '0'. 121 int i = 0;
122 ASSERT(buffer[buffer.length() - 1] != '0'); 122 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
123 // Set the last digit to be non-zero. This is sufficient to guarantee 123 int digit = buffer[i++] - '0';
124 // correct rounding. 124 ASSERT(0 <= digit && digit <= 9);
125 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; 125 result = 10 * result + digit;
126 *significant_exponent = 126 }
127 exponent + (buffer.length() - kMaxSignificantDecimalDigits); 127 *number_of_read_digits = i;
128 } 128 return result;
129 129 }
130 // Reads digits from the buffer and converts them to a uint64. 130
131 // Reads in as many digits as fit into a uint64. 131 // Reads a DiyFp from the buffer.
132 // When the string starts with "1844674407370955161" no further digit is rea d. 132 // The returned DiyFp is not necessarily normalized.
133 // Since 2^64 = 18446744073709551616 it would still be possible read another 133 // If remaining_decimals is zero then the returned DiyFp is accurate.
134 // digit if it was less or equal than 6, but this would complicate the code. 134 // Otherwise it has been rounded and has error of at most 1/2 ulp.
135 static uint64_t ReadUint64(Vector<const char> buffer, 135 static void ReadDiyFp(Vector<const char> buffer,
136 int* number_of_read_digits) { 136 DiyFp* result,
137 uint64_t result = 0; 137 int* remaining_decimals) {
138 int i = 0; 138 int read_digits;
139 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { 139 uint64_t significand = ReadUint64(buffer, &read_digits);
140 int digit = buffer[i++] - '0'; 140 if (buffer.length() == read_digits) {
141 ASSERT(0 <= digit && digit <= 9); 141 *result = DiyFp(significand, 0);
142 result = 10 * result + digit; 142 *remaining_decimals = 0;
143 } 143 } else {
144 *number_of_read_digits = i; 144 // Round the significand.
145 return result; 145 if (buffer[read_digits] >= '5') {
146 } 146 significand++;
147 147 }
148 148 // Compute the binary exponent.
149 // Reads a DiyFp from the buffer. 149 int exponent = 0;
150 // The returned DiyFp is not necessarily normalized. 150 *result = DiyFp(significand, exponent);
151 // If remaining_decimals is zero then the returned DiyFp is accurate. 151 *remaining_decimals = buffer.length() - read_digits;
152 // Otherwise it has been rounded and has error of at most 1/2 ulp. 152 }
153 static void ReadDiyFp(Vector<const char> buffer, 153 }
154 DiyFp* result, 154
155 int* remaining_decimals) { 155 static bool DoubleStrtod(Vector<const char> trimmed,
156 int read_digits; 156 int exponent,
157 uint64_t significand = ReadUint64(buffer, &read_digits); 157 double* result) {
158 if (buffer.length() == read_digits) {
159 *result = DiyFp(significand, 0);
160 *remaining_decimals = 0;
161 } else {
162 // Round the significand.
163 if (buffer[read_digits] >= '5') {
164 significand++;
165 }
166 // Compute the binary exponent.
167 int exponent = 0;
168 *result = DiyFp(significand, exponent);
169 *remaining_decimals = buffer.length() - read_digits;
170 }
171 }
172
173
174 static bool DoubleStrtod(Vector<const char> trimmed,
175 int exponent,
176 double* result) {
177 #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) 158 #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS)
178 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is 159 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
179 // 80 bits wide (as is the case on Linux) then double-rounding occurs an d the 160 // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
180 // result is not accurate. 161 // result is not accurate.
181 // We know that Windows32 uses 64 bits and is therefore accurate. 162 // We know that Windows32 uses 64 bits and is therefore accurate.
182 // Note that the ARM simulator is compiled for 32bits. It therefore exhi bits 163 // Note that the ARM simulator is compiled for 32bits. It therefore exhibits
183 // the same problem. 164 // the same problem.
184 return false; 165 return false;
185 #endif 166 #endif
186 if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { 167 if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
187 int read_digits; 168 int read_digits;
188 // The trimmed input fits into a double. 169 // The trimmed input fits into a double.
189 // If the 10^exponent (resp. 10^-exponent) fits into a double too th en we 170 // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
190 // can compute the result-double simply by multiplying (resp. dividi ng) the 171 // can compute the result-double simply by multiplying (resp. dividing) the
191 // two numbers. 172 // two numbers.
192 // This is possible because IEEE guarantees that floating-point oper ations 173 // This is possible because IEEE guarantees that floating-point operations
193 // return the best possible approximation. 174 // return the best possible approximation.
194 if (exponent < 0 && -exponent < kExactPowersOfTenSize) { 175 if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
195 // 10^-exponent fits into a double. 176 // 10^-exponent fits into a double.
196 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)) ; 177 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
197 ASSERT(read_digits == trimmed.length()); 178 ASSERT(read_digits == trimmed.length());
198 *result /= exact_powers_of_ten[-exponent]; 179 *result /= exact_powers_of_ten[-exponent];
199 return true; 180 return true;
200 } 181 }
201 if (0 <= exponent && exponent < kExactPowersOfTenSize) { 182 if (0 <= exponent && exponent < kExactPowersOfTenSize) {
202 // 10^exponent fits into a double. 183 // 10^exponent fits into a double.
203 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)) ; 184 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
204 ASSERT(read_digits == trimmed.length()); 185 ASSERT(read_digits == trimmed.length());
205 *result *= exact_powers_of_ten[exponent]; 186 *result *= exact_powers_of_ten[exponent];
206 return true; 187 return true;
207 } 188 }
208 int remaining_digits = 189 int remaining_digits =
209 kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); 190 kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
210 if ((0 <= exponent) && 191 if ((0 <= exponent) &&
211 (exponent - remaining_digits < kExactPowersOfTenSize)) { 192 (exponent - remaining_digits < kExactPowersOfTenSize)) {
212 // The trimmed string was short and we can multiply it with 193 // The trimmed string was short and we can multiply it with
213 // 10^remaining_digits. As a result the remaining exponent now f its 194 // 10^remaining_digits. As a result the remaining exponent now fits
214 // into a double too. 195 // into a double too.
215 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)) ; 196 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
216 ASSERT(read_digits == trimmed.length()); 197 ASSERT(read_digits == trimmed.length());
217 *result *= exact_powers_of_ten[remaining_digits]; 198 *result *= exact_powers_of_ten[remaining_digits];
218 *result *= exact_powers_of_ten[exponent - remaining_digits]; 199 *result *= exact_powers_of_ten[exponent - remaining_digits];
219 return true; 200 return true;
220 } 201 }
221 } 202 }
222 return false; 203 return false;
223 } 204 }
224 205
225 206 // Returns 10^exponent as an exact DiyFp.
226 // Returns 10^exponent as an exact DiyFp. 207 // The given exponent must be in the range [1; kDecimalExponentDistance[.
227 // The given exponent must be in the range [1; kDecimalExponentDistance[. 208 static DiyFp AdjustmentPowerOfTen(int exponent) {
228 static DiyFp AdjustmentPowerOfTen(int exponent) { 209 ASSERT(0 < exponent);
229 ASSERT(0 < exponent); 210 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
230 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); 211 // Simply hardcode the remaining powers for the given decimal exponent
231 // Simply hardcode the remaining powers for the given decimal exponent 212 // distance.
232 // distance. 213 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
233 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); 214 switch (exponent) {
234 switch (exponent) { 215 case 1:
235 case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); 216 return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60);
236 case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); 217 case 2:
237 case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); 218 return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57);
238 case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); 219 case 3:
239 case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); 220 return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54);
240 case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); 221 case 4:
241 case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); 222 return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50);
242 default: 223 case 5:
243 UNREACHABLE(); 224 return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47);
244 return DiyFp(0, 0); 225 case 6:
245 } 226 return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44);
246 } 227 case 7:
247 228 return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40);
248 229 default:
249 // If the function returns true then the result is the correct double. 230 UNREACHABLE();
250 // Otherwise it is either the correct double or the double that is just belo w 231 return DiyFp(0, 0);
251 // the correct double. 232 }
252 static bool DiyFpStrtod(Vector<const char> buffer, 233 }
253 int exponent, 234
254 double* result) { 235 // If the function returns true then the result is the correct double.
255 DiyFp input; 236 // Otherwise it is either the correct double or the double that is just below
256 int remaining_decimals; 237 // the correct double.
257 ReadDiyFp(buffer, &input, &remaining_decimals); 238 static bool DiyFpStrtod(Vector<const char> buffer,
258 // Since we may have dropped some digits the input is not accurate. 239 int exponent,
259 // If remaining_decimals is different than 0 than the error is at most 240 double* result) {
260 // .5 ulp (unit in the last place). 241 DiyFp input;
261 // We don't want to deal with fractions and therefore keep a common 242 int remaining_decimals;
262 // denominator. 243 ReadDiyFp(buffer, &input, &remaining_decimals);
263 const int kDenominatorLog = 3; 244 // Since we may have dropped some digits the input is not accurate.
264 const int kDenominator = 1 << kDenominatorLog; 245 // If remaining_decimals is different than 0 than the error is at most
265 // Move the remaining decimals into the exponent. 246 // .5 ulp (unit in the last place).
266 exponent += remaining_decimals; 247 // We don't want to deal with fractions and therefore keep a common
267 int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2); 248 // denominator.
268 249 const int kDenominatorLog = 3;
269 int old_e = input.e(); 250 const int kDenominator = 1 << kDenominatorLog;
270 input.Normalize(); 251 // Move the remaining decimals into the exponent.
271 error <<= old_e - input.e(); 252 exponent += remaining_decimals;
272 253 int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
273 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); 254
274 if (exponent < PowersOfTenCache::kMinDecimalExponent) { 255 int old_e = input.e();
275 *result = 0.0; 256 input.Normalize();
276 return true; 257 error <<= old_e - input.e();
277 } 258
278 DiyFp cached_power; 259 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
279 int cached_decimal_exponent; 260 if (exponent < PowersOfTenCache::kMinDecimalExponent) {
280 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, 261 *result = 0.0;
281 &cached_power, 262 return true;
282 &cached_decimal_expon ent); 263 }
283 264 DiyFp cached_power;
284 if (cached_decimal_exponent != exponent) { 265 int cached_decimal_exponent;
285 int adjustment_exponent = exponent - cached_decimal_exponent; 266 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, &cached_power,
286 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); 267 &cached_decimal_exponent);
287 input.Multiply(adjustment_power); 268
288 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent ) { 269 if (cached_decimal_exponent != exponent) {
289 // The product of input with the adjustment power fits into a 64 bit 270 int adjustment_exponent = exponent - cached_decimal_exponent;
290 // integer. 271 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
291 ASSERT(DiyFp::kSignificandSize == 64); 272 input.Multiply(adjustment_power);
292 } else { 273 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
293 // The adjustment power is exact. There is hence only an error o f 0.5. 274 // The product of input with the adjustment power fits into a 64 bit
294 error += kDenominator / 2; 275 // integer.
295 } 276 ASSERT(DiyFp::kSignificandSize == 64);
296 } 277 } else {
297 278 // The adjustment power is exact. There is hence only an error of 0.5.
298 input.Multiply(cached_power); 279 error += kDenominator / 2;
299 // The error introduced by a multiplication of a*b equals 280 }
300 // error_a + error_b + error_a*error_b/2^64 + 0.5 281 }
301 // Substituting a with 'input' and b with 'cached_power' we have 282
302 // error_b = 0.5 (all cached powers have an error of less than 0.5 ul p), 283 input.Multiply(cached_power);
303 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 284 // The error introduced by a multiplication of a*b equals
304 int error_b = kDenominator / 2; 285 // error_a + error_b + error_a*error_b/2^64 + 0.5
305 int error_ab = (error == 0 ? 0 : 1); // We round up to 1. 286 // Substituting a with 'input' and b with 'cached_power' we have
306 int fixed_error = kDenominator / 2; 287 // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp),
307 error += error_b + error_ab + fixed_error; 288 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
308 289 int error_b = kDenominator / 2;
309 old_e = input.e(); 290 int error_ab = (error == 0 ? 0 : 1); // We round up to 1.
310 input.Normalize(); 291 int fixed_error = kDenominator / 2;
311 error <<= old_e - input.e(); 292 error += error_b + error_ab + fixed_error;
312 293
313 // See if the double's significand changes if we add/subtract the error. 294 old_e = input.e();
314 int order_of_magnitude = DiyFp::kSignificandSize + input.e(); 295 input.Normalize();
315 int effective_significand_size = 296 error <<= old_e - input.e();
316 Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); 297
317 int precision_digits_count = 298 // See if the double's significand changes if we add/subtract the error.
318 DiyFp::kSignificandSize - effective_significand_size; 299 int order_of_magnitude = DiyFp::kSignificandSize + input.e();
319 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { 300 int effective_significand_size =
320 // This can only happen for very small denormals. In this case the 301 Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
321 // half-way multiplied by the denominator exceeds the range of an ui nt64. 302 int precision_digits_count =
322 // Simply shift everything to the right. 303 DiyFp::kSignificandSize - effective_significand_size;
323 int shift_amount = (precision_digits_count + kDenominatorLog) - 304 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
324 DiyFp::kSignificandSize + 1; 305 // This can only happen for very small denormals. In this case the
325 input.set_f(input.f() >> shift_amount); 306 // half-way multiplied by the denominator exceeds the range of an uint64.
326 input.set_e(input.e() + shift_amount); 307 // Simply shift everything to the right.
327 // We add 1 for the lost precision of error, and kDenominator for 308 int shift_amount = (precision_digits_count + kDenominatorLog) -
328 // the lost precision of input.f(). 309 DiyFp::kSignificandSize + 1;
329 error = (error >> shift_amount) + 1 + kDenominator; 310 input.set_f(input.f() >> shift_amount);
330 precision_digits_count -= shift_amount; 311 input.set_e(input.e() + shift_amount);
331 } 312 // We add 1 for the lost precision of error, and kDenominator for
332 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too . 313 // the lost precision of input.f().
333 ASSERT(DiyFp::kSignificandSize == 64); 314 error = (error >> shift_amount) + 1 + kDenominator;
334 ASSERT(precision_digits_count < 64); 315 precision_digits_count -= shift_amount;
335 uint64_t one64 = 1; 316 }
336 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; 317 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
337 uint64_t precision_bits = input.f() & precision_bits_mask; 318 ASSERT(DiyFp::kSignificandSize == 64);
338 uint64_t half_way = one64 << (precision_digits_count - 1); 319 ASSERT(precision_digits_count < 64);
339 precision_bits *= kDenominator; 320 uint64_t one64 = 1;
340 half_way *= kDenominator; 321 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
341 DiyFp rounded_input(input.f() >> precision_digits_count, 322 uint64_t precision_bits = input.f() & precision_bits_mask;
342 input.e() + precision_digits_count); 323 uint64_t half_way = one64 << (precision_digits_count - 1);
343 if (precision_bits >= half_way + error) { 324 precision_bits *= kDenominator;
344 rounded_input.set_f(rounded_input.f() + 1); 325 half_way *= kDenominator;
345 } 326 DiyFp rounded_input(input.f() >> precision_digits_count,
346 // If the last_bits are too close to the half-way case than we are too 327 input.e() + precision_digits_count);
347 // inaccurate and round down. In this case we return false so that we ca n 328 if (precision_bits >= half_way + error) {
348 // fall back to a more precise algorithm. 329 rounded_input.set_f(rounded_input.f() + 1);
349 330 }
350 *result = Double(rounded_input).value(); 331 // If the last_bits are too close to the half-way case than we are too
351 if (half_way - error < precision_bits && precision_bits < half_way + err or) { 332 // inaccurate and round down. In this case we return false so that we can
352 // Too imprecise. The caller will have to fall back to a slower vers ion. 333 // fall back to a more precise algorithm.
353 // However the returned number is guaranteed to be either the correc t 334
354 // double, or the next-lower double. 335 *result = Double(rounded_input).value();
355 return false; 336 if (half_way - error < precision_bits && precision_bits < half_way + error) {
356 } else { 337 // Too imprecise. The caller will have to fall back to a slower version.
357 return true; 338 // However the returned number is guaranteed to be either the correct
358 } 339 // double, or the next-lower double.
359 } 340 return false;
360 341 } else {
361 342 return true;
362 // Returns the correct double for the buffer*10^exponent. 343 }
363 // The variable guess should be a close guess that is either the correct dou ble 344 }
364 // or its lower neighbor (the nearest double less than the correct one). 345
365 // Preconditions: 346 // Returns the correct double for the buffer*10^exponent.
366 // buffer.length() + exponent <= kMaxDecimalPower + 1 347 // The variable guess should be a close guess that is either the correct double
367 // buffer.length() + exponent > kMinDecimalPower 348 // or its lower neighbor (the nearest double less than the correct one).
368 // buffer.length() <= kMaxDecimalSignificantDigits 349 // Preconditions:
369 static double BignumStrtod(Vector<const char> buffer, 350 // buffer.length() + exponent <= kMaxDecimalPower + 1
370 int exponent, 351 // buffer.length() + exponent > kMinDecimalPower
371 double guess) { 352 // buffer.length() <= kMaxDecimalSignificantDigits
372 if (guess == Double::Infinity()) { 353 static double BignumStrtod(Vector<const char> buffer,
373 return guess; 354 int exponent,
374 } 355 double guess) {
375 356 if (guess == Double::Infinity()) {
376 DiyFp upper_boundary = Double(guess).UpperBoundary(); 357 return guess;
377 358 }
378 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); 359
379 ASSERT(buffer.length() + exponent > kMinDecimalPower); 360 DiyFp upper_boundary = Double(guess).UpperBoundary();
380 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); 361
381 // Make sure that the Bignum will be able to hold all our numbers. 362 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
382 // Our Bignum implementation has a separate field for exponents. Shifts will 363 ASSERT(buffer.length() + exponent > kMinDecimalPower);
383 // consume at most one bigit (< 64 bits). 364 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
384 // ln(10) == 3.3219... 365 // Make sure that the Bignum will be able to hold all our numbers.
385 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBit s); 366 // Our Bignum implementation has a separate field for exponents. Shifts will
386 Bignum input; 367 // consume at most one bigit (< 64 bits).
387 Bignum boundary; 368 // ln(10) == 3.3219...
388 input.AssignDecimalString(buffer); 369 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
389 boundary.AssignUInt64(upper_boundary.f()); 370 Bignum input;
390 if (exponent >= 0) { 371 Bignum boundary;
391 input.MultiplyByPowerOfTen(exponent); 372 input.AssignDecimalString(buffer);
392 } else { 373 boundary.AssignUInt64(upper_boundary.f());
393 boundary.MultiplyByPowerOfTen(-exponent); 374 if (exponent >= 0) {
394 } 375 input.MultiplyByPowerOfTen(exponent);
395 if (upper_boundary.e() > 0) { 376 } else {
396 boundary.ShiftLeft(upper_boundary.e()); 377 boundary.MultiplyByPowerOfTen(-exponent);
397 } else { 378 }
398 input.ShiftLeft(-upper_boundary.e()); 379 if (upper_boundary.e() > 0) {
399 } 380 boundary.ShiftLeft(upper_boundary.e());
400 int comparison = Bignum::Compare(input, boundary); 381 } else {
401 if (comparison < 0) { 382 input.ShiftLeft(-upper_boundary.e());
402 return guess; 383 }
403 } else if (comparison > 0) { 384 int comparison = Bignum::Compare(input, boundary);
404 return Double(guess).NextDouble(); 385 if (comparison < 0) {
405 } else if ((Double(guess).Significand() & 1) == 0) { 386 return guess;
406 // Round towards even. 387 } else if (comparison > 0) {
407 return guess; 388 return Double(guess).NextDouble();
408 } else { 389 } else if ((Double(guess).Significand() & 1) == 0) {
409 return Double(guess).NextDouble(); 390 // Round towards even.
410 } 391 return guess;
411 } 392 } else {
412 393 return Double(guess).NextDouble();
413 394 }
414 double Strtod(Vector<const char> buffer, int exponent) { 395 }
415 Vector<const char> left_trimmed = TrimLeadingZeros(buffer); 396
416 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed); 397 double Strtod(Vector<const char> buffer, int exponent) {
417 exponent += left_trimmed.length() - trimmed.length(); 398 Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
418 if (trimmed.length() == 0) return 0.0; 399 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
419 if (trimmed.length() > kMaxSignificantDecimalDigits) { 400 exponent += left_trimmed.length() - trimmed.length();
420 char significant_buffer[kMaxSignificantDecimalDigits]; 401 if (trimmed.length() == 0)
421 int significant_exponent; 402 return 0.0;
422 TrimToMaxSignificantDigits(trimmed, exponent, 403 if (trimmed.length() > kMaxSignificantDecimalDigits) {
423 significant_buffer, &significant_exponent ); 404 char significant_buffer[kMaxSignificantDecimalDigits];
424 return Strtod(Vector<const char>(significant_buffer, 405 int significant_exponent;
425 kMaxSignificantDecimalDigits), 406 TrimToMaxSignificantDigits(trimmed, exponent, significant_buffer,
426 significant_exponent); 407 &significant_exponent);
427 } 408 return Strtod(
428 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { 409 Vector<const char>(significant_buffer, kMaxSignificantDecimalDigits),
429 return Double::Infinity(); 410 significant_exponent);
430 } 411 }
431 if (exponent + trimmed.length() <= kMinDecimalPower) { 412 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) {
432 return 0.0; 413 return Double::Infinity();
433 } 414 }
434 415 if (exponent + trimmed.length() <= kMinDecimalPower) {
435 double guess; 416 return 0.0;
436 if (DoubleStrtod(trimmed, exponent, &guess) || 417 }
437 DiyFpStrtod(trimmed, exponent, &guess)) { 418
438 return guess; 419 double guess;
439 } 420 if (DoubleStrtod(trimmed, exponent, &guess) ||
440 return BignumStrtod(trimmed, exponent, guess); 421 DiyFpStrtod(trimmed, exponent, &guess)) {
441 } 422 return guess;
423 }
424 return BignumStrtod(trimmed, exponent, guess);
425 }
442 426
443 } // namespace double_conversion 427 } // namespace double_conversion
444 428
445 } // namespace WTF 429 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa/fixed-dtoa.cc ('k') | third_party/WebKit/Source/wtf/dtoa/utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698