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 <stdarg.h> |
| 29 #include <limits.h> |
| 30 |
| 31 #include "strtod.h" |
| 32 #include "bignum.h" |
| 33 #include "cached-powers.h" |
| 34 #include "double.h" |
| 35 |
| 36 namespace double_conversion { |
| 37 |
| 38 // 2^53 = 9007199254740992. |
| 39 // Any integer with at most 15 decimal digits will hence fit into a double |
| 40 // (which has a 53bit significand) without loss of precision. |
| 41 static const int kMaxExactDoubleIntegerDecimalDigits = 15; |
| 42 // 2^64 = 18446744073709551616 > 10^19 |
| 43 static const int kMaxUint64DecimalDigits = 19; |
| 44 |
| 45 // Max double: 1.7976931348623157 x 10^308 |
| 46 // Min non-zero double: 4.9406564584124654 x 10^-324 |
| 47 // Any x >= 10^309 is interpreted as +infinity. |
| 48 // Any x <= 10^-324 is interpreted as 0. |
| 49 // Note that 2.5e-324 (despite being smaller than the min double) will be read |
| 50 // as non-zero (equal to the min non-zero double). |
| 51 static const int kMaxDecimalPower = 309; |
| 52 static const int kMinDecimalPower = -324; |
| 53 |
| 54 // 2^64 = 18446744073709551616 |
| 55 static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); |
| 56 |
| 57 |
| 58 static const double exact_powers_of_ten[] = { |
| 59 1.0, // 10^0 |
| 60 10.0, |
| 61 100.0, |
| 62 1000.0, |
| 63 10000.0, |
| 64 100000.0, |
| 65 1000000.0, |
| 66 10000000.0, |
| 67 100000000.0, |
| 68 1000000000.0, |
| 69 10000000000.0, // 10^10 |
| 70 100000000000.0, |
| 71 1000000000000.0, |
| 72 10000000000000.0, |
| 73 100000000000000.0, |
| 74 1000000000000000.0, |
| 75 10000000000000000.0, |
| 76 100000000000000000.0, |
| 77 1000000000000000000.0, |
| 78 10000000000000000000.0, |
| 79 100000000000000000000.0, // 10^20 |
| 80 1000000000000000000000.0, |
| 81 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 |
| 82 10000000000000000000000.0 |
| 83 }; |
| 84 static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); |
| 85 |
| 86 // Maximum number of significant digits in the decimal representation. |
| 87 // In fact the value is 772 (see conversions.cc), but to give us some margin |
| 88 // we round up to 780. |
| 89 static const int kMaxSignificantDecimalDigits = 780; |
| 90 |
| 91 static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { |
| 92 for (int i = 0; i < buffer.length(); i++) { |
| 93 if (buffer[i] != '0') { |
| 94 return buffer.SubVector(i, buffer.length()); |
| 95 } |
| 96 } |
| 97 return Vector<const char>(buffer.start(), 0); |
| 98 } |
| 99 |
| 100 |
| 101 static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { |
| 102 for (int i = buffer.length() - 1; i >= 0; --i) { |
| 103 if (buffer[i] != '0') { |
| 104 return buffer.SubVector(0, i + 1); |
| 105 } |
| 106 } |
| 107 return Vector<const char>(buffer.start(), 0); |
| 108 } |
| 109 |
| 110 |
| 111 static void TrimToMaxSignificantDigits(Vector<const char> buffer, |
| 112 int exponent, |
| 113 char* significant_buffer, |
| 114 int* significant_exponent) { |
| 115 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { |
| 116 significant_buffer[i] = buffer[i]; |
| 117 } |
| 118 // The input buffer has been trimmed. Therefore the last digit must be |
| 119 // different from '0'. |
| 120 ASSERT(buffer[buffer.length() - 1] != '0'); |
| 121 // Set the last digit to be non-zero. This is sufficient to guarantee |
| 122 // correct rounding. |
| 123 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; |
| 124 *significant_exponent = |
| 125 exponent + (buffer.length() - kMaxSignificantDecimalDigits); |
| 126 } |
| 127 |
| 128 // Reads digits from the buffer and converts them to a uint64. |
| 129 // Reads in as many digits as fit into a uint64. |
| 130 // When the string starts with "1844674407370955161" no further digit is read. |
| 131 // Since 2^64 = 18446744073709551616 it would still be possible read another |
| 132 // digit if it was less or equal than 6, but this would complicate the code. |
| 133 static uint64_t ReadUint64(Vector<const char> buffer, |
| 134 int* number_of_read_digits) { |
| 135 uint64_t result = 0; |
| 136 int i = 0; |
| 137 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { |
| 138 int digit = buffer[i++] - '0'; |
| 139 ASSERT(0 <= digit && digit <= 9); |
| 140 result = 10 * result + digit; |
| 141 } |
| 142 *number_of_read_digits = i; |
| 143 return result; |
| 144 } |
| 145 |
| 146 |
| 147 // Reads a DiyFp from the buffer. |
| 148 // The returned DiyFp is not necessarily normalized. |
| 149 // If remaining_decimals is zero then the returned DiyFp is accurate. |
| 150 // Otherwise it has been rounded and has error of at most 1/2 ulp. |
| 151 static void ReadDiyFp(Vector<const char> buffer, |
| 152 DiyFp* result, |
| 153 int* remaining_decimals) { |
| 154 int read_digits; |
| 155 uint64_t significand = ReadUint64(buffer, &read_digits); |
| 156 if (buffer.length() == read_digits) { |
| 157 *result = DiyFp(significand, 0); |
| 158 *remaining_decimals = 0; |
| 159 } else { |
| 160 // Round the significand. |
| 161 if (buffer[read_digits] >= '5') { |
| 162 significand++; |
| 163 } |
| 164 // Compute the binary exponent. |
| 165 int exponent = 0; |
| 166 *result = DiyFp(significand, exponent); |
| 167 *remaining_decimals = buffer.length() - read_digits; |
| 168 } |
| 169 } |
| 170 |
| 171 |
| 172 static bool DoubleStrtod(Vector<const char> trimmed, |
| 173 int exponent, |
| 174 double* result) { |
| 175 #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) |
| 176 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is |
| 177 // 80 bits wide (as is the case on Linux) then double-rounding occurs and the |
| 178 // result is not accurate. |
| 179 // We know that Windows32 uses 64 bits and is therefore accurate. |
| 180 // Note that the ARM simulator is compiled for 32bits. It therefore exhibits |
| 181 // the same problem. |
| 182 return false; |
| 183 #endif |
| 184 if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { |
| 185 int read_digits; |
| 186 // The trimmed input fits into a double. |
| 187 // If the 10^exponent (resp. 10^-exponent) fits into a double too then we |
| 188 // can compute the result-double simply by multiplying (resp. dividing) the |
| 189 // two numbers. |
| 190 // This is possible because IEEE guarantees that floating-point operations |
| 191 // return the best possible approximation. |
| 192 if (exponent < 0 && -exponent < kExactPowersOfTenSize) { |
| 193 // 10^-exponent fits into a double. |
| 194 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
| 195 ASSERT(read_digits == trimmed.length()); |
| 196 *result /= exact_powers_of_ten[-exponent]; |
| 197 return true; |
| 198 } |
| 199 if (0 <= exponent && exponent < kExactPowersOfTenSize) { |
| 200 // 10^exponent fits into a double. |
| 201 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
| 202 ASSERT(read_digits == trimmed.length()); |
| 203 *result *= exact_powers_of_ten[exponent]; |
| 204 return true; |
| 205 } |
| 206 int remaining_digits = |
| 207 kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); |
| 208 if ((0 <= exponent) && |
| 209 (exponent - remaining_digits < kExactPowersOfTenSize)) { |
| 210 // The trimmed string was short and we can multiply it with |
| 211 // 10^remaining_digits. As a result the remaining exponent now fits |
| 212 // into a double too. |
| 213 *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
| 214 ASSERT(read_digits == trimmed.length()); |
| 215 *result *= exact_powers_of_ten[remaining_digits]; |
| 216 *result *= exact_powers_of_ten[exponent - remaining_digits]; |
| 217 return true; |
| 218 } |
| 219 } |
| 220 return false; |
| 221 } |
| 222 |
| 223 |
| 224 // Returns 10^exponent as an exact DiyFp. |
| 225 // The given exponent must be in the range [1; kDecimalExponentDistance[. |
| 226 static DiyFp AdjustmentPowerOfTen(int exponent) { |
| 227 ASSERT(0 < exponent); |
| 228 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); |
| 229 // Simply hardcode the remaining powers for the given decimal exponent |
| 230 // distance. |
| 231 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); |
| 232 switch (exponent) { |
| 233 case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); |
| 234 case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); |
| 235 case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); |
| 236 case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); |
| 237 case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); |
| 238 case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); |
| 239 case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); |
| 240 default: |
| 241 UNREACHABLE(); |
| 242 return DiyFp(0, 0); |
| 243 } |
| 244 } |
| 245 |
| 246 |
| 247 // If the function returns true then the result is the correct double. |
| 248 // Otherwise it is either the correct double or the double that is just below |
| 249 // the correct double. |
| 250 static bool DiyFpStrtod(Vector<const char> buffer, |
| 251 int exponent, |
| 252 double* result) { |
| 253 DiyFp input; |
| 254 int remaining_decimals; |
| 255 ReadDiyFp(buffer, &input, &remaining_decimals); |
| 256 // Since we may have dropped some digits the input is not accurate. |
| 257 // If remaining_decimals is different than 0 than the error is at most |
| 258 // .5 ulp (unit in the last place). |
| 259 // We don't want to deal with fractions and therefore keep a common |
| 260 // denominator. |
| 261 const int kDenominatorLog = 3; |
| 262 const int kDenominator = 1 << kDenominatorLog; |
| 263 // Move the remaining decimals into the exponent. |
| 264 exponent += remaining_decimals; |
| 265 int error = (remaining_decimals == 0 ? 0 : kDenominator / 2); |
| 266 |
| 267 int old_e = input.e(); |
| 268 input.Normalize(); |
| 269 error <<= old_e - input.e(); |
| 270 |
| 271 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); |
| 272 if (exponent < PowersOfTenCache::kMinDecimalExponent) { |
| 273 *result = 0.0; |
| 274 return true; |
| 275 } |
| 276 DiyFp cached_power; |
| 277 int cached_decimal_exponent; |
| 278 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, |
| 279 &cached_power, |
| 280 &cached_decimal_exponent); |
| 281 |
| 282 if (cached_decimal_exponent != exponent) { |
| 283 int adjustment_exponent = exponent - cached_decimal_exponent; |
| 284 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); |
| 285 input.Multiply(adjustment_power); |
| 286 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { |
| 287 // The product of input with the adjustment power fits into a 64 bit |
| 288 // integer. |
| 289 ASSERT(DiyFp::kSignificandSize == 64); |
| 290 } else { |
| 291 // The adjustment power is exact. There is hence only an error of 0.5. |
| 292 error += kDenominator / 2; |
| 293 } |
| 294 } |
| 295 |
| 296 input.Multiply(cached_power); |
| 297 // The error introduced by a multiplication of a*b equals |
| 298 // error_a + error_b + error_a*error_b/2^64 + 0.5 |
| 299 // Substituting a with 'input' and b with 'cached_power' we have |
| 300 // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp), |
| 301 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 |
| 302 int error_b = kDenominator / 2; |
| 303 int error_ab = (error == 0 ? 0 : 1); // We round up to 1. |
| 304 int fixed_error = kDenominator / 2; |
| 305 error += error_b + error_ab + fixed_error; |
| 306 |
| 307 old_e = input.e(); |
| 308 input.Normalize(); |
| 309 error <<= old_e - input.e(); |
| 310 |
| 311 // See if the double's significand changes if we add/subtract the error. |
| 312 int order_of_magnitude = DiyFp::kSignificandSize + input.e(); |
| 313 int effective_significand_size = |
| 314 Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); |
| 315 int precision_digits_count = |
| 316 DiyFp::kSignificandSize - effective_significand_size; |
| 317 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { |
| 318 // This can only happen for very small denormals. In this case the |
| 319 // half-way multiplied by the denominator exceeds the range of an uint64. |
| 320 // Simply shift everything to the right. |
| 321 int shift_amount = (precision_digits_count + kDenominatorLog) - |
| 322 DiyFp::kSignificandSize + 1; |
| 323 input.set_f(input.f() >> shift_amount); |
| 324 input.set_e(input.e() + shift_amount); |
| 325 // We add 1 for the lost precision of error, and kDenominator for |
| 326 // the lost precision of input.f(). |
| 327 error = (error >> shift_amount) + 1 + kDenominator; |
| 328 precision_digits_count -= shift_amount; |
| 329 } |
| 330 // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. |
| 331 ASSERT(DiyFp::kSignificandSize == 64); |
| 332 ASSERT(precision_digits_count < 64); |
| 333 uint64_t one64 = 1; |
| 334 uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; |
| 335 uint64_t precision_bits = input.f() & precision_bits_mask; |
| 336 uint64_t half_way = one64 << (precision_digits_count - 1); |
| 337 precision_bits *= kDenominator; |
| 338 half_way *= kDenominator; |
| 339 DiyFp rounded_input(input.f() >> precision_digits_count, |
| 340 input.e() + precision_digits_count); |
| 341 if (precision_bits >= half_way + error) { |
| 342 rounded_input.set_f(rounded_input.f() + 1); |
| 343 } |
| 344 // If the last_bits are too close to the half-way case than we are too |
| 345 // inaccurate and round down. In this case we return false so that we can |
| 346 // fall back to a more precise algorithm. |
| 347 |
| 348 *result = Double(rounded_input).value(); |
| 349 if (half_way - error < precision_bits && precision_bits < half_way + error) { |
| 350 // Too imprecise. The caller will have to fall back to a slower version. |
| 351 // However the returned number is guaranteed to be either the correct |
| 352 // double, or the next-lower double. |
| 353 return false; |
| 354 } else { |
| 355 return true; |
| 356 } |
| 357 } |
| 358 |
| 359 |
| 360 // Returns the correct double for the buffer*10^exponent. |
| 361 // The variable guess should be a close guess that is either the correct double |
| 362 // or its lower neighbor (the nearest double less than the correct one). |
| 363 // Preconditions: |
| 364 // buffer.length() + exponent <= kMaxDecimalPower + 1 |
| 365 // buffer.length() + exponent > kMinDecimalPower |
| 366 // buffer.length() <= kMaxDecimalSignificantDigits |
| 367 static double BignumStrtod(Vector<const char> buffer, |
| 368 int exponent, |
| 369 double guess) { |
| 370 if (guess == Double::Infinity()) { |
| 371 return guess; |
| 372 } |
| 373 |
| 374 DiyFp upper_boundary = Double(guess).UpperBoundary(); |
| 375 |
| 376 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); |
| 377 ASSERT(buffer.length() + exponent > kMinDecimalPower); |
| 378 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); |
| 379 // Make sure that the Bignum will be able to hold all our numbers. |
| 380 // Our Bignum implementation has a separate field for exponents. Shifts will |
| 381 // consume at most one bigit (< 64 bits). |
| 382 // ln(10) == 3.3219... |
| 383 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); |
| 384 Bignum input; |
| 385 Bignum boundary; |
| 386 input.AssignDecimalString(buffer); |
| 387 boundary.AssignUInt64(upper_boundary.f()); |
| 388 if (exponent >= 0) { |
| 389 input.MultiplyByPowerOfTen(exponent); |
| 390 } else { |
| 391 boundary.MultiplyByPowerOfTen(-exponent); |
| 392 } |
| 393 if (upper_boundary.e() > 0) { |
| 394 boundary.ShiftLeft(upper_boundary.e()); |
| 395 } else { |
| 396 input.ShiftLeft(-upper_boundary.e()); |
| 397 } |
| 398 int comparison = Bignum::Compare(input, boundary); |
| 399 if (comparison < 0) { |
| 400 return guess; |
| 401 } else if (comparison > 0) { |
| 402 return Double(guess).NextDouble(); |
| 403 } else if ((Double(guess).Significand() & 1) == 0) { |
| 404 // Round towards even. |
| 405 return guess; |
| 406 } else { |
| 407 return Double(guess).NextDouble(); |
| 408 } |
| 409 } |
| 410 |
| 411 |
| 412 double Strtod(Vector<const char> buffer, int exponent) { |
| 413 Vector<const char> left_trimmed = TrimLeadingZeros(buffer); |
| 414 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed); |
| 415 exponent += left_trimmed.length() - trimmed.length(); |
| 416 if (trimmed.length() == 0) return 0.0; |
| 417 if (trimmed.length() > kMaxSignificantDecimalDigits) { |
| 418 char significant_buffer[kMaxSignificantDecimalDigits]; |
| 419 int significant_exponent; |
| 420 TrimToMaxSignificantDigits(trimmed, exponent, |
| 421 significant_buffer, &significant_exponent); |
| 422 return Strtod(Vector<const char>(significant_buffer, |
| 423 kMaxSignificantDecimalDigits), |
| 424 significant_exponent); |
| 425 } |
| 426 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { |
| 427 return Double::Infinity(); |
| 428 } |
| 429 if (exponent + trimmed.length() <= kMinDecimalPower) { |
| 430 return 0.0; |
| 431 } |
| 432 |
| 433 double guess; |
| 434 if (DoubleStrtod(trimmed, exponent, &guess) || |
| 435 DiyFpStrtod(trimmed, exponent, &guess)) { |
| 436 return guess; |
| 437 } |
| 438 return BignumStrtod(trimmed, exponent, guess); |
| 439 } |
| 440 |
| 441 } // namespace double_conversion |
OLD | NEW |