v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
strtod.cc
Go to the documentation of this file.
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <stdarg.h>
8
9#include <cmath>
10#include <limits>
11
15
16namespace v8 {
17namespace base {
18
19// 2^53 = 9007199254740992.
20// Any integer with at most 15 decimal digits will hence fit into a double
21// (which has a 53bit significand) without loss of precision.
23// 2^64 = 18446744073709551616 > 10^19
24static const int kMaxUint64DecimalDigits = 19;
25
26// Max double: 1.7976931348623157 x 10^308
27// Min non-zero double: 4.9406564584124654 x 10^-324
28// Any x >= 10^309 is interpreted as +infinity.
29// Any x <= 10^-324 is interpreted as 0.
30// Note that 2.5e-324 (despite being smaller than the min double) will be read
31// as non-zero (equal to the min non-zero double).
32static const int kMaxDecimalPower = 309;
33static const int kMinDecimalPower = -324;
34
35// 2^64 = 18446744073709551616
36static const uint64_t kMaxUint64 = 0xFFFF'FFFF'FFFF'FFFF;
37
38// clang-format off
39static const double exact_powers_of_ten[] = {
40 1.0, // 10^0
41 10.0,
42 100.0,
43 1000.0,
44 10000.0,
45 100000.0,
46 1000000.0,
47 10000000.0,
48 100000000.0,
49 1000000000.0,
50 10000000000.0, // 10^10
51 100000000000.0,
52 1000000000000.0,
53 10000000000000.0,
54 100000000000000.0,
55 1000000000000000.0,
56 10000000000000000.0,
57 100000000000000000.0,
58 1000000000000000000.0,
59 10000000000000000000.0,
60 100000000000000000000.0, // 10^20
61 1000000000000000000000.0,
62 // 10^22 = 0x21E19E0C9BAB2400000 = 0x878678326EAC9 * 2^22
63 10000000000000000000000.0
64};
65// clang-format on
67
68// Maximum number of significant digits in the decimal representation.
69// In fact the value is 772 (see conversions.cc), but to give us some margin
70// we round up to 780.
71static const int kMaxSignificantDecimalDigits = 780;
72
74 for (int i = 0; i < buffer.length(); i++) {
75 if (buffer[i] != '0') {
76 return buffer.SubVector(i, buffer.length());
77 }
78 }
79 return Vector<const char>(buffer.begin(), 0);
80}
81
83 for (int i = buffer.length() - 1; i >= 0; --i) {
84 if (buffer[i] != '0') {
85 return buffer.SubVector(0, i + 1);
86 }
87 }
88 return Vector<const char>(buffer.begin(), 0);
89}
90
91static void TrimToMaxSignificantDigits(Vector<const char> buffer, int exponent,
92 char* significant_buffer,
93 int* significant_exponent) {
94 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
95 significant_buffer[i] = buffer[i];
96 }
97 // The input buffer has been trimmed. Therefore the last digit must be
98 // different from '0'.
99 DCHECK_NE(buffer[buffer.length() - 1], '0');
100 // Set the last digit to be non-zero. This is sufficient to guarantee
101 // correct rounding.
102 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
103 *significant_exponent =
104 exponent + (buffer.length() - kMaxSignificantDecimalDigits);
105}
106
107// Reads digits from the buffer and converts them to a uint64.
108// Reads in as many digits as fit into a uint64.
109// When the string starts with "1844674407370955161" no further digit is read.
110// Since 2^64 = 18446744073709551616 it would still be possible read another
111// digit if it was less or equal than 6, but this would complicate the code.
112static uint64_t ReadUint64(Vector<const char> buffer,
113 int* number_of_read_digits) {
114 uint64_t result = 0;
115 int i = 0;
116 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
117 int digit = buffer[i++] - '0';
118 DCHECK(0 <= digit && digit <= 9);
119 result = 10 * result + digit;
120 }
121 *number_of_read_digits = i;
122 return result;
123}
124
125// Reads a DiyFp from the buffer.
126// The returned DiyFp is not necessarily normalized.
127// If remaining_decimals is zero then the returned DiyFp is accurate.
128// Otherwise it has been rounded and has error of at most 1/2 ulp.
130 int* remaining_decimals) {
131 int read_digits;
132 uint64_t significand = ReadUint64(buffer, &read_digits);
133 if (buffer.length() == read_digits) {
134 *result = DiyFp(significand, 0);
135 *remaining_decimals = 0;
136 } else {
137 // Round the significand.
138 if (buffer[read_digits] >= '5') {
139 significand++;
140 }
141 // Compute the binary exponent.
142 int exponent = 0;
143 *result = DiyFp(significand, exponent);
144 *remaining_decimals = buffer.length() - read_digits;
145 }
146}
147
148static bool DoubleStrtod(Vector<const char> trimmed, int exponent,
149 double* result) {
150#if (V8_TARGET_ARCH_IA32 || defined(USE_SIMULATOR)) && !defined(_MSC_VER)
151 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
152 // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
153 // result is not accurate.
154 // We know that Windows32 with MSVC, unlike with MinGW32, uses 64 bits and is
155 // therefore accurate.
156 // Note that the ARM simulators are compiled for 32bits. They
157 // therefore exhibit the same problem.
161 return false;
162#else
164 int read_digits;
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, &read_digits));
174 DCHECK(read_digits == trimmed.length());
175 *result /= exact_powers_of_ten[-exponent];
176 return true;
177 }
178 if (0 <= exponent && exponent < kExactPowersOfTenSize) {
179 // 10^exponent fits into a double.
180 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
181 DCHECK(read_digits == trimmed.length());
182 *result *= exact_powers_of_ten[exponent];
183 return true;
184 }
185 int remaining_digits =
187 if ((0 <= exponent) &&
188 (exponent - remaining_digits < kExactPowersOfTenSize)) {
189 // The trimmed string was short and we can multiply it with
190 // 10^remaining_digits. As a result the remaining exponent now fits
191 // into a double too.
192 *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
193 DCHECK(read_digits == trimmed.length());
194 *result *= exact_powers_of_ten[remaining_digits];
195 *result *= exact_powers_of_ten[exponent - remaining_digits];
196 return true;
197 }
198 }
199 return false;
200#endif
201}
202
203// Returns 10^exponent as an exact DiyFp.
204// The given exponent must be in the range [1; kDecimalExponentDistance[.
205static DiyFp AdjustmentPowerOfTen(int exponent) {
206 DCHECK_LT(0, exponent);
208 // Simply hardcode the remaining powers for the given decimal exponent
209 // distance.
211 switch (exponent) {
212 case 1:
213 return DiyFp(0xA000'0000'0000'0000, -60);
214 case 2:
215 return DiyFp(0xC800'0000'0000'0000, -57);
216 case 3:
217 return DiyFp(0xFA00'0000'0000'0000, -54);
218 case 4:
219 return DiyFp(0x9C40'0000'0000'0000, -50);
220 case 5:
221 return DiyFp(0xC350'0000'0000'0000, -47);
222 case 6:
223 return DiyFp(0xF424'0000'0000'0000, -44);
224 case 7:
225 return DiyFp(0x9896'8000'0000'0000, -40);
226 default:
227 UNREACHABLE();
228 }
229}
230
231// If the function returns true then the result is the correct double.
232// Otherwise it is either the correct double or the double that is just below
233// the correct double.
234static bool DiyFpStrtod(Vector<const char> buffer, int exponent,
235 double* result) {
236 DiyFp input;
237 int remaining_decimals;
238 ReadDiyFp(buffer, &input, &remaining_decimals);
239 // Since we may have dropped some digits the input is not accurate.
240 // If remaining_decimals is different than 0 than the error is at most
241 // .5 ulp (unit in the last place).
242 // We don't want to deal with fractions and therefore keep a common
243 // denominator.
244 const int kDenominatorLog = 3;
245 const int kDenominator = 1 << kDenominatorLog;
246 // Move the remaining decimals into the exponent.
247 exponent += remaining_decimals;
248 int64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
249
250 int old_e = input.e();
251 input.Normalize();
252 error <<= old_e - input.e();
253
256 *result = 0.0;
257 return true;
258 }
259 DiyFp cached_power;
260 int cached_decimal_exponent;
262 &cached_decimal_exponent);
263
264 if (cached_decimal_exponent != exponent) {
265 int adjustment_exponent = exponent - cached_decimal_exponent;
266 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
267 input.Multiply(adjustment_power);
268 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
269 // The product of input with the adjustment power fits into a 64 bit
270 // integer.
272 } else {
273 // The adjustment power is exact. There is hence only an error of 0.5.
274 error += kDenominator / 2;
275 }
276 }
277
278 input.Multiply(cached_power);
279 // The error introduced by a multiplication of a*b equals
280 // error_a + error_b + error_a*error_b/2^64 + 0.5
281 // Substituting a with 'input' and b with 'cached_power' we have
282 // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp),
283 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
284 int error_b = kDenominator / 2;
285 int error_ab = (error == 0 ? 0 : 1); // We round up to 1.
286 int fixed_error = kDenominator / 2;
287 error += error_b + error_ab + fixed_error;
288
289 old_e = input.e();
290 input.Normalize();
291 error <<= old_e - input.e();
292
293 // See if the double's significand changes if we add/subtract the error.
294 int order_of_magnitude = DiyFp::kSignificandSize + input.e();
295 int effective_significand_size =
297 int precision_digits_count =
298 DiyFp::kSignificandSize - effective_significand_size;
299 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
300 // This can only happen for very small denormals. In this case the
301 // half-way multiplied by the denominator exceeds the range of an uint64.
302 // Simply shift everything to the right.
303 int shift_amount = (precision_digits_count + kDenominatorLog) -
305 input.set_f(input.f() >> shift_amount);
306 input.set_e(input.e() + shift_amount);
307 // We add 1 for the lost precision of error, and kDenominator for
308 // the lost precision of input.f().
309 error = (error >> shift_amount) + 1 + kDenominator;
310 precision_digits_count -= shift_amount;
311 }
312 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
314 DCHECK_LT(precision_digits_count, 64);
315 uint64_t one64 = 1;
316 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
317 uint64_t precision_bits = input.f() & precision_bits_mask;
318 uint64_t half_way = one64 << (precision_digits_count - 1);
319 precision_bits *= kDenominator;
320 half_way *= kDenominator;
321 DiyFp rounded_input(input.f() >> precision_digits_count,
322 input.e() + precision_digits_count);
323 if (precision_bits >= half_way + error) {
324 rounded_input.set_f(rounded_input.f() + 1);
325 }
326 // If the last_bits are too close to the half-way case than we are too
327 // inaccurate and round down. In this case we return false so that we can
328 // fall back to a more precise algorithm.
329
330 *result = Double(rounded_input).value();
331 if (half_way - error < precision_bits && precision_bits < half_way + error) {
332 // Too imprecise. The caller will have to fall back to a slower version.
333 // However the returned number is guaranteed to be either the correct
334 // double, or the next-lower double.
335 return false;
336 } else {
337 return true;
338 }
339}
340
341// Returns the correct double for the buffer*10^exponent.
342// The variable guess should be a close guess that is either the correct double
343// or its lower neighbor (the nearest double less than the correct one).
344// Preconditions:
345// buffer.length() + exponent <= kMaxDecimalPower + 1
346// buffer.length() + exponent > kMinDecimalPower
347// buffer.length() <= kMaxDecimalSignificantDigits
348static double BignumStrtod(Vector<const char> buffer, int exponent,
349 double guess) {
350 if (guess == std::numeric_limits<double>::infinity()) {
351 return guess;
352 }
353
354 DiyFp upper_boundary = Double(guess).UpperBoundary();
355
356 DCHECK(buffer.length() + exponent <= kMaxDecimalPower + 1);
357 DCHECK_GT(buffer.length() + exponent, kMinDecimalPower);
359 // Make sure that the Bignum will be able to hold all our numbers.
360 // Our Bignum implementation has a separate field for exponents. Shifts will
361 // consume at most one bigit (< 64 bits).
362 // ln(10) == 3.3219...
364 Bignum input;
365 Bignum boundary;
366 input.AssignDecimalString(buffer);
367 boundary.AssignUInt64(upper_boundary.f());
368 if (exponent >= 0) {
369 input.MultiplyByPowerOfTen(exponent);
370 } else {
371 boundary.MultiplyByPowerOfTen(-exponent);
372 }
373 if (upper_boundary.e() > 0) {
374 boundary.ShiftLeft(upper_boundary.e());
375 } else {
376 input.ShiftLeft(-upper_boundary.e());
377 }
378 int comparison = Bignum::Compare(input, boundary);
379 if (comparison < 0) {
380 return guess;
381 } else if (comparison > 0) {
382 return Double(guess).NextDouble();
383 } else if ((Double(guess).Significand() & 1) == 0) {
384 // Round towards even.
385 return guess;
386 } else {
387 return Double(guess).NextDouble();
388 }
389}
390
391double Strtod(Vector<const char> buffer, int exponent) {
392 Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
393 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
394 exponent += left_trimmed.length() - trimmed.length();
395 if (trimmed.empty()) return 0.0;
396 if (trimmed.length() > kMaxSignificantDecimalDigits) {
397 char significant_buffer[kMaxSignificantDecimalDigits];
398 int significant_exponent;
399 TrimToMaxSignificantDigits(trimmed, exponent, significant_buffer,
400 &significant_exponent);
401 return Strtod(
403 significant_exponent);
404 }
405 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower)
406 return std::numeric_limits<double>::infinity();
407 if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
408
409 double guess;
410 if (DoubleStrtod(trimmed, exponent, &guess) ||
411 DiyFpStrtod(trimmed, exponent, &guess)) {
412 return guess;
413 }
414 return BignumStrtod(trimmed, exponent, guess);
415}
416
417} // namespace base
418} // namespace v8
void AssignDecimalString(Vector< const char > value)
Definition bignum.cc:76
void AssignUInt64(uint64_t value)
Definition bignum.cc:35
void MultiplyByPowerOfTen(int exponent)
Definition bignum.cc:265
static const int kMaxSignificantBits
Definition bignum.h:18
void ShiftLeft(int shift_amount)
Definition bignum.cc:206
static int Compare(const Bignum &a, const Bignum &b)
Definition bignum.cc:566
static const int kSignificandSize
Definition diy-fp.h:22
constexpr uint64_t f() const
Definition diy-fp.h:91
constexpr int e() const
Definition diy-fp.h:92
constexpr void set_f(uint64_t new_value)
Definition diy-fp.h:94
constexpr double NextDouble() const
Definition double.h:69
static int SignificandSizeForOrderOfMagnitude(int order)
Definition double.h:166
DiyFp UpperBoundary() const
Definition double.h:127
constexpr double value() const
Definition double.h:158
static const int kDecimalExponentDistance
static const int kMaxDecimalExponent
static void GetCachedPowerForDecimalExponent(int requested_exponent, DiyFp *power, int *found_exponent)
static const int kMinDecimalExponent
int length() const
Definition vector.h:64
Vector< T > SubVector(size_t from, size_t to) const
Definition vector.h:41
constexpr bool empty() const
Definition vector.h:73
constexpr T * begin() const
Definition vector.h:96
ZoneVector< RpoNumber > & result
static void TrimToMaxSignificantDigits(Vector< const char > buffer, int exponent, char *significant_buffer, int *significant_exponent)
Definition strtod.cc:91
static const double exact_powers_of_ten[]
Definition strtod.cc:39
static const uint64_t kMaxUint64
Definition strtod.cc:36
static Vector< const char > TrimLeadingZeros(Vector< const char > buffer)
Definition strtod.cc:73
static bool DiyFpStrtod(Vector< const char > buffer, int exponent, double *result)
Definition strtod.cc:234
static const int kMaxSignificantDecimalDigits
Definition strtod.cc:71
static const int kExactPowersOfTenSize
Definition strtod.cc:66
static DiyFp AdjustmentPowerOfTen(int exponent)
Definition strtod.cc:205
static uint64_t ReadUint64(Vector< const char > buffer, int *number_of_read_digits)
Definition strtod.cc:112
double Strtod(Vector< const char > buffer, int exponent)
Definition strtod.cc:391
static double BignumStrtod(Vector< const char > buffer, int exponent, double guess)
Definition strtod.cc:348
static bool DoubleStrtod(Vector< const char > trimmed, int exponent, double *result)
Definition strtod.cc:148
static const int kMaxUint64DecimalDigits
Definition strtod.cc:24
static Vector< const char > TrimTrailingZeros(Vector< const char > buffer)
Definition strtod.cc:82
static const int kMaxExactDoubleIntegerDecimalDigits
Definition strtod.cc:22
static const int kMinDecimalPower
Definition strtod.cc:33
static void ReadDiyFp(Vector< const char > buffer, DiyFp *result, int *remaining_decimals)
Definition strtod.cc:129
static const int kMaxDecimalPower
Definition strtod.cc:32
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define USE(...)
Definition macros.h:293
#define arraysize(array)
Definition macros.h:67