blob: ae278bd98cf657b6a5f92705e53d8e1df0c48779 [file] [log] [blame]
Ben Murdochf87a2032010-10-22 12:50:53 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdarg.h>
29#include <limits.h>
30
31#include "v8.h"
32
33#include "strtod.h"
34// #include "cached-powers.h"
35
36namespace v8 {
37namespace internal {
38
39// 2^53 = 9007199254740992.
40// Any integer with at most 15 decimal digits will hence fit into a double
41// (which has a 53bit significand) without loss of precision.
42static const int kMaxExactDoubleIntegerDecimalDigits = 15;
43// 2^64 = 18446744073709551616
44// Any integer with at most 19 digits will hence fit into a 64bit datatype.
45static const int kMaxUint64DecimalDigits = 19;
46// Max double: 1.7976931348623157 x 10^308
47// Min non-zero double: 4.9406564584124654 x 10^-324
48// Any x >= 10^309 is interpreted as +infinity.
49// Any x <= 10^-324 is interpreted as 0.
50// Note that 2.5e-324 (despite being smaller than the min double) will be read
51// as non-zero (equal to the min non-zero double).
52static const int kMaxDecimalPower = 309;
53static const int kMinDecimalPower = -324;
54
55static const double exact_powers_of_ten[] = {
56 1.0, // 10^0
57 10.0,
58 100.0,
59 1000.0,
60 10000.0,
61 100000.0,
62 1000000.0,
63 10000000.0,
64 100000000.0,
65 1000000000.0,
66 10000000000.0, // 10^10
67 100000000000.0,
68 1000000000000.0,
69 10000000000000.0,
70 100000000000000.0,
71 1000000000000000.0,
72 10000000000000000.0,
73 100000000000000000.0,
74 1000000000000000000.0,
75 10000000000000000000.0,
76 100000000000000000000.0, // 10^20
77 1000000000000000000000.0,
78 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
79 10000000000000000000000.0
80};
81
82static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
83
84
85extern "C" double gay_strtod(const char* s00, const char** se);
86
87static double old_strtod(Vector<const char> buffer, int exponent) {
88 // gay_strtod is broken on Linux,x86. For numbers with few decimal digits
89 // the computation is done using floating-point operations which (on Linux)
90 // are prone to double-rounding errors.
91 // By adding several zeroes to the buffer gay_strtod falls back to a slower
92 // (but correct) algorithm.
93 const int kInsertedZeroesCount = 20;
94 char gay_buffer[1024];
95 Vector<char> gay_buffer_vector(gay_buffer, sizeof(gay_buffer));
96 int pos = 0;
97 for (int i = 0; i < buffer.length(); ++i) {
98 gay_buffer_vector[pos++] = buffer[i];
99 }
100 for (int i = 0; i < kInsertedZeroesCount; ++i) {
101 gay_buffer_vector[pos++] = '0';
102 }
103 exponent -= kInsertedZeroesCount;
104 gay_buffer_vector[pos++] = 'e';
105 if (exponent < 0) {
106 gay_buffer_vector[pos++] = '-';
107 exponent = -exponent;
108 }
109 const int kNumberOfExponentDigits = 5;
110 for (int i = kNumberOfExponentDigits - 1; i >= 0; i--) {
111 gay_buffer_vector[pos + i] = exponent % 10 + '0';
112 exponent /= 10;
113 }
114 pos += kNumberOfExponentDigits;
115 gay_buffer_vector[pos] = '\0';
116 return gay_strtod(gay_buffer, NULL);
117}
118
119
120static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
121 for (int i = 0; i < buffer.length(); i++) {
122 if (buffer[i] != '0') {
123 return Vector<const char>(buffer.start() + i, buffer.length() - i);
124 }
125 }
126 return Vector<const char>(buffer.start(), 0);
127}
128
129
130static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
131 for (int i = buffer.length() - 1; i >= 0; --i) {
132 if (buffer[i] != '0') {
133 return Vector<const char>(buffer.start(), i + 1);
134 }
135 }
136 return Vector<const char>(buffer.start(), 0);
137}
138
139
140uint64_t ReadUint64(Vector<const char> buffer) {
141 ASSERT(buffer.length() <= kMaxUint64DecimalDigits);
142 uint64_t result = 0;
143 for (int i = 0; i < buffer.length(); ++i) {
144 int digit = buffer[i] - '0';
145 ASSERT(0 <= digit && digit <= 9);
146 result = 10 * result + digit;
147 }
148 return result;
149}
150
151
152static bool DoubleStrtod(Vector<const char> trimmed,
153 int exponent,
154 double* result) {
155#if (defined(V8_TARGET_ARCH_IA32) || defined(USE_SIMULATOR)) && !defined(WIN32)
156 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
157 // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
158 // result is not accurate.
159 // We know that Windows32 uses 64 bits and is therefore accurate.
160 // Note that the ARM simulator is compiled for 32bits. It therefore exhibits
161 // the same problem.
162 return false;
163#endif
164 if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
165 // The trimmed input fits into a double.
166 // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
167 // can compute the result-double simply by multiplying (resp. dividing) the
168 // two numbers.
169 // This is possible because IEEE guarantees that floating-point operations
170 // return the best possible approximation.
171 if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
172 // 10^-exponent fits into a double.
173 *result = static_cast<double>(ReadUint64(trimmed));
174 *result /= exact_powers_of_ten[-exponent];
175 return true;
176 }
177 if (0 <= exponent && exponent < kExactPowersOfTenSize) {
178 // 10^exponent fits into a double.
179 *result = static_cast<double>(ReadUint64(trimmed));
180 *result *= exact_powers_of_ten[exponent];
181 return true;
182 }
183 int remaining_digits =
184 kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
185 if ((0 <= exponent) &&
186 (exponent - remaining_digits < kExactPowersOfTenSize)) {
187 // The trimmed string was short and we can multiply it with
188 // 10^remaining_digits. As a result the remaining exponent now fits
189 // into a double too.
190 *result = static_cast<double>(ReadUint64(trimmed));
191 *result *= exact_powers_of_ten[remaining_digits];
192 *result *= exact_powers_of_ten[exponent - remaining_digits];
193 return true;
194 }
195 }
196 return false;
197}
198
199
200double Strtod(Vector<const char> buffer, int exponent) {
201 Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
202 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
203 exponent += left_trimmed.length() - trimmed.length();
204 if (trimmed.length() == 0) return 0.0;
205 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) return V8_INFINITY;
206 if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
207 double result;
208 if (DoubleStrtod(trimmed, exponent, &result)) {
209 return result;
210 }
211 return old_strtod(trimmed, exponent);
212}
213
214} } // namespace v8::internal