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);
-static double old_strtod(Vector<char> buffer, int exponent) {
+double 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';
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
+++ /dev/null
-// 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));
-}