mfbt/double-conversion/bignum-dtoa.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright 2010 the V8 project authors. All rights reserved.
michael@0 2 // Redistribution and use in source and binary forms, with or without
michael@0 3 // modification, are permitted provided that the following conditions are
michael@0 4 // met:
michael@0 5 //
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above
michael@0 9 // copyright notice, this list of conditions and the following
michael@0 10 // disclaimer in the documentation and/or other materials provided
michael@0 11 // with the distribution.
michael@0 12 // * Neither the name of Google Inc. nor the names of its
michael@0 13 // contributors may be used to endorse or promote products derived
michael@0 14 // from this software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27
michael@0 28 #include <math.h>
michael@0 29
michael@0 30 #include "bignum-dtoa.h"
michael@0 31
michael@0 32 #include "bignum.h"
michael@0 33 #include "ieee.h"
michael@0 34
michael@0 35 namespace double_conversion {
michael@0 36
michael@0 37 static int NormalizedExponent(uint64_t significand, int exponent) {
michael@0 38 ASSERT(significand != 0);
michael@0 39 while ((significand & Double::kHiddenBit) == 0) {
michael@0 40 significand = significand << 1;
michael@0 41 exponent = exponent - 1;
michael@0 42 }
michael@0 43 return exponent;
michael@0 44 }
michael@0 45
michael@0 46
michael@0 47 // Forward declarations:
michael@0 48 // Returns an estimation of k such that 10^(k-1) <= v < 10^k.
michael@0 49 static int EstimatePower(int exponent);
michael@0 50 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
michael@0 51 // and denominator.
michael@0 52 static void InitialScaledStartValues(uint64_t significand,
michael@0 53 int exponent,
michael@0 54 bool lower_boundary_is_closer,
michael@0 55 int estimated_power,
michael@0 56 bool need_boundary_deltas,
michael@0 57 Bignum* numerator,
michael@0 58 Bignum* denominator,
michael@0 59 Bignum* delta_minus,
michael@0 60 Bignum* delta_plus);
michael@0 61 // Multiplies numerator/denominator so that its values lies in the range 1-10.
michael@0 62 // Returns decimal_point s.t.
michael@0 63 // v = numerator'/denominator' * 10^(decimal_point-1)
michael@0 64 // where numerator' and denominator' are the values of numerator and
michael@0 65 // denominator after the call to this function.
michael@0 66 static void FixupMultiply10(int estimated_power, bool is_even,
michael@0 67 int* decimal_point,
michael@0 68 Bignum* numerator, Bignum* denominator,
michael@0 69 Bignum* delta_minus, Bignum* delta_plus);
michael@0 70 // Generates digits from the left to the right and stops when the generated
michael@0 71 // digits yield the shortest decimal representation of v.
michael@0 72 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
michael@0 73 Bignum* delta_minus, Bignum* delta_plus,
michael@0 74 bool is_even,
michael@0 75 Vector<char> buffer, int* length);
michael@0 76 // Generates 'requested_digits' after the decimal point.
michael@0 77 static void BignumToFixed(int requested_digits, int* decimal_point,
michael@0 78 Bignum* numerator, Bignum* denominator,
michael@0 79 Vector<char>(buffer), int* length);
michael@0 80 // Generates 'count' digits of numerator/denominator.
michael@0 81 // Once 'count' digits have been produced rounds the result depending on the
michael@0 82 // remainder (remainders of exactly .5 round upwards). Might update the
michael@0 83 // decimal_point when rounding up (for example for 0.9999).
michael@0 84 static void GenerateCountedDigits(int count, int* decimal_point,
michael@0 85 Bignum* numerator, Bignum* denominator,
michael@0 86 Vector<char>(buffer), int* length);
michael@0 87
michael@0 88
michael@0 89 void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
michael@0 90 Vector<char> buffer, int* length, int* decimal_point) {
michael@0 91 ASSERT(v > 0);
michael@0 92 ASSERT(!Double(v).IsSpecial());
michael@0 93 uint64_t significand;
michael@0 94 int exponent;
michael@0 95 bool lower_boundary_is_closer;
michael@0 96 if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) {
michael@0 97 float f = static_cast<float>(v);
michael@0 98 ASSERT(f == v);
michael@0 99 significand = Single(f).Significand();
michael@0 100 exponent = Single(f).Exponent();
michael@0 101 lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser();
michael@0 102 } else {
michael@0 103 significand = Double(v).Significand();
michael@0 104 exponent = Double(v).Exponent();
michael@0 105 lower_boundary_is_closer = Double(v).LowerBoundaryIsCloser();
michael@0 106 }
michael@0 107 bool need_boundary_deltas =
michael@0 108 (mode == BIGNUM_DTOA_SHORTEST || mode == BIGNUM_DTOA_SHORTEST_SINGLE);
michael@0 109
michael@0 110 bool is_even = (significand & 1) == 0;
michael@0 111 int normalized_exponent = NormalizedExponent(significand, exponent);
michael@0 112 // estimated_power might be too low by 1.
michael@0 113 int estimated_power = EstimatePower(normalized_exponent);
michael@0 114
michael@0 115 // Shortcut for Fixed.
michael@0 116 // The requested digits correspond to the digits after the point. If the
michael@0 117 // number is much too small, then there is no need in trying to get any
michael@0 118 // digits.
michael@0 119 if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) {
michael@0 120 buffer[0] = '\0';
michael@0 121 *length = 0;
michael@0 122 // Set decimal-point to -requested_digits. This is what Gay does.
michael@0 123 // Note that it should not have any effect anyways since the string is
michael@0 124 // empty.
michael@0 125 *decimal_point = -requested_digits;
michael@0 126 return;
michael@0 127 }
michael@0 128
michael@0 129 Bignum numerator;
michael@0 130 Bignum denominator;
michael@0 131 Bignum delta_minus;
michael@0 132 Bignum delta_plus;
michael@0 133 // Make sure the bignum can grow large enough. The smallest double equals
michael@0 134 // 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
michael@0 135 // The maximum double is 1.7976931348623157e308 which needs fewer than
michael@0 136 // 308*4 binary digits.
michael@0 137 ASSERT(Bignum::kMaxSignificantBits >= 324*4);
michael@0 138 InitialScaledStartValues(significand, exponent, lower_boundary_is_closer,
michael@0 139 estimated_power, need_boundary_deltas,
michael@0 140 &numerator, &denominator,
michael@0 141 &delta_minus, &delta_plus);
michael@0 142 // We now have v = (numerator / denominator) * 10^estimated_power.
michael@0 143 FixupMultiply10(estimated_power, is_even, decimal_point,
michael@0 144 &numerator, &denominator,
michael@0 145 &delta_minus, &delta_plus);
michael@0 146 // We now have v = (numerator / denominator) * 10^(decimal_point-1), and
michael@0 147 // 1 <= (numerator + delta_plus) / denominator < 10
michael@0 148 switch (mode) {
michael@0 149 case BIGNUM_DTOA_SHORTEST:
michael@0 150 case BIGNUM_DTOA_SHORTEST_SINGLE:
michael@0 151 GenerateShortestDigits(&numerator, &denominator,
michael@0 152 &delta_minus, &delta_plus,
michael@0 153 is_even, buffer, length);
michael@0 154 break;
michael@0 155 case BIGNUM_DTOA_FIXED:
michael@0 156 BignumToFixed(requested_digits, decimal_point,
michael@0 157 &numerator, &denominator,
michael@0 158 buffer, length);
michael@0 159 break;
michael@0 160 case BIGNUM_DTOA_PRECISION:
michael@0 161 GenerateCountedDigits(requested_digits, decimal_point,
michael@0 162 &numerator, &denominator,
michael@0 163 buffer, length);
michael@0 164 break;
michael@0 165 default:
michael@0 166 UNREACHABLE();
michael@0 167 }
michael@0 168 buffer[*length] = '\0';
michael@0 169 }
michael@0 170
michael@0 171
michael@0 172 // The procedure starts generating digits from the left to the right and stops
michael@0 173 // when the generated digits yield the shortest decimal representation of v. A
michael@0 174 // decimal representation of v is a number lying closer to v than to any other
michael@0 175 // double, so it converts to v when read.
michael@0 176 //
michael@0 177 // This is true if d, the decimal representation, is between m- and m+, the
michael@0 178 // upper and lower boundaries. d must be strictly between them if !is_even.
michael@0 179 // m- := (numerator - delta_minus) / denominator
michael@0 180 // m+ := (numerator + delta_plus) / denominator
michael@0 181 //
michael@0 182 // Precondition: 0 <= (numerator+delta_plus) / denominator < 10.
michael@0 183 // If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit
michael@0 184 // will be produced. This should be the standard precondition.
michael@0 185 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
michael@0 186 Bignum* delta_minus, Bignum* delta_plus,
michael@0 187 bool is_even,
michael@0 188 Vector<char> buffer, int* length) {
michael@0 189 // Small optimization: if delta_minus and delta_plus are the same just reuse
michael@0 190 // one of the two bignums.
michael@0 191 if (Bignum::Equal(*delta_minus, *delta_plus)) {
michael@0 192 delta_plus = delta_minus;
michael@0 193 }
michael@0 194 *length = 0;
michael@0 195 while (true) {
michael@0 196 uint16_t digit;
michael@0 197 digit = numerator->DivideModuloIntBignum(*denominator);
michael@0 198 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
michael@0 199 // digit = numerator / denominator (integer division).
michael@0 200 // numerator = numerator % denominator.
michael@0 201 buffer[(*length)++] = digit + '0';
michael@0 202
michael@0 203 // Can we stop already?
michael@0 204 // If the remainder of the division is less than the distance to the lower
michael@0 205 // boundary we can stop. In this case we simply round down (discarding the
michael@0 206 // remainder).
michael@0 207 // Similarly we test if we can round up (using the upper boundary).
michael@0 208 bool in_delta_room_minus;
michael@0 209 bool in_delta_room_plus;
michael@0 210 if (is_even) {
michael@0 211 in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus);
michael@0 212 } else {
michael@0 213 in_delta_room_minus = Bignum::Less(*numerator, *delta_minus);
michael@0 214 }
michael@0 215 if (is_even) {
michael@0 216 in_delta_room_plus =
michael@0 217 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
michael@0 218 } else {
michael@0 219 in_delta_room_plus =
michael@0 220 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
michael@0 221 }
michael@0 222 if (!in_delta_room_minus && !in_delta_room_plus) {
michael@0 223 // Prepare for next iteration.
michael@0 224 numerator->Times10();
michael@0 225 delta_minus->Times10();
michael@0 226 // We optimized delta_plus to be equal to delta_minus (if they share the
michael@0 227 // same value). So don't multiply delta_plus if they point to the same
michael@0 228 // object.
michael@0 229 if (delta_minus != delta_plus) {
michael@0 230 delta_plus->Times10();
michael@0 231 }
michael@0 232 } else if (in_delta_room_minus && in_delta_room_plus) {
michael@0 233 // Let's see if 2*numerator < denominator.
michael@0 234 // If yes, then the next digit would be < 5 and we can round down.
michael@0 235 int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator);
michael@0 236 if (compare < 0) {
michael@0 237 // Remaining digits are less than .5. -> Round down (== do nothing).
michael@0 238 } else if (compare > 0) {
michael@0 239 // Remaining digits are more than .5 of denominator. -> Round up.
michael@0 240 // Note that the last digit could not be a '9' as otherwise the whole
michael@0 241 // loop would have stopped earlier.
michael@0 242 // We still have an assert here in case the preconditions were not
michael@0 243 // satisfied.
michael@0 244 ASSERT(buffer[(*length) - 1] != '9');
michael@0 245 buffer[(*length) - 1]++;
michael@0 246 } else {
michael@0 247 // Halfway case.
michael@0 248 // TODO(floitsch): need a way to solve half-way cases.
michael@0 249 // For now let's round towards even (since this is what Gay seems to
michael@0 250 // do).
michael@0 251
michael@0 252 if ((buffer[(*length) - 1] - '0') % 2 == 0) {
michael@0 253 // Round down => Do nothing.
michael@0 254 } else {
michael@0 255 ASSERT(buffer[(*length) - 1] != '9');
michael@0 256 buffer[(*length) - 1]++;
michael@0 257 }
michael@0 258 }
michael@0 259 return;
michael@0 260 } else if (in_delta_room_minus) {
michael@0 261 // Round down (== do nothing).
michael@0 262 return;
michael@0 263 } else { // in_delta_room_plus
michael@0 264 // Round up.
michael@0 265 // Note again that the last digit could not be '9' since this would have
michael@0 266 // stopped the loop earlier.
michael@0 267 // We still have an ASSERT here, in case the preconditions were not
michael@0 268 // satisfied.
michael@0 269 ASSERT(buffer[(*length) -1] != '9');
michael@0 270 buffer[(*length) - 1]++;
michael@0 271 return;
michael@0 272 }
michael@0 273 }
michael@0 274 }
michael@0 275
michael@0 276
michael@0 277 // Let v = numerator / denominator < 10.
michael@0 278 // Then we generate 'count' digits of d = x.xxxxx... (without the decimal point)
michael@0 279 // from left to right. Once 'count' digits have been produced we decide wether
michael@0 280 // to round up or down. Remainders of exactly .5 round upwards. Numbers such
michael@0 281 // as 9.999999 propagate a carry all the way, and change the
michael@0 282 // exponent (decimal_point), when rounding upwards.
michael@0 283 static void GenerateCountedDigits(int count, int* decimal_point,
michael@0 284 Bignum* numerator, Bignum* denominator,
michael@0 285 Vector<char>(buffer), int* length) {
michael@0 286 ASSERT(count >= 0);
michael@0 287 for (int i = 0; i < count - 1; ++i) {
michael@0 288 uint16_t digit;
michael@0 289 digit = numerator->DivideModuloIntBignum(*denominator);
michael@0 290 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
michael@0 291 // digit = numerator / denominator (integer division).
michael@0 292 // numerator = numerator % denominator.
michael@0 293 buffer[i] = digit + '0';
michael@0 294 // Prepare for next iteration.
michael@0 295 numerator->Times10();
michael@0 296 }
michael@0 297 // Generate the last digit.
michael@0 298 uint16_t digit;
michael@0 299 digit = numerator->DivideModuloIntBignum(*denominator);
michael@0 300 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
michael@0 301 digit++;
michael@0 302 }
michael@0 303 buffer[count - 1] = digit + '0';
michael@0 304 // Correct bad digits (in case we had a sequence of '9's). Propagate the
michael@0 305 // carry until we hat a non-'9' or til we reach the first digit.
michael@0 306 for (int i = count - 1; i > 0; --i) {
michael@0 307 if (buffer[i] != '0' + 10) break;
michael@0 308 buffer[i] = '0';
michael@0 309 buffer[i - 1]++;
michael@0 310 }
michael@0 311 if (buffer[0] == '0' + 10) {
michael@0 312 // Propagate a carry past the top place.
michael@0 313 buffer[0] = '1';
michael@0 314 (*decimal_point)++;
michael@0 315 }
michael@0 316 *length = count;
michael@0 317 }
michael@0 318
michael@0 319
michael@0 320 // Generates 'requested_digits' after the decimal point. It might omit
michael@0 321 // trailing '0's. If the input number is too small then no digits at all are
michael@0 322 // generated (ex.: 2 fixed digits for 0.00001).
michael@0 323 //
michael@0 324 // Input verifies: 1 <= (numerator + delta) / denominator < 10.
michael@0 325 static void BignumToFixed(int requested_digits, int* decimal_point,
michael@0 326 Bignum* numerator, Bignum* denominator,
michael@0 327 Vector<char>(buffer), int* length) {
michael@0 328 // Note that we have to look at more than just the requested_digits, since
michael@0 329 // a number could be rounded up. Example: v=0.5 with requested_digits=0.
michael@0 330 // Even though the power of v equals 0 we can't just stop here.
michael@0 331 if (-(*decimal_point) > requested_digits) {
michael@0 332 // The number is definitively too small.
michael@0 333 // Ex: 0.001 with requested_digits == 1.
michael@0 334 // Set decimal-point to -requested_digits. This is what Gay does.
michael@0 335 // Note that it should not have any effect anyways since the string is
michael@0 336 // empty.
michael@0 337 *decimal_point = -requested_digits;
michael@0 338 *length = 0;
michael@0 339 return;
michael@0 340 } else if (-(*decimal_point) == requested_digits) {
michael@0 341 // We only need to verify if the number rounds down or up.
michael@0 342 // Ex: 0.04 and 0.06 with requested_digits == 1.
michael@0 343 ASSERT(*decimal_point == -requested_digits);
michael@0 344 // Initially the fraction lies in range (1, 10]. Multiply the denominator
michael@0 345 // by 10 so that we can compare more easily.
michael@0 346 denominator->Times10();
michael@0 347 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
michael@0 348 // If the fraction is >= 0.5 then we have to include the rounded
michael@0 349 // digit.
michael@0 350 buffer[0] = '1';
michael@0 351 *length = 1;
michael@0 352 (*decimal_point)++;
michael@0 353 } else {
michael@0 354 // Note that we caught most of similar cases earlier.
michael@0 355 *length = 0;
michael@0 356 }
michael@0 357 return;
michael@0 358 } else {
michael@0 359 // The requested digits correspond to the digits after the point.
michael@0 360 // The variable 'needed_digits' includes the digits before the point.
michael@0 361 int needed_digits = (*decimal_point) + requested_digits;
michael@0 362 GenerateCountedDigits(needed_digits, decimal_point,
michael@0 363 numerator, denominator,
michael@0 364 buffer, length);
michael@0 365 }
michael@0 366 }
michael@0 367
michael@0 368
michael@0 369 // Returns an estimation of k such that 10^(k-1) <= v < 10^k where
michael@0 370 // v = f * 2^exponent and 2^52 <= f < 2^53.
michael@0 371 // v is hence a normalized double with the given exponent. The output is an
michael@0 372 // approximation for the exponent of the decimal approimation .digits * 10^k.
michael@0 373 //
michael@0 374 // The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
michael@0 375 // Note: this property holds for v's upper boundary m+ too.
michael@0 376 // 10^k <= m+ < 10^k+1.
michael@0 377 // (see explanation below).
michael@0 378 //
michael@0 379 // Examples:
michael@0 380 // EstimatePower(0) => 16
michael@0 381 // EstimatePower(-52) => 0
michael@0 382 //
michael@0 383 // Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0.
michael@0 384 static int EstimatePower(int exponent) {
michael@0 385 // This function estimates log10 of v where v = f*2^e (with e == exponent).
michael@0 386 // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)).
michael@0 387 // Note that f is bounded by its container size. Let p = 53 (the double's
michael@0 388 // significand size). Then 2^(p-1) <= f < 2^p.
michael@0 389 //
michael@0 390 // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close
michael@0 391 // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)).
michael@0 392 // The computed number undershoots by less than 0.631 (when we compute log3
michael@0 393 // and not log10).
michael@0 394 //
michael@0 395 // Optimization: since we only need an approximated result this computation
michael@0 396 // can be performed on 64 bit integers. On x86/x64 architecture the speedup is
michael@0 397 // not really measurable, though.
michael@0 398 //
michael@0 399 // Since we want to avoid overshooting we decrement by 1e10 so that
michael@0 400 // floating-point imprecisions don't affect us.
michael@0 401 //
michael@0 402 // Explanation for v's boundary m+: the computation takes advantage of
michael@0 403 // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement
michael@0 404 // (even for denormals where the delta can be much more important).
michael@0 405
michael@0 406 const double k1Log10 = 0.30102999566398114; // 1/lg(10)
michael@0 407
michael@0 408 // For doubles len(f) == 53 (don't forget the hidden bit).
michael@0 409 const int kSignificandSize = Double::kSignificandSize;
michael@0 410 double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10);
michael@0 411 return static_cast<int>(estimate);
michael@0 412 }
michael@0 413
michael@0 414
michael@0 415 // See comments for InitialScaledStartValues.
michael@0 416 static void InitialScaledStartValuesPositiveExponent(
michael@0 417 uint64_t significand, int exponent,
michael@0 418 int estimated_power, bool need_boundary_deltas,
michael@0 419 Bignum* numerator, Bignum* denominator,
michael@0 420 Bignum* delta_minus, Bignum* delta_plus) {
michael@0 421 // A positive exponent implies a positive power.
michael@0 422 ASSERT(estimated_power >= 0);
michael@0 423 // Since the estimated_power is positive we simply multiply the denominator
michael@0 424 // by 10^estimated_power.
michael@0 425
michael@0 426 // numerator = v.
michael@0 427 numerator->AssignUInt64(significand);
michael@0 428 numerator->ShiftLeft(exponent);
michael@0 429 // denominator = 10^estimated_power.
michael@0 430 denominator->AssignPowerUInt16(10, estimated_power);
michael@0 431
michael@0 432 if (need_boundary_deltas) {
michael@0 433 // Introduce a common denominator so that the deltas to the boundaries are
michael@0 434 // integers.
michael@0 435 denominator->ShiftLeft(1);
michael@0 436 numerator->ShiftLeft(1);
michael@0 437 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
michael@0 438 // denominator (of 2) delta_plus equals 2^e.
michael@0 439 delta_plus->AssignUInt16(1);
michael@0 440 delta_plus->ShiftLeft(exponent);
michael@0 441 // Same for delta_minus. The adjustments if f == 2^p-1 are done later.
michael@0 442 delta_minus->AssignUInt16(1);
michael@0 443 delta_minus->ShiftLeft(exponent);
michael@0 444 }
michael@0 445 }
michael@0 446
michael@0 447
michael@0 448 // See comments for InitialScaledStartValues
michael@0 449 static void InitialScaledStartValuesNegativeExponentPositivePower(
michael@0 450 uint64_t significand, int exponent,
michael@0 451 int estimated_power, bool need_boundary_deltas,
michael@0 452 Bignum* numerator, Bignum* denominator,
michael@0 453 Bignum* delta_minus, Bignum* delta_plus) {
michael@0 454 // v = f * 2^e with e < 0, and with estimated_power >= 0.
michael@0 455 // This means that e is close to 0 (have a look at how estimated_power is
michael@0 456 // computed).
michael@0 457
michael@0 458 // numerator = significand
michael@0 459 // since v = significand * 2^exponent this is equivalent to
michael@0 460 // numerator = v * / 2^-exponent
michael@0 461 numerator->AssignUInt64(significand);
michael@0 462 // denominator = 10^estimated_power * 2^-exponent (with exponent < 0)
michael@0 463 denominator->AssignPowerUInt16(10, estimated_power);
michael@0 464 denominator->ShiftLeft(-exponent);
michael@0 465
michael@0 466 if (need_boundary_deltas) {
michael@0 467 // Introduce a common denominator so that the deltas to the boundaries are
michael@0 468 // integers.
michael@0 469 denominator->ShiftLeft(1);
michael@0 470 numerator->ShiftLeft(1);
michael@0 471 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
michael@0 472 // denominator (of 2) delta_plus equals 2^e.
michael@0 473 // Given that the denominator already includes v's exponent the distance
michael@0 474 // to the boundaries is simply 1.
michael@0 475 delta_plus->AssignUInt16(1);
michael@0 476 // Same for delta_minus. The adjustments if f == 2^p-1 are done later.
michael@0 477 delta_minus->AssignUInt16(1);
michael@0 478 }
michael@0 479 }
michael@0 480
michael@0 481
michael@0 482 // See comments for InitialScaledStartValues
michael@0 483 static void InitialScaledStartValuesNegativeExponentNegativePower(
michael@0 484 uint64_t significand, int exponent,
michael@0 485 int estimated_power, bool need_boundary_deltas,
michael@0 486 Bignum* numerator, Bignum* denominator,
michael@0 487 Bignum* delta_minus, Bignum* delta_plus) {
michael@0 488 // Instead of multiplying the denominator with 10^estimated_power we
michael@0 489 // multiply all values (numerator and deltas) by 10^-estimated_power.
michael@0 490
michael@0 491 // Use numerator as temporary container for power_ten.
michael@0 492 Bignum* power_ten = numerator;
michael@0 493 power_ten->AssignPowerUInt16(10, -estimated_power);
michael@0 494
michael@0 495 if (need_boundary_deltas) {
michael@0 496 // Since power_ten == numerator we must make a copy of 10^estimated_power
michael@0 497 // before we complete the computation of the numerator.
michael@0 498 // delta_plus = delta_minus = 10^estimated_power
michael@0 499 delta_plus->AssignBignum(*power_ten);
michael@0 500 delta_minus->AssignBignum(*power_ten);
michael@0 501 }
michael@0 502
michael@0 503 // numerator = significand * 2 * 10^-estimated_power
michael@0 504 // since v = significand * 2^exponent this is equivalent to
michael@0 505 // numerator = v * 10^-estimated_power * 2 * 2^-exponent.
michael@0 506 // Remember: numerator has been abused as power_ten. So no need to assign it
michael@0 507 // to itself.
michael@0 508 ASSERT(numerator == power_ten);
michael@0 509 numerator->MultiplyByUInt64(significand);
michael@0 510
michael@0 511 // denominator = 2 * 2^-exponent with exponent < 0.
michael@0 512 denominator->AssignUInt16(1);
michael@0 513 denominator->ShiftLeft(-exponent);
michael@0 514
michael@0 515 if (need_boundary_deltas) {
michael@0 516 // Introduce a common denominator so that the deltas to the boundaries are
michael@0 517 // integers.
michael@0 518 numerator->ShiftLeft(1);
michael@0 519 denominator->ShiftLeft(1);
michael@0 520 // With this shift the boundaries have their correct value, since
michael@0 521 // delta_plus = 10^-estimated_power, and
michael@0 522 // delta_minus = 10^-estimated_power.
michael@0 523 // These assignments have been done earlier.
michael@0 524 // The adjustments if f == 2^p-1 (lower boundary is closer) are done later.
michael@0 525 }
michael@0 526 }
michael@0 527
michael@0 528
michael@0 529 // Let v = significand * 2^exponent.
michael@0 530 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
michael@0 531 // and denominator. The functions GenerateShortestDigits and
michael@0 532 // GenerateCountedDigits will then convert this ratio to its decimal
michael@0 533 // representation d, with the required accuracy.
michael@0 534 // Then d * 10^estimated_power is the representation of v.
michael@0 535 // (Note: the fraction and the estimated_power might get adjusted before
michael@0 536 // generating the decimal representation.)
michael@0 537 //
michael@0 538 // The initial start values consist of:
michael@0 539 // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power.
michael@0 540 // - a scaled (common) denominator.
michael@0 541 // optionally (used by GenerateShortestDigits to decide if it has the shortest
michael@0 542 // decimal converting back to v):
michael@0 543 // - v - m-: the distance to the lower boundary.
michael@0 544 // - m+ - v: the distance to the upper boundary.
michael@0 545 //
michael@0 546 // v, m+, m-, and therefore v - m- and m+ - v all share the same denominator.
michael@0 547 //
michael@0 548 // Let ep == estimated_power, then the returned values will satisfy:
michael@0 549 // v / 10^ep = numerator / denominator.
michael@0 550 // v's boundarys m- and m+:
michael@0 551 // m- / 10^ep == v / 10^ep - delta_minus / denominator
michael@0 552 // m+ / 10^ep == v / 10^ep + delta_plus / denominator
michael@0 553 // Or in other words:
michael@0 554 // m- == v - delta_minus * 10^ep / denominator;
michael@0 555 // m+ == v + delta_plus * 10^ep / denominator;
michael@0 556 //
michael@0 557 // Since 10^(k-1) <= v < 10^k (with k == estimated_power)
michael@0 558 // or 10^k <= v < 10^(k+1)
michael@0 559 // we then have 0.1 <= numerator/denominator < 1
michael@0 560 // or 1 <= numerator/denominator < 10
michael@0 561 //
michael@0 562 // It is then easy to kickstart the digit-generation routine.
michael@0 563 //
michael@0 564 // The boundary-deltas are only filled if the mode equals BIGNUM_DTOA_SHORTEST
michael@0 565 // or BIGNUM_DTOA_SHORTEST_SINGLE.
michael@0 566
michael@0 567 static void InitialScaledStartValues(uint64_t significand,
michael@0 568 int exponent,
michael@0 569 bool lower_boundary_is_closer,
michael@0 570 int estimated_power,
michael@0 571 bool need_boundary_deltas,
michael@0 572 Bignum* numerator,
michael@0 573 Bignum* denominator,
michael@0 574 Bignum* delta_minus,
michael@0 575 Bignum* delta_plus) {
michael@0 576 if (exponent >= 0) {
michael@0 577 InitialScaledStartValuesPositiveExponent(
michael@0 578 significand, exponent, estimated_power, need_boundary_deltas,
michael@0 579 numerator, denominator, delta_minus, delta_plus);
michael@0 580 } else if (estimated_power >= 0) {
michael@0 581 InitialScaledStartValuesNegativeExponentPositivePower(
michael@0 582 significand, exponent, estimated_power, need_boundary_deltas,
michael@0 583 numerator, denominator, delta_minus, delta_plus);
michael@0 584 } else {
michael@0 585 InitialScaledStartValuesNegativeExponentNegativePower(
michael@0 586 significand, exponent, estimated_power, need_boundary_deltas,
michael@0 587 numerator, denominator, delta_minus, delta_plus);
michael@0 588 }
michael@0 589
michael@0 590 if (need_boundary_deltas && lower_boundary_is_closer) {
michael@0 591 // The lower boundary is closer at half the distance of "normal" numbers.
michael@0 592 // Increase the common denominator and adapt all but the delta_minus.
michael@0 593 denominator->ShiftLeft(1); // *2
michael@0 594 numerator->ShiftLeft(1); // *2
michael@0 595 delta_plus->ShiftLeft(1); // *2
michael@0 596 }
michael@0 597 }
michael@0 598
michael@0 599
michael@0 600 // This routine multiplies numerator/denominator so that its values lies in the
michael@0 601 // range 1-10. That is after a call to this function we have:
michael@0 602 // 1 <= (numerator + delta_plus) /denominator < 10.
michael@0 603 // Let numerator the input before modification and numerator' the argument
michael@0 604 // after modification, then the output-parameter decimal_point is such that
michael@0 605 // numerator / denominator * 10^estimated_power ==
michael@0 606 // numerator' / denominator' * 10^(decimal_point - 1)
michael@0 607 // In some cases estimated_power was too low, and this is already the case. We
michael@0 608 // then simply adjust the power so that 10^(k-1) <= v < 10^k (with k ==
michael@0 609 // estimated_power) but do not touch the numerator or denominator.
michael@0 610 // Otherwise the routine multiplies the numerator and the deltas by 10.
michael@0 611 static void FixupMultiply10(int estimated_power, bool is_even,
michael@0 612 int* decimal_point,
michael@0 613 Bignum* numerator, Bignum* denominator,
michael@0 614 Bignum* delta_minus, Bignum* delta_plus) {
michael@0 615 bool in_range;
michael@0 616 if (is_even) {
michael@0 617 // For IEEE doubles half-way cases (in decimal system numbers ending with 5)
michael@0 618 // are rounded to the closest floating-point number with even significand.
michael@0 619 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
michael@0 620 } else {
michael@0 621 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
michael@0 622 }
michael@0 623 if (in_range) {
michael@0 624 // Since numerator + delta_plus >= denominator we already have
michael@0 625 // 1 <= numerator/denominator < 10. Simply update the estimated_power.
michael@0 626 *decimal_point = estimated_power + 1;
michael@0 627 } else {
michael@0 628 *decimal_point = estimated_power;
michael@0 629 numerator->Times10();
michael@0 630 if (Bignum::Equal(*delta_minus, *delta_plus)) {
michael@0 631 delta_minus->Times10();
michael@0 632 delta_plus->AssignBignum(*delta_minus);
michael@0 633 } else {
michael@0 634 delta_minus->Times10();
michael@0 635 delta_plus->Times10();
michael@0 636 }
michael@0 637 }
michael@0 638 }
michael@0 639
michael@0 640 } // namespace double_conversion

mercurial