v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
fast-dtoa.cc
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <stdint.h>
8
9#include "src/base/logging.h"
13
14namespace v8 {
15namespace base {
16
17// The minimal and maximal target exponent define the range of w's binary
18// exponent, where 'w' is the result of multiplying the input by a cached power
19// of ten.
20//
21// A different range might be chosen on a different platform, to optimize digit
22// generation, but a smaller range requires more powers of ten to be cached.
23static const int kMinimalTargetExponent = -60;
24static const int kMaximalTargetExponent = -32;
25
26// Adjusts the last digit of the generated number, and screens out generated
27// solutions that may be inaccurate. A solution may be inaccurate if it is
28// outside the safe interval, or if we ctannot prove that it is closer to the
29// input than a neighboring representation of the same length.
30//
31// Input: * buffer containing the digits of too_high / 10^kappa
32// * the buffer's length
33// * distance_too_high_w == (too_high - w).f() * unit
34// * unsafe_interval == (too_high - too_low).f() * unit
35// * rest = (too_high - buffer * 10^kappa).f() * unit
36// * ten_kappa = 10^kappa * unit
37// * unit = the common multiplier
38// Output: returns true if the buffer is guaranteed to contain the closest
39// representable number to the input.
40// Modifies the generated digits in the buffer to approach (round towards) w.
41static bool RoundWeed(char* last_digit, uint64_t distance_too_high_w,
42 uint64_t unsafe_interval, uint64_t rest,
43 uint64_t ten_kappa, uint64_t unit) {
44 uint64_t small_distance = distance_too_high_w - unit;
45 uint64_t big_distance = distance_too_high_w + unit;
46 // Let w_low = too_high - big_distance, and
47 // w_high = too_high - small_distance.
48 // Note: w_low < w < w_high
49 //
50 // The real w (* unit) must lie somewhere inside the interval
51 // ]w_low; w_high[ (often written as "(w_low; w_high)")
52
53 // Basically the buffer currently contains a number in the unsafe interval
54 // ]too_low; too_high[ with too_low < w < too_high
55 //
56 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57 // ^v 1 unit ^ ^ ^ ^
58 // boundary_high --------------------- . . . .
59 // ^v 1 unit . . . .
60 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
61 // . . ^ . .
62 // . big_distance . . .
63 // . . . . rest
64 // small_distance . . . .
65 // v . . . .
66 // w_high - - - - - - - - - - - - - - - - - - . . . .
67 // ^v 1 unit . . . .
68 // w ---------------------------------------- . . . .
69 // ^v 1 unit v . . .
70 // w_low - - - - - - - - - - - - - - - - - - - - - . . .
71 // . . v
72 // buffer --------------------------------------------------+-------+--------
73 // . .
74 // safe_interval .
75 // v .
76 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - .
77 // ^v 1 unit .
78 // boundary_low ------------------------- unsafe_interval
79 // ^v 1 unit v
80 // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81 //
82 //
83 // Note that the value of buffer could lie anywhere inside the range too_low
84 // to too_high.
85 //
86 // boundary_low, boundary_high and w are approximations of the real boundaries
87 // and v (the input number). They are guaranteed to be precise up to one unit.
88 // In fact the error is guaranteed to be strictly less than one unit.
89 //
90 // Anything that lies outside the unsafe interval is guaranteed not to round
91 // to v when read again.
92 // Anything that lies inside the safe interval is guaranteed to round to v
93 // when read again.
94 // If the number inside the buffer lies inside the unsafe interval but not
95 // inside the safe interval then we simply do not know and bail out (returning
96 // false).
97 //
98 // Similarly we have to take into account the imprecision of 'w' when finding
99 // the closest representation of 'w'. If we have two potential
100 // representations, and one is closer to both w_low and w_high, then we know
101 // it is closer to the actual value v.
102 //
103 // By generating the digits of too_high we got the largest (closest to
104 // too_high) buffer that is still in the unsafe interval. In the case where
105 // w_high < buffer < too_high we try to decrement the buffer.
106 // This way the buffer approaches (rounds towards) w.
107 // There are 3 conditions that stop the decrementation process:
108 // 1) the buffer is already below w_high
109 // 2) decrementing the buffer would make it leave the unsafe interval
110 // 3) decrementing the buffer would yield a number below w_high and farther
111 // away than the current number. In other words:
112 // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
113 // Instead of using the buffer directly we use its distance to too_high.
114 // Conceptually rest ~= too_high - buffer
115 // We need to do the following tests in this order to avoid over- and
116 // underflows.
117 DCHECK(rest <= unsafe_interval);
118 while (rest < small_distance && // Negated condition 1
119 unsafe_interval - rest >= ten_kappa && // Negated condition 2
120 (rest + ten_kappa < small_distance || // buffer{-1} > w_high
121 small_distance - rest >= rest + ten_kappa - small_distance)) {
122 --*last_digit;
123 rest += ten_kappa;
124 }
125
126 // We have approached w+ as much as possible. We now test if approaching w-
127 // would require changing the buffer. If yes, then we have two possible
128 // representations close to w, but we cannot decide which one is closer.
129 if (rest < big_distance && unsafe_interval - rest >= ten_kappa &&
130 (rest + ten_kappa < big_distance ||
131 big_distance - rest > rest + ten_kappa - big_distance)) {
132 return false;
133 }
134
135 // Weeding test.
136 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
137 // Since too_low = too_high - unsafe_interval this is equivalent to
138 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
139 // Conceptually we have: rest ~= too_high - buffer
140 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
141}
142
143// Rounds the buffer upwards if the result is closer to v by possibly adding
144// 1 to the buffer. If the precision of the calculation is not sufficient to
145// round correctly, return false.
146// The rounding might shift the whole buffer in which case the kappa is
147// adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
148//
149// If 2*rest > ten_kappa then the buffer needs to be round up.
150// rest can have an error of +/- 1 unit. This function accounts for the
151// imprecision and returns false, if the rounding direction cannot be
152// unambiguously determined.
153//
154// Precondition: rest < ten_kappa.
155static bool RoundWeedCounted(Vector<char> buffer, int length, uint64_t rest,
156 uint64_t ten_kappa, uint64_t unit, int* kappa) {
157 DCHECK(rest < ten_kappa);
158 // The following tests are done in a specific order to avoid overflows. They
159 // will work correctly with any uint64 values of rest < ten_kappa and unit.
160 //
161 // If the unit is too big, then we don't know which way to round. For example
162 // a unit of 50 means that the real number lies within rest +/- 50. If
163 // 10^kappa == 40 then there is no way to tell which way to round.
164 if (unit >= ten_kappa) return false;
165 // Even if unit is just half the size of 10^kappa we are already completely
166 // lost. (And after the previous test we know that the expression will not
167 // over/underflow.)
168 if (ten_kappa - unit <= unit) return false;
169 // If 2 * (rest + unit) <= 10^kappa we can safely round down.
170 if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
171 return true;
172 }
173 // If 2 * (rest - unit) >= 10^kappa, then we can safely round up.
174 if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
175 // Increment the last digit recursively until we find a non '9' digit.
176 buffer[length - 1]++;
177 for (int i = length - 1; i > 0; --i) {
178 if (buffer[i] != '0' + 10) break;
179 buffer[i] = '0';
180 buffer[i - 1]++;
181 }
182 // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the
183 // exception of the first digit all digits are now '0'. Simply switch the
184 // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and
185 // the power (the kappa) is increased.
186 if (buffer[0] == '0' + 10) {
187 buffer[0] = '1';
188 (*kappa) += 1;
189 }
190 return true;
191 }
192 return false;
193}
194
195static const uint32_t kTen4 = 10000;
196static const uint32_t kTen5 = 100000;
197static const uint32_t kTen6 = 1000000;
198static const uint32_t kTen7 = 10000000;
199static const uint32_t kTen8 = 100000000;
200static const uint32_t kTen9 = 1000000000;
201
202struct DivMagic {
203 uint32_t mul;
204 uint32_t shift;
205};
206
207// This table was computed by libdivide. Essentially, the shift is
208// floor(log2(x)), and the mul is 2^(33 + shift) / x, rounded up and truncated
209// to 32 bits.
210static const DivMagic div[] = {
211 {0, 0}, // Not used, since 1 is not supported by the algorithm.
212 {0x9999999a, 3}, // 10
213 {0x47ae147b, 6}, // 100
214 {0x0624dd30, 9}, // 1000
215 {0xa36e2eb2, 13}, // 10000
216 {0x4f8b588f, 16}, // 100000
217 {0x0c6f7a0c, 19}, // 1000000
218 {0xad7f29ac, 23}, // 10000000
219 {0x5798ee24, 26} // 100000000
220};
221
222// Returns *val / divisor, and does *val %= divisor. d must be the DivMagic
223// corresponding to the divisor.
224//
225// This algorithm is exactly the same as libdivide's branch-free u32 algorithm,
226// except that we add back a branch anyway to support 1.
227//
228// GCC/Clang uses a slightly different algorithm that doesn't need
229// the extra rounding step (and that would allow us to do 1 without
230// a branch), but it requires a pre-shift for the case of 10000,
231// so it ends up slower, at least on x86-64.
232//
233// Note that this is actually a small loss for certain CPUs with
234// a very fast divider (e.g. Zen 3), but a significant win for most
235// others (including the entire Skylake family).
236static inline uint32_t fast_divmod(uint32_t* val, uint32_t divisor,
237 const DivMagic& d) {
238 if (divisor == 1) {
239 uint32_t digit = *val;
240 *val = 0;
241 return digit;
242 } else {
243 uint32_t q = (static_cast<uint64_t>(*val) * d.mul) >> 32;
244 uint32_t t = ((*val - q) >> 1) + q;
245 uint32_t digit = t >> d.shift;
246 *val -= digit * divisor;
247 return digit;
248 }
249}
250
251// Returns the biggest power of ten that is less than or equal than the given
252// number. We furthermore receive the maximum number of bits 'number' has.
253// If number_bits == 0 then 0^-1 is returned
254// The number of bits must be <= 32.
255// Precondition: number < (1 << (number_bits + 1)).
256static inline void BiggestPowerTen(uint32_t number, int number_bits,
257 uint32_t* power, unsigned* exponent) {
258 switch (number_bits) {
259 case 32:
260 case 31:
261 case 30:
262 if (kTen9 <= number) {
263 *power = kTen9;
264 *exponent = 9;
265 break;
266 }
267 [[fallthrough]];
268 case 29:
269 case 28:
270 case 27:
271 if (kTen8 <= number) {
272 *power = kTen8;
273 *exponent = 8;
274 break;
275 }
276 [[fallthrough]];
277 case 26:
278 case 25:
279 case 24:
280 if (kTen7 <= number) {
281 *power = kTen7;
282 *exponent = 7;
283 break;
284 }
285 [[fallthrough]];
286 case 23:
287 case 22:
288 case 21:
289 case 20:
290 if (kTen6 <= number) {
291 *power = kTen6;
292 *exponent = 6;
293 break;
294 }
295 [[fallthrough]];
296 case 19:
297 case 18:
298 case 17:
299 if (kTen5 <= number) {
300 *power = kTen5;
301 *exponent = 5;
302 break;
303 }
304 [[fallthrough]];
305 case 16:
306 case 15:
307 case 14:
308 if (kTen4 <= number) {
309 *power = kTen4;
310 *exponent = 4;
311 break;
312 }
313 [[fallthrough]];
314 case 13:
315 case 12:
316 case 11:
317 case 10:
318 if (1000 <= number) {
319 *power = 1000;
320 *exponent = 3;
321 break;
322 }
323 [[fallthrough]];
324 case 9:
325 case 8:
326 case 7:
327 if (100 <= number) {
328 *power = 100;
329 *exponent = 2;
330 break;
331 }
332 [[fallthrough]];
333 case 6:
334 case 5:
335 case 4:
336 if (10 <= number) {
337 *power = 10;
338 *exponent = 1;
339 break;
340 }
341 [[fallthrough]];
342 case 3:
343 case 2:
344 case 1:
345 if (1 <= number) {
346 *power = 1;
347 *exponent = 0;
348 break;
349 }
350 [[fallthrough]];
351 case 0:
352 *power = 0;
353 *exponent = -1;
354 break;
355 default:
356 // Following assignments are here to silence compiler warnings.
357 *power = 0;
358 *exponent = 0;
359 UNREACHABLE();
360 }
361}
362
363// Generates the digits of input number w.
364// w is a floating-point number (DiyFp), consisting of a significand and an
365// exponent. Its exponent is bounded by kMinimalTargetExponent and
366// kMaximalTargetExponent.
367// Hence -60 <= w.e() <= -32.
368//
369// Returns false if it fails, in which case the generated digits in the buffer
370// should not be used.
371// Preconditions:
372// * low, w and high are correct up to 1 ulp (unit in the last place). That
373// is, their error must be less than a unit of their last digits.
374// * low.e() == w.e() == high.e()
375// * low < w < high, and taking into account their error: low~ <= high~
376// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
377// Postconditions: returns false if procedure fails.
378// otherwise:
379// * buffer is not null-terminated, but len contains the number of digits.
380// * buffer contains the shortest possible decimal digit-sequence
381// such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
382// correct values of low and high (without their error).
383// * if more than one decimal representation gives the minimal number of
384// decimal digits then the one closest to W (where W is the correct value
385// of w) is chosen.
386// Remark: this procedure takes into account the imprecision of its input
387// numbers. If the precision is not enough to guarantee all the postconditions
388// then false is returned. This usually happens rarely (~0.5%).
389//
390// Say, for the sake of example, that
391// w.e() == -48, and w.f() == 0x1234567890ABCDEF
392// w's value can be computed by w.f() * 2^w.e()
393// We can obtain w's integral digits by simply shifting w.f() by -w.e().
394// -> w's integral part is 0x1234
395// w's fractional part is therefore 0x567890ABCDEF.
396// Printing w's integral part is easy (simply print 0x1234 in decimal).
397// In order to print its fraction we repeatedly multiply the fraction by 10 and
398// get each digit. Example the first digit after the point would be computed by
399// (0x567890ABCDEF * 10) >> 48. -> 3
400// The whole thing becomes slightly more complicated because we want to stop
401// once we have enough digits. That is, once the digits inside the buffer
402// represent 'w' we can stop. Everything inside the interval low - high
403// represents w. However we have to pay attention to low, high and w's
404// imprecision.
405static bool DigitGen(DiyFp low, DiyFp w, DiyFp high, char** outptr,
406 int* kappa) {
407 DCHECK(low.e() == w.e() && w.e() == high.e());
408 DCHECK(low.f() + 1 <= high.f() - 1);
410 // low, w and high are imprecise, but by less than one ulp (unit in the last
411 // place).
412 // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
413 // the new numbers are outside of the interval we want the final
414 // representation to lie in.
415 // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
416 // numbers that are certain to lie in the interval. We will use this fact
417 // later on.
418 // We will now start by generating the digits within the uncertain
419 // interval. Later we will weed out representations that lie outside the safe
420 // interval and thus _might_ lie outside the correct interval.
421 uint64_t unit = 1;
422 DiyFp too_low = DiyFp(low.f() - unit, low.e());
423 DiyFp too_high = DiyFp(high.f() + unit, high.e());
424 // too_low and too_high are guaranteed to lie outside the interval we want the
425 // generated number in.
426 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
427 // We now cut the input number into two parts: the integral digits and the
428 // fractionals. We will not write any decimal separator though, but adapt
429 // kappa instead.
430 // Reminder: we are currently computing the digits (stored inside the buffer)
431 // such that: too_low < buffer * 10^kappa < too_high
432 // We use too_high for the digit_generation and stop as soon as possible.
433 // If we stop early we effectively round down.
434 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
435 // Division by one is a shift.
436 uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
437 // Modulo by one is an and.
438 uint64_t fractionals = too_high.f() & (one.f() - 1);
439 uint32_t divisor;
440 unsigned divisor_exponent;
441 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), &divisor,
442 &divisor_exponent);
443 *kappa = divisor_exponent + 1;
444 // Loop invariant: buffer = too_high / 10^kappa (integer division)
445 // The invariant holds for the first iteration: kappa has been initialized
446 // with the divisor exponent + 1. And the divisor is the biggest power of ten
447 // that is smaller than integrals.
448 while (*kappa > 0) {
449 uint32_t digit = fast_divmod(&integrals, divisor, div[divisor_exponent]);
450 **outptr = '0' + digit;
451 (*outptr)++;
452 (*kappa)--;
453 // Note that kappa now equals the exponent of the divisor and that the
454 // invariant thus holds again.
455 uint64_t rest =
456 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
457 // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
458 // Reminder: unsafe_interval.e() == one.e()
459 if (rest < unsafe_interval.f()) {
460 // Rounding down (by not emitting the remaining digits) yields a number
461 // that lies within the unsafe interval.
462 return RoundWeed(*outptr - 1, DiyFp::Minus(too_high, w).f(),
463 unsafe_interval.f(), rest,
464 static_cast<uint64_t>(divisor) << -one.e(), unit);
465 }
466 if (*kappa <= 0) {
467 // Don't bother doing the division below. (The compiler ought to
468 // figure this out itself, but it doesn't.)
469 break;
470 }
471 divisor /= 10;
472 --divisor_exponent;
473 }
474
475 // The integrals have been generated. We are at the point of the decimal
476 // separator. In the following loop we simply multiply the remaining digits by
477 // 10 and divide by one. We just need to pay attention to multiply associated
478 // data (like the interval or 'unit'), too.
479 // Note that the multiplication by 10 does not overflow, because w.e >= -60
480 // and thus one.e >= -60.
481 DCHECK_GE(one.e(), -60);
482 DCHECK(fractionals < one.f());
483 DCHECK(0xFFFF'FFFF'FFFF'FFFF / 10 >= one.f());
484 while (true) {
485 fractionals *= 10;
486 unit *= 10;
487 unsafe_interval.set_f(unsafe_interval.f() * 10);
488 // Integer division by one.
489 int digit = static_cast<int>(fractionals >> -one.e());
490 **outptr = '0' + digit;
491 (*outptr)++;
492 fractionals &= one.f() - 1; // Modulo by one.
493 (*kappa)--;
494 if (fractionals < unsafe_interval.f()) {
495 return RoundWeed(*outptr - 1, DiyFp::Minus(too_high, w).f() * unit,
496 unsafe_interval.f(), fractionals, one.f(), unit);
497 }
498 }
499}
500
501// Generates (at most) requested_digits of input number w.
502// w is a floating-point number (DiyFp), consisting of a significand and an
503// exponent. Its exponent is bounded by kMinimalTargetExponent and
504// kMaximalTargetExponent.
505// Hence -60 <= w.e() <= -32.
506//
507// Returns false if it fails, in which case the generated digits in the buffer
508// should not be used.
509// Preconditions:
510// * w is correct up to 1 ulp (unit in the last place). That
511// is, its error must be strictly less than a unit of its last digit.
512// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
513//
514// Postconditions: returns false if procedure fails.
515// otherwise:
516// * buffer is not null-terminated, but length contains the number of
517// digits.
518// * the representation in buffer is the most precise representation of
519// requested_digits digits.
520// * buffer contains at most requested_digits digits of w. If there are less
521// than requested_digits digits then some trailing '0's have been removed.
522// * kappa is such that
523// w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2.
524//
525// Remark: This procedure takes into account the imprecision of its input
526// numbers. If the precision is not enough to guarantee all the postconditions
527// then false is returned. This usually happens rarely, but the failure-rate
528// increases with higher requested_digits.
529static bool DigitGenCounted(DiyFp w, int requested_digits, Vector<char> buffer,
530 int* length, int* kappa) {
534 // w is assumed to have an error less than 1 unit. Whenever w is scaled we
535 // also scale its error.
536 uint64_t w_error = 1;
537 // We cut the input number into two parts: the integral digits and the
538 // fractional digits. We don't emit any decimal separator, but adapt kappa
539 // instead. Example: instead of writing "1.2" we put "12" into the buffer and
540 // increase kappa by 1.
541 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
542 // Division by one is a shift.
543 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
544 // Modulo by one is an and.
545 uint64_t fractionals = w.f() & (one.f() - 1);
546 uint32_t divisor;
547 unsigned divisor_exponent;
548 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), &divisor,
549 &divisor_exponent);
550 *kappa = divisor_exponent + 1;
551 *length = 0;
552
553 // Loop invariant: buffer = w / 10^kappa (integer division)
554 // The invariant holds for the first iteration: kappa has been initialized
555 // with the divisor exponent + 1. And the divisor is the biggest power of ten
556 // that is smaller than 'integrals'.
557 while (*kappa > 0) {
558 uint32_t digit = fast_divmod(&integrals, divisor, div[divisor_exponent]);
559 buffer[*length] = '0' + digit;
560 (*length)++;
561 requested_digits--;
562 (*kappa)--;
563 // Note that kappa now equals the exponent of the divisor and that the
564 // invariant thus holds again.
565 if (requested_digits == 0) break;
566 divisor /= 10;
567 --divisor_exponent;
568 }
569
570 if (requested_digits == 0) {
571 uint64_t rest =
572 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
573 return RoundWeedCounted(buffer, *length, rest,
574 static_cast<uint64_t>(divisor) << -one.e(), w_error,
575 kappa);
576 }
577
578 // The integrals have been generated. We are at the point of the decimal
579 // separator. In the following loop we simply multiply the remaining digits by
580 // 10 and divide by one. We just need to pay attention to multiply associated
581 // data (the 'unit'), too.
582 // Note that the multiplication by 10 does not overflow, because w.e >= -60
583 // and thus one.e >= -60.
584 DCHECK_GE(one.e(), -60);
585 DCHECK(fractionals < one.f());
586 DCHECK(0xFFFF'FFFF'FFFF'FFFF / 10 >= one.f());
587 while (requested_digits > 0 && fractionals > w_error) {
588 fractionals *= 10;
589 w_error *= 10;
590 // Integer division by one.
591 int digit = static_cast<int>(fractionals >> -one.e());
592 buffer[*length] = '0' + digit;
593 (*length)++;
594 requested_digits--;
595 fractionals &= one.f() - 1; // Modulo by one.
596 (*kappa)--;
597 }
598 if (requested_digits != 0) return false;
599 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
600 kappa);
601}
602
603// Provides a decimal representation of v.
604// Returns true if it succeeds, otherwise the result cannot be trusted.
605// There will be *length digits inside the buffer (not null-terminated).
606// If the function returns true then
607// v == (double) (buffer * 10^decimal_exponent).
608// The digits in the buffer are the shortest representation possible: no
609// 0.09999999999999999 instead of 0.1. The shorter representation will even be
610// chosen even if the longer one would be closer to v.
611// The last digit will be closest to the actual v. That is, even if several
612// digits might correctly yield 'v' when read again, the closest will be
613// computed.
614static bool Grisu3(double v, char** outptr, int* decimal_exponent) {
616 // boundary_minus and boundary_plus are the boundaries between v and its
617 // closest floating-point neighbors. Any number strictly between
618 // boundary_minus and boundary_plus will round to v when convert to a double.
619 // Grisu3 will never output representations that lie exactly on a boundary.
620 DiyFp boundary_minus, boundary_plus;
621 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
622 DCHECK(boundary_plus.e() == w.e());
623 DiyFp ten_mk; // Cached power of ten: 10^-k
624 int mk; // -k
625 int ten_mk_minimal_binary_exponent =
627 int ten_mk_maximal_binary_exponent =
630 ten_mk_minimal_binary_exponent, ten_mk_maximal_binary_exponent, &ten_mk,
631 &mk);
632 DCHECK(
634 w.e() + ten_mk.e() + DiyFp::kSignificandSize) &&
635 (kMaximalTargetExponent >= w.e() + ten_mk.e() + DiyFp::kSignificandSize));
636 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
637 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
638
639 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
640 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
641 // off by a small amount.
642 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
643 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
644 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
645 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
646 DCHECK(scaled_w.e() ==
647 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
648 // In theory it would be possible to avoid some recomputations by computing
649 // the difference between w and boundary_minus/plus (a power of 2) and to
650 // compute scaled_boundary_minus/plus by subtracting/adding from
651 // scaled_w. However the code becomes much less readable and the speed
652 // enhancements are not terriffic.
653 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
654 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk);
655
656 // DigitGen will generate the digits of scaled_w. Therefore we have
657 // v == (double) (scaled_w * 10^-mk).
658 // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
659 // integer than it will be updated. For instance if scaled_w == 1.23 then
660 // the buffer will be filled with "123" und the decimal_exponent will be
661 // decreased by 2.
662 int kappa;
663 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
664 outptr, &kappa);
665 *decimal_exponent = -mk + kappa;
666 return result;
667}
668
669// The "counted" version of grisu3 (see above) only generates requested_digits
670// number of digits. This version does not generate the shortest representation,
671// and with enough requested digits 0.1 will at some point print as 0.9999999...
672// Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
673// therefore the rounding strategy for halfway cases is irrelevant.
674static bool Grisu3Counted(double v, int requested_digits, Vector<char> buffer,
675 int* length, int* decimal_exponent) {
677 DiyFp ten_mk; // Cached power of ten: 10^-k
678 int mk; // -k
679 int ten_mk_minimal_binary_exponent =
681 int ten_mk_maximal_binary_exponent =
684 ten_mk_minimal_binary_exponent, ten_mk_maximal_binary_exponent, &ten_mk,
685 &mk);
686 DCHECK(
688 w.e() + ten_mk.e() + DiyFp::kSignificandSize) &&
689 (kMaximalTargetExponent >= w.e() + ten_mk.e() + DiyFp::kSignificandSize));
690 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
691 // 64 bit significand and ten_mk is thus only precise up to 64 bits.
692
693 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
694 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
695 // off by a small amount.
696 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
697 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
698 // (f-1) * 2^e < w*10^k < (f+1) * 2^e
699 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
700
701 // We now have (double) (scaled_w * 10^-mk).
702 // DigitGen will generate the first requested_digits digits of scaled_w and
703 // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
704 // will not always be exactly the same since DigitGenCounted only produces a
705 // limited number of digits.)
706 int kappa;
707 bool result =
708 DigitGenCounted(scaled_w, requested_digits, buffer, length, &kappa);
709 *decimal_exponent = -mk + kappa;
710 return result;
711}
712
713bool FastDtoa(double v, FastDtoaMode mode, int requested_digits,
714 Vector<char> buffer, int* length, int* decimal_point) {
715 DCHECK_GT(v, 0);
716 DCHECK(!Double(v).IsSpecial());
717
718 bool result = false;
719 char* outptr = buffer.data();
720 int decimal_exponent = 0;
721 switch (mode) {
723 result = Grisu3(v, &outptr, &decimal_exponent);
724 *length = static_cast<int>(outptr - buffer.data());
725 break;
726 case FAST_DTOA_PRECISION: {
727 int local_length = 0;
728 result = Grisu3Counted(v, requested_digits, buffer, &local_length,
729 &decimal_exponent);
730 *length = local_length;
731 break;
732 }
733 default:
734 UNREACHABLE();
735 }
736 if (result) {
737 *decimal_point = *length + decimal_exponent;
738 buffer[*length] = '\0';
739 }
740 return result;
741}
742
743} // namespace base
744} // namespace v8
#define one
static DiyFp Minus(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:40
static const int kSignificandSize
Definition diy-fp.h:22
static DiyFp Times(const DiyFp &a, const DiyFp &b)
Definition diy-fp.h:50
constexpr uint64_t f() const
Definition diy-fp.h:91
constexpr int e() const
Definition diy-fp.h:92
constexpr void set_f(uint64_t new_value)
Definition diy-fp.h:94
DiyFp AsNormalizedDiyFp() const
Definition double.h:49
void NormalizedBoundaries(DiyFp *out_m_minus, DiyFp *out_m_plus) const
Definition double.h:136
static void GetCachedPowerForBinaryExponentRange(int min_exponent, int max_exponent, DiyFp *power, int *decimal_exponent)
constexpr T * data() const
Definition vector.h:100
ZoneVector< RpoNumber > & result
static void BiggestPowerTen(uint32_t number, int number_bits, uint32_t *power, unsigned *exponent)
Definition fast-dtoa.cc:256
static bool Grisu3(double v, char **outptr, int *decimal_exponent)
Definition fast-dtoa.cc:614
static bool DigitGen(DiyFp low, DiyFp w, DiyFp high, char **outptr, int *kappa)
Definition fast-dtoa.cc:405
static const uint32_t kTen8
Definition fast-dtoa.cc:199
static const int kMaximalTargetExponent
Definition fast-dtoa.cc:24
static bool DigitGenCounted(DiyFp w, int requested_digits, Vector< char > buffer, int *length, int *kappa)
Definition fast-dtoa.cc:529
static bool RoundWeed(char *last_digit, uint64_t distance_too_high_w, uint64_t unsafe_interval, uint64_t rest, uint64_t ten_kappa, uint64_t unit)
Definition fast-dtoa.cc:41
static bool Grisu3Counted(double v, int requested_digits, Vector< char > buffer, int *length, int *decimal_exponent)
Definition fast-dtoa.cc:674
static bool RoundWeedCounted(Vector< char > buffer, int length, uint64_t rest, uint64_t ten_kappa, uint64_t unit, int *kappa)
Definition fast-dtoa.cc:155
static const int kMinimalTargetExponent
Definition fast-dtoa.cc:23
static const uint32_t kTen7
Definition fast-dtoa.cc:198
static const uint32_t kTen6
Definition fast-dtoa.cc:197
static const uint32_t kTen5
Definition fast-dtoa.cc:196
static uint32_t fast_divmod(uint32_t *val, uint32_t divisor, const DivMagic &d)
Definition fast-dtoa.cc:236
@ FAST_DTOA_SHORTEST
Definition fast-dtoa.h:17
@ FAST_DTOA_PRECISION
Definition fast-dtoa.h:20
static const uint32_t kTen4
Definition fast-dtoa.cc:195
bool FastDtoa(double v, FastDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
Definition fast-dtoa.cc:713
static const uint32_t kTen9
Definition fast-dtoa.cc:200
static const DivMagic div[]
Definition fast-dtoa.cc:210
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_GT(v1, v2)
Definition logging.h:487