Skip to content

Commit

Permalink
Bug 1245414, part 10 - Apply the Mozilla patches via mfbt/decimal/upd…
Browse files Browse the repository at this point in the history
…ate.sh. r=Waldo

--HG--
extra : rebase_source : 791e169635d9bb13f0a7a9593007a7ed3acaf3a0
  • Loading branch information
jwatt committed Feb 11, 2016
1 parent faa1155 commit 3389490
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 48 deletions.
37 changes: 28 additions & 9 deletions mfbt/decimal/Decimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "platform/Decimal.h"
#include "Decimal.h"
#include "moz-decimal-utils.h"

#include "wtf/Allocator.h"
#include "wtf/MathExtras.h"
#include "wtf/Noncopyable.h"
#include "wtf/text/StringBuilder.h"
using namespace moz_decimal_utils;

#include <algorithm>
#include <float.h>
Expand Down Expand Up @@ -282,7 +280,7 @@ Decimal::Decimal(int32_t i32)
}

Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient)
: m_data(sign, exponent, coefficient)
: m_data(sign, coefficient ? exponent : 0, coefficient)
{
}

Expand Down Expand Up @@ -514,11 +512,15 @@ Decimal Decimal::operator/(const Decimal& rhs) const

bool Decimal::operator==(const Decimal& rhs) const
{
if (isNaN() || rhs.isNaN())
return false;
return m_data == rhs.m_data || compareTo(rhs).isZero();
}

