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