summaryrefslogtreecommitdiff
path: root/mfbt/decimal/floor-ceiling.patch
blob: 9e26787444ec8a3bb4bf16c500f6ceb6eadfe4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
diff --git a/mfbt/decimal/Decimal.cpp b/mfbt/decimal/Decimal.cpp
--- a/mfbt/decimal/Decimal.cpp
+++ b/mfbt/decimal/Decimal.cpp
@@ -618,26 +618,26 @@ Decimal::AlignedOperands Decimal::alignO
 Decimal Decimal::ceiling() const
 {
     if (isSpecial())
         return *this;
 
     if (exponent() >= 0)
         return *this;
 
-    uint64_t result = m_data.coefficient();
-    const int numberOfDigits = countDigits(result);
+    uint64_t coefficient = m_data.coefficient();
+    const int numberOfDigits = countDigits(coefficient);
     const int numberOfDropDigits = -exponent();
     if (numberOfDigits < numberOfDropDigits)
         return isPositive() ? Decimal(1) : zero(Positive);
 
-    result = scaleDown(result, numberOfDropDigits - 1);
-    if (sign() == Positive && result % 10 > 0)
-        result += 10;
-    result /= 10;
+    uint64_t result = scaleDown(coefficient, numberOfDropDigits);
+    uint64_t droppedDigits = coefficient - scaleUp(result, numberOfDropDigits);
+    if (droppedDigits && isPositive())
+        result += 1;
     return Decimal(sign(), 0, result);
 }
 
 Decimal Decimal::compareTo(const Decimal& rhs) const
 {
     const Decimal result(*this - rhs);
     switch (result.m_data.formatClass()) {
     case EncodedData::ClassInfinity:
@@ -660,26 +660,27 @@ Decimal Decimal::compareTo(const Decimal
 Decimal Decimal::floor() const
 {
     if (isSpecial())
         return *this;
 
     if (exponent() >= 0)
         return *this;
 
-    uint64_t result = m_data.coefficient();
-    const int numberOfDigits = countDigits(result);
+    uint64_t coefficient = m_data.coefficient();
+    const int numberOfDigits = countDigits(coefficient);
     const int numberOfDropDigits = -exponent();
     if (numberOfDigits < numberOfDropDigits)
         return isPositive() ? zero(Positive) : Decimal(-1);
 
-    result = scaleDown(result, numberOfDropDigits - 1);
-    if (isNegative() && result % 10 > 0)
-        result += 10;
-    result /= 10;
+    uint64_t result = scaleDown(coefficient, numberOfDropDigits);
+    uint64_t droppedDigits = coefficient - scaleUp(result, numberOfDropDigits);
+    if (droppedDigits && isNegative()) {
+        result += 1;
+    }
     return Decimal(sign(), 0, result);
 }
 
 Decimal Decimal::fromDouble(double doubleValue)
 {
     if (std::isfinite(doubleValue))
         return fromString(String::numberToStringECMAScript(doubleValue));
 
@@ -915,16 +916,18 @@ Decimal Decimal::round() const
         return *this;
 
     uint64_t result = m_data.coefficient();
     const int numberOfDigits = countDigits(result);
     const int numberOfDropDigits = -exponent();
     if (numberOfDigits < numberOfDropDigits)
         return zero(Positive);
 
+    // We're implementing round-half-away-from-zero, so we only need the one
+    // (the most significant) fractional digit:
     result = scaleDown(result, numberOfDropDigits - 1);
     if (result % 10 >= 5)
         result += 10;
     result /= 10;
     return Decimal(sign(), 0, result);
 }
 
 double Decimal::toDouble() const