13ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Copyright 2012 the V8 project authors. All rights reserved.
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// found in the LICENSE file.
4f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/strtod.h"
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
7f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#include <stdarg.h>
8b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include <cmath>
9f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
10b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/bignum.h"
11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/cached-powers.h"
12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/double.h"
13b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/globals.h"
14b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/utils.h"
15f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
16f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochnamespace v8 {
17f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochnamespace internal {
18f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
19f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// 2^53 = 9007199254740992.
20f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any integer with at most 15 decimal digits will hence fit into a double
21f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// (which has a 53bit significand) without loss of precision.
22f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxExactDoubleIntegerDecimalDigits = 15;
235913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// 2^64 = 18446744073709551616 > 10^19
24f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxUint64DecimalDigits = 19;
255913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
26f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Max double: 1.7976931348623157 x 10^308
27f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Min non-zero double: 4.9406564584124654 x 10^-324
28f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any x >= 10^309 is interpreted as +infinity.
29f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any x <= 10^-324 is interpreted as 0.
30f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Note that 2.5e-324 (despite being smaller than the min double) will be read
31f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// as non-zero (equal to the min non-zero double).
32f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxDecimalPower = 309;
33f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMinDecimalPower = -324;
34f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// 2^64 = 18446744073709551616
365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic const uint64_t kMaxUint64 = V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF);
375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
39f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const double exact_powers_of_ten[] = {
40f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1.0,  // 10^0
41f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10.0,
42f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100.0,
43f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000.0,
44f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000.0,
45f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000.0,
46f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000.0,
47f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000.0,
48f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000.0,
49f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000.0,
50f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000.0,  // 10^10
51f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000.0,
52f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000.0,
53f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000.0,
54f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000.0,
55f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000.0,
56f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000.0,
57f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000000.0,
58f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000000.0,
59f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000000.0,
60f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000000000.0,  // 10^20
61f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000000000.0,
62f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
63f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000000000.0
64f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch};
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic const int kExactPowersOfTenSize = arraysize(exact_powers_of_ten);
66f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
6790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Maximum number of significant digits in the decimal representation.
6890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// In fact the value is 772 (see conversions.cc), but to give us some margin
6990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// we round up to 780.
7090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic const int kMaxSignificantDecimalDigits = 780;
71f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
72f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
73f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  for (int i = 0; i < buffer.length(); i++) {
74f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (buffer[i] != '0') {
755913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return buffer.SubVector(i, buffer.length());
76f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
77f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
78f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return Vector<const char>(buffer.start(), 0);
79f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
80f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
81f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
82f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
83f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  for (int i = buffer.length() - 1; i >= 0; --i) {
84f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (buffer[i] != '0') {
855913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return buffer.SubVector(0, i + 1);
86f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
87f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
88f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return Vector<const char>(buffer.start(), 0);
89f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
90f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
91f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
9290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic void TrimToMaxSignificantDigits(Vector<const char> buffer,
9390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       int exponent,
9490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       char* significant_buffer,
9590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       int* significant_exponent) {
9690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
9790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    significant_buffer[i] = buffer[i];
9890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
9990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // The input buffer has been trimmed. Therefore the last digit must be
10090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // different from '0'.
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(buffer[buffer.length() - 1] != '0');
10290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Set the last digit to be non-zero. This is sufficient to guarantee
10390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // correct rounding.
10490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
10590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  *significant_exponent =
10690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner      exponent + (buffer.length() - kMaxSignificantDecimalDigits);
10790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner}
10890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
1105913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads digits from the buffer and converts them to a uint64.
1115913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads in as many digits as fit into a uint64.
1125913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// When the string starts with "1844674407370955161" no further digit is read.
1135913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Since 2^64 = 18446744073709551616 it would still be possible read another
1145913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// digit if it was less or equal than 6, but this would complicate the code.
1155913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic uint64_t ReadUint64(Vector<const char> buffer,
1165913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                           int* number_of_read_digits) {
117f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  uint64_t result = 0;
1185913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int i = 0;
1195913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
1205913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int digit = buffer[i++] - '0';
121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    DCHECK(0 <= digit && digit <= 9);
122f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    result = 10 * result + digit;
123f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
1245913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  *number_of_read_digits = i;
125f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return result;
126f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
127f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
128f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
1295913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads a DiyFp from the buffer.
1305913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// The returned DiyFp is not necessarily normalized.
1315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// If remaining_decimals is zero then the returned DiyFp is accurate.
1325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Otherwise it has been rounded and has error of at most 1/2 ulp.
1335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic void ReadDiyFp(Vector<const char> buffer,
1345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      DiyFp* result,
1355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      int* remaining_decimals) {
1365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int read_digits;
1375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t significand = ReadUint64(buffer, &read_digits);
1385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (buffer.length() == read_digits) {
1395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = DiyFp(significand, 0);
1405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *remaining_decimals = 0;
1415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  } else {
1425913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Round the significand.
1435913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    if (buffer[read_digits] >= '5') {
1445913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      significand++;
1455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    }
1465913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Compute the binary exponent.
1475913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int exponent = 0;
1485913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = DiyFp(significand, exponent);
1495913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *remaining_decimals = buffer.length() - read_digits;
1505913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
1515913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
1525913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
1535913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
154f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic bool DoubleStrtod(Vector<const char> trimmed,
155f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch                         int exponent,
156f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch                         double* result) {
157b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#if (V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87 || defined(USE_SIMULATOR)) && \
158b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    !defined(_MSC_VER)
159f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
160f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
161f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // result is not accurate.
1623ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // We know that Windows32 with MSVC, unlike with MinGW32, uses 64 bits and is
1633ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // therefore accurate.
1643ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // Note that the ARM and MIPS simulators are compiled for 32bits. They
1653ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  // therefore exhibit the same problem.
166f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return false;
167f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#endif
168f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
1695913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int read_digits;
170f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // The trimmed input fits into a double.
171f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
172f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // can compute the result-double simply by multiplying (resp. dividing) the
173f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // two numbers.
174f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // This is possible because IEEE guarantees that floating-point operations
175f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // return the best possible approximation.
176f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
177f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^-exponent fits into a double.
1785913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
179b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      DCHECK(read_digits == trimmed.length());
180f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result /= exact_powers_of_ten[-exponent];
181f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
182f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
183f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (0 <= exponent && exponent < kExactPowersOfTenSize) {
184f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^exponent fits into a double.
1855913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
186b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      DCHECK(read_digits == trimmed.length());
187f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[exponent];
188f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
189f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
190f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    int remaining_digits =
191f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch        kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
192f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if ((0 <= exponent) &&
193f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch        (exponent - remaining_digits < kExactPowersOfTenSize)) {
194f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // The trimmed string was short and we can multiply it with
195f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^remaining_digits. As a result the remaining exponent now fits
196f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // into a double too.
1975913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
198b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      DCHECK(read_digits == trimmed.length());
199f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[remaining_digits];
200f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[exponent - remaining_digits];
201f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
202f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
203f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
204f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return false;
205f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
206f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
207f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
2085913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Returns 10^exponent as an exact DiyFp.
2095913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// The given exponent must be in the range [1; kDecimalExponentDistance[.
2105913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic DiyFp AdjustmentPowerOfTen(int exponent) {
211b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(0 < exponent);
212b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(exponent < PowersOfTenCache::kDecimalExponentDistance);
2135913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Simply hardcode the remaining powers for the given decimal exponent
2145913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // distance.
215b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(PowersOfTenCache::kDecimalExponentDistance == 8);
2165913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  switch (exponent) {
2175913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60);
2185913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57);
2195913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54);
2205913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50);
2215913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47);
2225913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44);
2235913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40);
2245913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    default:
2255913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      UNREACHABLE();
2265913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return DiyFp(0, 0);
2275913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2285913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
2295913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2305913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// If the function returns true then the result is the correct double.
2325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Otherwise it is either the correct double or the double that is just below
2335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// the correct double.
2345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic bool DiyFpStrtod(Vector<const char> buffer,
2355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                        int exponent,
2365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                        double* result) {
2375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp input;
2385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int remaining_decimals;
2395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ReadDiyFp(buffer, &input, &remaining_decimals);
2405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Since we may have dropped some digits the input is not accurate.
2415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // If remaining_decimals is different than 0 than the error is at most
2425913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // .5 ulp (unit in the last place).
2435913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // We don't want to deal with fractions and therefore keep a common
2445913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // denominator.
2455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  const int kDenominatorLog = 3;
2465913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  const int kDenominator = 1 << kDenominatorLog;
2475913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Move the remaining decimals into the exponent.
2485913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  exponent += remaining_decimals;
249b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
2505913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2515913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int old_e = input.e();
2525913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Normalize();
2535913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error <<= old_e - input.e();
2545913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
255b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(exponent <= PowersOfTenCache::kMaxDecimalExponent);
2565913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (exponent < PowersOfTenCache::kMinDecimalExponent) {
2575913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = 0.0;
2585913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return true;
2595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp cached_power;
2615913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int cached_decimal_exponent;
2625913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
2635913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                                                     &cached_power,
2645913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                                                     &cached_decimal_exponent);
2655913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2665913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (cached_decimal_exponent != exponent) {
2675913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int adjustment_exponent = exponent - cached_decimal_exponent;
2685913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
2695913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.Multiply(adjustment_power);
2705913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
2715913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // The product of input with the adjustment power fits into a 64 bit
2725913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // integer.
273b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      DCHECK(DiyFp::kSignificandSize == 64);
2745913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    } else {
2755913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // The adjustment power is exact. There is hence only an error of 0.5.
2765913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      error += kDenominator / 2;
2775913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    }
2785913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2795913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2805913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Multiply(cached_power);
2815913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // The error introduced by a multiplication of a*b equals
2825913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_a + error_b + error_a*error_b/2^64 + 0.5
2835913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Substituting a with 'input' and b with 'cached_power' we have
2845913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_b = 0.5  (all cached powers have an error of less than 0.5 ulp),
2855913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
2865913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int error_b = kDenominator / 2;
2875913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int error_ab = (error == 0 ? 0 : 1);  // We round up to 1.
2885913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int fixed_error = kDenominator / 2;
2895913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error += error_b + error_ab + fixed_error;
2905913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2915913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  old_e = input.e();
2925913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Normalize();
2935913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error <<= old_e - input.e();
2945913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2955913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // See if the double's significand changes if we add/subtract the error.
2965913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int order_of_magnitude = DiyFp::kSignificandSize + input.e();
2975913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int effective_significand_size =
2985913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
2995913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int precision_digits_count =
3005913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      DiyFp::kSignificandSize - effective_significand_size;
3015913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
3025913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // This can only happen for very small denormals. In this case the
3035913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // half-way multiplied by the denominator exceeds the range of an uint64.
3045913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Simply shift everything to the right.
3055913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int shift_amount = (precision_digits_count + kDenominatorLog) -
3065913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck        DiyFp::kSignificandSize + 1;
3075913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.set_f(input.f() >> shift_amount);
3085913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.set_e(input.e() + shift_amount);
3095913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // We add 1 for the lost precision of error, and kDenominator for
3105913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // the lost precision of input.f().
3115913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    error = (error >> shift_amount) + 1 + kDenominator;
3125913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    precision_digits_count -= shift_amount;
3135913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3145913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
315b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(DiyFp::kSignificandSize == 64);
316b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(precision_digits_count < 64);
3175913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t one64 = 1;
3185913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
3195913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t precision_bits = input.f() & precision_bits_mask;
3205913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t half_way = one64 << (precision_digits_count - 1);
3215913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  precision_bits *= kDenominator;
3225913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  half_way *= kDenominator;
3235913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp rounded_input(input.f() >> precision_digits_count,
3245913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      input.e() + precision_digits_count);
3255913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (precision_bits >= half_way + error) {
3265913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    rounded_input.set_f(rounded_input.f() + 1);
3275913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3285913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // If the last_bits are too close to the half-way case than we are too
3295913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // inaccurate and round down. In this case we return false so that we can
3305913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // fall back to a more precise algorithm.
3315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  *result = Double(rounded_input).value();
3335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (half_way - error < precision_bits && precision_bits < half_way + error) {
3345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Too imprecise. The caller will have to fall back to a slower version.
3355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // However the returned number is guaranteed to be either the correct
3365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // double, or the next-lower double.
3375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return false;
3385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  } else {
3395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return true;
3405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
3425913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3435913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
34490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Returns the correct double for the buffer*10^exponent.
34590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// The variable guess should be a close guess that is either the correct double
34690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// or its lower neighbor (the nearest double less than the correct one).
34790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Preconditions:
34890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() + exponent <= kMaxDecimalPower + 1
34990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() + exponent > kMinDecimalPower
35090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() <= kMaxDecimalSignificantDigits
35190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic double BignumStrtod(Vector<const char> buffer,
35290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                           int exponent,
35390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                           double guess) {
35490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (guess == V8_INFINITY) {
35590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
35690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
35790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
35890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  DiyFp upper_boundary = Double(guess).UpperBoundary();
35990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
360b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(buffer.length() + exponent <= kMaxDecimalPower + 1);
361b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(buffer.length() + exponent > kMinDecimalPower);
362b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(buffer.length() <= kMaxSignificantDecimalDigits);
36390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Make sure that the Bignum will be able to hold all our numbers.
36490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Our Bignum implementation has a separate field for exponents. Shifts will
36590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // consume at most one bigit (< 64 bits).
36690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // ln(10) == 3.3219...
367b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  DCHECK(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
36890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  Bignum input;
36990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  Bignum boundary;
37090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  input.AssignDecimalString(buffer);
37190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  boundary.AssignUInt64(upper_boundary.f());
37290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (exponent >= 0) {
37390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    input.MultiplyByPowerOfTen(exponent);
37490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
37590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    boundary.MultiplyByPowerOfTen(-exponent);
37690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
37790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (upper_boundary.e() > 0) {
37890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    boundary.ShiftLeft(upper_boundary.e());
37990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
38090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    input.ShiftLeft(-upper_boundary.e());
38190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
38290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  int comparison = Bignum::Compare(input, boundary);
38390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (comparison < 0) {
38490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
38590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else if (comparison > 0) {
38690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return Double(guess).NextDouble();
38790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else if ((Double(guess).Significand() & 1) == 0) {
38890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    // Round towards even.
38990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
39090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
39190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return Double(guess).NextDouble();
39290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
39390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner}
39490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
39590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
396f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochdouble Strtod(Vector<const char> buffer, int exponent) {
397f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
398f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
399f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  exponent += left_trimmed.length() - trimmed.length();
400f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (trimmed.length() == 0) return 0.0;
40190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (trimmed.length() > kMaxSignificantDecimalDigits) {
40290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    char significant_buffer[kMaxSignificantDecimalDigits];
40390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    int significant_exponent;
40490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    TrimToMaxSignificantDigits(trimmed, exponent,
40590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                               significant_buffer, &significant_exponent);
4068a31eba00023874d4a1dcdc5f411cc4336776874Shimeng (Simon) Wang    return Strtod(Vector<const char>(significant_buffer,
4078a31eba00023874d4a1dcdc5f411cc4336776874Shimeng (Simon) Wang                                     kMaxSignificantDecimalDigits),
4088a31eba00023874d4a1dcdc5f411cc4336776874Shimeng (Simon) Wang                  significant_exponent);
40990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
410f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) return V8_INFINITY;
411f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
4125913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
41390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  double guess;
41490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (DoubleStrtod(trimmed, exponent, &guess) ||
41590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner      DiyFpStrtod(trimmed, exponent, &guess)) {
41690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
417f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
41890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  return BignumStrtod(trimmed, exponent, guess);
419f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
420f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
421014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace internal
422014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace v8
423