6 #include "src/base/logging.h" 9 #include "src/fast-dtoa.h" 11 #include "src/cached-powers.h" 12 #include "src/diy-fp.h" 13 #include "src/double.h" 24 static const int kMinimalTargetExponent = -60;
25 static const int kMaximalTargetExponent = -32;
43 static bool RoundWeed(Vector<char> buffer,
45 uint64_t distance_too_high_w,
46 uint64_t unsafe_interval,
50 uint64_t small_distance = distance_too_high_w - unit;
51 uint64_t big_distance = distance_too_high_w + unit;
123 DCHECK(rest <= unsafe_interval);
124 while (rest < small_distance &&
125 unsafe_interval - rest >= ten_kappa &&
126 (rest + ten_kappa < small_distance ||
127 small_distance - rest >= rest + ten_kappa - small_distance)) {
128 buffer[length - 1]--;
135 if (rest < big_distance &&
136 unsafe_interval - rest >= ten_kappa &&
137 (rest + ten_kappa < big_distance ||
138 big_distance - rest > rest + ten_kappa - big_distance)) {
147 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
163 static bool RoundWeedCounted(Vector<char> buffer,
169 DCHECK(rest < ten_kappa);
176 if (unit >= ten_kappa)
return false;
180 if (ten_kappa - unit <= unit)
return false;
182 if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
186 if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
188 buffer[length - 1]++;
189 for (
int i = length - 1;
i > 0; --
i) {
190 if (buffer[
i] !=
'0' + 10)
break;
198 if (buffer[0] ==
'0' + 10) {
208 static const uint32_t kTen4 = 10000;
209 static const uint32_t kTen5 = 100000;
210 static const uint32_t kTen6 = 1000000;
211 static const uint32_t kTen7 = 10000000;
212 static const uint32_t kTen8 = 100000000;
213 static const uint32_t kTen9 = 1000000000;
220 static void BiggestPowerTen(
uint32_t number,
224 switch (number_bits) {
228 if (kTen9 <= number) {
237 if (kTen8 <= number) {
246 if (kTen7 <= number) {
256 if (kTen6 <= number) {
265 if (kTen5 <= number) {
274 if (kTen4 <= number) {
284 if (1000 <= number) {
371 static bool DigitGen(DiyFp low,
377 DCHECK(low.e() == w.e() && w.e() == high.e());
378 DCHECK(low.f() + 1 <= high.f() - 1);
379 DCHECK(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
392 DiyFp too_low = DiyFp(low.f() - unit, low.e());
393 DiyFp too_high = DiyFp(high.f() + unit, high.e());
396 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
404 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
408 uint64_t fractionals = too_high.f() & (one.f() - 1);
410 int divisor_exponent;
411 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
412 &divisor, &divisor_exponent);
413 *kappa = divisor_exponent + 1;
420 int digit = integrals / divisor;
421 buffer[*length] =
'0' + digit;
423 integrals %= divisor;
428 (
static_cast<uint64_t
>(integrals) << -one.e()) + fractionals;
431 if (rest < unsafe_interval.f()) {
434 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
435 unsafe_interval.f(), rest,
436 static_cast<uint64_t
>(divisor) << -one.e(), unit);
447 DCHECK_GE(one.e(), -60);
448 DCHECK(fractionals < one.f());
449 DCHECK(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
453 unsafe_interval.set_f(unsafe_interval.f() * 10);
455 int digit =
static_cast<int>(fractionals >> -one.e());
456 buffer[*length] =
'0' + digit;
458 fractionals &= one.f() - 1;
460 if (fractionals < unsafe_interval.f()) {
461 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
462 unsafe_interval.f(), fractionals, one.f(), unit);
497 static bool DigitGenCounted(DiyFp w,
498 int requested_digits,
502 DCHECK(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
503 DCHECK_GE(kMinimalTargetExponent, -60);
504 DCHECK_LE(kMaximalTargetExponent, -32);
507 uint64_t w_error = 1;
512 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
516 uint64_t fractionals = w.f() & (one.f() - 1);
518 int divisor_exponent;
519 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
520 &divisor, &divisor_exponent);
521 *kappa = divisor_exponent + 1;
529 int digit = integrals / divisor;
530 buffer[*length] =
'0' + digit;
533 integrals %= divisor;
537 if (requested_digits == 0)
break;
541 if (requested_digits == 0) {
543 (
static_cast<uint64_t
>(integrals) << -one.e()) + fractionals;
544 return RoundWeedCounted(buffer, *length, rest,
545 static_cast<uint64_t>(divisor) << -one.e(), w_error,
555 DCHECK_GE(one.e(), -60);
556 DCHECK(fractionals < one.f());
557 DCHECK(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
558 while (requested_digits > 0 && fractionals > w_error) {
562 int digit =
static_cast<int>(fractionals >> -one.e());
563 buffer[*length] =
'0' + digit;
566 fractionals &= one.f() - 1;
569 if (requested_digits != 0)
return false;
570 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
586 static bool Grisu3(
double v,
589 int* decimal_exponent) {
590 DiyFp w = Double(v).AsNormalizedDiyFp();
595 DiyFp boundary_minus, boundary_plus;
596 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
597 DCHECK(boundary_plus.e() == w.e());
600 int ten_mk_minimal_binary_exponent =
601 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
602 int ten_mk_maximal_binary_exponent =
603 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
604 PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
605 ten_mk_minimal_binary_exponent,
606 ten_mk_maximal_binary_exponent,
608 DCHECK((kMinimalTargetExponent <= w.e() + ten_mk.e() +
609 DiyFp::kSignificandSize) &&
610 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
611 DiyFp::kSignificandSize));
621 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
622 DCHECK(scaled_w.e() ==
623 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
629 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
630 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk);
639 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
640 buffer, length, &kappa);
641 *decimal_exponent = -mk + kappa;
651 static bool Grisu3Counted(
double v,
652 int requested_digits,
655 int* decimal_exponent) {
656 DiyFp w = Double(v).AsNormalizedDiyFp();
659 int ten_mk_minimal_binary_exponent =
660 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
661 int ten_mk_maximal_binary_exponent =
662 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
663 PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
664 ten_mk_minimal_binary_exponent,
665 ten_mk_maximal_binary_exponent,
667 DCHECK((kMinimalTargetExponent <= w.e() + ten_mk.e() +
668 DiyFp::kSignificandSize) &&
669 (kMaximalTargetExponent >= w.e() + ten_mk.e() +
670 DiyFp::kSignificandSize));
680 DiyFp scaled_w = DiyFp::Times(w, ten_mk);
688 bool result = DigitGenCounted(scaled_w, requested_digits,
689 buffer, length, &kappa);
690 *decimal_exponent = -mk + kappa;
695 bool FastDtoa(
double v,
697 int requested_digits,
700 int* decimal_point) {
702 DCHECK(!Double(v).IsSpecial());
705 int decimal_exponent = 0;
707 case FAST_DTOA_SHORTEST:
708 result = Grisu3(v, buffer, length, &decimal_exponent);
710 case FAST_DTOA_PRECISION:
711 result = Grisu3Counted(v, requested_digits,
712 buffer, length, &decimal_exponent);
718 *decimal_point = *length + decimal_exponent;
719 buffer[*length] =
'\0';