mfbt/double-conversion/fast-dtoa.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/double-conversion/fast-dtoa.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,664 @@
     1.4 +// Copyright 2012 the V8 project authors. All rights reserved.
     1.5 +// Redistribution and use in source and binary forms, with or without
     1.6 +// modification, are permitted provided that the following conditions are
     1.7 +// met:
     1.8 +//
     1.9 +//     * Redistributions of source code must retain the above copyright
    1.10 +//       notice, this list of conditions and the following disclaimer.
    1.11 +//     * Redistributions in binary form must reproduce the above
    1.12 +//       copyright notice, this list of conditions and the following
    1.13 +//       disclaimer in the documentation and/or other materials provided
    1.14 +//       with the distribution.
    1.15 +//     * Neither the name of Google Inc. nor the names of its
    1.16 +//       contributors may be used to endorse or promote products derived
    1.17 +//       from this software without specific prior written permission.
    1.18 +//
    1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +
    1.31 +#include "fast-dtoa.h"
    1.32 +
    1.33 +#include "cached-powers.h"
    1.34 +#include "diy-fp.h"
    1.35 +#include "ieee.h"
    1.36 +
    1.37 +namespace double_conversion {
    1.38 +
    1.39 +// The minimal and maximal target exponent define the range of w's binary
    1.40 +// exponent, where 'w' is the result of multiplying the input by a cached power
    1.41 +// of ten.
    1.42 +//
    1.43 +// A different range might be chosen on a different platform, to optimize digit
    1.44 +// generation, but a smaller range requires more powers of ten to be cached.
    1.45 +static const int kMinimalTargetExponent = -60;
    1.46 +static const int kMaximalTargetExponent = -32;
    1.47 +
    1.48 +
    1.49 +// Adjusts the last digit of the generated number, and screens out generated
    1.50 +// solutions that may be inaccurate. A solution may be inaccurate if it is
    1.51 +// outside the safe interval, or if we cannot prove that it is closer to the
    1.52 +// input than a neighboring representation of the same length.
    1.53 +//
    1.54 +// Input: * buffer containing the digits of too_high / 10^kappa
    1.55 +//        * the buffer's length
    1.56 +//        * distance_too_high_w == (too_high - w).f() * unit
    1.57 +//        * unsafe_interval == (too_high - too_low).f() * unit
    1.58 +//        * rest = (too_high - buffer * 10^kappa).f() * unit
    1.59 +//        * ten_kappa = 10^kappa * unit
    1.60 +//        * unit = the common multiplier
    1.61 +// Output: returns true if the buffer is guaranteed to contain the closest
    1.62 +//    representable number to the input.
    1.63 +//  Modifies the generated digits in the buffer to approach (round towards) w.
    1.64 +static bool RoundWeed(Vector<char> buffer,
    1.65 +                      int length,
    1.66 +                      uint64_t distance_too_high_w,
    1.67 +                      uint64_t unsafe_interval,
    1.68 +                      uint64_t rest,
    1.69 +                      uint64_t ten_kappa,
    1.70 +                      uint64_t unit) {
    1.71 +  uint64_t small_distance = distance_too_high_w - unit;
    1.72 +  uint64_t big_distance = distance_too_high_w + unit;
    1.73 +  // Let w_low  = too_high - big_distance, and
    1.74 +  //     w_high = too_high - small_distance.
    1.75 +  // Note: w_low < w < w_high
    1.76 +  //
    1.77 +  // The real w (* unit) must lie somewhere inside the interval
    1.78 +  // ]w_low; w_high[ (often written as "(w_low; w_high)")
    1.79 +
    1.80 +  // Basically the buffer currently contains a number in the unsafe interval
    1.81 +  // ]too_low; too_high[ with too_low < w < too_high
    1.82 +  //
    1.83 +  //  too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    1.84 +  //                     ^v 1 unit            ^      ^                 ^      ^
    1.85 +  //  boundary_high ---------------------     .      .                 .      .
    1.86 +  //                     ^v 1 unit            .      .                 .      .
    1.87 +  //   - - - - - - - - - - - - - - - - - - -  +  - - + - - - - - -     .      .
    1.88 +  //                                          .      .         ^       .      .
    1.89 +  //                                          .  big_distance  .       .      .
    1.90 +  //                                          .      .         .       .    rest
    1.91 +  //                              small_distance     .         .       .      .
    1.92 +  //                                          v      .         .       .      .
    1.93 +  //  w_high - - - - - - - - - - - - - - - - - -     .         .       .      .
    1.94 +  //                     ^v 1 unit                   .         .       .      .
    1.95 +  //  w ----------------------------------------     .         .       .      .
    1.96 +  //                     ^v 1 unit                   v         .       .      .
    1.97 +  //  w_low  - - - - - - - - - - - - - - - - - - - - -         .       .      .
    1.98 +  //                                                           .       .      v
    1.99 +  //  buffer --------------------------------------------------+-------+--------
   1.100 +  //                                                           .       .
   1.101 +  //                                                  safe_interval    .
   1.102 +  //                                                           v       .
   1.103 +  //   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -     .
   1.104 +  //                     ^v 1 unit                                     .
   1.105 +  //  boundary_low -------------------------                     unsafe_interval
   1.106 +  //                     ^v 1 unit                                     v
   1.107 +  //  too_low  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   1.108 +  //
   1.109 +  //
   1.110 +  // Note that the value of buffer could lie anywhere inside the range too_low
   1.111 +  // to too_high.
   1.112 +  //
   1.113 +  // boundary_low, boundary_high and w are approximations of the real boundaries
   1.114 +  // and v (the input number). They are guaranteed to be precise up to one unit.
   1.115 +  // In fact the error is guaranteed to be strictly less than one unit.
   1.116 +  //
   1.117 +  // Anything that lies outside the unsafe interval is guaranteed not to round
   1.118 +  // to v when read again.
   1.119 +  // Anything that lies inside the safe interval is guaranteed to round to v
   1.120 +  // when read again.
   1.121 +  // If the number inside the buffer lies inside the unsafe interval but not
   1.122 +  // inside the safe interval then we simply do not know and bail out (returning
   1.123 +  // false).
   1.124 +  //
   1.125 +  // Similarly we have to take into account the imprecision of 'w' when finding
   1.126 +  // the closest representation of 'w'. If we have two potential
   1.127 +  // representations, and one is closer to both w_low and w_high, then we know
   1.128 +  // it is closer to the actual value v.
   1.129 +  //
   1.130 +  // By generating the digits of too_high we got the largest (closest to
   1.131 +  // too_high) buffer that is still in the unsafe interval. In the case where
   1.132 +  // w_high < buffer < too_high we try to decrement the buffer.
   1.133 +  // This way the buffer approaches (rounds towards) w.
   1.134 +  // There are 3 conditions that stop the decrementation process:
   1.135 +  //   1) the buffer is already below w_high
   1.136 +  //   2) decrementing the buffer would make it leave the unsafe interval
   1.137 +  //   3) decrementing the buffer would yield a number below w_high and farther
   1.138 +  //      away than the current number. In other words:
   1.139 +  //              (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
   1.140 +  // Instead of using the buffer directly we use its distance to too_high.
   1.141 +  // Conceptually rest ~= too_high - buffer
   1.142 +  // We need to do the following tests in this order to avoid over- and
   1.143 +  // underflows.
   1.144 +  ASSERT(rest <= unsafe_interval);
   1.145 +  while (rest < small_distance &&  // Negated condition 1
   1.146 +         unsafe_interval - rest >= ten_kappa &&  // Negated condition 2
   1.147 +         (rest + ten_kappa < small_distance ||  // buffer{-1} > w_high
   1.148 +          small_distance - rest >= rest + ten_kappa - small_distance)) {
   1.149 +    buffer[length - 1]--;
   1.150 +    rest += ten_kappa;
   1.151 +  }
   1.152 +
   1.153 +  // We have approached w+ as much as possible. We now test if approaching w-
   1.154 +  // would require changing the buffer. If yes, then we have two possible
   1.155 +  // representations close to w, but we cannot decide which one is closer.
   1.156 +  if (rest < big_distance &&
   1.157 +      unsafe_interval - rest >= ten_kappa &&
   1.158 +      (rest + ten_kappa < big_distance ||
   1.159 +       big_distance - rest > rest + ten_kappa - big_distance)) {
   1.160 +    return false;
   1.161 +  }
   1.162 +
   1.163 +  // Weeding test.
   1.164 +  //   The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
   1.165 +  //   Since too_low = too_high - unsafe_interval this is equivalent to
   1.166 +  //      [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
   1.167 +  //   Conceptually we have: rest ~= too_high - buffer
   1.168 +  return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
   1.169 +}
   1.170 +
   1.171 +
   1.172 +// Rounds the buffer upwards if the result is closer to v by possibly adding
   1.173 +// 1 to the buffer. If the precision of the calculation is not sufficient to
   1.174 +// round correctly, return false.
   1.175 +// The rounding might shift the whole buffer in which case the kappa is
   1.176 +// adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
   1.177 +//
   1.178 +// If 2*rest > ten_kappa then the buffer needs to be round up.
   1.179 +// rest can have an error of +/- 1 unit. This function accounts for the
   1.180 +// imprecision and returns false, if the rounding direction cannot be
   1.181 +// unambiguously determined.
   1.182 +//
   1.183 +// Precondition: rest < ten_kappa.
   1.184 +static bool RoundWeedCounted(Vector<char> buffer,
   1.185 +                             int length,
   1.186 +                             uint64_t rest,
   1.187 +                             uint64_t ten_kappa,
   1.188 +                             uint64_t unit,
   1.189 +                             int* kappa) {
   1.190 +  ASSERT(rest < ten_kappa);
   1.191 +  // The following tests are done in a specific order to avoid overflows. They
   1.192 +  // will work correctly with any uint64 values of rest < ten_kappa and unit.
   1.193 +  //
   1.194 +  // If the unit is too big, then we don't know which way to round. For example
   1.195 +  // a unit of 50 means that the real number lies within rest +/- 50. If
   1.196 +  // 10^kappa == 40 then there is no way to tell which way to round.
   1.197 +  if (unit >= ten_kappa) return false;
   1.198 +  // Even if unit is just half the size of 10^kappa we are already completely
   1.199 +  // lost. (And after the previous test we know that the expression will not
   1.200 +  // over/underflow.)
   1.201 +  if (ten_kappa - unit <= unit) return false;
   1.202 +  // If 2 * (rest + unit) <= 10^kappa we can safely round down.
   1.203 +  if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
   1.204 +    return true;
   1.205 +  }
   1.206 +  // If 2 * (rest - unit) >= 10^kappa, then we can safely round up.
   1.207 +  if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
   1.208 +    // Increment the last digit recursively until we find a non '9' digit.
   1.209 +    buffer[length - 1]++;
   1.210 +    for (int i = length - 1; i > 0; --i) {
   1.211 +      if (buffer[i] != '0' + 10) break;
   1.212 +      buffer[i] = '0';
   1.213 +      buffer[i - 1]++;
   1.214 +    }
   1.215 +    // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the
   1.216 +    // exception of the first digit all digits are now '0'. Simply switch the
   1.217 +    // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and
   1.218 +    // the power (the kappa) is increased.
   1.219 +    if (buffer[0] == '0' + 10) {
   1.220 +      buffer[0] = '1';
   1.221 +      (*kappa) += 1;
   1.222 +    }
   1.223 +    return true;
   1.224 +  }
   1.225 +  return false;
   1.226 +}
   1.227 +
   1.228 +// Returns the biggest power of ten that is less than or equal to the given
   1.229 +// number. We furthermore receive the maximum number of bits 'number' has.
   1.230 +//
   1.231 +// Returns power == 10^(exponent_plus_one-1) such that
   1.232 +//    power <= number < power * 10.
   1.233 +// If number_bits == 0 then 0^(0-1) is returned.
   1.234 +// The number of bits must be <= 32.
   1.235 +// Precondition: number < (1 << (number_bits + 1)).
   1.236 +
   1.237 +// Inspired by the method for finding an integer log base 10 from here:
   1.238 +// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
   1.239 +static unsigned int const kSmallPowersOfTen[] =
   1.240 +    {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
   1.241 +     1000000000};
   1.242 +
   1.243 +static void BiggestPowerTen(uint32_t number,
   1.244 +                            int number_bits,
   1.245 +                            uint32_t* power,
   1.246 +                            int* exponent_plus_one) {
   1.247 +  ASSERT(number < (1u << (number_bits + 1)));
   1.248 +  // 1233/4096 is approximately 1/lg(10).
   1.249 +  int exponent_plus_one_guess = ((number_bits + 1) * 1233 >> 12);
   1.250 +  // We increment to skip over the first entry in the kPowersOf10 table.
   1.251 +  // Note: kPowersOf10[i] == 10^(i-1).
   1.252 +  exponent_plus_one_guess++;
   1.253 +  // We don't have any guarantees that 2^number_bits <= number.
   1.254 +  // TODO(floitsch): can we change the 'while' into an 'if'? We definitely see
   1.255 +  // number < (2^number_bits - 1), but I haven't encountered
   1.256 +  // number < (2^number_bits - 2) yet.
   1.257 +  while (number < kSmallPowersOfTen[exponent_plus_one_guess]) {
   1.258 +    exponent_plus_one_guess--;
   1.259 +  }
   1.260 +  *power = kSmallPowersOfTen[exponent_plus_one_guess];
   1.261 +  *exponent_plus_one = exponent_plus_one_guess;
   1.262 +}
   1.263 +
   1.264 +// Generates the digits of input number w.
   1.265 +// w is a floating-point number (DiyFp), consisting of a significand and an
   1.266 +// exponent. Its exponent is bounded by kMinimalTargetExponent and
   1.267 +// kMaximalTargetExponent.
   1.268 +//       Hence -60 <= w.e() <= -32.
   1.269 +//
   1.270 +// Returns false if it fails, in which case the generated digits in the buffer
   1.271 +// should not be used.
   1.272 +// Preconditions:
   1.273 +//  * low, w and high are correct up to 1 ulp (unit in the last place). That
   1.274 +//    is, their error must be less than a unit of their last digits.
   1.275 +//  * low.e() == w.e() == high.e()
   1.276 +//  * low < w < high, and taking into account their error: low~ <= high~
   1.277 +//  * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
   1.278 +// Postconditions: returns false if procedure fails.
   1.279 +//   otherwise:
   1.280 +//     * buffer is not null-terminated, but len contains the number of digits.
   1.281 +//     * buffer contains the shortest possible decimal digit-sequence
   1.282 +//       such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
   1.283 +//       correct values of low and high (without their error).
   1.284 +//     * if more than one decimal representation gives the minimal number of
   1.285 +//       decimal digits then the one closest to W (where W is the correct value
   1.286 +//       of w) is chosen.
   1.287 +// Remark: this procedure takes into account the imprecision of its input
   1.288 +//   numbers. If the precision is not enough to guarantee all the postconditions
   1.289 +//   then false is returned. This usually happens rarely (~0.5%).
   1.290 +//
   1.291 +// Say, for the sake of example, that
   1.292 +//   w.e() == -48, and w.f() == 0x1234567890abcdef
   1.293 +// w's value can be computed by w.f() * 2^w.e()
   1.294 +// We can obtain w's integral digits by simply shifting w.f() by -w.e().
   1.295 +//  -> w's integral part is 0x1234
   1.296 +//  w's fractional part is therefore 0x567890abcdef.
   1.297 +// Printing w's integral part is easy (simply print 0x1234 in decimal).
   1.298 +// In order to print its fraction we repeatedly multiply the fraction by 10 and
   1.299 +// get each digit. Example the first digit after the point would be computed by
   1.300 +//   (0x567890abcdef * 10) >> 48. -> 3
   1.301 +// The whole thing becomes slightly more complicated because we want to stop
   1.302 +// once we have enough digits. That is, once the digits inside the buffer
   1.303 +// represent 'w' we can stop. Everything inside the interval low - high
   1.304 +// represents w. However we have to pay attention to low, high and w's
   1.305 +// imprecision.
   1.306 +static bool DigitGen(DiyFp low,
   1.307 +                     DiyFp w,
   1.308 +                     DiyFp high,
   1.309 +                     Vector<char> buffer,
   1.310 +                     int* length,
   1.311 +                     int* kappa) {
   1.312 +  ASSERT(low.e() == w.e() && w.e() == high.e());
   1.313 +  ASSERT(low.f() + 1 <= high.f() - 1);
   1.314 +  ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
   1.315 +  // low, w and high are imprecise, but by less than one ulp (unit in the last
   1.316 +  // place).
   1.317 +  // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
   1.318 +  // the new numbers are outside of the interval we want the final
   1.319 +  // representation to lie in.
   1.320 +  // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
   1.321 +  // numbers that are certain to lie in the interval. We will use this fact
   1.322 +  // later on.
   1.323 +  // We will now start by generating the digits within the uncertain
   1.324 +  // interval. Later we will weed out representations that lie outside the safe
   1.325 +  // interval and thus _might_ lie outside the correct interval.
   1.326 +  uint64_t unit = 1;
   1.327 +  DiyFp too_low = DiyFp(low.f() - unit, low.e());
   1.328 +  DiyFp too_high = DiyFp(high.f() + unit, high.e());
   1.329 +  // too_low and too_high are guaranteed to lie outside the interval we want the
   1.330 +  // generated number in.
   1.331 +  DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
   1.332 +  // We now cut the input number into two parts: the integral digits and the
   1.333 +  // fractionals. We will not write any decimal separator though, but adapt
   1.334 +  // kappa instead.
   1.335 +  // Reminder: we are currently computing the digits (stored inside the buffer)
   1.336 +  // such that:   too_low < buffer * 10^kappa < too_high
   1.337 +  // We use too_high for the digit_generation and stop as soon as possible.
   1.338 +  // If we stop early we effectively round down.
   1.339 +  DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
   1.340 +  // Division by one is a shift.
   1.341 +  uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
   1.342 +  // Modulo by one is an and.
   1.343 +  uint64_t fractionals = too_high.f() & (one.f() - 1);
   1.344 +  uint32_t divisor;
   1.345 +  int divisor_exponent_plus_one;
   1.346 +  BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
   1.347 +                  &divisor, &divisor_exponent_plus_one);
   1.348 +  *kappa = divisor_exponent_plus_one;
   1.349 +  *length = 0;
   1.350 +  // Loop invariant: buffer = too_high / 10^kappa  (integer division)
   1.351 +  // The invariant holds for the first iteration: kappa has been initialized
   1.352 +  // with the divisor exponent + 1. And the divisor is the biggest power of ten
   1.353 +  // that is smaller than integrals.
   1.354 +  while (*kappa > 0) {
   1.355 +    int digit = integrals / divisor;
   1.356 +    buffer[*length] = '0' + digit;
   1.357 +    (*length)++;
   1.358 +    integrals %= divisor;
   1.359 +    (*kappa)--;
   1.360 +    // Note that kappa now equals the exponent of the divisor and that the
   1.361 +    // invariant thus holds again.
   1.362 +    uint64_t rest =
   1.363 +        (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
   1.364 +    // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
   1.365 +    // Reminder: unsafe_interval.e() == one.e()
   1.366 +    if (rest < unsafe_interval.f()) {
   1.367 +      // Rounding down (by not emitting the remaining digits) yields a number
   1.368 +      // that lies within the unsafe interval.
   1.369 +      return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
   1.370 +                       unsafe_interval.f(), rest,
   1.371 +                       static_cast<uint64_t>(divisor) << -one.e(), unit);
   1.372 +    }
   1.373 +    divisor /= 10;
   1.374 +  }
   1.375 +
   1.376 +  // The integrals have been generated. We are at the point of the decimal
   1.377 +  // separator. In the following loop we simply multiply the remaining digits by
   1.378 +  // 10 and divide by one. We just need to pay attention to multiply associated
   1.379 +  // data (like the interval or 'unit'), too.
   1.380 +  // Note that the multiplication by 10 does not overflow, because w.e >= -60
   1.381 +  // and thus one.e >= -60.
   1.382 +  ASSERT(one.e() >= -60);
   1.383 +  ASSERT(fractionals < one.f());
   1.384 +  ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
   1.385 +  while (true) {
   1.386 +    fractionals *= 10;
   1.387 +    unit *= 10;
   1.388 +    unsafe_interval.set_f(unsafe_interval.f() * 10);
   1.389 +    // Integer division by one.
   1.390 +    int digit = static_cast<int>(fractionals >> -one.e());
   1.391 +    buffer[*length] = '0' + digit;
   1.392 +    (*length)++;
   1.393 +    fractionals &= one.f() - 1;  // Modulo by one.
   1.394 +    (*kappa)--;
   1.395 +    if (fractionals < unsafe_interval.f()) {
   1.396 +      return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
   1.397 +                       unsafe_interval.f(), fractionals, one.f(), unit);
   1.398 +    }
   1.399 +  }
   1.400 +}
   1.401 +
   1.402 +
   1.403 +
   1.404 +// Generates (at most) requested_digits digits of input number w.
   1.405 +// w is a floating-point number (DiyFp), consisting of a significand and an
   1.406 +// exponent. Its exponent is bounded by kMinimalTargetExponent and
   1.407 +// kMaximalTargetExponent.
   1.408 +//       Hence -60 <= w.e() <= -32.
   1.409 +//
   1.410 +// Returns false if it fails, in which case the generated digits in the buffer
   1.411 +// should not be used.
   1.412 +// Preconditions:
   1.413 +//  * w is correct up to 1 ulp (unit in the last place). That
   1.414 +//    is, its error must be strictly less than a unit of its last digit.
   1.415 +//  * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
   1.416 +//
   1.417 +// Postconditions: returns false if procedure fails.
   1.418 +//   otherwise:
   1.419 +//     * buffer is not null-terminated, but length contains the number of
   1.420 +//       digits.
   1.421 +//     * the representation in buffer is the most precise representation of
   1.422 +//       requested_digits digits.
   1.423 +//     * buffer contains at most requested_digits digits of w. If there are less
   1.424 +//       than requested_digits digits then some trailing '0's have been removed.
   1.425 +//     * kappa is such that
   1.426 +//            w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2.
   1.427 +//
   1.428 +// Remark: This procedure takes into account the imprecision of its input
   1.429 +//   numbers. If the precision is not enough to guarantee all the postconditions
   1.430 +//   then false is returned. This usually happens rarely, but the failure-rate
   1.431 +//   increases with higher requested_digits.
   1.432 +static bool DigitGenCounted(DiyFp w,
   1.433 +                            int requested_digits,
   1.434 +                            Vector<char> buffer,
   1.435 +                            int* length,
   1.436 +                            int* kappa) {
   1.437 +  ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
   1.438 +  ASSERT(kMinimalTargetExponent >= -60);
   1.439 +  ASSERT(kMaximalTargetExponent <= -32);
   1.440 +  // w is assumed to have an error less than 1 unit. Whenever w is scaled we
   1.441 +  // also scale its error.
   1.442 +  uint64_t w_error = 1;
   1.443 +  // We cut the input number into two parts: the integral digits and the
   1.444 +  // fractional digits. We don't emit any decimal separator, but adapt kappa
   1.445 +  // instead. Example: instead of writing "1.2" we put "12" into the buffer and
   1.446 +  // increase kappa by 1.
   1.447 +  DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
   1.448 +  // Division by one is a shift.
   1.449 +  uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
   1.450 +  // Modulo by one is an and.
   1.451 +  uint64_t fractionals = w.f() & (one.f() - 1);
   1.452 +  uint32_t divisor;
   1.453 +  int divisor_exponent_plus_one;
   1.454 +  BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
   1.455 +                  &divisor, &divisor_exponent_plus_one);
   1.456 +  *kappa = divisor_exponent_plus_one;
   1.457 +  *length = 0;
   1.458 +
   1.459 +  // Loop invariant: buffer = w / 10^kappa  (integer division)
   1.460 +  // The invariant holds for the first iteration: kappa has been initialized
   1.461 +  // with the divisor exponent + 1. And the divisor is the biggest power of ten
   1.462 +  // that is smaller than 'integrals'.
   1.463 +  while (*kappa > 0) {
   1.464 +    int digit = integrals / divisor;
   1.465 +    buffer[*length] = '0' + digit;
   1.466 +    (*length)++;
   1.467 +    requested_digits--;
   1.468 +    integrals %= divisor;
   1.469 +    (*kappa)--;
   1.470 +    // Note that kappa now equals the exponent of the divisor and that the
   1.471 +    // invariant thus holds again.
   1.472 +    if (requested_digits == 0) break;
   1.473 +    divisor /= 10;
   1.474 +  }
   1.475 +
   1.476 +  if (requested_digits == 0) {
   1.477 +    uint64_t rest =
   1.478 +        (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
   1.479 +    return RoundWeedCounted(buffer, *length, rest,
   1.480 +                            static_cast<uint64_t>(divisor) << -one.e(), w_error,
   1.481 +                            kappa);
   1.482 +  }
   1.483 +
   1.484 +  // The integrals have been generated. We are at the point of the decimal
   1.485 +  // separator. In the following loop we simply multiply the remaining digits by
   1.486 +  // 10 and divide by one. We just need to pay attention to multiply associated
   1.487 +  // data (the 'unit'), too.
   1.488 +  // Note that the multiplication by 10 does not overflow, because w.e >= -60
   1.489 +  // and thus one.e >= -60.
   1.490 +  ASSERT(one.e() >= -60);
   1.491 +  ASSERT(fractionals < one.f());
   1.492 +  ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
   1.493 +  while (requested_digits > 0 && fractionals > w_error) {
   1.494 +    fractionals *= 10;
   1.495 +    w_error *= 10;
   1.496 +    // Integer division by one.
   1.497 +    int digit = static_cast<int>(fractionals >> -one.e());
   1.498 +    buffer[*length] = '0' + digit;
   1.499 +    (*length)++;
   1.500 +    requested_digits--;
   1.501 +    fractionals &= one.f() - 1;  // Modulo by one.
   1.502 +    (*kappa)--;
   1.503 +  }
   1.504 +  if (requested_digits != 0) return false;
   1.505 +  return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
   1.506 +                          kappa);
   1.507 +}
   1.508 +
   1.509 +
   1.510 +// Provides a decimal representation of v.
   1.511 +// Returns true if it succeeds, otherwise the result cannot be trusted.
   1.512 +// There will be *length digits inside the buffer (not null-terminated).
   1.513 +// If the function returns true then
   1.514 +//        v == (double) (buffer * 10^decimal_exponent).
   1.515 +// The digits in the buffer are the shortest representation possible: no
   1.516 +// 0.09999999999999999 instead of 0.1. The shorter representation will even be
   1.517 +// chosen even if the longer one would be closer to v.
   1.518 +// The last digit will be closest to the actual v. That is, even if several
   1.519 +// digits might correctly yield 'v' when read again, the closest will be
   1.520 +// computed.
   1.521 +static bool Grisu3(double v,
   1.522 +                   FastDtoaMode mode,
   1.523 +                   Vector<char> buffer,
   1.524 +                   int* length,
   1.525 +                   int* decimal_exponent) {
   1.526 +  DiyFp w = Double(v).AsNormalizedDiyFp();
   1.527 +  // boundary_minus and boundary_plus are the boundaries between v and its
   1.528 +  // closest floating-point neighbors. Any number strictly between
   1.529 +  // boundary_minus and boundary_plus will round to v when convert to a double.
   1.530 +  // Grisu3 will never output representations that lie exactly on a boundary.
   1.531 +  DiyFp boundary_minus, boundary_plus;
   1.532 +  if (mode == FAST_DTOA_SHORTEST) {
   1.533 +    Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
   1.534 +  } else {
   1.535 +    ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
   1.536 +    float single_v = static_cast<float>(v);
   1.537 +    Single(single_v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
   1.538 +  }
   1.539 +  ASSERT(boundary_plus.e() == w.e());
   1.540 +  DiyFp ten_mk;  // Cached power of ten: 10^-k
   1.541 +  int mk;        // -k
   1.542 +  int ten_mk_minimal_binary_exponent =
   1.543 +     kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   1.544 +  int ten_mk_maximal_binary_exponent =
   1.545 +     kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   1.546 +  PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
   1.547 +      ten_mk_minimal_binary_exponent,
   1.548 +      ten_mk_maximal_binary_exponent,
   1.549 +      &ten_mk, &mk);
   1.550 +  ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
   1.551 +          DiyFp::kSignificandSize) &&
   1.552 +         (kMaximalTargetExponent >= w.e() + ten_mk.e() +
   1.553 +          DiyFp::kSignificandSize));
   1.554 +  // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
   1.555 +  // 64 bit significand and ten_mk is thus only precise up to 64 bits.
   1.556 +
   1.557 +  // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
   1.558 +  // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
   1.559 +  // off by a small amount.
   1.560 +  // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
   1.561 +  // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
   1.562 +  //           (f-1) * 2^e < w*10^k < (f+1) * 2^e
   1.563 +  DiyFp scaled_w = DiyFp::Times(w, ten_mk);
   1.564 +  ASSERT(scaled_w.e() ==
   1.565 +         boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
   1.566 +  // In theory it would be possible to avoid some recomputations by computing
   1.567 +  // the difference between w and boundary_minus/plus (a power of 2) and to
   1.568 +  // compute scaled_boundary_minus/plus by subtracting/adding from
   1.569 +  // scaled_w. However the code becomes much less readable and the speed
   1.570 +  // enhancements are not terriffic.
   1.571 +  DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
   1.572 +  DiyFp scaled_boundary_plus  = DiyFp::Times(boundary_plus,  ten_mk);
   1.573 +
   1.574 +  // DigitGen will generate the digits of scaled_w. Therefore we have
   1.575 +  // v == (double) (scaled_w * 10^-mk).
   1.576 +  // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
   1.577 +  // integer than it will be updated. For instance if scaled_w == 1.23 then
   1.578 +  // the buffer will be filled with "123" und the decimal_exponent will be
   1.579 +  // decreased by 2.
   1.580 +  int kappa;
   1.581 +  bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
   1.582 +                         buffer, length, &kappa);
   1.583 +  *decimal_exponent = -mk + kappa;
   1.584 +  return result;
   1.585 +}
   1.586 +
   1.587 +
   1.588 +// The "counted" version of grisu3 (see above) only generates requested_digits
   1.589 +// number of digits. This version does not generate the shortest representation,
   1.590 +// and with enough requested digits 0.1 will at some point print as 0.9999999...
   1.591 +// Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
   1.592 +// therefore the rounding strategy for halfway cases is irrelevant.
   1.593 +static bool Grisu3Counted(double v,
   1.594 +                          int requested_digits,
   1.595 +                          Vector<char> buffer,
   1.596 +                          int* length,
   1.597 +                          int* decimal_exponent) {
   1.598 +  DiyFp w = Double(v).AsNormalizedDiyFp();
   1.599 +  DiyFp ten_mk;  // Cached power of ten: 10^-k
   1.600 +  int mk;        // -k
   1.601 +  int ten_mk_minimal_binary_exponent =
   1.602 +     kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   1.603 +  int ten_mk_maximal_binary_exponent =
   1.604 +     kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize);
   1.605 +  PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
   1.606 +      ten_mk_minimal_binary_exponent,
   1.607 +      ten_mk_maximal_binary_exponent,
   1.608 +      &ten_mk, &mk);
   1.609 +  ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
   1.610 +          DiyFp::kSignificandSize) &&
   1.611 +         (kMaximalTargetExponent >= w.e() + ten_mk.e() +
   1.612 +          DiyFp::kSignificandSize));
   1.613 +  // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
   1.614 +  // 64 bit significand and ten_mk is thus only precise up to 64 bits.
   1.615 +
   1.616 +  // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
   1.617 +  // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
   1.618 +  // off by a small amount.
   1.619 +  // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
   1.620 +  // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
   1.621 +  //           (f-1) * 2^e < w*10^k < (f+1) * 2^e
   1.622 +  DiyFp scaled_w = DiyFp::Times(w, ten_mk);
   1.623 +
   1.624 +  // We now have (double) (scaled_w * 10^-mk).
   1.625 +  // DigitGen will generate the first requested_digits digits of scaled_w and
   1.626 +  // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
   1.627 +  // will not always be exactly the same since DigitGenCounted only produces a
   1.628 +  // limited number of digits.)
   1.629 +  int kappa;
   1.630 +  bool result = DigitGenCounted(scaled_w, requested_digits,
   1.631 +                                buffer, length, &kappa);
   1.632 +  *decimal_exponent = -mk + kappa;
   1.633 +  return result;
   1.634 +}
   1.635 +
   1.636 +
   1.637 +bool FastDtoa(double v,
   1.638 +              FastDtoaMode mode,
   1.639 +              int requested_digits,
   1.640 +              Vector<char> buffer,
   1.641 +              int* length,
   1.642 +              int* decimal_point) {
   1.643 +  ASSERT(v > 0);
   1.644 +  ASSERT(!Double(v).IsSpecial());
   1.645 +
   1.646 +  bool result = false;
   1.647 +  int decimal_exponent = 0;
   1.648 +  switch (mode) {
   1.649 +    case FAST_DTOA_SHORTEST:
   1.650 +    case FAST_DTOA_SHORTEST_SINGLE:
   1.651 +      result = Grisu3(v, mode, buffer, length, &decimal_exponent);
   1.652 +      break;
   1.653 +    case FAST_DTOA_PRECISION:
   1.654 +      result = Grisu3Counted(v, requested_digits,
   1.655 +                             buffer, length, &decimal_exponent);
   1.656 +      break;
   1.657 +    default:
   1.658 +      UNREACHABLE();
   1.659 +  }
   1.660 +  if (result) {
   1.661 +    *decimal_point = *length + decimal_exponent;
   1.662 +    buffer[*length] = '\0';
   1.663 +  }
   1.664 +  return result;
   1.665 +}
   1.666 +
   1.667 +}  // namespace double_conversion

mercurial