bool Decimal::operator!=(const Decimal& rhs) const
{
if (isNaN() || rhs.isNaN())
return true;
if (m_data == rhs.m_data)
return false;
const Decimal result = compareTo(rhs);
Expand All @@ -537,6 +539,8 @@ bool Decimal::operator<(const Decimal& rhs) const

bool Decimal::operator<=(const Decimal& rhs) const
{
if (isNaN() || rhs.isNaN())
return false;
if (m_data == rhs.m_data)
return true;
const Decimal result = compareTo(rhs);
Expand All @@ -555,6 +559,8 @@ bool Decimal::operator>(const Decimal& rhs) const

bool Decimal::operator>=(const Decimal& rhs) const
{
if (isNaN() || rhs.isNaN())
return false;
if (m_data == rhs.m_data)
return true;
const Decimal result = compareTo(rhs);
Expand Down Expand Up @@ -687,7 +693,7 @@ Decimal Decimal::floor() const
Decimal Decimal::fromDouble(double doubleValue)
{
if (std::isfinite(doubleValue))
return fromString(String::numberToStringECMAScript(doubleValue));
return fromString(mozToString(doubleValue));

if (std::isinf(doubleValue))
return infinity(doubleValue < 0 ? Negative : Positive);
Expand Down Expand Up @@ -928,7 +934,7 @@ double Decimal::toDouble() const
{
if (isFinite()) {
bool valid;
const double doubleValue = toString().toDouble(&valid);
const double doubleValue = mozToDouble(toString(), &valid);
return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN();
}

Expand Down Expand Up @@ -981,7 +987,7 @@ String Decimal::toString() const
}
}

const String digits = String::number(coefficient);
const String digits = mozToString(coefficient);
int coefficientLength = static_cast<int>(digits.length());
const int adjustedExponent = originalExponent + coefficientLength - 1;
if (originalExponent <= 0 && adjustedExponent >= -6) {
Expand Down Expand Up @@ -1023,6 +1029,19 @@ String Decimal::toString() const
return builder.toString();
}

bool Decimal::toString(char* strBuf, size_t bufLength) const
{
ASSERT(bufLength > 0);
String str = toString();
size_t length = str.copy(strBuf, bufLength);
if (length < bufLength) {
strBuf[length] = '\0';
return true;
}
strBuf[bufLength - 1] = '\0';
return false;
}

Decimal Decimal::zero(Sign sign)
{
return Decimal(EncodedData(sign, EncodedData::ClassZero));
Expand Down
115 changes: 76 additions & 39 deletions mfbt/decimal/Decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,39 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Imported from:
* https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/platform/Decimal.h
* Check UPSTREAM-GIT-SHA for the commit ID of the last update from Blink core.
*/

#ifndef Decimal_h
#define Decimal_h

#include "platform/PlatformExport.h"
#include "wtf/Allocator.h"
#include "wtf/Assertions.h"
#include "wtf/text/WTFString.h"
#include "mozilla/Assertions.h"
#include <stdint.h>
#include "mozilla/Types.h"

#include <string>

#ifndef ASSERT
#define DEFINED_ASSERT_FOR_DECIMAL_H 1
#define ASSERT MOZ_ASSERT
#endif

#define PLATFORM_EXPORT

// To use USING_FAST_MALLOC we'd need:
// https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/wtf/Allocator.h
// Since we don't allocate Decimal objects, no need.
#define USING_FAST_MALLOC(type) \
void ignore_this_dummy_method() = delete

#define DISALLOW_NEW() \
private: \
void* operator new(size_t) = delete; \
void* operator new(size_t, void*) = delete; \
public:

namespace blink {

Expand Down Expand Up @@ -96,29 +121,29 @@ class PLATFORM_EXPORT Decimal {
Sign m_sign;
};

Decimal(int32_t = 0);
Decimal(Sign, int exponent, uint64_t coefficient);
Decimal(const Decimal&);
MFBT_API explicit Decimal(int32_t = 0);
MFBT_API Decimal(Sign, int exponent, uint64_t coefficient);
MFBT_API Decimal(const Decimal&);

Decimal& operator=(const Decimal&);
Decimal& operator+=(const Decimal&);
Decimal& operator-=(const Decimal&);
Decimal& operator*=(const Decimal&);
Decimal& operator/=(const Decimal&);
MFBT_API Decimal& operator=(const Decimal&);
MFBT_API Decimal& operator+=(const Decimal&);
MFBT_API Decimal& operator-=(const Decimal&);
MFBT_API Decimal& operator*=(const Decimal&);
MFBT_API Decimal& operator/=(const Decimal&);

Decimal operator-() const;
MFBT_API Decimal operator-() const;

bool operator==(const Decimal&) const;
bool operator!=(const Decimal&) const;
bool operator<(const Decimal&) const;
bool operator<=(const Decimal&) const;
bool operator>(const Decimal&) const;
bool operator>=(const Decimal&) const;
MFBT_API bool operator==(const Decimal&) const;
MFBT_API bool operator!=(const Decimal&) const;
MFBT_API bool operator<(const Decimal&) const;
MFBT_API bool operator<=(const Decimal&) const;
MFBT_API bool operator>(const Decimal&) const;
MFBT_API bool operator>=(const Decimal&) const;

Decimal operator+(const Decimal&) const;
Decimal operator-(const Decimal&) const;
Decimal operator*(const Decimal&) const;
Decimal operator/(const Decimal&) const;
MFBT_API Decimal operator+(const Decimal&) const;
MFBT_API Decimal operator-(const Decimal&) const;
MFBT_API Decimal operator*(const Decimal&) const;
MFBT_API Decimal operator/(const Decimal&) const;

int exponent() const
{
Expand All @@ -134,31 +159,32 @@ class PLATFORM_EXPORT Decimal {
bool isSpecial() const { return m_data.isSpecial(); }
bool isZero() const { return m_data.isZero(); }

Decimal abs() const;
Decimal ceil() const;
Decimal floor() const;
Decimal remainder(const Decimal&) const;
Decimal round() const;
MFBT_API Decimal abs() const;
MFBT_API Decimal ceil() const;
MFBT_API Decimal floor() const;
MFBT_API Decimal remainder(const Decimal&) const;
MFBT_API Decimal round() const;

double toDouble() const;
MFBT_API double toDouble() const;
// Note: toString method supports infinity and nan but fromString not.
String toString() const;
MFBT_API std::string toString() const;
MFBT_API bool toString(char* strBuf, size_t bufLength) const;

static Decimal fromDouble(double);
static MFBT_API Decimal fromDouble(double);
// fromString supports following syntax EBNF:
// number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)?
// | sign? '.' digit+ (exponent-marker sign? digit+)?
// sign ::= '+' | '-'
// exponent-marker ::= 'e' | 'E'
// digit ::= '0' | '1' | ... | '9'
// Note: fromString doesn't support "infinity" and "nan".
static Decimal fromString(const String&);
static Decimal infinity(Sign);
static Decimal nan();
static Decimal zero(Sign);
static MFBT_API Decimal fromString(const std::string& aValue);
static MFBT_API Decimal infinity(Sign);
static MFBT_API Decimal nan();
static MFBT_API Decimal zero(Sign);

// You should not use below methods. We expose them for unit testing.
explicit Decimal(const EncodedData&);
MFBT_API explicit Decimal(const EncodedData&);
const EncodedData& value() const { return m_data; }

private:
Expand All @@ -168,10 +194,10 @@ class PLATFORM_EXPORT Decimal {
int exponent;
};

Decimal(double);
Decimal compareTo(const Decimal&) const;
MFBT_API explicit Decimal(double);
MFBT_API Decimal compareTo(const Decimal&) const;

static AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs);
static MFBT_API AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs);
static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; }

Sign sign() const { return m_data.sign(); }
Expand All @@ -181,4 +207,15 @@ class PLATFORM_EXPORT Decimal {

} // namespace blink

namespace mozilla {
typedef blink::Decimal Decimal;
} // namespace mozilla

#undef USING_FAST_MALLOC

#ifdef DEFINED_ASSERT_FOR_DECIMAL_H
#undef DEFINED_ASSERT_FOR_DECIMAL_H
#undef ASSERT
#endif

#endif // Decimal_h
1 change: 1 addition & 0 deletions mfbt/decimal/UPSTREAM-GIT-SHA
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cad4c9e3b3c9e80bb189059373db528272bca96f

0 comments on commit 3389490

Please sign in to comment.