mfbt/decimal/floor-ceiling.patch

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/decimal/floor-ceiling.patch	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +diff --git a/mfbt/decimal/Decimal.cpp b/mfbt/decimal/Decimal.cpp
     1.5 +--- a/mfbt/decimal/Decimal.cpp
     1.6 ++++ b/mfbt/decimal/Decimal.cpp
     1.7 +@@ -618,26 +618,26 @@ Decimal::AlignedOperands Decimal::alignO
     1.8 + Decimal Decimal::ceiling() const
     1.9 + {
    1.10 +     if (isSpecial())
    1.11 +         return *this;
    1.12 + 
    1.13 +     if (exponent() >= 0)
    1.14 +         return *this;
    1.15 + 
    1.16 +-    uint64_t result = m_data.coefficient();
    1.17 +-    const int numberOfDigits = countDigits(result);
    1.18 ++    uint64_t coefficient = m_data.coefficient();
    1.19 ++    const int numberOfDigits = countDigits(coefficient);
    1.20 +     const int numberOfDropDigits = -exponent();
    1.21 +     if (numberOfDigits < numberOfDropDigits)
    1.22 +         return isPositive() ? Decimal(1) : zero(Positive);
    1.23 + 
    1.24 +-    result = scaleDown(result, numberOfDropDigits - 1);
    1.25 +-    if (sign() == Positive && result % 10 > 0)
    1.26 +-        result += 10;
    1.27 +-    result /= 10;
    1.28 ++    uint64_t result = scaleDown(coefficient, numberOfDropDigits);
    1.29 ++    uint64_t droppedDigits = coefficient - scaleUp(result, numberOfDropDigits);
    1.30 ++    if (droppedDigits && isPositive())
    1.31 ++        result += 1;
    1.32 +     return Decimal(sign(), 0, result);
    1.33 + }
    1.34 + 
    1.35 + Decimal Decimal::compareTo(const Decimal& rhs) const
    1.36 + {
    1.37 +     const Decimal result(*this - rhs);
    1.38 +     switch (result.m_data.formatClass()) {
    1.39 +     case EncodedData::ClassInfinity:
    1.40 +@@ -660,26 +660,27 @@ Decimal Decimal::compareTo(const Decimal
    1.41 + Decimal Decimal::floor() const
    1.42 + {
    1.43 +     if (isSpecial())
    1.44 +         return *this;
    1.45 + 
    1.46 +     if (exponent() >= 0)
    1.47 +         return *this;
    1.48 + 
    1.49 +-    uint64_t result = m_data.coefficient();
    1.50 +-    const int numberOfDigits = countDigits(result);
    1.51 ++    uint64_t coefficient = m_data.coefficient();
    1.52 ++    const int numberOfDigits = countDigits(coefficient);
    1.53 +     const int numberOfDropDigits = -exponent();
    1.54 +     if (numberOfDigits < numberOfDropDigits)
    1.55 +         return isPositive() ? zero(Positive) : Decimal(-1);
    1.56 + 
    1.57 +-    result = scaleDown(result, numberOfDropDigits - 1);
    1.58 +-    if (isNegative() && result % 10 > 0)
    1.59 +-        result += 10;
    1.60 +-    result /= 10;
    1.61 ++    uint64_t result = scaleDown(coefficient, numberOfDropDigits);
    1.62 ++    uint64_t droppedDigits = coefficient - scaleUp(result, numberOfDropDigits);
    1.63 ++    if (droppedDigits && isNegative()) {
    1.64 ++        result += 1;
    1.65 ++    }
    1.66 +     return Decimal(sign(), 0, result);
    1.67 + }
    1.68 + 
    1.69 + Decimal Decimal::fromDouble(double doubleValue)
    1.70 + {
    1.71 +     if (std::isfinite(doubleValue))
    1.72 +         return fromString(String::numberToStringECMAScript(doubleValue));
    1.73 + 
    1.74 +@@ -915,16 +916,18 @@ Decimal Decimal::round() const
    1.75 +         return *this;
    1.76 + 
    1.77 +     uint64_t result = m_data.coefficient();
    1.78 +     const int numberOfDigits = countDigits(result);
    1.79 +     const int numberOfDropDigits = -exponent();
    1.80 +     if (numberOfDigits < numberOfDropDigits)
    1.81 +         return zero(Positive);
    1.82 + 
    1.83 ++    // We're implementing round-half-away-from-zero, so we only need the one
    1.84 ++    // (the most significant) fractional digit:
    1.85 +     result = scaleDown(result, numberOfDropDigits - 1);
    1.86 +     if (result % 10 >= 5)
    1.87 +         result += 10;
    1.88 +     result /= 10;
    1.89 +     return Decimal(sign(), 0, result);
    1.90 + }
    1.91 + 
    1.92 + double Decimal::toDouble() const

mercurial