Strtod fast-case that uses DiyFps and cached powers of ten.
authorfloitschV8@gmail.com <floitschV8@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 21 Oct 2010 11:54:32 +0000 (11:54 +0000)
committerfloitschV8@gmail.com <floitschV8@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 21 Oct 2010 11:54:32 +0000 (11:54 +0000)
This is a fixed version of r5677.
Review URL: http://codereview.chromium.org/3898007

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5686 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/cached-powers.cc
src/cached-powers.h
src/double.h
src/fast-dtoa.cc
src/strtod.cc
test/cctest/test-strtod.cc

index 8f82286..43dbc78 100644 (file)
@@ -42,6 +42,11 @@ struct CachedPower {
 };
 
 static const CachedPower kCachedPowers[] = {
+  {V8_2PART_UINT64_C(0xfa8fd5a0, 081c0288), -1220, -348},
+  {V8_2PART_UINT64_C(0xbaaee17f, a23ebf76), -1193, -340},
+  {V8_2PART_UINT64_C(0x8b16fb20, 3055ac76), -1166, -332},
+  {V8_2PART_UINT64_C(0xcf42894a, 5dce35ea), -1140, -324},
+  {V8_2PART_UINT64_C(0x9a6bb0aa, 55653b2d), -1113, -316},
   {V8_2PART_UINT64_C(0xe61acf03, 3d1a45df), -1087, -308},
   {V8_2PART_UINT64_C(0xab70fe17, c79ac6ca), -1060, -300},
   {V8_2PART_UINT64_C(0xff77b1fc, bebcdc4f), -1034, -292},
@@ -129,24 +134,44 @@ static const CachedPower kCachedPowers[] = {
 static const int kCachedPowersLength = ARRAY_SIZE(kCachedPowers);
 static const int kCachedPowersOffset = -kCachedPowers[0].decimal_exponent;
 static const double kD_1_LOG2_10 = 0.30102999566398114;  //  1 / lg(10)
-static const int kCachedPowersDecimalDistance =
+const int PowersOfTenCache::kDecimalExponentDistance =
     kCachedPowers[1].decimal_exponent - kCachedPowers[0].decimal_exponent;
+const int PowersOfTenCache::kMinDecimalExponent =
+    kCachedPowers[0].decimal_exponent;
+const int PowersOfTenCache::kMaxDecimalExponent =
+    kCachedPowers[kCachedPowersLength - 1].decimal_exponent;
 
-void GetCachedPowerForBinaryExponentRange(int min_exponent,
-                                          int max_exponent,
-                                          DiyFp* power,
-                                          int* decimal_exponent) {
-    int kQ = DiyFp::kSignificandSize;
-    double k = ceiling((min_exponent + kQ - 1) * kD_1_LOG2_10);
-    int foo = kCachedPowersOffset;
-    int index =
-        (foo + static_cast<int>(k) - 1) / kCachedPowersDecimalDistance + 1;
-    ASSERT(0 <= index && index < kCachedPowersLength);
-    CachedPower cached_power = kCachedPowers[index];
-    ASSERT(min_exponent <= cached_power.binary_exponent);
-    ASSERT(cached_power.binary_exponent <= max_exponent);
-    *decimal_exponent = cached_power.decimal_exponent;
-    *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
+void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
+    int min_exponent,
+    int max_exponent,
+    DiyFp* power,
+    int* decimal_exponent) {
+  int kQ = DiyFp::kSignificandSize;
+  double k = ceiling((min_exponent + kQ - 1) * kD_1_LOG2_10);
+  int foo = kCachedPowersOffset;
+  int index =
+      (foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
+  ASSERT(0 <= index && index < kCachedPowersLength);
+  CachedPower cached_power = kCachedPowers[index];
+  ASSERT(min_exponent <= cached_power.binary_exponent);
+  ASSERT(cached_power.binary_exponent <= max_exponent);
+  *decimal_exponent = cached_power.decimal_exponent;
+  *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
+}
+
+
+void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent,
+                                                        DiyFp* power,
+                                                        int* found_exponent) {
+  ASSERT(kMinDecimalExponent <= requested_exponent);
+  ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
+  int index =
+      (requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance;
+  CachedPower cached_power = kCachedPowers[index];
+  *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
+  *found_exponent = cached_power.decimal_exponent;
+  ASSERT(*found_exponent <= requested_exponent);
+  ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance);
 }
 
 } }  // namespace v8::internal
