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

Side by Side Diff: src/strtod.cc

Issue 430503007: Rename ASSERT* to DCHECK*. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE and fixes Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « src/string-stream.cc ('k') | src/stub-cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdarg.h> 5 #include <stdarg.h>
6 #include <cmath> 6 #include <cmath>
7 7
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/bignum.h" 10 #include "src/bignum.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 92
93 static void TrimToMaxSignificantDigits(Vector<const char> buffer, 93 static void TrimToMaxSignificantDigits(Vector<const char> buffer,
94 int exponent, 94 int exponent,
95 char* significant_buffer, 95 char* significant_buffer,
96 int* significant_exponent) { 96 int* significant_exponent) {
97 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { 97 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
98 significant_buffer[i] = buffer[i]; 98 significant_buffer[i] = buffer[i];
99 } 99 }
100 // The input buffer has been trimmed. Therefore the last digit must be 100 // The input buffer has been trimmed. Therefore the last digit must be
101 // different from '0'. 101 // different from '0'.
102 ASSERT(buffer[buffer.length() - 1] != '0'); 102 DCHECK(buffer[buffer.length() - 1] != '0');
103 // Set the last digit to be non-zero. This is sufficient to guarantee 103 // Set the last digit to be non-zero. This is sufficient to guarantee
104 // correct rounding. 104 // correct rounding.
105 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; 105 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
106 *significant_exponent = 106 *significant_exponent =
107 exponent + (buffer.length() - kMaxSignificantDecimalDigits); 107 exponent + (buffer.length() - kMaxSignificantDecimalDigits);
108 } 108 }
109 109
110 110
111 // Reads digits from the buffer and converts them to a uint64. 111 // Reads digits from the buffer and converts them to a uint64.
112 // Reads in as many digits as fit into a uint64. 112 // Reads in as many digits as fit into a uint64.
113 // When the string starts with "1844674407370955161" no further digit is read. 113 // When the string starts with "1844674407370955161" no further digit is read.
114 // Since 2^64 = 18446744073709551616 it would still be possible read another 114 // Since 2^64 = 18446744073709551616 it would still be possible read another
115 // digit if it was less or equal than 6, but this would complicate the code. 115 // digit if it was less or equal than 6, but this would complicate the code.
116 static uint64_t ReadUint64(Vector<const char> buffer, 116 static uint64_t ReadUint64(Vector<const char> buffer,
117 int* number_of_read_digits) { 117 int* number_of_read_digits) {
118 uint64_t result = 0; 118 uint64_t result = 0;
119 int i = 0; 119 int i = 0;
120 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { 120 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
121 int digit = buffer[i++] - '0'; 121 int digit = buffer[i++] - '0';
122 ASSERT(0 <= digit && digit <= 9); 122 DCHECK(0 <= digit && digit <= 9);
123 result = 10 * result + digit; 123 result = 10 * result + digit;
124 } 124 }
125 *number_of_read_digits = i; 125 *number_of_read_digits = i;
126 return result; 126 return result;
127 } 127 }
128 128
129 129
130 // Reads a DiyFp from the buffer. 130 // Reads a DiyFp from the buffer.
131 // The returned DiyFp is not necessarily normalized. 131 // The returned DiyFp is not necessarily normalized.
132 // If remaining_decimals is zero then the returned DiyFp is accurate. 132 // If remaining_decimals is zero then the returned DiyFp is accurate.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 int read_digits; 170 int read_digits;
171 // The trimmed input fits into a double. 171 // The trimmed input fits into a double.
172 // If the 10^exponent (resp. 10^-exponent) fits into a double too then we 172 // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
173 // can compute the result-double simply by multiplying (resp. dividing) the 173 // can compute the result-double simply by multiplying (resp. dividing) the
174 // two numbers. 174 // two numbers.
175 // This is possible because IEEE guarantees that floating-point operations 175 // This is possible because IEEE guarantees that floating-point operations
176 // return the best possible approximation. 176 // return the best possible approximation.
177 if (exponent < 0 && -exponent < kExactPowersOfTenSize) { 177 if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
178 // 10^-exponent fits into a double. 178 // 10^-exponent fits into a double.
179 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 179 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
180 ASSERT(read_digits == trimmed.length()); 180 DCHECK(read_digits == trimmed.length());
181 *result /= exact_powers_of_ten[-exponent]; 181 *result /= exact_powers_of_ten[-exponent];
182 return true; 182 return true;
183 } 183 }
184 if (0 <= exponent && exponent < kExactPowersOfTenSize) { 184 if (0 <= exponent && exponent < kExactPowersOfTenSize) {
185 // 10^exponent fits into a double. 185 // 10^exponent fits into a double.
186 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 186 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
187 ASSERT(read_digits == trimmed.length()); 187 DCHECK(read_digits == trimmed.length());
188 *result *= exact_powers_of_ten[exponent]; 188 *result *= exact_powers_of_ten[exponent];
189 return true; 189 return true;
190 } 190 }
191 int remaining_digits = 191 int remaining_digits =
192 kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); 192 kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
193 if ((0 <= exponent) && 193 if ((0 <= exponent) &&
194 (exponent - remaining_digits < kExactPowersOfTenSize)) { 194 (exponent - remaining_digits < kExactPowersOfTenSize)) {
195 // The trimmed string was short and we can multiply it with 195 // The trimmed string was short and we can multiply it with
196 // 10^remaining_digits. As a result the remaining exponent now fits 196 // 10^remaining_digits. As a result the remaining exponent now fits
197 // into a double too. 197 // into a double too.
198 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); 198 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
199 ASSERT(read_digits == trimmed.length()); 199 DCHECK(read_digits == trimmed.length());
200 *result *= exact_powers_of_ten[remaining_digits]; 200 *result *= exact_powers_of_ten[remaining_digits];
201 *result *= exact_powers_of_ten[exponent - remaining_digits]; 201 *result *= exact_powers_of_ten[exponent - remaining_digits];
202 return true; 202 return true;
203 } 203 }
204 } 204 }
205 return false; 205 return false;
206 } 206 }
207 207
208 208
209 // Returns 10^exponent as an exact DiyFp. 209 // Returns 10^exponent as an exact DiyFp.
210 // The given exponent must be in the range [1; kDecimalExponentDistance[. 210 // The given exponent must be in the range [1; kDecimalExponentDistance[.
211 static DiyFp AdjustmentPowerOfTen(int exponent) { 211 static DiyFp AdjustmentPowerOfTen(int exponent) {
212 ASSERT(0 < exponent); 212 DCHECK(0 < exponent);
213 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); 213 DCHECK(exponent < PowersOfTenCache::kDecimalExponentDistance);
214 // Simply hardcode the remaining powers for the given decimal exponent 214 // Simply hardcode the remaining powers for the given decimal exponent
215 // distance. 215 // distance.
216 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); 216 DCHECK(PowersOfTenCache::kDecimalExponentDistance == 8);
217 switch (exponent) { 217 switch (exponent) {
218 case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60); 218 case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60);
219 case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57); 219 case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57);
220 case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54); 220 case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54);
221 case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50); 221 case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50);
222 case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47); 222 case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47);
223 case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44); 223 case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44);
224 case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40); 224 case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40);
225 default: 225 default:
226 UNREACHABLE(); 226 UNREACHABLE();
(...skipping 19 matching lines...) Expand all
246 const int kDenominatorLog = 3; 246 const int kDenominatorLog = 3;
247 const int kDenominator = 1 << kDenominatorLog; 247 const int kDenominator = 1 << kDenominatorLog;
248 // Move the remaining decimals into the exponent. 248 // Move the remaining decimals into the exponent.
249 exponent += remaining_decimals; 249 exponent += remaining_decimals;
250 int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2); 250 int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
251 251
252 int old_e = input.e(); 252 int old_e = input.e();
253 input.Normalize(); 253 input.Normalize();
254 error <<= old_e - input.e(); 254 error <<= old_e - input.e();
255 255
256 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); 256 DCHECK(exponent <= PowersOfTenCache::kMaxDecimalExponent);
257 if (exponent < PowersOfTenCache::kMinDecimalExponent) { 257 if (exponent < PowersOfTenCache::kMinDecimalExponent) {
258 *result = 0.0; 258 *result = 0.0;
259 return true; 259 return true;
260 } 260 }
261 DiyFp cached_power; 261 DiyFp cached_power;
262 int cached_decimal_exponent; 262 int cached_decimal_exponent;
263 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, 263 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
264 &cached_power, 264 &cached_power,
265 &cached_decimal_exponent); 265 &cached_decimal_exponent);
266 266
267 if (cached_decimal_exponent != exponent) { 267 if (cached_decimal_exponent != exponent) {
268 int adjustment_exponent = exponent - cached_decimal_exponent; 268 int adjustment_exponent = exponent - cached_decimal_exponent;
269 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); 269 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
270 input.Multiply(adjustment_power); 270 input.Multiply(adjustment_power);
271 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { 271 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
272 // The product of input with the adjustment power fits into a 64 bit 272 // The product of input with the adjustment power fits into a 64 bit
273 // integer. 273 // integer.
274 ASSERT(DiyFp::kSignificandSize == 64); 274 DCHECK(DiyFp::kSignificandSize == 64);
275 } else { 275 } else {
276 // The adjustment power is exact. There is hence only an error of 0.5. 276 // The adjustment power is exact. There is hence only an error of 0.5.
277 error += kDenominator / 2; 277 error += kDenominator / 2;
278 } 278 }
279 } 279 }
280 280
281 input.Multiply(cached_power); 281 input.Multiply(cached_power);
282 // The error introduced by a multiplication of a*b equals 282 // The error introduced by a multiplication of a*b equals
283 // error_a + error_b + error_a*error_b/2^64 + 0.5 283 // error_a + error_b + error_a*error_b/2^64 + 0.5
284 // Substituting a with 'input' and b with 'cached_power' we have 284 // Substituting a with 'input' and b with 'cached_power' we have
(...skipping 21 matching lines...) Expand all
306 int shift_amount = (precision_digits_count + kDenominatorLog) - 306 int shift_amount = (precision_digits_count + kDenominatorLog) -
307 DiyFp::kSignificandSize + 1; 307 DiyFp::kSignificandSize + 1;
308 input.set_f(input.f() >> shift_amount); 308 input.set_f(input.f() >> shift_amount);
309 input.set_e(input.e() + shift_amount); 309 input.set_e(input.e() + shift_amount);
310 // We add 1 for the lost precision of error, and kDenominator for 310 // We add 1 for the lost precision of error, and kDenominator for
311 // the lost precision of input.f(). 311 // the lost precision of input.f().
312 error = (error >> shift_amount) + 1 + kDenominator; 312 error = (error >> shift_amount) + 1 + kDenominator;
313 precision_digits_count -= shift_amount; 313 precision_digits_count -= shift_amount;
314 } 314 }
315 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. 315 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
316 ASSERT(DiyFp::kSignificandSize == 64); 316 DCHECK(DiyFp::kSignificandSize == 64);
317 ASSERT(precision_digits_count < 64); 317 DCHECK(precision_digits_count < 64);
318 uint64_t one64 = 1; 318 uint64_t one64 = 1;
319 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; 319 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
320 uint64_t precision_bits = input.f() & precision_bits_mask; 320 uint64_t precision_bits = input.f() & precision_bits_mask;
321 uint64_t half_way = one64 << (precision_digits_count - 1); 321 uint64_t half_way = one64 << (precision_digits_count - 1);
322 precision_bits *= kDenominator; 322 precision_bits *= kDenominator;
323 half_way *= kDenominator; 323 half_way *= kDenominator;
324 DiyFp rounded_input(input.f() >> precision_digits_count, 324 DiyFp rounded_input(input.f() >> precision_digits_count,
325 input.e() + precision_digits_count); 325 input.e() + precision_digits_count);
326 if (precision_bits >= half_way + error) { 326 if (precision_bits >= half_way + error) {
327 rounded_input.set_f(rounded_input.f() + 1); 327 rounded_input.set_f(rounded_input.f() + 1);
(...skipping 23 matching lines...) Expand all
351 // buffer.length() <= kMaxDecimalSignificantDigits 351 // buffer.length() <= kMaxDecimalSignificantDigits
352 static double BignumStrtod(Vector<const char> buffer, 352 static double BignumStrtod(Vector<const char> buffer,
353 int exponent, 353 int exponent,
354 double guess) { 354 double guess) {
355 if (guess == V8_INFINITY) { 355 if (guess == V8_INFINITY) {
356 return guess; 356 return guess;
357 } 357 }
358 358
359 DiyFp upper_boundary = Double(guess).UpperBoundary(); 359 DiyFp upper_boundary = Double(guess).UpperBoundary();
360 360
361 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); 361 DCHECK(buffer.length() + exponent <= kMaxDecimalPower + 1);
362 ASSERT(buffer.length() + exponent > kMinDecimalPower); 362 DCHECK(buffer.length() + exponent > kMinDecimalPower);
363 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); 363 DCHECK(buffer.length() <= kMaxSignificantDecimalDigits);
364 // Make sure that the Bignum will be able to hold all our numbers. 364 // Make sure that the Bignum will be able to hold all our numbers.
365 // Our Bignum implementation has a separate field for exponents. Shifts will 365 // Our Bignum implementation has a separate field for exponents. Shifts will
366 // consume at most one bigit (< 64 bits). 366 // consume at most one bigit (< 64 bits).
367 // ln(10) == 3.3219... 367 // ln(10) == 3.3219...
368 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); 368 DCHECK(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
369 Bignum input; 369 Bignum input;
370 Bignum boundary; 370 Bignum boundary;
371 input.AssignDecimalString(buffer); 371 input.AssignDecimalString(buffer);
372 boundary.AssignUInt64(upper_boundary.f()); 372 boundary.AssignUInt64(upper_boundary.f());
373 if (exponent >= 0) { 373 if (exponent >= 0) {
374 input.MultiplyByPowerOfTen(exponent); 374 input.MultiplyByPowerOfTen(exponent);
375 } else { 375 } else {
376 boundary.MultiplyByPowerOfTen(-exponent); 376 boundary.MultiplyByPowerOfTen(-exponent);
377 } 377 }
378 if (upper_boundary.e() > 0) { 378 if (upper_boundary.e() > 0) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 413
414 double guess; 414 double guess;
415 if (DoubleStrtod(trimmed, exponent, &guess) || 415 if (DoubleStrtod(trimmed, exponent, &guess) ||
416 DiyFpStrtod(trimmed, exponent, &guess)) { 416 DiyFpStrtod(trimmed, exponent, &guess)) {
417 return guess; 417 return guess;
418 } 418 }
419 return BignumStrtod(trimmed, exponent, guess); 419 return BignumStrtod(trimmed, exponent, guess);
420 } 420 }
421 421
422 } } // namespace v8::internal 422 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/string-stream.cc ('k') | src/stub-cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698