Minor code cleaning
This commit is contained in:
parent
a425ad5552
commit
4f99e25b9d
@ -36,12 +36,12 @@ public:
|
|||||||
double Value() const { return d; }
|
double Value() const { return d; }
|
||||||
uint64_t Uint64Value() const { return u; }
|
uint64_t Uint64Value() const { return u; }
|
||||||
|
|
||||||
double NextDouble() const {
|
double NextPositiveDouble() const {
|
||||||
RAPIDJSON_ASSERT(!Sign());
|
RAPIDJSON_ASSERT(!Sign());
|
||||||
return Double(u + 1).Value();
|
return Double(u + 1).Value();
|
||||||
}
|
}
|
||||||
|
|
||||||
double PreviousDouble() const {
|
double PreviousPositiveDouble() const {
|
||||||
RAPIDJSON_ASSERT(!Sign());
|
RAPIDJSON_ASSERT(!Sign());
|
||||||
if (d == 0.0)
|
if (d == 0.0)
|
||||||
return 0.0;
|
return 0.0;
|
||||||
@ -245,29 +245,19 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Compare(const BigInteger& rhs) const {
|
int Compare(const BigInteger& rhs) const {
|
||||||
if (count_ != rhs.count_) {
|
if (count_ != rhs.count_)
|
||||||
if (count_ < rhs.count_)
|
return count_ < rhs.count_ ? -1 : 1;
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = count_; i > 0;) {
|
for (size_t i = count_; i-- > 0;)
|
||||||
i--;
|
if (digits_[i] != rhs.digits_[i])
|
||||||
if (digits_[i] != rhs.digits_[i]) {
|
return digits_[i] < rhs.digits_[i] ? -1 : 1;
|
||||||
if (digits_[i] < rhs.digits_[i])
|
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetCount() const { return count_; }
|
size_t GetCount() const { return count_; }
|
||||||
Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
|
Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
|
||||||
bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
|
bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void AppendDecimal64(const char* begin, const char* end) {
|
void AppendDecimal64(const char* begin, const char* end) {
|
||||||
@ -276,8 +266,7 @@ private:
|
|||||||
*this = u;
|
*this = u;
|
||||||
else {
|
else {
|
||||||
unsigned exp = end - begin;
|
unsigned exp = end - begin;
|
||||||
MultiplyPow5(exp) <<= exp; // *this *= 10^exp
|
(MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
|
||||||
*this += u;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,8 +287,7 @@ private:
|
|||||||
// Assume a * b + k < 2^128
|
// Assume a * b + k < 2^128
|
||||||
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
|
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
|
||||||
#if defined(_MSC_VER) && defined(_M_AMD64)
|
#if defined(_MSC_VER) && defined(_M_AMD64)
|
||||||
uint64_t low = _umul128(a, b, outHigh);
|
uint64_t low = _umul128(a, b, outHigh) + k;
|
||||||
low += k;
|
|
||||||
if (low < k)
|
if (low < k)
|
||||||
(*outHigh)++;
|
(*outHigh)++;
|
||||||
return low;
|
return low;
|
||||||
@ -309,14 +297,8 @@ private:
|
|||||||
*outHigh = p >> 64;
|
*outHigh = p >> 64;
|
||||||
return static_cast<uint64_t>(p);
|
return static_cast<uint64_t>(p);
|
||||||
#else
|
#else
|
||||||
const uint64_t a0 = a & 0xFFFFFFFF;
|
const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
|
||||||
const uint64_t a1 = a >> 32;
|
uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
|
||||||
const uint64_t b0 = b & 0xFFFFFFFF;
|
|
||||||
const uint64_t b1 = b >> 32;
|
|
||||||
uint64_t x0 = a0 * b0;
|
|
||||||
uint64_t x1 = a0 * b1;
|
|
||||||
uint64_t x2 = a1 * b0;
|
|
||||||
uint64_t x3 = a1 * b1;
|
|
||||||
x1 += (x0 >> 32); // can't give carry
|
x1 += (x0 >> 32); // can't give carry
|
||||||
x1 += x2;
|
x1 += x2;
|
||||||
if (x1 < x2)
|
if (x1 < x2)
|
||||||
@ -378,15 +360,9 @@ inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp, bool* adj
|
|||||||
const Double db(b);
|
const Double db(b);
|
||||||
const uint64_t bInt = db.IntegerSignificand();
|
const uint64_t bInt = db.IntegerSignificand();
|
||||||
const int bExp = db.IntegerExponent();
|
const int bExp = db.IntegerExponent();
|
||||||
|
|
||||||
const int hExp = bExp - 1;
|
const int hExp = bExp - 1;
|
||||||
|
|
||||||
int dS_Exp2 = 0;
|
int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
|
||||||
int dS_Exp5 = 0;
|
|
||||||
int bS_Exp2 = 0;
|
|
||||||
int bS_Exp5 = 0;
|
|
||||||
int hS_Exp2 = 0;
|
|
||||||
int hS_Exp5 = 0;
|
|
||||||
|
|
||||||
// Adjust for decimal exponent
|
// Adjust for decimal exponent
|
||||||
if (dExp >= 0) {
|
if (dExp >= 0) {
|
||||||
@ -467,17 +443,17 @@ inline double FullPrecision(double d, int p, const char* decimals, size_t length
|
|||||||
// Use slow-path with BigInteger comparison
|
// Use slow-path with BigInteger comparison
|
||||||
|
|
||||||
// Trim leading zeros
|
// Trim leading zeros
|
||||||
while (decimals[0] == '0' && length > 1) {
|
while (*decimals == '0' && length > 1) {
|
||||||
decimals++;
|
|
||||||
length--;
|
length--;
|
||||||
|
decimals++;
|
||||||
decimalPosition--;
|
decimalPosition--;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim trailing zeros
|
// Trim trailing zeros
|
||||||
while (decimals[length - 1] == '0' && length > 1) {
|
while (decimals[length - 1] == '0' && length > 1) {
|
||||||
length--;
|
length--;
|
||||||
exp++;
|
|
||||||
decimalPosition--;
|
decimalPosition--;
|
||||||
|
exp++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim right-most digits
|
// Trim right-most digits
|
||||||
@ -502,17 +478,12 @@ inline double FullPrecision(double d, int p, const char* decimals, size_t length
|
|||||||
else if (cmp == 0) {
|
else if (cmp == 0) {
|
||||||
// Round towards even
|
// Round towards even
|
||||||
if (approx.Significand() & 1)
|
if (approx.Significand() & 1)
|
||||||
return adjustToNegative ? approx.PreviousDouble() : approx.NextDouble();
|
return adjustToNegative ? approx.PreviousPositiveDouble() : approx.NextPositiveDouble();
|
||||||
else
|
else
|
||||||
return approx.Value();
|
return approx.Value();
|
||||||
}
|
}
|
||||||
else {
|
else // adjustment
|
||||||
// adjustment
|
approx = adjustToNegative ? approx.PreviousPositiveDouble() : approx.NextPositiveDouble();
|
||||||
if (adjustToNegative)
|
|
||||||
approx = approx.PreviousDouble();
|
|
||||||
else
|
|
||||||
approx = approx.NextDouble();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should not happen, but in case there is really a bug, break the infinite-loop
|
// This should not happen, but in case there is really a bug, break the infinite-loop
|
||||||
|
Loading…
x
Reference in New Issue
Block a user