strtod.cc revision 90bac256d9f48d4ee52d0e08bf0e5cad57b3c51c
1f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Copyright 2010 the V8 project authors. All rights reserved.
2f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Redistribution and use in source and binary forms, with or without
3f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// modification, are permitted provided that the following conditions are
4f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// met:
5f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//
6f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//     * Redistributions of source code must retain the above copyright
7f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       notice, this list of conditions and the following disclaimer.
8f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//     * Redistributions in binary form must reproduce the above
9f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       copyright notice, this list of conditions and the following
10f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       disclaimer in the documentation and/or other materials provided
11f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       with the distribution.
12f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//     * Neither the name of Google Inc. nor the names of its
13f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       contributors may be used to endorse or promote products derived
14f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//       from this software without specific prior written permission.
15f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch//
16f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
28f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#include <stdarg.h>
29f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#include <limits.h>
30f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
31f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#include "v8.h"
32f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
33f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#include "strtod.h"
3490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner#include "bignum.h"
355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck#include "cached-powers.h"
365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck#include "double.h"
37f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
38f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochnamespace v8 {
39f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochnamespace internal {
40f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
41f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// 2^53 = 9007199254740992.
42f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any integer with at most 15 decimal digits will hence fit into a double
43f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// (which has a 53bit significand) without loss of precision.
44f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxExactDoubleIntegerDecimalDigits = 15;
455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// 2^64 = 18446744073709551616 > 10^19
46f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxUint64DecimalDigits = 19;
475913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
48f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Max double: 1.7976931348623157 x 10^308
49f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Min non-zero double: 4.9406564584124654 x 10^-324
50f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any x >= 10^309 is interpreted as +infinity.
51f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Any x <= 10^-324 is interpreted as 0.
52f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// Note that 2.5e-324 (despite being smaller than the min double) will be read
53f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch// as non-zero (equal to the min non-zero double).
54f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMaxDecimalPower = 309;
55f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kMinDecimalPower = -324;
56f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
575913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// 2^64 = 18446744073709551616
585913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic const uint64_t kMaxUint64 = V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF);
595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
61f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const double exact_powers_of_ten[] = {
62f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1.0,  // 10^0
63f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10.0,
64f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100.0,
65f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000.0,
66f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000.0,
67f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000.0,
68f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000.0,
69f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000.0,
70f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000.0,
71f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000.0,
72f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000.0,  // 10^10
73f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000.0,
74f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000.0,
75f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000.0,
76f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000.0,
77f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000.0,
78f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000.0,
79f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000000.0,
80f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000000.0,
81f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000000.0,
82f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  100000000000000000000.0,  // 10^20
83f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  1000000000000000000000.0,
84f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
85f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  10000000000000000000000.0
86f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch};
87f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
88f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
8990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Maximum number of significant digits in the decimal representation.
9090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// In fact the value is 772 (see conversions.cc), but to give us some margin
9190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// we round up to 780.
9290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic const int kMaxSignificantDecimalDigits = 780;
93f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
94f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
95f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  for (int i = 0; i < buffer.length(); i++) {
96f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (buffer[i] != '0') {
975913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return buffer.SubVector(i, buffer.length());
98f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
99f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
100f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return Vector<const char>(buffer.start(), 0);
101f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
102f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
103f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
104f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
105f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  for (int i = buffer.length() - 1; i >= 0; --i) {
106f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (buffer[i] != '0') {
1075913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return buffer.SubVector(0, i + 1);
108f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
109f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
110f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return Vector<const char>(buffer.start(), 0);
111f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
112f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
113f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
11490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic void TrimToMaxSignificantDigits(Vector<const char> buffer,
11590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       int exponent,
11690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       char* significant_buffer,
11790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                                       int* significant_exponent) {
11890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
11990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    significant_buffer[i] = buffer[i];
12090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
12190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // The input buffer has been trimmed. Therefore the last digit must be
12290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // different from '0'.
12390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  ASSERT(buffer[buffer.length() - 1] != '0');
12490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Set the last digit to be non-zero. This is sufficient to guarantee
12590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // correct rounding.
12690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
12790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  *significant_exponent =
12890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner      exponent + (buffer.length() - kMaxSignificantDecimalDigits);
12990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner}
13090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
1315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads digits from the buffer and converts them to a uint64.
1325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads in as many digits as fit into a uint64.
1335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// When the string starts with "1844674407370955161" no further digit is read.
1345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Since 2^64 = 18446744073709551616 it would still be possible read another
1355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// digit if it was less or equal than 6, but this would complicate the code.
1365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic uint64_t ReadUint64(Vector<const char> buffer,
1375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                           int* number_of_read_digits) {
138f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  uint64_t result = 0;
1395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int i = 0;
1405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
1415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int digit = buffer[i++] - '0';
142f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    ASSERT(0 <= digit && digit <= 9);
143f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    result = 10 * result + digit;
144f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
1455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  *number_of_read_digits = i;
146f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return result;
147f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
148f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
149f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
1505913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Reads a DiyFp from the buffer.
1515913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// The returned DiyFp is not necessarily normalized.
1525913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// If remaining_decimals is zero then the returned DiyFp is accurate.
1535913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Otherwise it has been rounded and has error of at most 1/2 ulp.
1545913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic void ReadDiyFp(Vector<const char> buffer,
1555913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      DiyFp* result,
1565913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      int* remaining_decimals) {
1575913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int read_digits;
1585913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t significand = ReadUint64(buffer, &read_digits);
1595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (buffer.length() == read_digits) {
1605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = DiyFp(significand, 0);
1615913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *remaining_decimals = 0;
1625913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  } else {
1635913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Round the significand.
1645913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    if (buffer[read_digits] >= '5') {
1655913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      significand++;
1665913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    }
1675913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Compute the binary exponent.
1685913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int exponent = 0;
1695913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = DiyFp(significand, exponent);
1705913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *remaining_decimals = buffer.length() - read_digits;
1715913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
1725913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
1735913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
1745913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
175f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochstatic bool DoubleStrtod(Vector<const char> trimmed,
176f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch                         int exponent,
177f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch                         double* result) {
178f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#if (defined(V8_TARGET_ARCH_IA32) || defined(USE_SIMULATOR)) && !defined(WIN32)
179f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
180f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
181f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // result is not accurate.
182f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // We know that Windows32 uses 64 bits and is therefore accurate.
183f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // Note that the ARM simulator is compiled for 32bits. It therefore exhibits
184f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  // the same problem.
185f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return false;
186f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch#endif
187f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
1885913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int read_digits;
189f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // The trimmed input fits into a double.
190f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
191f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // can compute the result-double simply by multiplying (resp. dividing) the
192f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // two numbers.
193f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // This is possible because IEEE guarantees that floating-point operations
194f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    // return the best possible approximation.
195f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
196f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^-exponent fits into a double.
1975913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
1985913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      ASSERT(read_digits == trimmed.length());
199f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result /= exact_powers_of_ten[-exponent];
200f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
201f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
202f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if (0 <= exponent && exponent < kExactPowersOfTenSize) {
203f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^exponent fits into a double.
2045913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
2055913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      ASSERT(read_digits == trimmed.length());
206f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[exponent];
207f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
208f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
209f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    int remaining_digits =
210f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch        kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
211f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    if ((0 <= exponent) &&
212f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch        (exponent - remaining_digits < kExactPowersOfTenSize)) {
213f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // The trimmed string was short and we can multiply it with
214f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // 10^remaining_digits. As a result the remaining exponent now fits
215f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      // into a double too.
2165913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
2175913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      ASSERT(read_digits == trimmed.length());
218f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[remaining_digits];
219f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      *result *= exact_powers_of_ten[exponent - remaining_digits];
220f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch      return true;
221f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    }
222f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
223f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  return false;
224f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
225f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
226f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
2275913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Returns 10^exponent as an exact DiyFp.
2285913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// The given exponent must be in the range [1; kDecimalExponentDistance[.
2295913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic DiyFp AdjustmentPowerOfTen(int exponent) {
2305913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(0 < exponent);
2315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
2325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Simply hardcode the remaining powers for the given decimal exponent
2335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // distance.
2345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
2355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  switch (exponent) {
2365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60);
2375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57);
2385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54);
2395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50);
2405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47);
2415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44);
2425913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40);
2435913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    default:
2445913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      UNREACHABLE();
2455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      return DiyFp(0, 0);
2465913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2475913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
2485913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2495913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2505913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// If the function returns true then the result is the correct double.
2515913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// Otherwise it is either the correct double or the double that is just below
2525913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck// the correct double.
2535913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reckstatic bool DiyFpStrtod(Vector<const char> buffer,
2545913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                        int exponent,
2555913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                        double* result) {
2565913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp input;
2575913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int remaining_decimals;
2585913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ReadDiyFp(buffer, &input, &remaining_decimals);
2595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Since we may have dropped some digits the input is not accurate.
2605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // If remaining_decimals is different than 0 than the error is at most
2615913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // .5 ulp (unit in the last place).
2625913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // We don't want to deal with fractions and therefore keep a common
2635913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // denominator.
2645913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  const int kDenominatorLog = 3;
2655913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  const int kDenominator = 1 << kDenominatorLog;
2665913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Move the remaining decimals into the exponent.
2675913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  exponent += remaining_decimals;
2685913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
2695913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2705913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int old_e = input.e();
2715913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Normalize();
2725913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error <<= old_e - input.e();
2735913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2745913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
2755913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (exponent < PowersOfTenCache::kMinDecimalExponent) {
2765913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    *result = 0.0;
2775913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return true;
2785913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2795913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp cached_power;
2805913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int cached_decimal_exponent;
2815913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
2825913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                                                     &cached_power,
2835913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                                                     &cached_decimal_exponent);
2845913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2855913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (cached_decimal_exponent != exponent) {
2865913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int adjustment_exponent = exponent - cached_decimal_exponent;
2875913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
2885913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.Multiply(adjustment_power);
2895913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
2905913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // The product of input with the adjustment power fits into a 64 bit
2915913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // integer.
2925913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      ASSERT(DiyFp::kSignificandSize == 64);
2935913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    } else {
2945913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      // The adjustment power is exact. There is hence only an error of 0.5.
2955913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      error += kDenominator / 2;
2965913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    }
2975913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
2985913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
2995913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Multiply(cached_power);
3005913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // The error introduced by a multiplication of a*b equals
3015913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_a + error_b + error_a*error_b/2^64 + 0.5
3025913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // Substituting a with 'input' and b with 'cached_power' we have
3035913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_b = 0.5  (all cached powers have an error of less than 0.5 ulp),
3045913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  //   error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
3055913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int error_b = kDenominator / 2;
3065913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int error_ab = (error == 0 ? 0 : 1);  // We round up to 1.
3075913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int fixed_error = kDenominator / 2;
3085913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error += error_b + error_ab + fixed_error;
3095913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3105913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  old_e = input.e();
3115913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  input.Normalize();
3125913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  error <<= old_e - input.e();
3135913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3145913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // See if the double's significand changes if we add/subtract the error.
3155913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int order_of_magnitude = DiyFp::kSignificandSize + input.e();
3165913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int effective_significand_size =
3175913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
3185913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  int precision_digits_count =
3195913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      DiyFp::kSignificandSize - effective_significand_size;
3205913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
3215913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // This can only happen for very small denormals. In this case the
3225913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // half-way multiplied by the denominator exceeds the range of an uint64.
3235913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Simply shift everything to the right.
3245913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    int shift_amount = (precision_digits_count + kDenominatorLog) -
3255913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck        DiyFp::kSignificandSize + 1;
3265913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.set_f(input.f() >> shift_amount);
3275913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    input.set_e(input.e() + shift_amount);
3285913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // We add 1 for the lost precision of error, and kDenominator for
3295913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // the lost precision of input.f().
3305913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    error = (error >> shift_amount) + 1 + kDenominator;
3315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    precision_digits_count -= shift_amount;
3325913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3335913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
3345913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(DiyFp::kSignificandSize == 64);
3355913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  ASSERT(precision_digits_count < 64);
3365913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t one64 = 1;
3375913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
3385913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t precision_bits = input.f() & precision_bits_mask;
3395913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  uint64_t half_way = one64 << (precision_digits_count - 1);
3405913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  precision_bits *= kDenominator;
3415913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  half_way *= kDenominator;
3425913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  DiyFp rounded_input(input.f() >> precision_digits_count,
3435913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck                      input.e() + precision_digits_count);
3445913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (precision_bits >= half_way + error) {
3455913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    rounded_input.set_f(rounded_input.f() + 1);
3465913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3475913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // If the last_bits are too close to the half-way case than we are too
3485913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // inaccurate and round down. In this case we return false so that we can
3495913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  // fall back to a more precise algorithm.
3505913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3515913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  *result = Double(rounded_input).value();
3525913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  if (half_way - error < precision_bits && precision_bits < half_way + error) {
3535913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // Too imprecise. The caller will have to fall back to a slower version.
3545913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // However the returned number is guaranteed to be either the correct
3555913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    // double, or the next-lower double.
3565913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return false;
3575913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  } else {
3585913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    return true;
3595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck  }
3605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck}
3615913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
3625913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
36390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Returns the correct double for the buffer*10^exponent.
36490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// The variable guess should be a close guess that is either the correct double
36590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// or its lower neighbor (the nearest double less than the correct one).
36690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner// Preconditions:
36790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() + exponent <= kMaxDecimalPower + 1
36890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() + exponent > kMinDecimalPower
36990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner//   buffer.length() <= kMaxDecimalSignificantDigits
37090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brennerstatic double BignumStrtod(Vector<const char> buffer,
37190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                           int exponent,
37290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                           double guess) {
37390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (guess == V8_INFINITY) {
37490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
37590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
37690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
37790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  DiyFp upper_boundary = Double(guess).UpperBoundary();
37890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
37990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
38090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  ASSERT(buffer.length() + exponent > kMinDecimalPower);
38190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
38290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Make sure that the Bignum will be able to hold all our numbers.
38390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // Our Bignum implementation has a separate field for exponents. Shifts will
38490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // consume at most one bigit (< 64 bits).
38590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  // ln(10) == 3.3219...
38690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
38790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  Bignum input;
38890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  Bignum boundary;
38990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  input.AssignDecimalString(buffer);
39090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  boundary.AssignUInt64(upper_boundary.f());
39190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (exponent >= 0) {
39290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    input.MultiplyByPowerOfTen(exponent);
39390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
39490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    boundary.MultiplyByPowerOfTen(-exponent);
39590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
39690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (upper_boundary.e() > 0) {
39790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    boundary.ShiftLeft(upper_boundary.e());
39890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
39990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    input.ShiftLeft(-upper_boundary.e());
40090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
40190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  int comparison = Bignum::Compare(input, boundary);
40290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (comparison < 0) {
40390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
40490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else if (comparison > 0) {
40590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return Double(guess).NextDouble();
40690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else if ((Double(guess).Significand() & 1) == 0) {
40790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    // Round towards even.
40890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
40990bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  } else {
41090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return Double(guess).NextDouble();
41190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
41290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner}
41390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
41490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner
415f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdochdouble Strtod(Vector<const char> buffer, int exponent) {
416f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
417f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
418f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  exponent += left_trimmed.length() - trimmed.length();
419f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (trimmed.length() == 0) return 0.0;
42090bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (trimmed.length() > kMaxSignificantDecimalDigits) {
42190bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    char significant_buffer[kMaxSignificantDecimalDigits];
42290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    int significant_exponent;
42390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    TrimToMaxSignificantDigits(trimmed, exponent,
42490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner                               significant_buffer, &significant_exponent);
42590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    trimmed =
42690bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner        Vector<const char>(significant_buffer, kMaxSignificantDecimalDigits);
42790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    exponent = significant_exponent;
42890bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  }
429f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) return V8_INFINITY;
430f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
4315913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck
43290bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  double guess;
43390bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  if (DoubleStrtod(trimmed, exponent, &guess) ||
43490bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner      DiyFpStrtod(trimmed, exponent, &guess)) {
43590bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner    return guess;
436f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch  }
43790bac256d9f48d4ee52d0e08bf0e5cad57b3c51cRussell Brenner  return BignumStrtod(trimmed, exponent, guess);
438f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch}
439f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
440f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch} }  // namespace v8::internal
441