mfbt/decimal/Decimal.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial