1// Copyright 2012 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 <cmath>
30
31#include "globals.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
132// Reads digits from the buffer and converts them to a uint64.
133// Reads in as many digits as fit into a uint64.
134// When the string starts with "1844674407370955161" no further digit is read.
135// Since 2^64 = 18446744073709551616 it would still be possible read another
136// digit if it was less or equal than 6, but this would complicate the code.
137static uint64_t ReadUint64(Vector<const char> buffer,
138                           int* number_of_read_digits) {
139  uint64_t result = 0;
140  int i = 0;
141  while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
142    int digit = buffer[i++] - '0';
143    ASSERT(0 <= digit && digit <= 9);
144    result = 10 * result + digit;
145  }
146  *number_of_read_digits = i;
147  return result;
148}
149
150
151// Reads a DiyFp from the buffer.
152// The returned DiyFp is not necessarily normalized.
153// If remaining_decimals is zero then the returned DiyFp is accurate.
154// Otherwise it has been rounded and has error of at most 1/2 ulp.
155static void ReadDiyFp(Vector<const char> buffer,
156                      DiyFp* result,
157                      int* remaining_decimals) {
158  int read_digits;
159  uint64_t significand = ReadUint64(buffer, &read_digits);
160  if (buffer.length() == read_digits) {
161    *result = DiyFp(significand, 0);
162    *remaining_decimals = 0;
163  } else {
164    // Round the significand.
165    if (buffer[read_digits] >= '5') {
166      significand++;
167    }
168    // Compute the binary exponent.
169    int exponent = 0;
170    *result = DiyFp(significand, exponent);
171    *remaining_decimals = buffer.length() - read_digits;
172  }
173}
174
175
176static bool DoubleStrtod(Vector<const char> trimmed,
177                         int exponent,
178                         double* result) {
179#if (V8_TARGET_ARCH_IA32 || defined(USE_SIMULATOR)) && !defined(_MSC_VER)
180  // On x86 the floating-point stack can be 64 or 80 bits wide. If it is
181  // 80 bits wide (as is the case on Linux) then double-rounding occurs and the
182  // result is not accurate.
183  // We know that Windows32 with MSVC, unlike with MinGW32, uses 64 bits and is
184  // therefore accurate.
185  // Note that the ARM and MIPS simulators are compiled for 32bits. They
186  // therefore exhibit the same problem.
187  return false;
188#endif
189  if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
190    int read_digits;
191    // The trimmed input fits into a double.
192    // If the 10^exponent (resp. 10^-exponent) fits into a double too then we
193    // can compute the result-double simply by multiplying (resp. dividing) the
194    // two numbers.
195    // This is possible because IEEE guarantees that floating-point operations
196    // return the best possible approximation.
197    if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
198      // 10^-exponent fits into a double.
199      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
200      ASSERT(read_digits == trimmed.length());
201      *result /= exact_powers_of_ten[-exponent];
202      return true;
203    }
204    if (0 <= exponent && exponent < kExactPowersOfTenSize) {
205      // 10^exponent fits into a double.
206      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
207      ASSERT(read_digits == trimmed.length());
208      *result *= exact_powers_of_ten[exponent];
209      return true;
210    }
211    int remaining_digits =
212        kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
213    if ((0 <= exponent) &&
214        (exponent - remaining_digits < kExactPowersOfTenSize)) {
215      // The trimmed string was short and we can multiply it with
216      // 10^remaining_digits. As a result the remaining exponent now fits
217      // into a double too.
218      *result = static_cast<double>(ReadUint64(trimmed, &read_digits));
219      ASSERT(read_digits == trimmed.length());
220      *result *= exact_powers_of_ten[remaining_digits];
221      *result *= exact_powers_of_ten[exponent - remaining_digits];
222      return true;
223    }
224  }
225  return false;
226}
227
228
229// Returns 10^exponent as an exact DiyFp.
230// The given exponent must be in the range [1; kDecimalExponentDistance[.
231static DiyFp AdjustmentPowerOfTen(int exponent) {
232  ASSERT(0 < exponent);
233  ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
234  // Simply hardcode the remaining powers for the given decimal exponent
235  // distance.
236  ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
237  switch (exponent) {
238    case 1: return DiyFp(V8_2PART_UINT64_C(0xa0000000, 00000000), -60);
239    case 2: return DiyFp(V8_2PART_UINT64_C(0xc8000000, 00000000), -57);
240    case 3: return DiyFp(V8_2PART_UINT64_C(0xfa000000, 00000000), -54);
241    case 4: return DiyFp(V8_2PART_UINT64_C(0x9c400000, 00000000), -50);
242    case 5: return DiyFp(V8_2PART_UINT64_C(0xc3500000, 00000000), -47);
243    case 6: return DiyFp(V8_2PART_UINT64_C(0xf4240000, 00000000), -44);
244    case 7: return DiyFp(V8_2PART_UINT64_C(0x98968000, 00000000), -40);
245    default:
246      UNREACHABLE();
247      return DiyFp(0, 0);
248  }
249}
250
251
252// If the function returns true then the result is the correct double.
253// Otherwise it is either the correct double or the double that is just below
254// the correct double.
255static bool DiyFpStrtod(Vector<const char> buffer,
256                        int exponent,
257                        double* result) {
258  DiyFp input;
259  int remaining_decimals;
260  ReadDiyFp(buffer, &input, &remaining_decimals);
261  // Since we may have dropped some digits the input is not accurate.
262  // If remaining_decimals is different than 0 than the error is at most
263  // .5 ulp (unit in the last place).
264  // We don't want to deal with fractions and therefore keep a common
265  // denominator.
266  const int kDenominatorLog = 3;
267  const int kDenominator = 1 << kDenominatorLog;
268  // Move the remaining decimals into the exponent.
269  exponent += remaining_decimals;
270  int error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
271
272  int old_e = input.e();
273  input.Normalize();
274  error <<= old_e - input.e();
275
276  ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
277  if (exponent < PowersOfTenCache::kMinDecimalExponent) {
278    *result = 0.0;
279    return true;
280  }
281  DiyFp cached_power;
282  int cached_decimal_exponent;
283  PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
284                                                     &cached_power,
285                                                     &cached_decimal_exponent);
286
287  if (cached_decimal_exponent != exponent) {
288    int adjustment_exponent = exponent - cached_decimal_exponent;
289    DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
290    input.Multiply(adjustment_power);
291    if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
292      // The product of input with the adjustment power fits into a 64 bit
293      // integer.
294      ASSERT(DiyFp::kSignificandSize == 64);
295    } else {
296      // The adjustment power is exact. There is hence only an error of 0.5.
297      error += kDenominator / 2;
298    }
299  }
300
301  input.Multiply(cached_power);
302  // The error introduced by a multiplication of a*b equals
303  //   error_a + error_b + error_a*error_b/2^64 + 0.5
304  // Substituting a with 'input' and b with 'cached_power' we have
305  //   error_b = 0.5  (all cached powers have an error of less than 0.5 ulp),
306  //   error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
307  int error_b = kDenominator / 2;
308  int error_ab = (error == 0 ? 0 : 1);  // We round up to 1.
309  int fixed_error = kDenominator / 2;
310  error += error_b + error_ab + fixed_error;
311
312  old_e = input.e();
313  input.Normalize();
314  error <<= old_e - input.e();
315
316  // See if the double's significand changes if we add/subtract the error.
317  int order_of_magnitude = DiyFp::kSignificandSize + input.e();
318  int effective_significand_size =
319      Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
320  int precision_digits_count =
321      DiyFp::kSignificandSize - effective_significand_size;
322  if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
323    // This can only happen for very small denormals. In this case the
324    // half-way multiplied by the denominator exceeds the range of an uint64.
325    // Simply shift everything to the right.
326    int shift_amount = (precision_digits_count + kDenominatorLog) -
327        DiyFp::kSignificandSize + 1;
328    input.set_f(input.f() >> shift_amount);
329    input.set_e(input.e() + shift_amount);
330    // We add 1 for the lost precision of error, and kDenominator for
331    // the lost precision of input.f().
332    error = (error >> shift_amount) + 1 + kDenominator;
333    precision_digits_count -= shift_amount;
334  }
335  // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
336  ASSERT(DiyFp::kSignificandSize == 64);
337  ASSERT(precision_digits_count < 64);
338  uint64_t one64 = 1;
339  uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
340  uint64_t precision_bits = input.f() & precision_bits_mask;
341  uint64_t half_way = one64 << (precision_digits_count - 1);
342  precision_bits *= kDenominator;
343  half_way *= kDenominator;
344  DiyFp rounded_input(input.f() >> precision_digits_count,
345                      input.e() + precision_digits_count);
346  if (precision_bits >= half_way + error) {
347    rounded_input.set_f(rounded_input.f() + 1);
348  }
349  // If the last_bits are too close to the half-way case than we are too
350  // inaccurate and round down. In this case we return false so that we can
351  // fall back to a more precise algorithm.
352
353  *result = Double(rounded_input).value();
354  if (half_way - error < precision_bits && precision_bits < half_way + error) {
355    // Too imprecise. The caller will have to fall back to a slower version.
356    // However the returned number is guaranteed to be either the correct
357    // double, or the next-lower double.
358    return false;
359  } else {
360    return true;
361  }
362}
363
364
365// Returns the correct double for the buffer*10^exponent.
366// The variable guess should be a close guess that is either the correct double
367// or its lower neighbor (the nearest double less than the correct one).
368// Preconditions:
369//   buffer.length() + exponent <= kMaxDecimalPower + 1
370//   buffer.length() + exponent > kMinDecimalPower
371//   buffer.length() <= kMaxDecimalSignificantDigits
372static double BignumStrtod(Vector<const char> buffer,
373                           int exponent,
374                           double guess) {
375  if (guess == V8_INFINITY) {
376    return guess;
377  }
378
379  DiyFp upper_boundary = Double(guess).UpperBoundary();
380
381  ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
382  ASSERT(buffer.length() + exponent > kMinDecimalPower);
383  ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
384  // Make sure that the Bignum will be able to hold all our numbers.
385  // Our Bignum implementation has a separate field for exponents. Shifts will
386  // consume at most one bigit (< 64 bits).
387  // ln(10) == 3.3219...
388  ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
389  Bignum input;
390  Bignum boundary;
391  input.AssignDecimalString(buffer);
392  boundary.AssignUInt64(upper_boundary.f());
393  if (exponent >= 0) {
394    input.MultiplyByPowerOfTen(exponent);
395  } else {
396    boundary.MultiplyByPowerOfTen(-exponent);
397  }
398  if (upper_boundary.e() > 0) {
399    boundary.ShiftLeft(upper_boundary.e());
400  } else {
401    input.ShiftLeft(-upper_boundary.e());
402  }
403  int comparison = Bignum::Compare(input, boundary);
404  if (comparison < 0) {
405    return guess;
406  } else if (comparison > 0) {
407    return Double(guess).NextDouble();
408  } else if ((Double(guess).Significand() & 1) == 0) {
409    // Round towards even.
410    return guess;
411  } else {
412    return Double(guess).NextDouble();
413  }
414}
415
416
417double Strtod(Vector<const char> buffer, int exponent) {
418  Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
419  Vector<const char> trimmed = TrimTrailingZeros(left_trimmed);
420  exponent += left_trimmed.length() - trimmed.length();
421  if (trimmed.length() == 0) return 0.0;
422  if (trimmed.length() > kMaxSignificantDecimalDigits) {
423    char significant_buffer[kMaxSignificantDecimalDigits];
424    int significant_exponent;
425    TrimToMaxSignificantDigits(trimmed, exponent,
426                               significant_buffer, &significant_exponent);
427    return Strtod(Vector<const char>(significant_buffer,
428                                     kMaxSignificantDecimalDigits),
429                  significant_exponent);
430  }
431  if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) return V8_INFINITY;
432  if (exponent + trimmed.length() <= kMinDecimalPower) return 0.0;
433
434  double guess;
435  if (DoubleStrtod(trimmed, exponent, &guess) ||
436      DiyFpStrtod(trimmed, exponent, &guess)) {
437    return guess;
438  }
439  return BignumStrtod(trimmed, exponent, guess);
440}
441
442} }  // namespace v8::internal
443