Decimal.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Imported from:
  32. * https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/platform/Decimal.h
  33. * Check UPSTREAM-GIT-SHA for the commit ID of the last update from Blink core.
  34. */
  35. #ifndef Decimal_h
  36. #define Decimal_h
  37. #include "mozilla/Assertions.h"
  38. #include <stdint.h>
  39. #include "mozilla/Types.h"
  40. #include <string>
  41. #ifndef ASSERT
  42. #define DEFINED_ASSERT_FOR_DECIMAL_H 1
  43. #define ASSERT MOZ_ASSERT
  44. #endif
  45. #define PLATFORM_EXPORT
  46. // To use USING_FAST_MALLOC we'd need:
  47. // https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/wtf/Allocator.h
  48. // Since we don't allocate Decimal objects, no need.
  49. #define USING_FAST_MALLOC(type) \
  50. void ignore_this_dummy_method() = delete
  51. #define DISALLOW_NEW() \
  52. private: \
  53. void* operator new(size_t) = delete; \
  54. void* operator new(size_t, void*) = delete; \
  55. public:
  56. namespace blink {
  57. namespace DecimalPrivate {
  58. class SpecialValueHandler;
  59. }
  60. // This class represents decimal base floating point number.
  61. //
  62. // FIXME: Once all C++ compiler support decimal type, we should replace this
  63. // class to compiler supported one. See below URI for current status of decimal
  64. // type for C++: // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1977.html
  65. class PLATFORM_EXPORT Decimal {
  66. USING_FAST_MALLOC(Decimal);
  67. public:
  68. enum Sign {
  69. Positive,
  70. Negative,
  71. };
  72. // You should not use EncodedData other than unit testing.
  73. class EncodedData {
  74. DISALLOW_NEW();
  75. // For accessing FormatClass.
  76. friend class Decimal;
  77. friend class DecimalPrivate::SpecialValueHandler;
  78. public:
  79. EncodedData(Sign, int exponent, uint64_t coefficient);
  80. bool operator==(const EncodedData&) const;
  81. bool operator!=(const EncodedData& another) const { return !operator==(another); }
  82. uint64_t coefficient() const { return m_coefficient; }
  83. int countDigits() const;
  84. int exponent() const { return m_exponent; }
  85. bool isFinite() const { return !isSpecial(); }
  86. bool isInfinity() const { return m_formatClass == ClassInfinity; }
  87. bool isNaN() const { return m_formatClass == ClassNaN; }
  88. bool isSpecial() const { return m_formatClass == ClassInfinity || m_formatClass == ClassNaN; }
  89. bool isZero() const { return m_formatClass == ClassZero; }
  90. Sign sign() const { return m_sign; }
  91. void setSign(Sign sign) { m_sign = sign; }
  92. private:
  93. enum FormatClass {
  94. ClassInfinity,
  95. ClassNormal,
  96. ClassNaN,
  97. ClassZero,
  98. };
  99. EncodedData(Sign, FormatClass);
  100. FormatClass formatClass() const { return m_formatClass; }
  101. uint64_t m_coefficient;
  102. int16_t m_exponent;
  103. FormatClass m_formatClass;
  104. Sign m_sign;
  105. };
  106. MFBT_API explicit Decimal(int32_t = 0);
  107. MFBT_API Decimal(Sign, int exponent, uint64_t coefficient);
  108. MFBT_API Decimal(const Decimal&);
  109. MFBT_API Decimal& operator=(const Decimal&);
  110. MFBT_API Decimal& operator+=(const Decimal&);
  111. MFBT_API Decimal& operator-=(const Decimal&);
  112. MFBT_API Decimal& operator*=(const Decimal&);
  113. MFBT_API Decimal& operator/=(const Decimal&);
  114. MFBT_API Decimal operator-() const;
  115. MFBT_API bool operator==(const Decimal&) const;
  116. MFBT_API bool operator!=(const Decimal&) const;
  117. MFBT_API bool operator<(const Decimal&) const;
  118. MFBT_API bool operator<=(const Decimal&) const;
  119. MFBT_API bool operator>(const Decimal&) const;
  120. MFBT_API bool operator>=(const Decimal&) const;
  121. MFBT_API Decimal operator+(const Decimal&) const;
  122. MFBT_API Decimal operator-(const Decimal&) const;
  123. MFBT_API Decimal operator*(const Decimal&) const;
  124. MFBT_API Decimal operator/(const Decimal&) const;
  125. int exponent() const
  126. {
  127. ASSERT(isFinite());
  128. return m_data.exponent();
  129. }
  130. bool isFinite() const { return m_data.isFinite(); }
  131. bool isInfinity() const { return m_data.isInfinity(); }
  132. bool isNaN() const { return m_data.isNaN(); }
  133. bool isNegative() const { return sign() == Negative; }
  134. bool isPositive() const { return sign() == Positive; }
  135. bool isSpecial() const { return m_data.isSpecial(); }
  136. bool isZero() const { return m_data.isZero(); }
  137. MFBT_API Decimal abs() const;
  138. MFBT_API Decimal ceil() const;
  139. MFBT_API Decimal floor() const;
  140. MFBT_API Decimal remainder(const Decimal&) const;
  141. MFBT_API Decimal round() const;
  142. MFBT_API double toDouble() const;
  143. // Note: toString method supports infinity and nan but fromString not.
  144. MFBT_API std::string toString() const;
  145. MFBT_API bool toString(char* strBuf, size_t bufLength) const;
  146. static MFBT_API Decimal fromDouble(double);
  147. // fromString supports following syntax EBNF:
  148. // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)?
  149. // | sign? '.' digit+ (exponent-marker sign? digit+)?
  150. // sign ::= '+' | '-'
  151. // exponent-marker ::= 'e' | 'E'
  152. // digit ::= '0' | '1' | ... | '9'
  153. // Note: fromString doesn't support "infinity" and "nan".
  154. static MFBT_API Decimal fromString(const std::string& aValue);
  155. static MFBT_API Decimal infinity(Sign);
  156. static MFBT_API Decimal nan();
  157. static MFBT_API Decimal zero(Sign);
  158. // You should not use below methods. We expose them for unit testing.
  159. MFBT_API explicit Decimal(const EncodedData&);
  160. const EncodedData& value() const { return m_data; }
  161. private:
  162. struct AlignedOperands {
  163. uint64_t lhsCoefficient;
  164. uint64_t rhsCoefficient;
  165. int exponent;
  166. };
  167. MFBT_API explicit Decimal(double);
  168. MFBT_API Decimal compareTo(const Decimal&) const;
  169. static MFBT_API AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs);
  170. static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; }
  171. Sign sign() const { return m_data.sign(); }
  172. EncodedData m_data;
  173. };
  174. } // namespace blink
  175. namespace mozilla {
  176. typedef blink::Decimal Decimal;
  177. } // namespace mozilla
  178. #undef USING_FAST_MALLOC
  179. #ifdef DEFINED_ASSERT_FOR_DECIMAL_H
  180. #undef DEFINED_ASSERT_FOR_DECIMAL_H
  181. #undef ASSERT
  182. #endif
  183. #endif // Decimal_h