fast-dtoa.cc revision 25f6136652d8341ed047e7fc1a450af5bd218ea9
1// 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 "v8.h"
29
30#include "fast-dtoa.h"
31
32#include "cached-powers.h"
33#include "diy-fp.h"
34#include "double.h"
35
36namespace v8 {
37namespace internal {
38
39// The minimal and maximal target exponent define the range of w's binary
40// exponent, where 'w' is the result of multiplying the input by a cached power
41// of ten.
42//
43// A different range might be chosen on a different platform, to optimize digit
44// generation, but a smaller range requires more powers of ten to be cached.
45static const int minimal_target_exponent = -60;
46static const int maximal_target_exponent = -32;
47
48
49// Adjusts the last digit of the generated number, and screens out generated
50// solutions that may be inaccurate. A solution may be inaccurate if it is
51// outside the safe interval, or if we ctannot prove that it is closer to the
52// input than a neighboring representation of the same length.
53//
54// Input: * buffer containing the digits of too_high / 10^kappa
55//        * the buffer's length
56//        * distance_too_high_w == (too_high - w).f() * unit
57//        * unsafe_interval == (too_high - too_low).f() * unit
58//        * rest = (too_high - buffer * 10^kappa).f() * unit
59//        * ten_kappa = 10^kappa * unit
60//        * unit = the common multiplier
61// Output: returns true if the buffer is guaranteed to contain the closest
62//    representable number to the input.
63//  Modifies the generated digits in the buffer to approach (round towards) w.
64bool RoundWeed(Vector<char> buffer,
65               int length,
66               uint64_t distance_too_high_w,
67               uint64_t unsafe_interval,
68               uint64_t rest,
69               uint64_t ten_kappa,
70               uint64_t unit) {
71  uint64_t small_distance = distance_too_high_w - unit;
72  uint64_t big_distance = distance_too_high_w + unit;
73  // Let w_low  = too_high - big_distance, and
74  //     w_high = too_high - small_distance.
75  // Note: w_low < w < w_high
76  //
77  // The real w (* unit) must lie somewhere inside the interval
78  // ]w_low; w_low[ (often written as "(w_low; w_low)")
79
80  // Basically the buffer currently contains a number in the unsafe interval
81  // ]too_low; too_high[ with too_low < w < too_high
82  //
83  //  too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84  //                     ^v 1 unit            ^      ^                 ^      ^
85  //  boundary_high ---------------------     .      .                 .      .
86  //                     ^v 1 unit            .      .                 .      .
87  //   - - - - - - - - - - - - - - - - - - -  +  - - + - - - - - -     .      .
88  //                                          .      .         ^       .      .
89  //                                          .  big_distance  .       .      .
90  //                                          .      .         .       .    rest
91  //                              small_distance     .         .       .      .
92  //                                          v      .         .       .      .
93  //  w_high - - - - - - - - - - - - - - - - - -     .         .       .      .
94  //                     ^v 1 unit                   .         .       .      .
95  //  w ----------------------------------------     .         .       .      .
96  //                     ^v 1 unit                   v         .       .      .
97  //  w_low  - - - - - - - - - - - - - - - - - - - - -         .       .      .
98  //                                                           .       .      v
99  //  buffer --------------------------------------------------+-------+--------
100  //                                                           .       .
101  //                                                  safe_interval    .
102  //                                                           v       .
103  //   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -     .
104  //                     ^v 1 unit                                     .
105  //  boundary_low -------------------------                     unsafe_interval
106  //                     ^v 1 unit                                     v
107  //  too_low  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108  //
109  //
110  // Note that the value of buffer could lie anywhere inside the range too_low
111  // to too_high.
112  //
113  // boundary_low, boundary_high and w are approximations of the real boundaries
114  // and v (the input number). They are guaranteed to be precise up to one unit.
115  // In fact the error is guaranteed to be strictly less than one unit.
116  //
117  // Anything that lies outside the unsafe interval is guaranteed not to round
118  // to v when read again.
119  // Anything that lies inside the safe interval is guaranteed to round to v
120  // when read again.
121  // If the number inside the buffer lies inside the unsafe interval but not
122  // inside the safe interval then we simply do not know and bail out (returning
123  // false).
124  //
125  // Similarly we have to take into account the imprecision of 'w' when rounding
126  // the buffer. If we have two potential representations we need to make sure
127  // that the chosen one is closer to w_low and w_high since v can be anywhere
128  // between them.
129  //
130  // By generating the digits of too_high we got the largest (closest to
131  // too_high) buffer that is still in the unsafe interval. In the case where
132  // w_high < buffer < too_high we try to decrement the buffer.
133  // This way the buffer approaches (rounds towards) w.
134  // There are 3 conditions that stop the decrementation process:
135  //   1) the buffer is already below w_high
136  //   2) decrementing the buffer would make it leave the unsafe interval
137  //   3) decrementing the buffer would yield a number below w_high and farther
138  //      away than the current number. In other words:
139  //              (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
140  // Instead of using the buffer directly we use its distance to too_high.
141  // Conceptually rest ~= too_high - buffer
142  while (rest < small_distance &&  // Negated condition 1
143         unsafe_interval - rest >= ten_kappa &&  // Negated condition 2
144         (rest + ten_kappa < small_distance ||  // buffer{-1} > w_high
145          small_distance - rest >= rest + ten_kappa - small_distance)) {
146    buffer[length - 1]--;
147    rest += ten_kappa;
148  }
149
150  // We have approached w+ as much as possible. We now test if approaching w-
151  // would require changing the buffer. If yes, then we have two possible
152  // representations close to w, but we cannot decide which one is closer.
153  if (rest < big_distance &&
154      unsafe_interval - rest >= ten_kappa &&
155      (rest + ten_kappa < big_distance ||
156       big_distance - rest > rest + ten_kappa - big_distance)) {
157    return false;
158  }
159
160  // Weeding test.
161  //   The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
162  //   Since too_low = too_high - unsafe_interval this is equivalent to
163  //      [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
164  //   Conceptually we have: rest ~= too_high - buffer
165  return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
166}
167
168
169
170static const uint32_t kTen4 = 10000;
171static const uint32_t kTen5 = 100000;
172static const uint32_t kTen6 = 1000000;
173static const uint32_t kTen7 = 10000000;
174static const uint32_t kTen8 = 100000000;
175static const uint32_t kTen9 = 1000000000;
176
177// Returns the biggest power of ten that is less than or equal than the given
178// number. We furthermore receive the maximum number of bits 'number' has.
179// If number_bits == 0 then 0^-1 is returned
180// The number of bits must be <= 32.
181// Precondition: (1 << number_bits) <= number < (1 << (number_bits + 1)).
182static void BiggestPowerTen(uint32_t number,
183                            int number_bits,
184                            uint32_t* power,
185                            int* exponent) {
186  switch (number_bits) {
187    case 32:
188    case 31:
189    case 30:
190      if (kTen9 <= number) {
191        *power = kTen9;
192        *exponent = 9;
193        break;
194      }  // else fallthrough
195    case 29:
196    case 28:
197    case 27:
198      if (kTen8 <= number) {
199        *power = kTen8;
200        *exponent = 8;
201        break;
202      }  // else fallthrough
203    case 26:
204    case 25:
205    case 24:
206      if (kTen7 <= number) {
207        *power = kTen7;
208        *exponent = 7;
209        break;
210      }  // else fallthrough
211    case 23:
212    case 22:
213    case 21:
214    case 20:
215      if (kTen6 <= number) {
216        *power = kTen6;
217        *exponent = 6;
218        break;
219      }  // else fallthrough
220    case 19:
221    case 18:
222    case 17:
223      if (kTen5 <= number) {
224        *power = kTen5;
225        *exponent = 5;
226        break;
227      }  // else fallthrough
228    case 16:
229    case 15:
230    case 14:
231      if (kTen4 <= number) {
232        *power = kTen4;
233        *exponent = 4;
234        break;
235      }  // else fallthrough
236    case 13:
237    case 12:
238    case 11:
239    case 10:
240      if (1000 <= number) {
241        *power = 1000;
242        *exponent = 3;
243        break;
244      }  // else fallthrough
245    case 9:
246    case 8:
247    case 7:
248      if (100 <= number) {
249        *power = 100;
250        *exponent = 2;
251        break;
252      }  // else fallthrough
253    case 6:
254    case 5:
255    case 4:
256      if (10 <= number) {
257        *power = 10;
258        *exponent = 1;
259        break;
260      }  // else fallthrough
261    case 3:
262    case 2:
263    case 1:
264      if (1 <= number) {
265        *power = 1;
266        *exponent = 0;
267        break;
268      }  // else fallthrough
269    case 0:
270      *power = 0;
271      *exponent = -1;
272      break;
273    default:
274      // Following assignments are here to silence compiler warnings.
275      *power = 0;
276      *exponent = 0;
277      UNREACHABLE();
278  }
279}
280
281
282// Generates the digits of input number w.
283// w is a floating-point number (DiyFp), consisting of a significand and an
284// exponent. Its exponent is bounded by minimal_target_exponent and
285// maximal_target_exponent.
286//       Hence -60 <= w.e() <= -32.
287//
288// Returns false if it fails, in which case the generated digits in the buffer
289// should not be used.
290// Preconditions:
291//  * low, w and high are correct up to 1 ulp (unit in the last place). That
292//    is, their error must be less that a unit of their last digits.
293//  * low.e() == w.e() == high.e()
294//  * low < w < high, and taking into account their error: low~ <= high~
295//  * minimal_target_exponent <= w.e() <= maximal_target_exponent
296// Postconditions: returns false if procedure fails.
297//   otherwise:
298//     * buffer is not null-terminated, but len contains the number of digits.
299//     * buffer contains the shortest possible decimal digit-sequence
300//       such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
301//       correct values of low and high (without their error).
302//     * if more than one decimal representation gives the minimal number of
303//       decimal digits then the one closest to W (where W is the correct value
304//       of w) is chosen.
305// Remark: this procedure takes into account the imprecision of its input
306//   numbers. If the precision is not enough to guarantee all the postconditions
307//   then false is returned. This usually happens rarely (~0.5%).
308//
309// Say, for the sake of example, that
310//   w.e() == -48, and w.f() == 0x1234567890abcdef
311// w's value can be computed by w.f() * 2^w.e()
312// We can obtain w's integral digits by simply shifting w.f() by -w.e().
313//  -> w's integral part is 0x1234
314//  w's fractional part is therefore 0x567890abcdef.
315// Printing w's integral part is easy (simply print 0x1234 in decimal).
316// In order to print its fraction we repeatedly multiply the fraction by 10 and
317// get each digit. Example the first digit after the point would be computed by
318//   (0x567890abcdef * 10) >> 48. -> 3
319// The whole thing becomes slightly more complicated because we want to stop
320// once we have enough digits. That is, once the digits inside the buffer
321// represent 'w' we can stop. Everything inside the interval low - high
322// represents w. However we have to pay attention to low, high and w's
323// imprecision.
324bool DigitGen(DiyFp low,
325              DiyFp w,
326              DiyFp high,
327              Vector<char> buffer,
328              int* length,
329              int* kappa) {
330  ASSERT(low.e() == w.e() && w.e() == high.e());
331  ASSERT(low.f() + 1 <= high.f() - 1);
332  ASSERT(minimal_target_exponent <= w.e() && w.e() <= maximal_target_exponent);
333  // low, w and high are imprecise, but by less than one ulp (unit in the last
334  // place).
335  // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
336  // the new numbers are outside of the interval we want the final
337  // representation to lie in.
338  // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
339  // numbers that are certain to lie in the interval. We will use this fact
340  // later on.
341  // We will now start by generating the digits within the uncertain
342  // interval. Later we will weed out representations that lie outside the safe
343  // interval and thus _might_ lie outside the correct interval.
344  uint64_t unit = 1;
345  DiyFp too_low = DiyFp(low.f() - unit, low.e());
346  DiyFp too_high = DiyFp(high.f() + unit, high.e());
347  // too_low and too_high are guaranteed to lie outside the interval we want the
348  // generated number in.
349  DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
350  // We now cut the input number into two parts: the integral digits and the
351  // fractionals. We will not write any decimal separator though, but adapt
352  // kappa instead.
353  // Reminder: we are currently computing the digits (stored inside the buffer)
354  // such that:   too_low < buffer * 10^kappa < too_high
355  // We use too_high for the digit_generation and stop as soon as possible.
356  // If we stop early we effectively round down.
357  DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
358  // Division by one is a shift.
359  uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
360  // Modulo by one is an and.
361  uint64_t fractionals = too_high.f() & (one.f() - 1);
362  uint32_t divider;
363  int divider_exponent;
364  BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
365                  &divider, &divider_exponent);
366  *kappa = divider_exponent + 1;
367  *length = 0;
368  // Loop invariant: buffer = too_high / 10^kappa  (integer division)
369  // The invariant holds for the first iteration: kappa has been initialized
370  // with the divider exponent + 1. And the divider is the biggest power of ten
371  // that is smaller than integrals.
372  while (*kappa > 0) {
373    int digit = integrals / divider;
374    buffer[*length] = '0' + digit;
375    (*length)++;
376    integrals %= divider;
377    (*kappa)--;
378    // Note that kappa now equals the exponent of the divider and that the
379    // invariant thus holds again.
380    uint64_t rest =
381        (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
382    // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
383    // Reminder: unsafe_interval.e() == one.e()
384    if (rest < unsafe_interval.f()) {
385      // Rounding down (by not emitting the remaining digits) yields a number
386      // that lies within the unsafe interval.
387      return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
388                       unsafe_interval.f(), rest,
389                       static_cast<uint64_t>(divider) << -one.e(), unit);
390    }
391    divider /= 10;
392  }
393
394  // The integrals have been generated. We are at the point of the decimal
395  // separator. In the following loop we simply multiply the remaining digits by
396  // 10 and divide by one. We just need to pay attention to multiply associated
397  // data (like the interval or 'unit'), too.
398  // Instead of multiplying by 10 we multiply by 5 (cheaper operation) and
399  // increase its (imaginary) exponent. At the same time we decrease the
400  // divider's (one's) exponent and shift its significand.
401  // Basically, if fractionals was a DiyFp (with fractionals.e == one.e):
402  //      fractionals.f *= 10;
403  //      fractionals.f >>= 1; fractionals.e++; // value remains unchanged.
404  //      one.f >>= 1; one.e++;                 // value remains unchanged.
405  //      and we have again fractionals.e == one.e which allows us to divide
406  //           fractionals.f() by one.f()
407  // We simply combine the *= 10 and the >>= 1.
408  while (true) {
409    fractionals *= 5;
410    unit *= 5;
411    unsafe_interval.set_f(unsafe_interval.f() * 5);
412    unsafe_interval.set_e(unsafe_interval.e() + 1);  // Will be optimized out.
413    one.set_f(one.f() >> 1);
414    one.set_e(one.e() + 1);
415    // Integer division by one.
416    int digit = static_cast<int>(fractionals >> -one.e());
417    buffer[*length] = '0' + digit;
418    (*length)++;
419    fractionals &= one.f() - 1;  // Modulo by one.
420    (*kappa)--;
421    if (fractionals < unsafe_interval.f()) {
422      return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
423                       unsafe_interval.f(), fractionals, one.f(), unit);
424    }
425  }
426}
427
428
429// Provides a decimal representation of v.
430// Returns true if it succeeds, otherwise the result cannot be trusted.
431// There will be *length digits inside the buffer (not null-terminated).
432// If the function returns true then
433//        v == (double) (buffer * 10^decimal_exponent).
434// The digits in the buffer are the shortest representation possible: no
435// 0.09999999999999999 instead of 0.1. The shorter representation will even be
436// chosen even if the longer one would be closer to v.
437// The last digit will be closest to the actual v. That is, even if several
438// digits might correctly yield 'v' when read again, the closest will be
439// computed.
440bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
441  DiyFp w = Double(v).AsNormalizedDiyFp();
442  // boundary_minus and boundary_plus are the boundaries between v and its
443  // closest floating-point neighbors. Any number strictly between
444  // boundary_minus and boundary_plus will round to v when convert to a double.
445  // Grisu3 will never output representations that lie exactly on a boundary.
446  DiyFp boundary_minus, boundary_plus;
447  Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
448  ASSERT(boundary_plus.e() == w.e());
449  DiyFp ten_mk;  // Cached power of ten: 10^-k
450  int mk;        // -k
451  GetCachedPower(w.e() + DiyFp::kSignificandSize, minimal_target_exponent,
452                 maximal_target_exponent, &mk, &ten_mk);
453  ASSERT(minimal_target_exponent <= w.e() + ten_mk.e() +
454         DiyFp::kSignificandSize &&
455         maximal_target_exponent >= w.e() + ten_mk.e() +
456         DiyFp::kSignificandSize);
457  // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
458  // 64 bit significand and ten_mk is thus only precise up to 64 bits.
459
460  // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
461  // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
462  // off by a small amount.
463  // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
464  // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
465  //           (f-1) * 2^e < w*10^k < (f+1) * 2^e
466  DiyFp scaled_w = DiyFp::Times(w, ten_mk);
467  ASSERT(scaled_w.e() ==
468         boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
469  // In theory it would be possible to avoid some recomputations by computing
470  // the difference between w and boundary_minus/plus (a power of 2) and to
471  // compute scaled_boundary_minus/plus by subtracting/adding from
472  // scaled_w. However the code becomes much less readable and the speed
473  // enhancements are not terriffic.
474  DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
475  DiyFp scaled_boundary_plus  = DiyFp::Times(boundary_plus,  ten_mk);
476
477  // DigitGen will generate the digits of scaled_w. Therefore we have
478  // v == (double) (scaled_w * 10^-mk).
479  // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
480  // integer than it will be updated. For instance if scaled_w == 1.23 then
481  // the buffer will be filled with "123" und the decimal_exponent will be
482  // decreased by 2.
483  int kappa;
484  bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
485                         buffer, length, &kappa);
486  *decimal_exponent = -mk + kappa;
487  return result;
488}
489
490
491bool FastDtoa(double v,
492              Vector<char> buffer,
493              int* length,
494              int* point) {
495  ASSERT(v > 0);
496  ASSERT(!Double(v).IsSpecial());
497
498  int decimal_exponent;
499  bool result = grisu3(v, buffer, length, &decimal_exponent);
500  *point = *length + decimal_exponent;
501  buffer[*length] = '\0';
502  return result;
503}
504
505} }  // namespace v8::internal
506