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