|
1 /* |
|
2 * Copyright (C) 2012 Google Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are |
|
6 * met: |
|
7 * |
|
8 * * Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * * Redistributions in binary form must reproduce the above |
|
11 * copyright notice, this list of conditions and the following disclaimer |
|
12 * in the documentation and/or other materials provided with the |
|
13 * distribution. |
|
14 * * Neither the name of Google Inc. nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from |
|
16 * this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 /** |
|
32 * Imported from: |
|
33 * http://src.chromium.org/viewvc/blink/trunk/Source/core/platform/Decimal.h |
|
34 * Check hg log for the svn rev of the last update from Blink core. |
|
35 */ |
|
36 |
|
37 #ifndef Decimal_h |
|
38 #define Decimal_h |
|
39 |
|
40 #include "mozilla/Assertions.h" |
|
41 #include <stdint.h> |
|
42 #include "mozilla/Types.h" |
|
43 |
|
44 #include <string> |
|
45 |
|
46 #ifndef ASSERT |
|
47 #define DEFINED_ASSERT_FOR_DECIMAL_H 1 |
|
48 #define ASSERT MOZ_ASSERT |
|
49 #endif |
|
50 |
|
51 // To use WTF_MAKE_FAST_ALLOCATED we'd need: |
|
52 // http://src.chromium.org/viewvc/blink/trunk/Source/wtf/FastMalloc.h |
|
53 // Since we don't allocate Decimal objects, no need. |
|
54 #define WTF_MAKE_FAST_ALLOCATED \ |
|
55 void ignore_this_dummy_method() MOZ_DELETE |
|
56 |
|
57 namespace WebCore { |
|
58 |
|
59 namespace DecimalPrivate { |
|
60 class SpecialValueHandler; |
|
61 } |
|
62 |
|
63 // This class represents decimal base floating point number. |
|
64 // |
|
65 // FIXME: Once all C++ compiler support decimal type, we should replace this |
|
66 // class to compiler supported one. See below URI for current status of decimal |
|
67 // type for C++: // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1977.html |
|
68 class Decimal { |
|
69 WTF_MAKE_FAST_ALLOCATED; |
|
70 public: |
|
71 enum Sign { |
|
72 Positive, |
|
73 Negative, |
|
74 }; |
|
75 |
|
76 // You should not use EncodedData other than unit testing. |
|
77 class EncodedData { |
|
78 // For accessing FormatClass. |
|
79 friend class Decimal; |
|
80 friend class DecimalPrivate::SpecialValueHandler; |
|
81 public: |
|
82 EncodedData(Sign, int exponent, uint64_t coefficient); |
|
83 |
|
84 bool operator==(const EncodedData&) const; |
|
85 bool operator!=(const EncodedData& another) const { return !operator==(another); } |
|
86 |
|
87 uint64_t coefficient() const { return m_coefficient; } |
|
88 int countDigits() const; |
|
89 int exponent() const { return m_exponent; } |
|
90 bool isFinite() const { return !isSpecial(); } |
|
91 bool isInfinity() const { return m_formatClass == ClassInfinity; } |
|
92 bool isNaN() const { return m_formatClass == ClassNaN; } |
|
93 bool isSpecial() const { return m_formatClass == ClassInfinity || m_formatClass == ClassNaN; } |
|
94 bool isZero() const { return m_formatClass == ClassZero; } |
|
95 Sign sign() const { return m_sign; } |
|
96 void setSign(Sign sign) { m_sign = sign; } |
|
97 |
|
98 private: |
|
99 enum FormatClass { |
|
100 ClassInfinity, |
|
101 ClassNormal, |
|
102 ClassNaN, |
|
103 ClassZero, |
|
104 }; |
|
105 |
|
106 EncodedData(Sign, FormatClass); |
|
107 FormatClass formatClass() const { return m_formatClass; } |
|
108 |
|
109 uint64_t m_coefficient; |
|
110 int16_t m_exponent; |
|
111 FormatClass m_formatClass; |
|
112 Sign m_sign; |
|
113 }; |
|
114 |
|
115 MFBT_API Decimal(int32_t = 0); |
|
116 MFBT_API Decimal(Sign, int exponent, uint64_t coefficient); |
|
117 MFBT_API Decimal(const Decimal&); |
|
118 |
|
119 MFBT_API Decimal& operator=(const Decimal&); |
|
120 MFBT_API Decimal& operator+=(const Decimal&); |
|
121 MFBT_API Decimal& operator-=(const Decimal&); |
|
122 MFBT_API Decimal& operator*=(const Decimal&); |
|
123 MFBT_API Decimal& operator/=(const Decimal&); |
|
124 |
|
125 MFBT_API Decimal operator-() const; |
|
126 |
|
127 MFBT_API bool operator==(const Decimal&) const; |
|
128 MFBT_API bool operator!=(const Decimal&) const; |
|
129 MFBT_API bool operator<(const Decimal&) const; |
|
130 MFBT_API bool operator<=(const Decimal&) const; |
|
131 MFBT_API bool operator>(const Decimal&) const; |
|
132 MFBT_API bool operator>=(const Decimal&) const; |
|
133 |
|
134 MFBT_API Decimal operator+(const Decimal&) const; |
|
135 MFBT_API Decimal operator-(const Decimal&) const; |
|
136 MFBT_API Decimal operator*(const Decimal&) const; |
|
137 MFBT_API Decimal operator/(const Decimal&) const; |
|
138 |
|
139 int exponent() const |
|
140 { |
|
141 ASSERT(isFinite()); |
|
142 return m_data.exponent(); |
|
143 } |
|
144 |
|
145 bool isFinite() const { return m_data.isFinite(); } |
|
146 bool isInfinity() const { return m_data.isInfinity(); } |
|
147 bool isNaN() const { return m_data.isNaN(); } |
|
148 bool isNegative() const { return sign() == Negative; } |
|
149 bool isPositive() const { return sign() == Positive; } |
|
150 bool isSpecial() const { return m_data.isSpecial(); } |
|
151 bool isZero() const { return m_data.isZero(); } |
|
152 |
|
153 MFBT_API Decimal abs() const; |
|
154 MFBT_API Decimal ceiling() const; |
|
155 MFBT_API Decimal floor() const; |
|
156 MFBT_API Decimal remainder(const Decimal&) const; |
|
157 MFBT_API Decimal round() const; |
|
158 |
|
159 MFBT_API double toDouble() const; |
|
160 // Note: toString method supports infinity and nan but fromString not. |
|
161 MFBT_API std::string toString() const; |
|
162 MFBT_API bool toString(char* strBuf, size_t bufLength) const; |
|
163 |
|
164 static MFBT_API Decimal fromDouble(double); |
|
165 // fromString supports following syntax EBNF: |
|
166 // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)? |
|
167 // | sign? '.' digit+ (exponent-marker sign? digit+)? |
|
168 // sign ::= '+' | '-' |
|
169 // exponent-marker ::= 'e' | 'E' |
|
170 // digit ::= '0' | '1' | ... | '9' |
|
171 // Note: fromString doesn't support "infinity" and "nan". |
|
172 static MFBT_API Decimal fromString(const std::string& aValue); |
|
173 static MFBT_API Decimal infinity(Sign); |
|
174 static MFBT_API Decimal nan(); |
|
175 static MFBT_API Decimal zero(Sign); |
|
176 |
|
177 // You should not use below methods. We expose them for unit testing. |
|
178 MFBT_API explicit Decimal(const EncodedData&); |
|
179 const EncodedData& value() const { return m_data; } |
|
180 |
|
181 private: |
|
182 struct AlignedOperands { |
|
183 uint64_t lhsCoefficient; |
|
184 uint64_t rhsCoefficient; |
|
185 int exponent; |
|
186 }; |
|
187 |
|
188 MFBT_API Decimal(double); |
|
189 MFBT_API Decimal compareTo(const Decimal&) const; |
|
190 |
|
191 static MFBT_API AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs); |
|
192 static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; } |
|
193 |
|
194 Sign sign() const { return m_data.sign(); } |
|
195 |
|
196 EncodedData m_data; |
|
197 }; |
|
198 |
|
199 } // namespace WebCore |
|
200 |
|
201 namespace mozilla { |
|
202 typedef WebCore::Decimal Decimal; |
|
203 } |
|
204 |
|
205 #undef WTF_MAKE_FAST_ALLOCATED |
|
206 |
|
207 #ifdef DEFINED_ASSERT_FOR_DECIMAL_H |
|
208 #undef DEFINED_ASSERT_FOR_DECIMAL_H |
|
209 #undef ASSERT |
|
210 #endif |
|
211 |
|
212 #endif // Decimal_h |
|
213 |