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