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