index 0c78343..2ae5619 100644 (file)
 namespace v8 {
 namespace internal {
 
-void GetCachedPowerForBinaryExponentRange(int min_exponent,
-                                          int max_exponent,
-                                          DiyFp* power,
-                                          int* decimal_exponent);
+class PowersOfTenCache {
+ public:
+
+  // Not all powers of ten are cached. The decimal exponent of two neighboring
+  // cached numbers will differ by kDecimalExponentDistance.
+  static const int kDecimalExponentDistance;
+
+  static const int kMinDecimalExponent;
+  static const int kMaxDecimalExponent;
+
+  // Returns a cached power-of-ten with a binary exponent in the range
+  // [min_exponent; max_exponent] (boundaries included).
+  static void GetCachedPowerForBinaryExponentRange(int min_exponent,
+                                                   int max_exponent,
+                                                   DiyFp* power,
+                                                   int* decimal_exponent);
+
+  // Returns a cached power of ten x ~= 10^k such that
+  //   k <= decimal_exponent < k + kCachedPowersDecimalDistance.
+  // The given decimal_exponent must satisfy
+  //   kMinDecimalExponent <= requested_exponent, and
+  //   requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance.
+  static void GetCachedPowerForDecimalExponent(int requested_exponent,
+                                               DiyFp* power,
+                                               int* found_exponent);
+};
 
 } }  // namespace v8::internal
 
index 65f8c94..e805173 100644 (file)
@@ -45,10 +45,14 @@ class Double {
   static const uint64_t kSignificandMask =
       V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
   static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000);
+  static const int kPhysicalSignificandSize = 52;  // Excludes the hidden bit.
+  static const int kSignificandSize = 53;
 
   Double() : d64_(0) {}
   explicit Double(double d) : d64_(double_to_uint64(d)) {}
   explicit Double(uint64_t d64) : d64_(d64) {}
+  explicit Double(DiyFp diy_fp)
+    : d64_(DiyFpToUint64(diy_fp)) {}
 
   DiyFp AsDiyFp() const {
     ASSERT(!IsSpecial());
@@ -67,9 +71,9 @@ class Double {
       f <<= 1;
       e--;
     }
-    // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
-    f <<= DiyFp::kSignificandSize - kSignificandSize - 1;
-    e -= DiyFp::kSignificandSize - kSignificandSize - 1;
+    // Do the final shifts in one go.
+    f <<= DiyFp::kSignificandSize - kSignificandSize;
+    e -= DiyFp::kSignificandSize - kSignificandSize;
     return DiyFp(f, e);
   }
 
@@ -82,7 +86,8 @@ class Double {
     if (IsDenormal()) return kDenormalExponent;
 
     uint64_t d64 = AsUint64();
-    int biased_e = static_cast<int>((d64 & kExponentMask) >> kSignificandSize);
+    int biased_e =
+        static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
     return biased_e - kExponentBias;
   }
 
