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
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.
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.
30 #include "fast-dtoa.h"
32 #include "cached-powers.h"
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
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.
45 static const int minimal_target_exponent = -60;
46 static const int maximal_target_exponent = -32;
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.
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.
64 bool RoundWeed(Vector<char> buffer,
66 uint64_t distance_too_high_w,
67 uint64_t unsafe_interval,
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
77 // The real w (* unit) must lie somewhere inside the interval
78 // ]w_low; w_low[ (often written as "(w_low; w_low)")
80 // Basically the buffer currently contains a number in the unsafe interval
81 // ]too_low; too_high[ with too_low < w < too_high
83 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85 // boundary_high --------------------- . . . .
87 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
89 // . big_distance . . .
91 // small_distance . . . .
93 // w_high - - - - - - - - - - - - - - - - - - . . . .
95 // w ---------------------------------------- . . . .
97 // w_low - - - - - - - - - - - - - - - - - - - - - . . .
99 // buffer --------------------------------------------------+-------+--------
103 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - .
105 // boundary_low ------------------------- unsafe_interval
107 // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110 // Note that the value of buffer could lie anywhere inside the range too_low
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.
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
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
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
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]--;
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)) {
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);
170 static const uint32_t kTen4 = 10000;
171 static const uint32_t kTen5 = 100000;
172 static const uint32_t kTen6 = 1000000;
173 static const uint32_t kTen7 = 10000000;
174 static const uint32_t kTen8 = 100000000;
175 static const uint32_t kTen9 = 1000000000;
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)).
182 static void BiggestPowerTen(uint32_t number,
186 switch (number_bits) {
190 if (kTen9 <= number) {
194 } // else fallthrough
198 if (kTen8 <= number) {
202 } // else fallthrough
206 if (kTen7 <= number) {
210 } // else fallthrough
215 if (kTen6 <= number) {
219 } // else fallthrough
223 if (kTen5 <= number) {
227 } // else fallthrough
231 if (kTen4 <= number) {
235 } // else fallthrough
240 if (1000 <= number) {
244 } // else fallthrough
252 } // else fallthrough
260 } // else fallthrough
268 } // else fallthrough
274 // Following assignments are here to silence compiler warnings.
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.
288 // Returns false if it fails, in which case the generated digits in the buffer
289 // should not be used.
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.
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
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%).
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
324 bool DigitGen(DiyFp low,
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
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
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.
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
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);
363 int divider_exponent;
364 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
365 ÷r, ÷r_exponent);
366 *kappa = divider_exponent + 1;
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.
373 int digit = integrals / divider;
374 buffer[*length] = '0' + digit;
376 integrals %= divider;
378 // Note that kappa now equals the exponent of the divider and that the
379 // invariant thus holds again.
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);
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.
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;
419 fractionals &= one.f() - 1; // Modulo by one.
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);
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
440 bool 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
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.
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);
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
484 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
485 buffer, length, &kappa);
486 *decimal_exponent = -mk + kappa;
491 bool FastDtoa(double v,
496 ASSERT(!Double(v).IsSpecial());
498 int decimal_exponent;
499 bool result = grisu3(v, buffer, length, &decimal_exponent);
500 *point = *length + decimal_exponent;
501 buffer[*length] = '\0';
505 } } // namespace v8::internal