Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
michael@0 | 2 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 3 | // modification, are permitted provided that the following conditions are |
michael@0 | 4 | // met: |
michael@0 | 5 | // |
michael@0 | 6 | // * Redistributions of source code must retain the above copyright |
michael@0 | 7 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 8 | // * Redistributions in binary form must reproduce the above |
michael@0 | 9 | // copyright notice, this list of conditions and the following |
michael@0 | 10 | // disclaimer in the documentation and/or other materials provided |
michael@0 | 11 | // with the distribution. |
michael@0 | 12 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 13 | // contributors may be used to endorse or promote products derived |
michael@0 | 14 | // from this software without specific prior written permission. |
michael@0 | 15 | // |
michael@0 | 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | |
michael@0 | 28 | #include <stdarg.h> |
michael@0 | 29 | #include <limits.h> |
michael@0 | 30 | |
michael@0 | 31 | #include "strtod.h" |
michael@0 | 32 | #include "bignum.h" |
michael@0 | 33 | #include "cached-powers.h" |
michael@0 | 34 | #include "ieee.h" |
michael@0 | 35 | |
michael@0 | 36 | namespace double_conversion { |
michael@0 | 37 | |
michael@0 | 38 | // 2^53 = 9007199254740992. |
michael@0 | 39 | // Any integer with at most 15 decimal digits will hence fit into a double |
michael@0 | 40 | // (which has a 53bit significand) without loss of precision. |
michael@0 | 41 | static const int kMaxExactDoubleIntegerDecimalDigits = 15; |
michael@0 | 42 | // 2^64 = 18446744073709551616 > 10^19 |
michael@0 | 43 | static const int kMaxUint64DecimalDigits = 19; |
michael@0 | 44 | |
michael@0 | 45 | // Max double: 1.7976931348623157 x 10^308 |
michael@0 | 46 | // Min non-zero double: 4.9406564584124654 x 10^-324 |
michael@0 | 47 | // Any x >= 10^309 is interpreted as +infinity. |
michael@0 | 48 | // Any x <= 10^-324 is interpreted as 0. |
michael@0 | 49 | // Note that 2.5e-324 (despite being smaller than the min double) will be read |
michael@0 | 50 | // as non-zero (equal to the min non-zero double). |
michael@0 | 51 | static const int kMaxDecimalPower = 309; |
michael@0 | 52 | static const int kMinDecimalPower = -324; |
michael@0 | 53 | |
michael@0 | 54 | // 2^64 = 18446744073709551616 |
michael@0 | 55 | static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | static const double exact_powers_of_ten[] = { |
michael@0 | 59 | 1.0, // 10^0 |
michael@0 | 60 | 10.0, |
michael@0 | 61 | 100.0, |
michael@0 | 62 | 1000.0, |
michael@0 | 63 | 10000.0, |
michael@0 | 64 | 100000.0, |
michael@0 | 65 | 1000000.0, |
michael@0 | 66 | 10000000.0, |
michael@0 | 67 | 100000000.0, |
michael@0 | 68 | 1000000000.0, |
michael@0 | 69 | 10000000000.0, // 10^10 |
michael@0 | 70 | 100000000000.0, |
michael@0 | 71 | 1000000000000.0, |
michael@0 | 72 | 10000000000000.0, |
michael@0 | 73 | 100000000000000.0, |
michael@0 | 74 | 1000000000000000.0, |
michael@0 | 75 | 10000000000000000.0, |
michael@0 | 76 | 100000000000000000.0, |
michael@0 | 77 | 1000000000000000000.0, |
michael@0 | 78 | 10000000000000000000.0, |
michael@0 | 79 | 100000000000000000000.0, // 10^20 |
michael@0 | 80 | 1000000000000000000000.0, |
michael@0 | 81 | // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 |
michael@0 | 82 | 10000000000000000000000.0 |
michael@0 | 83 | }; |
michael@0 | 84 | static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); |
michael@0 | 85 | |
michael@0 | 86 | // Maximum number of significant digits in the decimal representation. |
michael@0 | 87 | // In fact the value is 772 (see conversions.cc), but to give us some margin |
michael@0 | 88 | // we round up to 780. |
michael@0 | 89 | static const int kMaxSignificantDecimalDigits = 780; |
michael@0 | 90 | |
michael@0 | 91 | static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { |
michael@0 | 92 | for (int i = 0; i < buffer.length(); i++) { |
michael@0 | 93 | if (buffer[i] != '0') { |
michael@0 | 94 | return buffer.SubVector(i, buffer.length()); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | return Vector<const char>(buffer.start(), 0); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | |
michael@0 | 101 | static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { |
michael@0 | 102 | for (int i = buffer.length() - 1; i >= 0; --i) { |
michael@0 | 103 | if (buffer[i] != '0') { |
michael@0 | 104 | return buffer.SubVector(0, i + 1); |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | return Vector<const char>(buffer.start(), 0); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | static void CutToMaxSignificantDigits(Vector<const char> buffer, |
michael@0 | 112 | int exponent, |
michael@0 | 113 | char* significant_buffer, |
michael@0 | 114 | int* significant_exponent) { |
michael@0 | 115 | for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { |
michael@0 | 116 | significant_buffer[i] = buffer[i]; |
michael@0 | 117 | } |
michael@0 | 118 | // The input buffer has been trimmed. Therefore the last digit must be |
michael@0 | 119 | // different from '0'. |
michael@0 | 120 | ASSERT(buffer[buffer.length() - 1] != '0'); |
michael@0 | 121 | // Set the last digit to be non-zero. This is sufficient to guarantee |
michael@0 | 122 | // correct rounding. |
michael@0 | 123 | significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; |
michael@0 | 124 | *significant_exponent = |
michael@0 | 125 | exponent + (buffer.length() - kMaxSignificantDecimalDigits); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | |
michael@0 | 129 | // Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits. |
michael@0 | 130 | // If possible the input-buffer is reused, but if the buffer needs to be |
michael@0 | 131 | // modified (due to cutting), then the input needs to be copied into the |
michael@0 | 132 | // buffer_copy_space. |
michael@0 | 133 | static void TrimAndCut(Vector<const char> buffer, int exponent, |
michael@0 | 134 | char* buffer_copy_space, int space_size, |
michael@0 | 135 | Vector<const char>* trimmed, int* updated_exponent) { |
michael@0 | 136 | Vector<const char> left_trimmed = TrimLeadingZeros(buffer); |
michael@0 | 137 | Vector<const char> right_trimmed = TrimTrailingZeros(left_trimmed); |
michael@0 | 138 | exponent += left_trimmed.length() - right_trimmed.length(); |
michael@0 | 139 | if (right_trimmed.length() > kMaxSignificantDecimalDigits) { |
michael@0 | 140 | ASSERT(space_size >= kMaxSignificantDecimalDigits); |
michael@0 | 141 | CutToMaxSignificantDigits(right_trimmed, exponent, |
michael@0 | 142 | buffer_copy_space, updated_exponent); |
michael@0 | 143 | *trimmed = Vector<const char>(buffer_copy_space, |
michael@0 | 144 | kMaxSignificantDecimalDigits); |
michael@0 | 145 | } else { |
michael@0 | 146 | *trimmed = right_trimmed; |
michael@0 | 147 | *updated_exponent = exponent; |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | |
michael@0 | 152 | // Reads digits from the buffer and converts them to a uint64. |
michael@0 | 153 | // Reads in as many digits as fit into a uint64. |
michael@0 | 154 | // When the string starts with "1844674407370955161" no further digit is read. |
michael@0 | 155 | // Since 2^64 = 18446744073709551616 it would still be possible read another |
michael@0 | 156 | // digit if it was less or equal than 6, but this would complicate the code. |
michael@0 | 157 | static uint64_t ReadUint64(Vector<const char> buffer, |
michael@0 | 158 | int* number_of_read_digits) { |
michael@0 | 159 | uint64_t result = 0; |
michael@0 | 160 | int i = 0; |
michael@0 | 161 | while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { |
michael@0 | 162 | int digit = buffer[i++] - '0'; |
michael@0 | 163 | ASSERT(0 <= digit && digit <= 9); |
michael@0 | 164 | result = 10 * result + digit; |
michael@0 | 165 | } |
michael@0 | 166 | *number_of_read_digits = i; |
michael@0 | 167 | return result; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | // Reads a DiyFp from the buffer. |
michael@0 | 172 | // The returned DiyFp is not necessarily normalized. |
michael@0 | 173 | // If remaining_decimals is zero then the returned DiyFp is accurate. |
michael@0 | 174 | // Otherwise it has been rounded and has error of at most 1/2 ulp. |
michael@0 | 175 | static void ReadDiyFp(Vector<const char> buffer, |
michael@0 | 176 | DiyFp* result, |
michael@0 | 177 | int* remaining_decimals) { |
michael@0 | 178 | int read_digits; |
michael@0 | 179 | uint64_t significand = ReadUint64(buffer, &read_digits); |
michael@0 | 180 | if (buffer.length() == read_digits) { |
michael@0 | 181 | *result = DiyFp(significand, 0); |
michael@0 | 182 | *remaining_decimals = 0; |
michael@0 | 183 | } else { |
michael@0 | 184 | // Round the significand. |
michael@0 | 185 | if (buffer[read_digits] >= '5') { |
michael@0 | 186 | significand++; |
michael@0 | 187 | } |
michael@0 | 188 | // Compute the binary exponent. |
michael@0 | 189 | int exponent = 0; |
michael@0 | 190 | *result = DiyFp(significand, exponent); |
michael@0 | 191 | *remaining_decimals = buffer.length() - read_digits; |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | |
michael@0 | 196 | static bool DoubleStrtod(Vector<const char> trimmed, |
michael@0 | 197 | int exponent, |
michael@0 | 198 | double* result) { |
michael@0 | 199 | #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) |
michael@0 | 200 | // On x86 the floating-point stack can be 64 or 80 bits wide. If it is |
michael@0 | 201 | // 80 bits wide (as is the case on Linux) then double-rounding occurs and the |
michael@0 | 202 | // result is not accurate. |
michael@0 | 203 | // We know that Windows32 uses 64 bits and is therefore accurate. |
michael@0 | 204 | // Note that the ARM simulator is compiled for 32bits. It therefore exhibits |
michael@0 | 205 | // the same problem. |
michael@0 | 206 | return false; |
michael@0 | 207 | #endif |
michael@0 | 208 | if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { |
michael@0 | 209 | int read_digits; |
michael@0 | 210 | // The trimmed input fits into a double. |
michael@0 | 211 | // If the 10^exponent (resp. 10^-exponent) fits into a double too then we |
michael@0 | 212 | // can compute the result-double simply by multiplying (resp. dividing) the |
michael@0 | 213 | // two numbers. |
michael@0 | 214 | // This is possible because IEEE guarantees that floating-point operations |
michael@0 | 215 | // return the best possible approximation. |
michael@0 | 216 | if (exponent < 0 && -exponent < kExactPowersOfTenSize) { |
michael@0 | 217 | // 10^-exponent fits into a double. |
michael@0 | 218 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
michael@0 | 219 | ASSERT(read_digits == trimmed.length()); |
michael@0 | 220 | *result /= exact_powers_of_ten[-exponent]; |
michael@0 | 221 | return true; |
michael@0 | 222 | } |
michael@0 | 223 | if (0 <= exponent && exponent < kExactPowersOfTenSize) { |
michael@0 | 224 | // 10^exponent fits into a double. |
michael@0 | 225 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
michael@0 | 226 | ASSERT(read_digits == trimmed.length()); |
michael@0 | 227 | *result *= exact_powers_of_ten[exponent]; |
michael@0 | 228 | return true; |
michael@0 | 229 | } |
michael@0 | 230 | int remaining_digits = |
michael@0 | 231 | kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); |
michael@0 | 232 | if ((0 <= exponent) && |
michael@0 | 233 | (exponent - remaining_digits < kExactPowersOfTenSize)) { |
michael@0 | 234 | // The trimmed string was short and we can multiply it with |
michael@0 | 235 | // 10^remaining_digits. As a result the remaining exponent now fits |
michael@0 | 236 | // into a double too. |
michael@0 | 237 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
michael@0 | 238 | ASSERT(read_digits == trimmed.length()); |
michael@0 | 239 | *result *= exact_powers_of_ten[remaining_digits]; |
michael@0 | 240 | *result *= exact_powers_of_ten[exponent - remaining_digits]; |
michael@0 | 241 | return true; |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | return false; |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | |
michael@0 | 248 | // Returns 10^exponent as an exact DiyFp. |
michael@0 | 249 | // The given exponent must be in the range [1; kDecimalExponentDistance[. |
michael@0 | 250 | static DiyFp AdjustmentPowerOfTen(int exponent) { |
michael@0 | 251 | ASSERT(0 < exponent); |
michael@0 | 252 | ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); |
michael@0 | 253 | // Simply hardcode the remaining powers for the given decimal exponent |
michael@0 | 254 | // distance. |
michael@0 | 255 | ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); |
michael@0 | 256 | switch (exponent) { |
michael@0 | 257 | case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); |
michael@0 | 258 | case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); |
michael@0 | 259 | case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); |
michael@0 | 260 | case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); |
michael@0 | 261 | case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); |
michael@0 | 262 | case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); |
michael@0 | 263 | case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); |
michael@0 | 264 | default: |
michael@0 | 265 | UNREACHABLE(); |
michael@0 | 266 | return DiyFp(0, 0); |
michael@0 | 267 | } |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | |
michael@0 | 271 | // If the function returns true then the result is the correct double. |
michael@0 | 272 | // Otherwise it is either the correct double or the double that is just below |
michael@0 | 273 | // the correct double. |
michael@0 | 274 | static bool DiyFpStrtod(Vector<const char> buffer, |
michael@0 | 275 | int exponent, |
michael@0 | 276 | double* result) { |
michael@0 | 277 | DiyFp input; |
michael@0 | 278 | int remaining_decimals; |
michael@0 | 279 | ReadDiyFp(buffer, &input, &remaining_decimals); |
michael@0 | 280 | // Since we may have dropped some digits the input is not accurate. |
michael@0 | 281 | // If remaining_decimals is different than 0 than the error is at most |
michael@0 | 282 | // .5 ulp (unit in the last place). |
michael@0 | 283 | // We don't want to deal with fractions and therefore keep a common |
michael@0 | 284 | // denominator. |
michael@0 | 285 | const int kDenominatorLog = 3; |
michael@0 | 286 | const int kDenominator = 1 << kDenominatorLog; |
michael@0 | 287 | // Move the remaining decimals into the exponent. |
michael@0 | 288 | exponent += remaining_decimals; |
michael@0 | 289 | int error = (remaining_decimals == 0 ? 0 : kDenominator / 2); |
michael@0 | 290 | |
michael@0 | 291 | int old_e = input.e(); |
michael@0 | 292 | input.Normalize(); |
michael@0 | 293 | error <<= old_e - input.e(); |
michael@0 | 294 | |
michael@0 | 295 | ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); |
michael@0 | 296 | if (exponent < PowersOfTenCache::kMinDecimalExponent) { |
michael@0 | 297 | *result = 0.0; |
michael@0 | 298 | return true; |
michael@0 | 299 | } |
michael@0 | 300 | DiyFp cached_power; |
michael@0 | 301 | int cached_decimal_exponent; |
michael@0 | 302 | PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, |
michael@0 | 303 | &cached_power, |
michael@0 | 304 | &cached_decimal_exponent); |
michael@0 | 305 | |
michael@0 | 306 | if (cached_decimal_exponent != exponent) { |
michael@0 | 307 | int adjustment_exponent = exponent - cached_decimal_exponent; |
michael@0 | 308 | DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); |
michael@0 | 309 | input.Multiply(adjustment_power); |
michael@0 | 310 | if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { |
michael@0 | 311 | // The product of input with the adjustment power fits into a 64 bit |
michael@0 | 312 | // integer. |
michael@0 | 313 | ASSERT(DiyFp::kSignificandSize == 64); |
michael@0 | 314 | } else { |
michael@0 | 315 | // The adjustment power is exact. There is hence only an error of 0.5. |
michael@0 | 316 | error += kDenominator / 2; |
michael@0 | 317 | } |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | input.Multiply(cached_power); |
michael@0 | 321 | // The error introduced by a multiplication of a*b equals |
michael@0 | 322 | // error_a + error_b + error_a*error_b/2^64 + 0.5 |
michael@0 | 323 | // Substituting a with 'input' and b with 'cached_power' we have |
michael@0 | 324 | // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp), |
michael@0 | 325 | // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 |
michael@0 | 326 | int error_b = kDenominator / 2; |
michael@0 | 327 | int error_ab = (error == 0 ? 0 : 1); // We round up to 1. |
michael@0 | 328 | int fixed_error = kDenominator / 2; |
michael@0 | 329 | error += error_b + error_ab + fixed_error; |
michael@0 | 330 | |
michael@0 | 331 | old_e = input.e(); |
michael@0 | 332 | input.Normalize(); |
michael@0 | 333 | error <<= old_e - input.e(); |
michael@0 | 334 | |
michael@0 | 335 | // See if the double's significand changes if we add/subtract the error. |
michael@0 | 336 | int order_of_magnitude = DiyFp::kSignificandSize + input.e(); |
michael@0 | 337 | int effective_significand_size = |
michael@0 | 338 | Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); |
michael@0 | 339 | int precision_digits_count = |
michael@0 | 340 | DiyFp::kSignificandSize - effective_significand_size; |
michael@0 | 341 | if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { |
michael@0 | 342 | // This can only happen for very small denormals. In this case the |
michael@0 | 343 | // half-way multiplied by the denominator exceeds the range of an uint64. |
michael@0 | 344 | // Simply shift everything to the right. |
michael@0 | 345 | int shift_amount = (precision_digits_count + kDenominatorLog) - |
michael@0 | 346 | DiyFp::kSignificandSize + 1; |
michael@0 | 347 | input.set_f(input.f() >> shift_amount); |
michael@0 | 348 | input.set_e(input.e() + shift_amount); |
michael@0 | 349 | // We add 1 for the lost precision of error, and kDenominator for |
michael@0 | 350 | // the lost precision of input.f(). |
michael@0 | 351 | error = (error >> shift_amount) + 1 + kDenominator; |
michael@0 | 352 | precision_digits_count -= shift_amount; |
michael@0 | 353 | } |
michael@0 | 354 | // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. |
michael@0 | 355 | ASSERT(DiyFp::kSignificandSize == 64); |
michael@0 | 356 | ASSERT(precision_digits_count < 64); |
michael@0 | 357 | uint64_t one64 = 1; |
michael@0 | 358 | uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; |
michael@0 | 359 | uint64_t precision_bits = input.f() & precision_bits_mask; |
michael@0 | 360 | uint64_t half_way = one64 << (precision_digits_count - 1); |
michael@0 | 361 | precision_bits *= kDenominator; |
michael@0 | 362 | half_way *= kDenominator; |
michael@0 | 363 | DiyFp rounded_input(input.f() >> precision_digits_count, |
michael@0 | 364 | input.e() + precision_digits_count); |
michael@0 | 365 | if (precision_bits >= half_way + error) { |
michael@0 | 366 | rounded_input.set_f(rounded_input.f() + 1); |
michael@0 | 367 | } |
michael@0 | 368 | // If the last_bits are too close to the half-way case than we are too |
michael@0 | 369 | // inaccurate and round down. In this case we return false so that we can |
michael@0 | 370 | // fall back to a more precise algorithm. |
michael@0 | 371 | |
michael@0 | 372 | *result = Double(rounded_input).value(); |
michael@0 | 373 | if (half_way - error < precision_bits && precision_bits < half_way + error) { |
michael@0 | 374 | // Too imprecise. The caller will have to fall back to a slower version. |
michael@0 | 375 | // However the returned number is guaranteed to be either the correct |
michael@0 | 376 | // double, or the next-lower double. |
michael@0 | 377 | return false; |
michael@0 | 378 | } else { |
michael@0 | 379 | return true; |
michael@0 | 380 | } |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | |
michael@0 | 384 | // Returns |
michael@0 | 385 | // - -1 if buffer*10^exponent < diy_fp. |
michael@0 | 386 | // - 0 if buffer*10^exponent == diy_fp. |
michael@0 | 387 | // - +1 if buffer*10^exponent > diy_fp. |
michael@0 | 388 | // Preconditions: |
michael@0 | 389 | // buffer.length() + exponent <= kMaxDecimalPower + 1 |
michael@0 | 390 | // buffer.length() + exponent > kMinDecimalPower |
michael@0 | 391 | // buffer.length() <= kMaxDecimalSignificantDigits |
michael@0 | 392 | static int CompareBufferWithDiyFp(Vector<const char> buffer, |
michael@0 | 393 | int exponent, |
michael@0 | 394 | DiyFp diy_fp) { |
michael@0 | 395 | ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); |
michael@0 | 396 | ASSERT(buffer.length() + exponent > kMinDecimalPower); |
michael@0 | 397 | ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); |
michael@0 | 398 | // Make sure that the Bignum will be able to hold all our numbers. |
michael@0 | 399 | // Our Bignum implementation has a separate field for exponents. Shifts will |
michael@0 | 400 | // consume at most one bigit (< 64 bits). |
michael@0 | 401 | // ln(10) == 3.3219... |
michael@0 | 402 | ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); |
michael@0 | 403 | Bignum buffer_bignum; |
michael@0 | 404 | Bignum diy_fp_bignum; |
michael@0 | 405 | buffer_bignum.AssignDecimalString(buffer); |
michael@0 | 406 | diy_fp_bignum.AssignUInt64(diy_fp.f()); |
michael@0 | 407 | if (exponent >= 0) { |
michael@0 | 408 | buffer_bignum.MultiplyByPowerOfTen(exponent); |
michael@0 | 409 | } else { |
michael@0 | 410 | diy_fp_bignum.MultiplyByPowerOfTen(-exponent); |
michael@0 | 411 | } |
michael@0 | 412 | if (diy_fp.e() > 0) { |
michael@0 | 413 | diy_fp_bignum.ShiftLeft(diy_fp.e()); |
michael@0 | 414 | } else { |
michael@0 | 415 | buffer_bignum.ShiftLeft(-diy_fp.e()); |
michael@0 | 416 | } |
michael@0 | 417 | return Bignum::Compare(buffer_bignum, diy_fp_bignum); |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | |
michael@0 | 421 | // Returns true if the guess is the correct double. |
michael@0 | 422 | // Returns false, when guess is either correct or the next-lower double. |
michael@0 | 423 | static bool ComputeGuess(Vector<const char> trimmed, int exponent, |
michael@0 | 424 | double* guess) { |
michael@0 | 425 | if (trimmed.length() == 0) { |
michael@0 | 426 | *guess = 0.0; |
michael@0 | 427 | return true; |
michael@0 | 428 | } |
michael@0 | 429 | if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { |
michael@0 | 430 | *guess = Double::Infinity(); |
michael@0 | 431 | return true; |
michael@0 | 432 | } |
michael@0 | 433 | if (exponent + trimmed.length() <= kMinDecimalPower) { |
michael@0 | 434 | *guess = 0.0; |
michael@0 | 435 | return true; |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | if (DoubleStrtod(trimmed, exponent, guess) || |
michael@0 | 439 | DiyFpStrtod(trimmed, exponent, guess)) { |
michael@0 | 440 | return true; |
michael@0 | 441 | } |
michael@0 | 442 | if (*guess == Double::Infinity()) { |
michael@0 | 443 | return true; |
michael@0 | 444 | } |
michael@0 | 445 | return false; |
michael@0 | 446 | } |
michael@0 | 447 | |
michael@0 | 448 | double Strtod(Vector<const char> buffer, int exponent) { |
michael@0 | 449 | char copy_buffer[kMaxSignificantDecimalDigits]; |
michael@0 | 450 | Vector<const char> trimmed; |
michael@0 | 451 | int updated_exponent; |
michael@0 | 452 | TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, |
michael@0 | 453 | &trimmed, &updated_exponent); |
michael@0 | 454 | exponent = updated_exponent; |
michael@0 | 455 | |
michael@0 | 456 | double guess; |
michael@0 | 457 | bool is_correct = ComputeGuess(trimmed, exponent, &guess); |
michael@0 | 458 | if (is_correct) return guess; |
michael@0 | 459 | |
michael@0 | 460 | DiyFp upper_boundary = Double(guess).UpperBoundary(); |
michael@0 | 461 | int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); |
michael@0 | 462 | if (comparison < 0) { |
michael@0 | 463 | return guess; |
michael@0 | 464 | } else if (comparison > 0) { |
michael@0 | 465 | return Double(guess).NextDouble(); |
michael@0 | 466 | } else if ((Double(guess).Significand() & 1) == 0) { |
michael@0 | 467 | // Round towards even. |
michael@0 | 468 | return guess; |
michael@0 | 469 | } else { |
michael@0 | 470 | return Double(guess).NextDouble(); |
michael@0 | 471 | } |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | float Strtof(Vector<const char> buffer, int exponent) { |
michael@0 | 475 | char copy_buffer[kMaxSignificantDecimalDigits]; |
michael@0 | 476 | Vector<const char> trimmed; |
michael@0 | 477 | int updated_exponent; |
michael@0 | 478 | TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, |
michael@0 | 479 | &trimmed, &updated_exponent); |
michael@0 | 480 | exponent = updated_exponent; |
michael@0 | 481 | |
michael@0 | 482 | double double_guess; |
michael@0 | 483 | bool is_correct = ComputeGuess(trimmed, exponent, &double_guess); |
michael@0 | 484 | |
michael@0 | 485 | float float_guess = static_cast<float>(double_guess); |
michael@0 | 486 | if (float_guess == double_guess) { |
michael@0 | 487 | // This shortcut triggers for integer values. |
michael@0 | 488 | return float_guess; |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | // We must catch double-rounding. Say the double has been rounded up, and is |
michael@0 | 492 | // now a boundary of a float, and rounds up again. This is why we have to |
michael@0 | 493 | // look at previous too. |
michael@0 | 494 | // Example (in decimal numbers): |
michael@0 | 495 | // input: 12349 |
michael@0 | 496 | // high-precision (4 digits): 1235 |
michael@0 | 497 | // low-precision (3 digits): |
michael@0 | 498 | // when read from input: 123 |
michael@0 | 499 | // when rounded from high precision: 124. |
michael@0 | 500 | // To do this we simply look at the neigbors of the correct result and see |
michael@0 | 501 | // if they would round to the same float. If the guess is not correct we have |
michael@0 | 502 | // to look at four values (since two different doubles could be the correct |
michael@0 | 503 | // double). |
michael@0 | 504 | |
michael@0 | 505 | double double_next = Double(double_guess).NextDouble(); |
michael@0 | 506 | double double_previous = Double(double_guess).PreviousDouble(); |
michael@0 | 507 | |
michael@0 | 508 | float f1 = static_cast<float>(double_previous); |
michael@0 | 509 | #if defined(DEBUG) |
michael@0 | 510 | float f2 = float_guess; |
michael@0 | 511 | #endif |
michael@0 | 512 | float f3 = static_cast<float>(double_next); |
michael@0 | 513 | float f4; |
michael@0 | 514 | if (is_correct) { |
michael@0 | 515 | f4 = f3; |
michael@0 | 516 | } else { |
michael@0 | 517 | double double_next2 = Double(double_next).NextDouble(); |
michael@0 | 518 | f4 = static_cast<float>(double_next2); |
michael@0 | 519 | } |
michael@0 | 520 | ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4); |
michael@0 | 521 | |
michael@0 | 522 | // If the guess doesn't lie near a single-precision boundary we can simply |
michael@0 | 523 | // return its float-value. |
michael@0 | 524 | if (f1 == f4) { |
michael@0 | 525 | return float_guess; |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | ASSERT((f1 != f2 && f2 == f3 && f3 == f4) || |
michael@0 | 529 | (f1 == f2 && f2 != f3 && f3 == f4) || |
michael@0 | 530 | (f1 == f2 && f2 == f3 && f3 != f4)); |
michael@0 | 531 | |
michael@0 | 532 | // guess and next are the two possible canditates (in the same way that |
michael@0 | 533 | // double_guess was the lower candidate for a double-precision guess). |
michael@0 | 534 | float guess = f1; |
michael@0 | 535 | float next = f4; |
michael@0 | 536 | DiyFp upper_boundary; |
michael@0 | 537 | if (guess == 0.0f) { |
michael@0 | 538 | float min_float = 1e-45f; |
michael@0 | 539 | upper_boundary = Double(static_cast<double>(min_float) / 2).AsDiyFp(); |
michael@0 | 540 | } else { |
michael@0 | 541 | upper_boundary = Single(guess).UpperBoundary(); |
michael@0 | 542 | } |
michael@0 | 543 | int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); |
michael@0 | 544 | if (comparison < 0) { |
michael@0 | 545 | return guess; |
michael@0 | 546 | } else if (comparison > 0) { |
michael@0 | 547 | return next; |
michael@0 | 548 | } else if ((Single(guess).Significand() & 1) == 0) { |
michael@0 | 549 | // Round towards even. |
michael@0 | 550 | return guess; |
michael@0 | 551 | } else { |
michael@0 | 552 | return next; |
michael@0 | 553 | } |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | } // namespace double_conversion |