@@ -156,12 +161,54 @@ class Double {
 
   double value() const { return uint64_to_double(d64_); }
 
+  // Returns the significand size for a given order of magnitude.
+  // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
+  // This function returns the number of significant binary digits v will have
+  // once its encoded into a double. In almost all cases this is equal to
+  // kSignificandSize. The only exception are denormals. They start with leading
+  // zeroes and their effective significand-size is hence smaller.
+  static int SignificandSizeForOrderOfMagnitude(int order) {
+    if (order >= (kDenormalExponent + kSignificandSize)) {
+      return kSignificandSize;
+    }
+    if (order <= kDenormalExponent) return 0;
+    return order - kDenormalExponent;
+  }
+
  private:
-  static const int kSignificandSize = 52;  // Excludes the hidden bit.
-  static const int kExponentBias = 0x3FF + kSignificandSize;
+  static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
   static const int kDenormalExponent = -kExponentBias + 1;
+  static const int kMaxExponent = 0x7FF - kExponentBias;
+  static const uint64_t kInfinity = V8_2PART_UINT64_C(0x7FF00000, 00000000);
 
-  uint64_t d64_;
+  const uint64_t d64_;
+
+  static uint64_t DiyFpToUint64(DiyFp diy_fp) {
+    uint64_t significand = diy_fp.f();
+    int exponent = diy_fp.e();
+    while (significand > kHiddenBit + kSignificandMask) {
+      significand >>= 1;
+      exponent++;
+    }
+    if (exponent >= kMaxExponent) {
+      return kInfinity;
+    }
+    if (exponent < kDenormalExponent) {
+      return 0;
+    }
+    while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
+      significand <<= 1;
+      exponent--;
+    }
+    uint64_t biased_exponent;
+    if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
+      biased_exponent = 0;
+    } else {
+      biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
+    }
+    return (significand & kSignificandMask) |
+        (biased_exponent << kPhysicalSignificandSize);
+  }
 };
 
 } }  // namespace v8::internal
