| Index: src/strtod.cc
|
| diff --git a/src/strtod.cc b/src/strtod.cc
|
| index 5f7a69d9c5a46ae8722f8363129e45b5348ef9aa..bfbb5db7b9cd1e29d48b088a5e99c02c54ba7f01 100644
|
| --- a/src/strtod.cc
|
| +++ b/src/strtod.cc
|
| @@ -36,9 +36,47 @@
|
| namespace v8 {
|
| namespace internal {
|
|
|
| +// 2^53 = 9007199254740992.
|
| +// Any integer with at most 15 decimal digits will hence fit into a double
|
| +// (which has a 53bit significand) without loss of precision.
|
| +static const int kMaxExactDoubleIntegerDecimalDigits = 15;
|
| +// 2^64 = 18446744073709551616
|
| +// Any integer with at most 19 digits will hence fit into a 64bit datatype.
|
| +static const int kMaxUint64DecimalDigits = 19;
|
| +
|
| +static const double exact_powers_of_ten[] = {
|
| + 1.0, // 10^0
|
| + 10.0,
|
| + 100.0,
|
| + 1000.0,
|
| + 10000.0,
|
| + 100000.0,
|
| + 1000000.0,
|
| + 10000000.0,
|
| + 100000000.0,
|
| + 1000000000.0,
|
| + 10000000000.0, // 10^10
|
| + 100000000000.0,
|
| + 1000000000000.0,
|
| + 10000000000000.0,
|
| + 100000000000000.0,
|
| + 1000000000000000.0,
|
| + 10000000000000000.0,
|
| + 100000000000000000.0,
|
| + 1000000000000000000.0,
|
| + 10000000000000000000.0,
|
| + 100000000000000000000.0, // 10^20
|
| + 1000000000000000000000.0,
|
| + // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
|
| + 10000000000000000000000.0
|
| +};
|
| +
|
| +static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
|
| +
|
| +
|
| extern "C" double gay_strtod(const char* s00, const char** se);
|
|
|
| -double strtod(Vector<char> buffer, int exponent) {
|
| +static double old_strtod(Vector<char> buffer, int exponent) {
|
| char gay_buffer[1024];
|
| Vector<char> gay_buffer_vector(gay_buffer, sizeof(gay_buffer));
|
| buffer.start()[buffer.length()] = '\0';
|
| @@ -46,4 +84,63 @@ double strtod(Vector<char> buffer, int exponent) {
|
| return gay_strtod(gay_buffer, NULL);
|
| }
|
|
|
| +
|
| +static Vector<char> TrimTrailingZeros(Vector<char> buffer) {
|
| + for (int i = buffer.length() - 1; i >= 0; --i) {
|
| + if (buffer[i] != '0') {
|
| + return Vector<char>(buffer.start(), i + 1);
|
| + }
|
| + }
|
| + return Vector<char>(buffer.start(), 0);
|
| +}
|
| +
|
| +
|
| +uint64_t ReadUint64(Vector<char> buffer) {
|
| + ASSERT(buffer.length() <= kMaxUint64DecimalDigits);
|
| + uint64_t result = 0;
|
| + for (int i = 0; i < buffer.length(); ++i) {
|
| + int digit = buffer[i] - '0';
|
| + ASSERT(0 <= digit && digit <= 9);
|
| + result = 10 * result + digit;
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +
|
| +double Strtod(Vector<char> buffer, int exponent) {
|
| + Vector<char> trimmed = TrimTrailingZeros(buffer);
|
| + if (trimmed.length() == 0) return 0.0;
|
| + exponent += buffer.length() - trimmed.length();
|
| + if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
|
| + // The trimmed input fits into a double.
|
| + // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
|
| + // can compute the result-double simply by multiplying (resp. dividing) the
|
| + // two numbers.
|
| + // This is possible because IEEE guarantees that floating-point operations
|
| + // return the best possible approximation.
|
| + if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
|
| + // 10^-exponent fits into a double.
|
| + double buffer_d = static_cast<double>(ReadUint64(trimmed));
|
| + return buffer_d / exact_powers_of_ten[-exponent];
|
| + }
|
| + if (0 <= exponent && exponent < kExactPowersOfTenSize) {
|
| + // 10^exponent fits into a double.
|
| + double buffer_d = static_cast<double>(ReadUint64(trimmed));
|
| + return buffer_d * exact_powers_of_ten[exponent];
|
| + }
|
| + int remaining_digits =
|
| + kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
|
| + if ((0 <= exponent) &&
|
| + (exponent - remaining_digits < kExactPowersOfTenSize)) {
|
| + // The trimmed string was short and we can multiply it with
|
| + // 10^remaining_digits. As a result the remaining exponent now fits
|
| + // into a double too.
|
| + double buffer_d = static_cast<double>(ReadUint64(trimmed));
|
| + buffer_d *= exact_powers_of_ten[remaining_digits];
|
| + return buffer_d * exact_powers_of_ten[exponent - remaining_digits];
|
| + }
|
| + }
|
| + return old_strtod(trimmed, exponent);
|
| +}
|
| +
|
| } } // namespace v8::internal
|
|
|