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