index ce825f9..c7f6aa1 100644 (file)
@@ -613,9 +613,10 @@ static bool Grisu3(double v,
      kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   int ten_mk_maximal_binary_exponent =
      kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
-  GetCachedPowerForBinaryExponentRange(ten_mk_minimal_binary_exponent,
-                                       ten_mk_maximal_binary_exponent,
-                                       &ten_mk, &mk);
+  PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
+      ten_mk_minimal_binary_exponent,
+      ten_mk_maximal_binary_exponent,
+      &ten_mk, &mk);
   ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
           DiyFp::kSignificandSize) &&
          (kMaximalTargetExponent >= w.e() + ten_mk.e() +
@@ -671,9 +672,10 @@ static bool Grisu3Counted(double v,
      kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   int ten_mk_maximal_binary_exponent =
      kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
-  GetCachedPowerForBinaryExponentRange(ten_mk_minimal_binary_exponent,
-                                       ten_mk_maximal_binary_exponent,
-                                       &ten_mk, &mk);
+  PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
+      ten_mk_minimal_binary_exponent,
+      ten_mk_maximal_binary_exponent,
+      &ten_mk, &mk);
   ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
           DiyFp::kSignificandSize) &&
          (kMaximalTargetExponent >= w.e() + ten_mk.e() +
index ae278bd..a309e7b 100644 (file)
@@ -31,7 +31,8 @@
 #include "v8.h"
 
 #include "strtod.h"
-// #include "cached-powers.h"
+#include "cached-powers.h"
+#include "double.h"
 
 namespace v8 {
 namespace internal {
@@ -40,9 +41,9 @@ namespace internal {
 // 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.
+// 2^64 = 18446744073709551616 > 10^19
 static const int kMaxUint64DecimalDigits = 19;
+
 // Max double: 1.7976931348623157 x 10^308
 // Min non-zero double: 4.9406564584124654 x 10^-324
 // Any x >= 10^309 is interpreted as +infinity.
@@ -52,6 +53,10 @@ static const int kMaxUint64DecimalDigits = 19;
 static const int kMaxDecimalPower = 309;
 static const int kMinDecimalPower = -324;
 
+// 2^64 = 18446744073709551616
+static const uint64_t kMaxUint64 = V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF);
+
+
 static const double exact_powers_of_ten[] = {
   1.0,  // 10^0
   10.0,
@@ -137,18 +142,50 @@ static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
 }
 
 
-uint64_t ReadUint64(Vector<const char> buffer) {
-  ASSERT(buffer.length() <= kMaxUint64DecimalDigits);
+// Reads digits from the buffer and converts them to a uint64.
+// Reads in as many digits as fit into a uint64.
+// When the string starts with "1844674407370955161" no further digit is read.
+// Since 2^64 = 18446744073709551616 it would still be possible read another
+// digit if it was less or equal than 6, but this would complicate the code.
+static uint64_t ReadUint64(Vector<const char> buffer,
+                           int* number_of_read_digits) {
   uint64_t result = 0;
-  for (int i = 0; i < buffer.length(); ++i) {
-    int digit = buffer[i] - '0';
+  int i = 0;
+  while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
+    int digit = buffer[i++] - '0';
     ASSERT(0 <= digit && digit <= 9);
     result = 10 * result + digit;
   }
+  *number_of_read_digits = i;
   return result;
 }
 
 
+// Reads a DiyFp from the buffer.
+// The returned DiyFp is not necessarily normalized.
+// If remaining_decimals is zero then the returned DiyFp is accurate.
+// Otherwise it has been rounded and has error of at most 1/2 ulp.
+static void ReadDiyFp(Vector<const char> buffer,
+                      DiyFp* result,
+                      int* remaining_decimals) {
+  int read_digits;
+  uint64_t significand = ReadUint64(buffer, &read_digits);
+  if (buffer.length() == read_digits) {
+    *result = DiyFp(significand, 0);
+    *remaining_decimals = 0;
+  } else {
+    // Round the significand.
+    if (buffer[read_digits] >= '5') {
+      significand++;
+    }
+    // Compute the binary exponent.
+    int exponent = 0;
+    *result = DiyFp(significand, exponent);
+    *remaining_decimals = buffer.length() - read_digits;
+  }
+}
+
+
 static bool DoubleStrtod(Vector<const char> trimmed,
                          int exponent,
                          double* result) {
@@ -162,6 +199,7 @@ static bool DoubleStrtod(Vector<const char> trimmed,
   return false;
 #endif
   if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
+    int read_digits;
     // 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
@@ -170,13 +208,15 @@ static bool DoubleStrtod(Vector<const char> trimmed,
     // return the best possible approximation.
     if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
       // 10^-exponent fits into a double.
-      *result = static_cast<double>(ReadUint64(trimmed));
+      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
+      ASSERT(read_digits == trimmed.length());
       *result /= exact_powers_of_ten[-exponent];
       return true;
     }
     if (0 <= exponent && exponent < kExactPowersOfTenSize) {
       // 10^exponent fits into a double.
-      *result = static_cast<double>(ReadUint64(trimmed));
+      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
+      ASSERT(read_digits == trimmed.length());
       *result *= exact_powers_of_ten[exponent];
       return true;
     }
@@ -187,7 +227,8 @@ static bool DoubleStrtod(Vector<const char> trimmed,
       // 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.
-      *result = static_cast<double>(ReadUint64(trimmed));
+      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
+      ASSERT(read_digits == trimmed.length());
       *result *= exact_powers_of_ten[remaining_digits];
       *result *= exact_powers_of_ten[exponent - remaining_digits];
       return true;
@@ -197,6 +238,142 @@ static bool DoubleStrtod(Vector<const char> trimmed,
 }
 
 
+// Returns 10^exponent as an exact DiyFp.
+// The given exponent must be in the range [1; kDecimalExponentDistance[.
+static DiyFp AdjustmentPowerOfTen(int exponent) {
+  ASSERT(0 < exponent);
+  ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
+  // Simply hardcode the remaining powers for the given decimal exponent
+  // distance.
+  ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
+  switch (exponent) {
+    case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60);
+    case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57);
+    case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54);
+    case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50);
+    case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47);
+    case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44);
+    case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40);
+    default:
+      UNREACHABLE();
+      return DiyFp(0, 0);
+  }
+}
+
+
+// If the function returns true then the result is the correct double.
+// Otherwise it is either the correct double or the double that is just below
+// the correct double.
+static bool DiyFpStrtod(Vector<const char> buffer,
+                        int exponent,
+                        double* result) {
+  DiyFp input;
+  int remaining_decimals;
+  ReadDiyFp(buffer, &input, &remaining_decimals);
+  // Since we may have dropped some digits the input is not accurate.
+  // If remaining_decimals is different than 0 than the error is at most
+  // .5 ulp (unit in the last place).
+  // We don't want to deal with fractions and therefore keep a common
+  // denominator.
+  const int kDenominatorLog = 3;
+  const int kDenominator = 1 << kDenominatorLog;
+  // Move the remaining decimals into the exponent.
+  exponent += remaining_decimals;
+  int error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
+
+  int old_e = input.e();
+  input.Normalize();
+  error <<= old_e - input.e();
+
+  ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
+  if (exponent < PowersOfTenCache::kMinDecimalExponent) {
+    *result = 0.0;
+    return true;
+  }
+  DiyFp cached_power;
+  int cached_decimal_exponent;
+  PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
+                                                     &cached_power,
+                                                     &cached_decimal_exponent);
+
+  if (cached_decimal_exponent != exponent) {
+    int adjustment_exponent = exponent - cached_decimal_exponent;
+    DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
+    input.Multiply(adjustment_power);
+    if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
+      // The product of input with the adjustment power fits into a 64 bit
+      // integer.
+      ASSERT(DiyFp::kSignificandSize == 64);
+    } else {
+      // The adjustment power is exact. There is hence only an error of 0.5.
+      error += kDenominator / 2;
+    }
+  }
+
+  input.Multiply(cached_power);
+  // The error introduced by a multiplication of a*b equals
+  //   error_a + error_b + error_a*error_b/2^64 + 0.5
+  // Substituting a with 'input' and b with 'cached_power' we have
+  //   error_b = 0.5  (all cached powers have an error of less than 0.5 ulp),
+  //   error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
+  int error_b = kDenominator / 2;
+  int error_ab = (error == 0 ? 0 : 1);  // We round up to 1.
+  int fixed_error = kDenominator / 2;
+  error += error_b + error_ab + fixed_error;
+
+  old_e = input.e();
+  input.Normalize();
+  error <<= old_e - input.e();
+
+  // See if the double's significand changes if we add/subtract the error.
+  int order_of_magnitude = DiyFp::kSignificandSize + input.e();
+  int effective_significand_size =
+      Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
+  int precision_digits_count =
+      DiyFp::kSignificandSize - effective_significand_size;
+  if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
+    // This can only happen for very small denormals. In this case the
+    // half-way multiplied by the denominator exceeds the range of an uint64.
+    // Simply shift everything to the right.
+    int shift_amount = (precision_digits_count + kDenominatorLog) -
+        DiyFp::kSignificandSize + 1;
+    input.set_f(input.f() >> shift_amount);
+    input.set_e(input.e() + shift_amount);
+    // We add 1 for the lost precision of error, and kDenominator for
+    // the lost precision of input.f().
+    error = (error >> shift_amount) + 1 + kDenominator;
+    precision_digits_count -= shift_amount;
+  }
+  // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
+  ASSERT(DiyFp::kSignificandSize == 64);
+  ASSERT(precision_digits_count < 64);
+  uint64_t one64 = 1;
+  uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
+  uint64_t precision_bits = input.f() & precision_bits_mask;
+  uint64_t half_way = one64 << (precision_digits_count - 1);
+  precision_bits *= kDenominator;
+  half_way *= kDenominator;
+  DiyFp rounded_input(input.f() >> precision_digits_count,
+                      input.e() + precision_digits_count);
+  if (precision_bits >= half_way + error) {
+    rounded_input.set_f(rounded_input.f() + 1);
+  }
+  // If the last_bits are too close to the half-way case than we are too
+  // inaccurate and round down. In this case we return false so that we can
+  // fall back to a more precise algorithm.
+
+  *result = Double(rounded_input).value();
+  if (half_way - error < precision_bits && precision_bits < half_way + error) {
+    // Too imprecise. The caller will have to fall back to a slower version.
+    // However the returned number is guaranteed to be either the correct
+    // double, or the next-lower double.
+    return false;
+  } else {
+    return true;
+  }
+}
+
+
 double Strtod(Vector<const char> buffer, int exponent) {
   Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
   Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
@@ -204,8 +381,10 @@ double Strtod(Vector<const char> buffer, int exponent) {
   if (trimmed.length() == 0) return 0.0;
   if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) return V8_INFINITY;
   if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
+
   double result;
-  if (DoubleStrtod(trimmed, exponent, &result)) {
+  if (DoubleStrtod(trimmed, exponent, &result) ||
+      DiyFpStrtod(trimmed, exponent, &result)) {
     return result;
   }
   return old_strtod(trimmed, exponent);
index ae1c00d..56b26ea 100644 (file)
@@ -198,10 +198,58 @@ TEST(Strtod) {
   CHECK_EQ(1234e304, StrtodChar("0000000123400000", 299));
   CHECK_EQ(V8_INFINITY, StrtodChar("00000000180000000", 300));
   CHECK_EQ(17e307, StrtodChar("00000000170000000", 300));
+  CHECK_EQ(1.7976931348623157E+308, StrtodChar("17976931348623157", 292));
+  CHECK_EQ(1.7976931348623158E+308, StrtodChar("17976931348623158", 292));
+  CHECK_EQ(V8_INFINITY, StrtodChar("17976931348623159", 292));
 
   // The following number is the result of 89255.0/1e-22. Both floating-point
   // numbers can be accurately represented with doubles. However on Linux,x86
   // the floating-point stack is set to 80bits and the double-rounding
   // introduces an error.
   CHECK_EQ(89255e-22, StrtodChar("89255", -22));
+  CHECK_EQ(104110013277974872254e-225,
+           StrtodChar("104110013277974872254", -225));
+
+  CHECK_EQ(123456789e108, StrtodChar("123456789", 108));
+  CHECK_EQ(123456789e109, StrtodChar("123456789", 109));
+  CHECK_EQ(123456789e110, StrtodChar("123456789", 110));
+  CHECK_EQ(123456789e111, StrtodChar("123456789", 111));
+  CHECK_EQ(123456789e112, StrtodChar("123456789", 112));
+  CHECK_EQ(123456789e113, StrtodChar("123456789", 113));
+  CHECK_EQ(123456789e114, StrtodChar("123456789", 114));
+  CHECK_EQ(123456789e115, StrtodChar("123456789", 115));
+
+  CHECK_EQ(1234567890123456789012345e108,
+           StrtodChar("1234567890123456789012345", 108));
+  CHECK_EQ(1234567890123456789012345e109,
+           StrtodChar("1234567890123456789012345", 109));
+  CHECK_EQ(1234567890123456789012345e110,
+           StrtodChar("1234567890123456789012345", 110));
+  CHECK_EQ(1234567890123456789012345e111,
+           StrtodChar("1234567890123456789012345", 111));
+  CHECK_EQ(1234567890123456789012345e112,
+           StrtodChar("1234567890123456789012345", 112));
+  CHECK_EQ(1234567890123456789012345e113,
+           StrtodChar("1234567890123456789012345", 113));
+  CHECK_EQ(1234567890123456789012345e114,
+           StrtodChar("1234567890123456789012345", 114));
+  CHECK_EQ(1234567890123456789012345e115,
+           StrtodChar("1234567890123456789012345", 115));
+
+  CHECK_EQ(1234567890123456789052345e108,
+           StrtodChar("1234567890123456789052345", 108));
+  CHECK_EQ(1234567890123456789052345e109,
+           StrtodChar("1234567890123456789052345", 109));
+  CHECK_EQ(1234567890123456789052345e110,
+           StrtodChar("1234567890123456789052345", 110));
+  CHECK_EQ(1234567890123456789052345e111,
+           StrtodChar("1234567890123456789052345", 111));
+  CHECK_EQ(1234567890123456789052345e112,
+           StrtodChar("1234567890123456789052345", 112));
+  CHECK_EQ(1234567890123456789052345e113,
+           StrtodChar("1234567890123456789052345", 113));
+  CHECK_EQ(1234567890123456789052345e114,
+           StrtodChar("1234567890123456789052345", 114));
+  CHECK_EQ(1234567890123456789052345e115,
+           StrtodChar("1234567890123456789052345", 115));
 }