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 "fast-dtoa.h"
29 
30 #include "cached-powers.h"
31 #include "diy-fp.h"
32 #include "ieee.h"
33 
34 namespace double_conversion {
35 
36 // The minimal and maximal target exponent define the range of w's binary
37 // exponent, where 'w' is the result of multiplying the input by a cached power
38 // of ten.
39 //
40 // A different range might be chosen on a different platform, to optimize digit
41 // generation, but a smaller range requires more powers of ten to be cached.
42 static const int kMinimalTargetExponent = -60;
43 static const int kMaximalTargetExponent = -32;
44 
45 
46 // Adjusts the last digit of the generated number, and screens out generated
47 // solutions that may be inaccurate. A solution may be inaccurate if it is
48 // outside the safe interval, or if we cannot prove that it is closer to the
49 // input than a neighboring representation of the same length.
50 //
51 // Input: * buffer containing the digits of too_high / 10^kappa
52 //        * the buffer's length
53 //        * distance_too_high_w == (too_high - w).f() * unit
54 //        * unsafe_interval == (too_high - too_low).f() * unit
55 //        * rest = (too_high - buffer * 10^kappa).f() * unit
56 //        * ten_kappa = 10^kappa * unit
57 //        * unit = the common multiplier
58 // Output: returns true if the buffer is guaranteed to contain the closest
59 //    representable number to the input.
60 //  Modifies the generated digits in the buffer to approach (round towards) w.
RoundWeed(Vector<char> buffer,int length,uint64_t distance_too_high_w,uint64_t unsafe_interval,uint64_t rest,uint64_t ten_kappa,uint64_t unit)61 static bool RoundWeed(Vector<char> buffer,
62                       int length,
63                       uint64_t distance_too_high_w,
64                       uint64_t unsafe_interval,
65                       uint64_t rest,
66                       uint64_t ten_kappa,
67                       uint64_t unit) {
68   uint64_t small_distance = distance_too_high_w - unit;
69   uint64_t big_distance = distance_too_high_w + unit;
70   // Let w_low  = too_high - big_distance, and
71   //     w_high = too_high - small_distance.
72   // Note: w_low < w < w_high
73   //
74   // The real w (* unit) must lie somewhere inside the interval
75   // ]w_low; w_high[ (often written as "(w_low; w_high)")
76 
77   // Basically the buffer currently contains a number in the unsafe interval
78   // ]too_low; too_high[ with too_low < w < too_high
79   //
80   //  too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81   //                     ^v 1 unit            ^      ^                 ^      ^
82   //  boundary_high ---------------------     .      .                 .      .
83   //                     ^v 1 unit            .      .                 .      .
84   //   - - - - - - - - - - - - - - - - - - -  +  - - + - - - - - -     .      .
85   //                                          .      .         ^       .      .
86   //                                          .  big_distance  .       .      .
87   //                                          .      .         .       .    rest
88   //                              small_distance     .         .       .      .
89   //                                          v      .         .       .      .
90   //  w_high - - - - - - - - - - - - - - - - - -     .         .       .      .
91   //                     ^v 1 unit                   .         .       .      .
92   //  w ----------------------------------------     .         .       .      .
93   //                     ^v 1 unit                   v         .       .      .
94   //  w_low  - - - - - - - - - - - - - - - - - - - - -         .       .      .
95   //                                                           .       .      v
96   //  buffer --------------------------------------------------+-------+--------
97   //                                                           .       .
98   //                                                  safe_interval    .
99   //                                                           v       .
100   //   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -     .
101   //                     ^v 1 unit                                     .
102   //  boundary_low -------------------------                     unsafe_interval
103   //                     ^v 1 unit                                     v
104   //  too_low  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105   //
106   //
107   // Note that the value of buffer could lie anywhere inside the range too_low
108   // to too_high.
109   //
110   // boundary_low, boundary_high and w are approximations of the real boundaries
111   // and v (the input number). They are guaranteed to be precise up to one unit.
112   // In fact the error is guaranteed to be strictly less than one unit.
113   //
114   // Anything that lies outside the unsafe interval is guaranteed not to round
115   // to v when read again.
116   // Anything that lies inside the safe interval is guaranteed to round to v
117   // when read again.
118   // If the number inside the buffer lies inside the unsafe interval but not
119   // inside the safe interval then we simply do not know and bail out (returning
120   // false).
121   //
122   // Similarly we have to take into account the imprecision of 'w' when finding
123   // the closest representation of 'w'. If we have two potential
124   // representations, and one is closer to both w_low and w_high, then we know
125   // it is closer to the actual value v.
126   //
127   // By generating the digits of too_high we got the largest (closest to
128   // too_high) buffer that is still in the unsafe interval. In the case where
129   // w_high < buffer < too_high we try to decrement the buffer.
130   // This way the buffer approaches (rounds towards) w.
131   // There are 3 conditions that stop the decrementation process:
132   //   1) the buffer is already below w_high
133   //   2) decrementing the buffer would make it leave the unsafe interval
134   //   3) decrementing the buffer would yield a number below w_high and farther
135   //      away than the current number. In other words:
136   //              (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
137   // Instead of using the buffer directly we use its distance to too_high.
138   // Conceptually rest ~= too_high - buffer
139   // We need to do the following tests in this order to avoid over- and
140   // underflows.
141   ASSERT(rest <= unsafe_interval);
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 // Rounds the buffer upwards if the result is closer to v by possibly adding
170 // 1 to the buffer. If the precision of the calculation is not sufficient to
171 // round correctly, return false.
172 // The rounding might shift the whole buffer in which case the kappa is
173 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
174 //
175 // If 2*rest > ten_kappa then the buffer needs to be round up.
176 // rest can have an error of +/- 1 unit. This function accounts for the
177 // imprecision and returns false, if the rounding direction cannot be
178 // unambiguously determined.
179 //
180 // Precondition: rest < ten_kappa.
RoundWeedCounted(Vector<char> buffer,int length,uint64_t rest,uint64_t ten_kappa,uint64_t unit,int * kappa)181 static bool RoundWeedCounted(Vector<char> buffer,
182                              int length,
183                              uint64_t rest,
184                              uint64_t ten_kappa,
185                              uint64_t unit,
186                              int* kappa) {
187   ASSERT(rest < ten_kappa);
188   // The following tests are done in a specific order to avoid overflows. They
189   // will work correctly with any uint64 values of rest < ten_kappa and unit.
190   //
191   // If the unit is too big, then we don't know which way to round. For example
192   // a unit of 50 means that the real number lies within rest +/- 50. If
193   // 10^kappa == 40 then there is no way to tell which way to round.
194   if (unit >= ten_kappa) return false;
195   // Even if unit is just half the size of 10^kappa we are already completely
196   // lost. (And after the previous test we know that the expression will not
197   // over/underflow.)
198   if (ten_kappa - unit <= unit) return false;
199   // If 2 * (rest + unit) <= 10^kappa we can safely round down.
200   if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
201     return true;
202   }
203   // If 2 * (rest - unit) >= 10^kappa, then we can safely round up.
204   if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
205     // Increment the last digit recursively until we find a non '9' digit.
206     buffer[length - 1]++;
207     for (int i = length - 1; i > 0; --i) {
208       if (buffer[i] != '0' + 10) break;
209       buffer[i] = '0';
210       buffer[i - 1]++;
211     }
212     // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the
213     // exception of the first digit all digits are now '0'. Simply switch the
214     // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and
215     // the power (the kappa) is increased.
216     if (buffer[0] == '0' + 10) {
217       buffer[0] = '1';
218       (*kappa) += 1;
219     }
220     return true;
221   }
222   return false;
223 }
224 
225 // Returns the biggest power of ten that is less than or equal to the given
226 // number. We furthermore receive the maximum number of bits 'number' has.
227 //
228 // Returns power == 10^(exponent_plus_one-1) such that
229 //    power <= number < power * 10.
230 // If number_bits == 0 then 0^(0-1) is returned.
231 // The number of bits must be <= 32.
232 // Precondition: number < (1 << (number_bits + 1)).
233 
234 // Inspired by the method for finding an integer log base 10 from here:
235 // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
236 static unsigned int const kSmallPowersOfTen[] =
237     {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
238      1000000000};
239 
BiggestPowerTen(uint32_t number,int number_bits,uint32_t * power,int * exponent_plus_one)240 static void BiggestPowerTen(uint32_t number,
241                             int number_bits,
242                             uint32_t* power,
243                             int* exponent_plus_one) {
244   ASSERT(number < (1u << (number_bits + 1)));
245   // 1233/4096 is approximately 1/lg(10).
246   int exponent_plus_one_guess = ((number_bits + 1) * 1233 >> 12);
247   // We increment to skip over the first entry in the kPowersOf10 table.
248   // Note: kPowersOf10[i] == 10^(i-1).
249   exponent_plus_one_guess++;
250   // We don't have any guarantees that 2^number_bits <= number.
251   // TODO(floitsch): can we change the 'while' into an 'if'? We definitely see
252   // number < (2^number_bits - 1), but I haven't encountered
253   // number < (2^number_bits - 2) yet.
254   while (number < kSmallPowersOfTen[exponent_plus_one_guess]) {
255     exponent_plus_one_guess--;
256   }
257   *power = kSmallPowersOfTen[exponent_plus_one_guess];
258   *exponent_plus_one = exponent_plus_one_guess;
259 }
260 
261 // Generates the digits of input number w.
262 // w is a floating-point number (DiyFp), consisting of a significand and an
263 // exponent. Its exponent is bounded by kMinimalTargetExponent and
264 // kMaximalTargetExponent.
265 //       Hence -60 <= w.e() <= -32.
266 //
267 // Returns false if it fails, in which case the generated digits in the buffer
268 // should not be used.
269 // Preconditions:
270 //  * low, w and high are correct up to 1 ulp (unit in the last place). That
271 //    is, their error must be less than a unit of their last digits.
272 //  * low.e() == w.e() == high.e()
273 //  * low < w < high, and taking into account their error: low~ <= high~
274 //  * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
275 // Postconditions: returns false if procedure fails.
276 //   otherwise:
277 //     * buffer is not null-terminated, but len contains the number of digits.
278 //     * buffer contains the shortest possible decimal digit-sequence
279 //       such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
280 //       correct values of low and high (without their error).
281 //     * if more than one decimal representation gives the minimal number of
282 //       decimal digits then the one closest to W (where W is the correct value
283 //       of w) is chosen.
284 // Remark: this procedure takes into account the imprecision of its input
285 //   numbers. If the precision is not enough to guarantee all the postconditions
286 //   then false is returned. This usually happens rarely (~0.5%).
287 //
288 // Say, for the sake of example, that
289 //   w.e() == -48, and w.f() == 0x1234567890abcdef
290 // w's value can be computed by w.f() * 2^w.e()
291 // We can obtain w's integral digits by simply shifting w.f() by -w.e().
292 //  -> w's integral part is 0x1234
293 //  w's fractional part is therefore 0x567890abcdef.
294 // Printing w's integral part is easy (simply print 0x1234 in decimal).
295 // In order to print its fraction we repeatedly multiply the fraction by 10 and
296 // get each digit. Example the first digit after the point would be computed by
297 //   (0x567890abcdef * 10) >> 48. -> 3
298 // The whole thing becomes slightly more complicated because we want to stop
299 // once we have enough digits. That is, once the digits inside the buffer
300 // represent 'w' we can stop. Everything inside the interval low - high
301 // represents w. However we have to pay attention to low, high and w's
302 // imprecision.
DigitGen(DiyFp low,DiyFp w,DiyFp high,Vector<char> buffer,int * length,int * kappa)303 static bool DigitGen(DiyFp low,
304                      DiyFp w,
305                      DiyFp high,
306                      Vector<char> buffer,
307                      int* length,
308                      int* kappa) {
309   ASSERT(low.e() == w.e() && w.e() == high.e());
310   ASSERT(low.f() + 1 <= high.f() - 1);
311   ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
312   // low, w and high are imprecise, but by less than one ulp (unit in the last
313   // place).
314   // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
315   // the new numbers are outside of the interval we want the final
316   // representation to lie in.
317   // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
318   // numbers that are certain to lie in the interval. We will use this fact
319   // later on.
320   // We will now start by generating the digits within the uncertain
321   // interval. Later we will weed out representations that lie outside the safe
322   // interval and thus _might_ lie outside the correct interval.
323   uint64_t unit = 1;
324   DiyFp too_low = DiyFp(low.f() - unit, low.e());
325   DiyFp too_high = DiyFp(high.f() + unit, high.e());
326   // too_low and too_high are guaranteed to lie outside the interval we want the
327   // generated number in.
328   DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
329   // We now cut the input number into two parts: the integral digits and the
330   // fractionals. We will not write any decimal separator though, but adapt
331   // kappa instead.
332   // Reminder: we are currently computing the digits (stored inside the buffer)
333   // such that:   too_low < buffer * 10^kappa < too_high
334   // We use too_high for the digit_generation and stop as soon as possible.
335   // If we stop early we effectively round down.
336   DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
337   // Division by one is a shift.
338   uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
339   // Modulo by one is an and.
340   uint64_t fractionals = too_high.f() & (one.f() - 1);
341   uint32_t divisor;
342   int divisor_exponent_plus_one;
343   BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
344                   &divisor, &divisor_exponent_plus_one);
345   *kappa = divisor_exponent_plus_one;
346   *length = 0;
347   // Loop invariant: buffer = too_high / 10^kappa  (integer division)
348   // The invariant holds for the first iteration: kappa has been initialized
349   // with the divisor exponent + 1. And the divisor is the biggest power of ten
350   // that is smaller than integrals.
351   while (*kappa > 0) {
352     int digit = integrals / divisor;
353     buffer[*length] = '0' + digit;
354     (*length)++;
355     integrals %= divisor;
356     (*kappa)--;
357     // Note that kappa now equals the exponent of the divisor and that the
358     // invariant thus holds again.
359     uint64_t rest =
360         (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
361     // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
362     // Reminder: unsafe_interval.e() == one.e()
363     if (rest < unsafe_interval.f()) {
364       // Rounding down (by not emitting the remaining digits) yields a number
365       // that lies within the unsafe interval.
366       return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
367                        unsafe_interval.f(), rest,
368                        static_cast<uint64_t>(divisor) << -one.e(), unit);
369     }
370     divisor /= 10;
371   }
372 
373   // The integrals have been generated. We are at the point of the decimal
374   // separator. In the following loop we simply multiply the remaining digits by
375   // 10 and divide by one. We just need to pay attention to multiply associated
376   // data (like the interval or 'unit'), too.
377   // Note that the multiplication by 10 does not overflow, because w.e >= -60
378   // and thus one.e >= -60.
379   ASSERT(one.e() >= -60);
380   ASSERT(fractionals < one.f());
381   ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
382   while (true) {
383     fractionals *= 10;
384     unit *= 10;
385     unsafe_interval.set_f(unsafe_interval.f() * 10);
386     // Integer division by one.
387     int digit = static_cast<int>(fractionals >> -one.e());
388     buffer[*length] = '0' + digit;
389     (*length)++;
390     fractionals &= one.f() - 1;  // Modulo by one.
391     (*kappa)--;
392     if (fractionals < unsafe_interval.f()) {
393       return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
394                        unsafe_interval.f(), fractionals, one.f(), unit);
395     }
396   }
397 }
398 
399 
400 
401 // Generates (at most) requested_digits digits of input number w.
402 // w is a floating-point number (DiyFp), consisting of a significand and an
403 // exponent. Its exponent is bounded by kMinimalTargetExponent and
404 // kMaximalTargetExponent.
405 //       Hence -60 <= w.e() <= -32.
406 //
407 // Returns false if it fails, in which case the generated digits in the buffer
408 // should not be used.
409 // Preconditions:
410 //  * w is correct up to 1 ulp (unit in the last place). That
411 //    is, its error must be strictly less than a unit of its last digit.
412 //  * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
413 //
414 // Postconditions: returns false if procedure fails.
415 //   otherwise:
416 //     * buffer is not null-terminated, but length contains the number of
417 //       digits.
418 //     * the representation in buffer is the most precise representation of
419 //       requested_digits digits.
420 //     * buffer contains at most requested_digits digits of w. If there are less
421 //       than requested_digits digits then some trailing '0's have been removed.
422 //     * kappa is such that
423 //            w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2.
424 //
425 // Remark: This procedure takes into account the imprecision of its input
426 //   numbers. If the precision is not enough to guarantee all the postconditions
427 //   then false is returned. This usually happens rarely, but the failure-rate
428 //   increases with higher requested_digits.
DigitGenCounted(DiyFp w,int requested_digits,Vector<char> buffer,int * length,int * kappa)429 static bool DigitGenCounted(DiyFp w,
430                             int requested_digits,
431                             Vector<char> buffer,
432                             int* length,
433                             int* kappa) {
434   ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
435   ASSERT(kMinimalTargetExponent >= -60);
436   ASSERT(kMaximalTargetExponent <= -32);
437   // w is assumed to have an error less than 1 unit. Whenever w is scaled we
438   // also scale its error.
439   uint64_t w_error = 1;
440   // We cut the input number into two parts: the integral digits and the
441   // fractional digits. We don't emit any decimal separator, but adapt kappa
442   // instead. Example: instead of writing "1.2" we put "12" into the buffer and
443   // increase kappa by 1.
444   DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
445   // Division by one is a shift.
446   uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
447   // Modulo by one is an and.
448   uint64_t fractionals = w.f() & (one.f() - 1);
449   uint32_t divisor;
450   int divisor_exponent_plus_one;
451   BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
452                   &divisor, &divisor_exponent_plus_one);
453   *kappa = divisor_exponent_plus_one;
454   *length = 0;
455 
456   // Loop invariant: buffer = w / 10^kappa  (integer division)
457   // The invariant holds for the first iteration: kappa has been initialized
458   // with the divisor exponent + 1. And the divisor is the biggest power of ten
459   // that is smaller than 'integrals'.
460   while (*kappa > 0) {
461     int digit = integrals / divisor;
462     buffer[*length] = '0' + digit;
463     (*length)++;
464     requested_digits--;
465     integrals %= divisor;
466     (*kappa)--;
467     // Note that kappa now equals the exponent of the divisor and that the
468     // invariant thus holds again.
469     if (requested_digits == 0) break;
470     divisor /= 10;
471   }
472 
473   if (requested_digits == 0) {
474     uint64_t rest =
475         (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
476     return RoundWeedCounted(buffer, *length, rest,
477                             static_cast<uint64_t>(divisor) << -one.e(), w_error,
478                             kappa);
479   }
480 
481   // The integrals have been generated. We are at the point of the decimal
482   // separator. In the following loop we simply multiply the remaining digits by
483   // 10 and divide by one. We just need to pay attention to multiply associated
484   // data (the 'unit'), too.
485   // Note that the multiplication by 10 does not overflow, because w.e >= -60
486   // and thus one.e >= -60.
487   ASSERT(one.e() >= -60);
488   ASSERT(fractionals < one.f());
489   ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
490   while (requested_digits > 0 && fractionals > w_error) {
491     fractionals *= 10;
492     w_error *= 10;
493     // Integer division by one.
494     int digit = static_cast<int>(fractionals >> -one.e());
495     buffer[*length] = '0' + digit;
496     (*length)++;
497     requested_digits--;
498     fractionals &= one.f() - 1;  // Modulo by one.
499     (*kappa)--;
500   }
501   if (requested_digits != 0) return false;
502   return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
503                           kappa);
504 }
505 
506 
507 // Provides a decimal representation of v.
508 // Returns true if it succeeds, otherwise the result cannot be trusted.
509 // There will be *length digits inside the buffer (not null-terminated).
510 // If the function returns true then
511 //        v == (double) (buffer * 10^decimal_exponent).
512 // The digits in the buffer are the shortest representation possible: no
513 // 0.09999999999999999 instead of 0.1. The shorter representation will even be
514 // chosen even if the longer one would be closer to v.
515 // The last digit will be closest to the actual v. That is, even if several
516 // digits might correctly yield 'v' when read again, the closest will be
517 // computed.
Grisu3(double v,FastDtoaMode mode,Vector<char> buffer,int * length,int * decimal_exponent)518 static bool Grisu3(double v,
519                    FastDtoaMode mode,
520                    Vector<char> buffer,
521                    int* length,
522                    int* decimal_exponent) {
523   DiyFp w = Double(v).AsNormalizedDiyFp();
524   // boundary_minus and boundary_plus are the boundaries between v and its
525   // closest floating-point neighbors. Any number strictly between
526   // boundary_minus and boundary_plus will round to v when convert to a double.
527   // Grisu3 will never output representations that lie exactly on a boundary.
528   DiyFp boundary_minus, boundary_plus;
529   if (mode == FAST_DTOA_SHORTEST) {
530     Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
531   } else {
532     ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
533     float single_v = static_cast<float>(v);
534     Single(single_v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
535   }
536   ASSERT(boundary_plus.e() == w.e());
537   DiyFp ten_mk;  // Cached power of ten: 10^-k
538   int mk;        // -k
539   int ten_mk_minimal_binary_exponent =
540      kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
541   int ten_mk_maximal_binary_exponent =
542      kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
543   PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
544       ten_mk_minimal_binary_exponent,
545       ten_mk_maximal_binary_exponent,
546       &ten_mk, &mk);
547   ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
548           DiyFp::kSignificandSize) &&
549          (kMaximalTargetExponent >= w.e() + ten_mk.e() +
550           DiyFp::kSignificandSize));
551   // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
552   // 64 bit significand and ten_mk is thus only precise up to 64 bits.
553 
554   // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
555   // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
556   // off by a small amount.
557   // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
558   // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
559   //           (f-1) * 2^e < w*10^k < (f+1) * 2^e
560   DiyFp scaled_w = DiyFp::Times(w, ten_mk);
561   ASSERT(scaled_w.e() ==
562          boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
563   // In theory it would be possible to avoid some recomputations by computing
564   // the difference between w and boundary_minus/plus (a power of 2) and to
565   // compute scaled_boundary_minus/plus by subtracting/adding from
566   // scaled_w. However the code becomes much less readable and the speed
567   // enhancements are not terriffic.
568   DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
569   DiyFp scaled_boundary_plus  = DiyFp::Times(boundary_plus,  ten_mk);
570 
571   // DigitGen will generate the digits of scaled_w. Therefore we have
572   // v == (double) (scaled_w * 10^-mk).
573   // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
574   // integer than it will be updated. For instance if scaled_w == 1.23 then
575   // the buffer will be filled with "123" und the decimal_exponent will be
576   // decreased by 2.
577   int kappa;
578   bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
579                          buffer, length, &kappa);
580   *decimal_exponent = -mk + kappa;
581   return result;
582 }
583 
584 
585 // The "counted" version of grisu3 (see above) only generates requested_digits
586 // number of digits. This version does not generate the shortest representation,
587 // and with enough requested digits 0.1 will at some point print as 0.9999999...
588 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
589 // therefore the rounding strategy for halfway cases is irrelevant.
Grisu3Counted(double v,int requested_digits,Vector<char> buffer,int * length,int * decimal_exponent)590 static bool Grisu3Counted(double v,
591                           int requested_digits,
592                           Vector<char> buffer,
593                           int* length,
594                           int* decimal_exponent) {
595   DiyFp w = Double(v).AsNormalizedDiyFp();
596   DiyFp ten_mk;  // Cached power of ten: 10^-k
597   int mk;        // -k
598   int ten_mk_minimal_binary_exponent =
599      kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
600   int ten_mk_maximal_binary_exponent =
601      kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
602   PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
603       ten_mk_minimal_binary_exponent,
604       ten_mk_maximal_binary_exponent,
605       &ten_mk, &mk);
606   ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
607           DiyFp::kSignificandSize) &&
608          (kMaximalTargetExponent >= w.e() + ten_mk.e() +
609           DiyFp::kSignificandSize));
610   // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
611   // 64 bit significand and ten_mk is thus only precise up to 64 bits.
612 
613   // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
614   // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
615   // off by a small amount.
616   // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
617   // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
618   //           (f-1) * 2^e < w*10^k < (f+1) * 2^e
619   DiyFp scaled_w = DiyFp::Times(w, ten_mk);
620 
621   // We now have (double) (scaled_w * 10^-mk).
622   // DigitGen will generate the first requested_digits digits of scaled_w and
623   // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
624   // will not always be exactly the same since DigitGenCounted only produces a
625   // limited number of digits.)
626   int kappa;
627   bool result = DigitGenCounted(scaled_w, requested_digits,
628                                 buffer, length, &kappa);
629   *decimal_exponent = -mk + kappa;
630   return result;
631 }
632 
633 
FastDtoa(double v,FastDtoaMode mode,int requested_digits,Vector<char> buffer,int * length,int * decimal_point)634 bool FastDtoa(double v,
635               FastDtoaMode mode,
636               int requested_digits,
637               Vector<char> buffer,
638               int* length,
639               int* decimal_point) {
640   ASSERT(v > 0);
641   ASSERT(!Double(v).IsSpecial());
642 
643   bool result = false;
644   int decimal_exponent = 0;
645   switch (mode) {
646     case FAST_DTOA_SHORTEST:
647     case FAST_DTOA_SHORTEST_SINGLE:
648       result = Grisu3(v, mode, buffer, length, &decimal_exponent);
649       break;
650     case FAST_DTOA_PRECISION:
651       result = Grisu3Counted(v, requested_digits,
652                              buffer, length, &decimal_exponent);
653       break;
654     default:
655       UNREACHABLE();
656   }
657   if (result) {
658     *decimal_point = *length + decimal_exponent;
659     buffer[*length] = '\0';
660   }
661   return result;
662 }
663 
664 }  // namespace double_conversion
665