moz-constexpr-decimal.patch (7547B)
1 diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp 2 index 7fdf38c5cb7c3..8bce5c3312f55 100644 3 --- a/dom/html/HTMLInputElement.cpp 4 +++ b/dom/html/HTMLInputElement.cpp 5 @@ -187,16 +187,18 @@ static const nsAttrValue::EnumTable kCaptureTable[] = { 6 7 static const nsAttrValue::EnumTable* kCaptureDefault = &kCaptureTable[2]; 8 9 -const Decimal HTMLInputElement::kStepScaleFactorDate = Decimal(86400000); 10 -const Decimal HTMLInputElement::kStepScaleFactorNumberRange = Decimal(1); 11 -const Decimal HTMLInputElement::kStepScaleFactorTime = Decimal(1000); 12 -const Decimal HTMLInputElement::kStepScaleFactorMonth = Decimal(1); 13 -const Decimal HTMLInputElement::kStepScaleFactorWeek = Decimal(7 * 86400000); 14 -const Decimal HTMLInputElement::kDefaultStepBase = Decimal(0); 15 -const Decimal HTMLInputElement::kDefaultStepBaseWeek = Decimal(-259200000); 16 -const Decimal HTMLInputElement::kDefaultStep = Decimal(1); 17 -const Decimal HTMLInputElement::kDefaultStepTime = Decimal(60); 18 -const Decimal HTMLInputElement::kStepAny = Decimal(0); 19 +using namespace blink; 20 + 21 +constexpr Decimal HTMLInputElement::kStepScaleFactorDate(86400000_d); 22 +constexpr Decimal HTMLInputElement::kStepScaleFactorNumberRange(1_d); 23 +constexpr Decimal HTMLInputElement::kStepScaleFactorTime(1000_d); 24 +constexpr Decimal HTMLInputElement::kStepScaleFactorMonth(1_d); 25 +constexpr Decimal HTMLInputElement::kStepScaleFactorWeek(7 * 86400000_d); 26 +constexpr Decimal HTMLInputElement::kDefaultStepBase(0_d); 27 +constexpr Decimal HTMLInputElement::kDefaultStepBaseWeek(-259200000_d); 28 +constexpr Decimal HTMLInputElement::kDefaultStep(1_d); 29 +constexpr Decimal HTMLInputElement::kDefaultStepTime(60_d); 30 +constexpr Decimal HTMLInputElement::kStepAny(0_d); 31 32 const double HTMLInputElement::kMinimumYear = 1; 33 const double HTMLInputElement::kMaximumYear = 275760; 34 diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp 35 index cc828e28439f5..7d2bcfa712c5d 100644 36 --- a/mozglue/misc/decimal/Decimal.cpp 37 +++ b/mozglue/misc/decimal/Decimal.cpp 38 @@ -41,12 +41,6 @@ namespace blink { 39 40 namespace DecimalPrivate { 41 42 -static int const ExponentMax = 1023; 43 -static int const ExponentMin = -1023; 44 -static int const Precision = 18; 45 - 46 -static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's 47 - 48 // This class handles Decimal special values. 49 class SpecialValueHandler { 50 STACK_ALLOCATED(); 51 @@ -230,43 +224,6 @@ static uint64_t scaleUp(uint64_t x, int n) 52 53 using namespace DecimalPrivate; 54 55 -Decimal::EncodedData::EncodedData(Sign sign, FormatClass formatClass) 56 - : m_coefficient(0) 57 - , m_exponent(0) 58 - , m_formatClass(formatClass) 59 - , m_sign(sign) 60 -{ 61 -} 62 - 63 -Decimal::EncodedData::EncodedData(Sign sign, int exponent, uint64_t coefficient) 64 - : m_formatClass(coefficient ? ClassNormal : ClassZero) 65 - , m_sign(sign) 66 -{ 67 - if (exponent >= ExponentMin && exponent <= ExponentMax) { 68 - while (coefficient > MaxCoefficient) { 69 - coefficient /= 10; 70 - ++exponent; 71 - } 72 - } 73 - 74 - if (exponent > ExponentMax) { 75 - m_coefficient = 0; 76 - m_exponent = 0; 77 - m_formatClass = ClassInfinity; 78 - return; 79 - } 80 - 81 - if (exponent < ExponentMin) { 82 - m_coefficient = 0; 83 - m_exponent = 0; 84 - m_formatClass = ClassZero; 85 - return; 86 - } 87 - 88 - m_coefficient = coefficient; 89 - m_exponent = static_cast<int16_t>(exponent); 90 -} 91 - 92 bool Decimal::EncodedData::operator==(const EncodedData& another) const 93 { 94 return m_sign == another.m_sign 95 @@ -275,15 +232,12 @@ bool Decimal::EncodedData::operator==(const EncodedData& another) const 96 && m_coefficient == another.m_coefficient; 97 } 98 99 + 100 Decimal::Decimal(int32_t i32) 101 - : m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32)) 102 -{ 103 -} 104 + : Decimal(DecimalLiteral{i32}) {} 105 106 Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient) 107 - : m_data(sign, coefficient ? exponent : 0, coefficient) 108 -{ 109 -} 110 + : m_data(sign, coefficient ? exponent : 0, coefficient) {} 111 112 Decimal::Decimal(const EncodedData& data) 113 : m_data(data) 114 diff --git a/mozglue/misc/decimal/Decimal.h b/mozglue/misc/decimal/Decimal.h 115 index 10d0e2c7cefa3..4bb9a841e585f 100644 116 --- a/mozglue/misc/decimal/Decimal.h 117 +++ b/mozglue/misc/decimal/Decimal.h 118 @@ -65,9 +65,28 @@ 119 namespace blink { 120 121 namespace DecimalPrivate { 122 +constexpr int ExponentMax = 1023; 123 +constexpr int ExponentMin = -1023; 124 +constexpr int Precision = 18; 125 + 126 +static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's 127 class SpecialValueHandler; 128 } 129 130 +struct DecimalLiteral { 131 + int32_t value; 132 + friend constexpr DecimalLiteral operator*(int32_t lhs, DecimalLiteral rhs) { 133 + return {lhs * rhs.value}; 134 + } 135 + constexpr DecimalLiteral operator-() { 136 + return {-value}; 137 + } 138 +}; 139 + 140 +constexpr DecimalLiteral operator""_d(unsigned long long value) { 141 + return {static_cast<int32_t>(value)}; 142 +} 143 + 144 // This class represents decimal base floating point number. 145 // 146 // FIXME: Once all C++ compiler support decimal type, we should replace this 147 @@ -88,7 +107,32 @@ public: 148 friend class Decimal; 149 friend class DecimalPrivate::SpecialValueHandler; 150 public: 151 - EncodedData(Sign, int exponent, uint64_t coefficient); 152 + constexpr EncodedData(Sign sign, int exponent, uint64_t coefficient) 153 + : m_coefficient(0), 154 + m_exponent(0), 155 + m_formatClass(coefficient ? ClassNormal : ClassZero), 156 + m_sign(sign) { 157 + if (exponent >= DecimalPrivate::ExponentMin && 158 + exponent <= DecimalPrivate::ExponentMax) { 159 + while (coefficient > DecimalPrivate::MaxCoefficient) { 160 + coefficient /= 10; 161 + ++exponent; 162 + } 163 + } 164 + 165 + if (exponent > DecimalPrivate::ExponentMax) { 166 + m_formatClass = ClassInfinity; 167 + return; 168 + } 169 + 170 + if (exponent < DecimalPrivate::ExponentMin) { 171 + m_formatClass = ClassZero; 172 + return; 173 + } 174 + 175 + m_coefficient = coefficient; 176 + m_exponent = static_cast<int16_t>(exponent); 177 + } 178 179 bool operator==(const EncodedData&) const; 180 bool operator!=(const EncodedData& another) const { return !operator==(another); } 181 @@ -112,7 +156,12 @@ public: 182 ClassZero, 183 }; 184 185 - EncodedData(Sign, FormatClass); 186 + constexpr EncodedData(Sign sign, FormatClass formatClass) 187 + : m_coefficient(0), 188 + m_exponent(0), 189 + m_formatClass(formatClass), 190 + m_sign(sign) {} 191 + 192 FormatClass formatClass() const { return m_formatClass; } 193 194 uint64_t m_coefficient; 195 @@ -121,8 +170,13 @@ public: 196 Sign m_sign; 197 }; 198 199 - MFBT_API explicit Decimal(int32_t = 0); 200 - MFBT_API Decimal(Sign, int exponent, uint64_t coefficient); 201 + constexpr explicit Decimal(DecimalLiteral i32) 202 + : m_data(i32.value < 0 ? Negative : Positive, 0, 203 + i32.value < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32.value)) 204 + : static_cast<uint64_t>(i32.value)) {} 205 + 206 + MFBT_API explicit Decimal(int32_t i32 = 0); 207 + MFBT_API Decimal(Sign sign, int exponent, uint64_t coefficient); 208 MFBT_API Decimal(const Decimal&); 209 210 MFBT_API Decimal& operator=(const Decimal&); 211 @@ -209,6 +263,7 @@ private: 212 213 namespace mozilla { 214 typedef blink::Decimal Decimal; 215 +using blink::operator""_d; 216 } // namespace mozilla 217 218 #undef USING_FAST_MALLOC