Implement fast case for strtod.
If there are few non-zero digits and the 10^exponent fits into a double then we can compute the result using 1 (or 2) double operations. BUG= TEST= Review URL: http://codereview.chromium.org/3584015 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5603 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
d4afed6bff
commit
ce666fc2fe
@ -354,7 +354,7 @@ static double InternalStringToInt(Iterator current, EndMark end, int radix) {
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos] = '\0';
|
||||
Vector<char> buffer_vector(buffer, buffer_pos);
|
||||
return sign ? -strtod(buffer_vector, NULL) : strtod(buffer_vector, NULL);
|
||||
return sign ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0);
|
||||
}
|
||||
|
||||
// The following code causes accumulating rounding error for numbers greater
|
||||
@ -653,7 +653,7 @@ static double InternalStringToDouble(Iterator current,
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos] = '\0';
|
||||
|
||||
double converted = strtod(Vector<char>(buffer, buffer_pos), exponent);
|
||||
double converted = Strtod(Vector<char>(buffer, buffer_pos), exponent);
|
||||
return sign? -converted: converted;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -33,7 +33,7 @@ namespace internal {
|
||||
|
||||
// The buffer must only contain digits in the range [0-9]. It must not
|
||||
// contain a dot or a sign. It must not start with '0', and must not be empty.
|
||||
double strtod(Vector<char> buffer, int exponent);
|
||||
double Strtod(Vector<char> buffer, int exponent);
|
||||
|
||||
} } // namespace v8::internal
|
||||
|
||||
|
@ -70,6 +70,7 @@ SOURCES = {
|
||||
'test-sockets.cc',
|
||||
'test-spaces.cc',
|
||||
'test-strings.cc',
|
||||
'test-strtod.cc',
|
||||
'test-thread-termination.cc',
|
||||
'test-threads.cc',
|
||||
'test-type-info.cc',
|
||||
|
155
test/cctest/test-strtod.cc
Normal file
155
test/cctest/test-strtod.cc
Normal file
@ -0,0 +1,155 @@
|
||||
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "v8.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "cctest.h"
|
||||
#include "strtod.h"
|
||||
|
||||
using namespace v8::internal;
|
||||
|
||||
static const int kBufferSize = 100;
|
||||
|
||||
|
||||
TEST(Strtod) {
|
||||
char buffer[kBufferSize];
|
||||
Vector<char> vector;
|
||||
|
||||
strncpy(buffer, "0", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
|
||||
CHECK_EQ(0.0, Strtod(vector, 1));
|
||||
CHECK_EQ(0.0, Strtod(vector, 2));
|
||||
CHECK_EQ(0.0, Strtod(vector, -2));
|
||||
CHECK_EQ(0.0, Strtod(vector, -999));
|
||||
CHECK_EQ(0.0, Strtod(vector, +999));
|
||||
|
||||
strncpy(buffer, "1", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(1.0, Strtod(vector, 0));
|
||||
CHECK_EQ(10.0, Strtod(vector, 1));
|
||||
CHECK_EQ(100.0, Strtod(vector, 2));
|
||||
CHECK_EQ(1e20, Strtod(vector, 20));
|
||||
CHECK_EQ(1e22, Strtod(vector, 22));
|
||||
CHECK_EQ(1e23, Strtod(vector, 23));
|
||||
CHECK_EQ(1e35, Strtod(vector, 35));
|
||||
CHECK_EQ(1e36, Strtod(vector, 36));
|
||||
CHECK_EQ(1e37, Strtod(vector, 37));
|
||||
CHECK_EQ(1e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(1e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(1e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(1e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(1e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(1e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(1e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(1e-39, Strtod(vector, -39));
|
||||
|
||||
strncpy(buffer, "2", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(2.0, Strtod(vector, 0));
|
||||
CHECK_EQ(20.0, Strtod(vector, 1));
|
||||
CHECK_EQ(200.0, Strtod(vector, 2));
|
||||
CHECK_EQ(2e20, Strtod(vector, 20));
|
||||
CHECK_EQ(2e22, Strtod(vector, 22));
|
||||
CHECK_EQ(2e23, Strtod(vector, 23));
|
||||
CHECK_EQ(2e35, Strtod(vector, 35));
|
||||
CHECK_EQ(2e36, Strtod(vector, 36));
|
||||
CHECK_EQ(2e37, Strtod(vector, 37));
|
||||
CHECK_EQ(2e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(2e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(2e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(2e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(2e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(2e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(2e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(2e-39, Strtod(vector, -39));
|
||||
|
||||
strncpy(buffer, "9", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(9.0, Strtod(vector, 0));
|
||||
CHECK_EQ(90.0, Strtod(vector, 1));
|
||||
CHECK_EQ(900.0, Strtod(vector, 2));
|
||||
CHECK_EQ(9e20, Strtod(vector, 20));
|
||||
CHECK_EQ(9e22, Strtod(vector, 22));
|
||||
CHECK_EQ(9e23, Strtod(vector, 23));
|
||||
CHECK_EQ(9e35, Strtod(vector, 35));
|
||||
CHECK_EQ(9e36, Strtod(vector, 36));
|
||||
CHECK_EQ(9e37, Strtod(vector, 37));
|
||||
CHECK_EQ(9e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(9e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(9e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(9e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(9e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(9e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(9e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(9e-39, Strtod(vector, -39));
|
||||
|
||||
strncpy(buffer, "12345", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(12345.0, Strtod(vector, 0));
|
||||
CHECK_EQ(123450.0, Strtod(vector, 1));
|
||||
CHECK_EQ(1234500.0, Strtod(vector, 2));
|
||||
CHECK_EQ(12345e20, Strtod(vector, 20));
|
||||
CHECK_EQ(12345e22, Strtod(vector, 22));
|
||||
CHECK_EQ(12345e23, Strtod(vector, 23));
|
||||
CHECK_EQ(12345e30, Strtod(vector, 30));
|
||||
CHECK_EQ(12345e31, Strtod(vector, 31));
|
||||
CHECK_EQ(12345e32, Strtod(vector, 32));
|
||||
CHECK_EQ(12345e35, Strtod(vector, 35));
|
||||
CHECK_EQ(12345e36, Strtod(vector, 36));
|
||||
CHECK_EQ(12345e37, Strtod(vector, 37));
|
||||
CHECK_EQ(12345e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(12345e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(12345e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(12345e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(12345e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(12345e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(12345e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(12345e-39, Strtod(vector, -39));
|
||||
|
||||
strncpy(buffer, "12345678901234", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(12345678901234.0, Strtod(vector, 0));
|
||||
CHECK_EQ(123456789012340.0, Strtod(vector, 1));
|
||||
CHECK_EQ(1234567890123400.0, Strtod(vector, 2));
|
||||
CHECK_EQ(12345678901234e20, Strtod(vector, 20));
|
||||
CHECK_EQ(12345678901234e22, Strtod(vector, 22));
|
||||
CHECK_EQ(12345678901234e23, Strtod(vector, 23));
|
||||
CHECK_EQ(12345678901234e30, Strtod(vector, 30));
|
||||
CHECK_EQ(12345678901234e31, Strtod(vector, 31));
|
||||
CHECK_EQ(12345678901234e32, Strtod(vector, 32));
|
||||
CHECK_EQ(12345678901234e35, Strtod(vector, 35));
|
||||
CHECK_EQ(12345678901234e36, Strtod(vector, 36));
|
||||
CHECK_EQ(12345678901234e37, Strtod(vector, 37));
|
||||
CHECK_EQ(12345678901234e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(12345678901234e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(12345678901234e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(12345678901234e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(12345678901234e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(12345678901234e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(12345678901234e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(12345678901234e-39, Strtod(vector, -39));
|
||||
|
||||
strncpy(buffer, "123456789012345", kBufferSize);
|
||||
vector = Vector<char>(buffer, strlen(buffer));
|
||||
CHECK_EQ(123456789012345.0, Strtod(vector, 0));
|
||||
CHECK_EQ(1234567890123450.0, Strtod(vector, 1));
|
||||
CHECK_EQ(12345678901234500.0, Strtod(vector, 2));
|
||||
CHECK_EQ(123456789012345e20, Strtod(vector, 20));
|
||||
CHECK_EQ(123456789012345e22, Strtod(vector, 22));
|
||||
CHECK_EQ(123456789012345e23, Strtod(vector, 23));
|
||||
CHECK_EQ(123456789012345e35, Strtod(vector, 35));
|
||||
CHECK_EQ(123456789012345e36, Strtod(vector, 36));
|
||||
CHECK_EQ(123456789012345e37, Strtod(vector, 37));
|
||||
CHECK_EQ(123456789012345e39, Strtod(vector, 39));
|
||||
CHECK_EQ(123456789012345e-1, Strtod(vector, -1));
|
||||
CHECK_EQ(123456789012345e-2, Strtod(vector, -2));
|
||||
CHECK_EQ(123456789012345e-5, Strtod(vector, -5));
|
||||
CHECK_EQ(123456789012345e-20, Strtod(vector, -20));
|
||||
CHECK_EQ(123456789012345e-22, Strtod(vector, -22));
|
||||
CHECK_EQ(123456789012345e-23, Strtod(vector, -23));
|
||||
CHECK_EQ(123456789012345e-25, Strtod(vector, -25));
|
||||
CHECK_EQ(123456789012345e-39, Strtod(vector, -39));
|
||||
}
|
Loading…
Reference in New Issue
Block a user