ICU-20796 Updating double-conversion from upstream master.
This commit is contained in:
parent
33ca188a7f
commit
d36779df6c
@ -105,8 +105,9 @@ number_integerwidth.o number_longnames.o number_modifiers.o number_notation.o nu
|
||||
number_padding.o number_patternmodifier.o number_patternstring.o \
|
||||
number_rounding.o number_scientific.o number_utils.o number_asformat.o \
|
||||
number_mapper.o number_multiplier.o number_currencysymbols.o number_skeletons.o number_capi.o \
|
||||
double-conversion.o double-conversion-bignum-dtoa.o double-conversion-bignum.o \
|
||||
double-conversion-cached-powers.o double-conversion-diy-fp.o \
|
||||
double-conversion-string-to-double.o double-conversion-double-to-string.o \
|
||||
double-conversion-bignum-dtoa.o double-conversion-bignum.o \
|
||||
double-conversion-cached-powers.o \
|
||||
double-conversion-fast-dtoa.o double-conversion-strtod.o \
|
||||
string_segment.o numparse_parsednumber.o numparse_impl.o \
|
||||
numparse_symbols.o numparse_decimal.o numparse_scientific.o numparse_currency.o \
|
||||
|
@ -49,7 +49,7 @@ U_NAMESPACE_BEGIN
|
||||
namespace double_conversion {
|
||||
|
||||
static int NormalizedExponent(uint64_t significand, int exponent) {
|
||||
ASSERT(significand != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(significand != 0);
|
||||
while ((significand & Double::kHiddenBit) == 0) {
|
||||
significand = significand << 1;
|
||||
exponent = exponent - 1;
|
||||
@ -90,26 +90,26 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// Generates 'requested_digits' after the decimal point.
|
||||
static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length);
|
||||
Vector<char> buffer, int* length);
|
||||
// Generates 'count' digits of numerator/denominator.
|
||||
// Once 'count' digits have been produced rounds the result depending on the
|
||||
// remainder (remainders of exactly .5 round upwards). Might update the
|
||||
// decimal_point when rounding up (for example for 0.9999).
|
||||
static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length);
|
||||
Vector<char> buffer, int* length);
|
||||
|
||||
|
||||
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
Vector<char> buffer, int* length, int* decimal_point) {
|
||||
ASSERT(v > 0);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(v > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
uint64_t significand;
|
||||
int exponent;
|
||||
bool lower_boundary_is_closer;
|
||||
if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) {
|
||||
float f = static_cast<float>(v);
|
||||
ASSERT(f == v);
|
||||
DOUBLE_CONVERSION_ASSERT(f == v);
|
||||
significand = Single(f).Significand();
|
||||
exponent = Single(f).Exponent();
|
||||
lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser();
|
||||
@ -148,7 +148,7 @@ void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
// 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
|
||||
// The maximum double is 1.7976931348623157e308 which needs fewer than
|
||||
// 308*4 binary digits.
|
||||
ASSERT(Bignum::kMaxSignificantBits >= 324*4);
|
||||
DOUBLE_CONVERSION_ASSERT(Bignum::kMaxSignificantBits >= 324*4);
|
||||
InitialScaledStartValues(significand, exponent, lower_boundary_is_closer,
|
||||
estimated_power, need_boundary_deltas,
|
||||
&numerator, &denominator,
|
||||
@ -177,7 +177,7 @@ void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
buffer, length);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
buffer[*length] = '\0';
|
||||
}
|
||||
@ -209,7 +209,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
for (;;) {
|
||||
uint16_t digit;
|
||||
digit = numerator->DivideModuloIntBignum(*denominator);
|
||||
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
// digit = numerator / denominator (integer division).
|
||||
// numerator = numerator % denominator.
|
||||
buffer[(*length)++] = static_cast<char>(digit + '0');
|
||||
@ -255,7 +255,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// loop would have stopped earlier.
|
||||
// We still have an assert here in case the preconditions were not
|
||||
// satisfied.
|
||||
ASSERT(buffer[(*length) - 1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) - 1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
} else {
|
||||
// Halfway case.
|
||||
@ -266,7 +266,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
if ((buffer[(*length) - 1] - '0') % 2 == 0) {
|
||||
// Round down => Do nothing.
|
||||
} else {
|
||||
ASSERT(buffer[(*length) - 1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) - 1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
}
|
||||
}
|
||||
@ -278,9 +278,9 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// Round up.
|
||||
// Note again that the last digit could not be '9' since this would have
|
||||
// stopped the loop earlier.
|
||||
// We still have an ASSERT here, in case the preconditions were not
|
||||
// We still have an DOUBLE_CONVERSION_ASSERT here, in case the preconditions were not
|
||||
// satisfied.
|
||||
ASSERT(buffer[(*length) -1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) -1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
return;
|
||||
}
|
||||
@ -297,11 +297,11 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char> buffer, int* length) {
|
||||
ASSERT(count >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(count >= 0);
|
||||
for (int i = 0; i < count - 1; ++i) {
|
||||
uint16_t digit;
|
||||
digit = numerator->DivideModuloIntBignum(*denominator);
|
||||
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
// digit = numerator / denominator (integer division).
|
||||
// numerator = numerator % denominator.
|
||||
buffer[i] = static_cast<char>(digit + '0');
|
||||
@ -314,7 +314,7 @@ static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
|
||||
digit++;
|
||||
}
|
||||
ASSERT(digit <= 10);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 10);
|
||||
buffer[count - 1] = static_cast<char>(digit + '0');
|
||||
// Correct bad digits (in case we had a sequence of '9's). Propagate the
|
||||
// carry until we hat a non-'9' or til we reach the first digit.
|
||||
@ -339,7 +339,7 @@ static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
// Input verifies: 1 <= (numerator + delta) / denominator < 10.
|
||||
static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length) {
|
||||
Vector<char> buffer, int* length) {
|
||||
// Note that we have to look at more than just the requested_digits, since
|
||||
// a number could be rounded up. Example: v=0.5 with requested_digits=0.
|
||||
// Even though the power of v equals 0 we can't just stop here.
|
||||
@ -355,7 +355,7 @@ static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
} else if (-(*decimal_point) == requested_digits) {
|
||||
// We only need to verify if the number rounds down or up.
|
||||
// Ex: 0.04 and 0.06 with requested_digits == 1.
|
||||
ASSERT(*decimal_point == -requested_digits);
|
||||
DOUBLE_CONVERSION_ASSERT(*decimal_point == -requested_digits);
|
||||
// Initially the fraction lies in range (1, 10]. Multiply the denominator
|
||||
// by 10 so that we can compare more easily.
|
||||
denominator->Times10();
|
||||
@ -434,7 +434,7 @@ static void InitialScaledStartValuesPositiveExponent(
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Bignum* delta_minus, Bignum* delta_plus) {
|
||||
// A positive exponent implies a positive power.
|
||||
ASSERT(estimated_power >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(estimated_power >= 0);
|
||||
// Since the estimated_power is positive we simply multiply the denominator
|
||||
// by 10^estimated_power.
|
||||
|
||||
@ -520,7 +520,7 @@ static void InitialScaledStartValuesNegativeExponentNegativePower(
|
||||
// numerator = v * 10^-estimated_power * 2 * 2^-exponent.
|
||||
// Remember: numerator has been abused as power_ten. So no need to assign it
|
||||
// to itself.
|
||||
ASSERT(numerator == power_ten);
|
||||
DOUBLE_CONVERSION_ASSERT(numerator == power_ten);
|
||||
numerator->MultiplyByUInt64(significand);
|
||||
|
||||
// denominator = 2 * 2^-exponent with exponent < 0.
|
||||
|
@ -34,6 +34,9 @@
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
|
||||
#include "double-conversion-bignum.h"
|
||||
@ -44,136 +47,129 @@ U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
Bignum::Bignum()
|
||||
: bigits_buffer_(), bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) {
|
||||
for (int i = 0; i < kBigitCapacity; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
Bignum::Chunk& Bignum::RawBigit(const int index) {
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
|
||||
return bigits_buffer_[index];
|
||||
}
|
||||
|
||||
|
||||
const Bignum::Chunk& Bignum::RawBigit(const int index) const {
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
|
||||
return bigits_buffer_[index];
|
||||
}
|
||||
|
||||
|
||||
template<typename S>
|
||||
static int BitSize(S value) {
|
||||
static int BitSize(const S value) {
|
||||
(void) value; // Mark variable as used.
|
||||
return 8 * sizeof(value);
|
||||
}
|
||||
|
||||
// Guaranteed to lie in one Bigit.
|
||||
void Bignum::AssignUInt16(uint16_t value) {
|
||||
ASSERT(kBigitSize >= BitSize(value));
|
||||
void Bignum::AssignUInt16(const uint16_t value) {
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize >= BitSize(value));
|
||||
Zero();
|
||||
if (value == 0) return;
|
||||
|
||||
EnsureCapacity(1);
|
||||
bigits_[0] = value;
|
||||
used_digits_ = 1;
|
||||
if (value > 0) {
|
||||
RawBigit(0) = value;
|
||||
used_bigits_ = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignUInt64(uint64_t value) {
|
||||
const int kUInt64Size = 64;
|
||||
|
||||
Zero();
|
||||
if (value == 0) return;
|
||||
|
||||
int needed_bigits = kUInt64Size / kBigitSize + 1;
|
||||
EnsureCapacity(needed_bigits);
|
||||
for (int i = 0; i < needed_bigits; ++i) {
|
||||
bigits_[i] = value & kBigitMask;
|
||||
value = value >> kBigitSize;
|
||||
for(int i = 0; value > 0; ++i) {
|
||||
RawBigit(i) = value & kBigitMask;
|
||||
value >>= kBigitSize;
|
||||
++used_bigits_;
|
||||
}
|
||||
used_digits_ = needed_bigits;
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignBignum(const Bignum& other) {
|
||||
exponent_ = other.exponent_;
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
bigits_[i] = other.bigits_[i];
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
RawBigit(i) = other.RawBigit(i);
|
||||
}
|
||||
// Clear the excess digits (if there were any).
|
||||
for (int i = other.used_digits_; i < used_digits_; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
used_digits_ = other.used_digits_;
|
||||
used_bigits_ = other.used_bigits_;
|
||||
}
|
||||
|
||||
|
||||
static uint64_t ReadUInt64(Vector<const char> buffer,
|
||||
int from,
|
||||
int digits_to_read) {
|
||||
static uint64_t ReadUInt64(const Vector<const char> buffer,
|
||||
const int from,
|
||||
const int digits_to_read) {
|
||||
uint64_t result = 0;
|
||||
for (int i = from; i < from + digits_to_read; ++i) {
|
||||
int digit = buffer[i] - '0';
|
||||
ASSERT(0 <= digit && digit <= 9);
|
||||
const int digit = buffer[i] - '0';
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
|
||||
result = result * 10 + digit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignDecimalString(Vector<const char> value) {
|
||||
void Bignum::AssignDecimalString(const Vector<const char> value) {
|
||||
// 2^64 = 18446744073709551616 > 10^19
|
||||
const int kMaxUint64DecimalDigits = 19;
|
||||
static const int kMaxUint64DecimalDigits = 19;
|
||||
Zero();
|
||||
int length = value.length();
|
||||
unsigned int pos = 0;
|
||||
unsigned pos = 0;
|
||||
// Let's just say that each digit needs 4 bits.
|
||||
while (length >= kMaxUint64DecimalDigits) {
|
||||
uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
|
||||
const uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
|
||||
pos += kMaxUint64DecimalDigits;
|
||||
length -= kMaxUint64DecimalDigits;
|
||||
MultiplyByPowerOfTen(kMaxUint64DecimalDigits);
|
||||
AddUInt64(digits);
|
||||
}
|
||||
uint64_t digits = ReadUInt64(value, pos, length);
|
||||
const uint64_t digits = ReadUInt64(value, pos, length);
|
||||
MultiplyByPowerOfTen(length);
|
||||
AddUInt64(digits);
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
static int HexCharValue(char c) {
|
||||
if ('0' <= c && c <= '9') return c - '0';
|
||||
if ('a' <= c && c <= 'f') return 10 + c - 'a';
|
||||
ASSERT('A' <= c && c <= 'F');
|
||||
static uint64_t HexCharValue(const int c) {
|
||||
if ('0' <= c && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if ('a' <= c && c <= 'f') {
|
||||
return 10 + c - 'a';
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT('A' <= c && c <= 'F');
|
||||
return 10 + c - 'A';
|
||||
}
|
||||
|
||||
|
||||
// Unlike AssignDecimalString(), this function is "only" used
|
||||
// for unit-tests and therefore not performance critical.
|
||||
void Bignum::AssignHexString(Vector<const char> value) {
|
||||
Zero();
|
||||
int length = value.length();
|
||||
|
||||
int needed_bigits = length * 4 / kBigitSize + 1;
|
||||
EnsureCapacity(needed_bigits);
|
||||
int string_index = length - 1;
|
||||
for (int i = 0; i < needed_bigits - 1; ++i) {
|
||||
// These bigits are guaranteed to be "full".
|
||||
Chunk current_bigit = 0;
|
||||
for (int j = 0; j < kBigitSize / 4; j++) {
|
||||
current_bigit += HexCharValue(value[string_index--]) << (j * 4);
|
||||
// Required capacity could be reduced by ignoring leading zeros.
|
||||
EnsureCapacity(((value.length() * 4) + kBigitSize - 1) / kBigitSize);
|
||||
DOUBLE_CONVERSION_ASSERT(sizeof(uint64_t) * 8 >= kBigitSize + 4); // TODO: static_assert
|
||||
// Accumulates converted hex digits until at least kBigitSize bits.
|
||||
// Works with non-factor-of-four kBigitSizes.
|
||||
uint64_t tmp = 0; // Accumulates converted hex digits until at least
|
||||
for (int cnt = 0; !value.is_empty(); value.pop_back()) {
|
||||
tmp |= (HexCharValue(value.last()) << cnt);
|
||||
if ((cnt += 4) >= kBigitSize) {
|
||||
RawBigit(used_bigits_++) = (tmp & kBigitMask);
|
||||
cnt -= kBigitSize;
|
||||
tmp >>= kBigitSize;
|
||||
}
|
||||
bigits_[i] = current_bigit;
|
||||
}
|
||||
used_digits_ = needed_bigits - 1;
|
||||
|
||||
Chunk most_significant_bigit = 0; // Could be = 0;
|
||||
for (int j = 0; j <= string_index; ++j) {
|
||||
most_significant_bigit <<= 4;
|
||||
most_significant_bigit += HexCharValue(value[j]);
|
||||
}
|
||||
if (most_significant_bigit != 0) {
|
||||
bigits_[used_digits_] = most_significant_bigit;
|
||||
used_digits_++;
|
||||
if (tmp > 0) {
|
||||
RawBigit(used_bigits_++) = tmp;
|
||||
}
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AddUInt64(uint64_t operand) {
|
||||
if (operand == 0) return;
|
||||
void Bignum::AddUInt64(const uint64_t operand) {
|
||||
if (operand == 0) {
|
||||
return;
|
||||
}
|
||||
Bignum other;
|
||||
other.AssignUInt64(operand);
|
||||
AddBignum(other);
|
||||
@ -181,8 +177,8 @@ void Bignum::AddUInt64(uint64_t operand) {
|
||||
|
||||
|
||||
void Bignum::AddBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
|
||||
// If this has a greater exponent than other append zero-bigits to this.
|
||||
// After this call exponent_ <= other.exponent_.
|
||||
@ -200,48 +196,52 @@ void Bignum::AddBignum(const Bignum& other) {
|
||||
// cccccccccccc 0000
|
||||
// In both cases we might need a carry bigit.
|
||||
|
||||
EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_);
|
||||
EnsureCapacity(1 + (std::max)(BigitLength(), other.BigitLength()) - exponent_);
|
||||
Chunk carry = 0;
|
||||
int bigit_pos = other.exponent_ - exponent_;
|
||||
ASSERT(bigit_pos >= 0);
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry;
|
||||
bigits_[bigit_pos] = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
bigit_pos++;
|
||||
DOUBLE_CONVERSION_ASSERT(bigit_pos >= 0);
|
||||
for (int i = used_bigits_; i < bigit_pos; ++i) {
|
||||
RawBigit(i) = 0;
|
||||
}
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
|
||||
const Chunk sum = my + other.RawBigit(i) + carry;
|
||||
RawBigit(bigit_pos) = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
++bigit_pos;
|
||||
}
|
||||
|
||||
while (carry != 0) {
|
||||
Chunk sum = bigits_[bigit_pos] + carry;
|
||||
bigits_[bigit_pos] = sum & kBigitMask;
|
||||
const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
|
||||
const Chunk sum = my + carry;
|
||||
RawBigit(bigit_pos) = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
bigit_pos++;
|
||||
++bigit_pos;
|
||||
}
|
||||
used_digits_ = Max(bigit_pos, used_digits_);
|
||||
ASSERT(IsClamped());
|
||||
used_bigits_ = (std::max)(bigit_pos, static_cast<int>(used_bigits_));
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
}
|
||||
|
||||
|
||||
void Bignum::SubtractBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
// We require this to be bigger than other.
|
||||
ASSERT(LessEqual(other, *this));
|
||||
DOUBLE_CONVERSION_ASSERT(LessEqual(other, *this));
|
||||
|
||||
Align(other);
|
||||
|
||||
int offset = other.exponent_ - exponent_;
|
||||
const int offset = other.exponent_ - exponent_;
|
||||
Chunk borrow = 0;
|
||||
int i;
|
||||
for (i = 0; i < other.used_digits_; ++i) {
|
||||
ASSERT((borrow == 0) || (borrow == 1));
|
||||
Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow;
|
||||
bigits_[i + offset] = difference & kBigitMask;
|
||||
for (i = 0; i < other.used_bigits_; ++i) {
|
||||
DOUBLE_CONVERSION_ASSERT((borrow == 0) || (borrow == 1));
|
||||
const Chunk difference = RawBigit(i + offset) - other.RawBigit(i) - borrow;
|
||||
RawBigit(i + offset) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
}
|
||||
while (borrow != 0) {
|
||||
Chunk difference = bigits_[i + offset] - borrow;
|
||||
bigits_[i + offset] = difference & kBigitMask;
|
||||
const Chunk difference = RawBigit(i + offset) - borrow;
|
||||
RawBigit(i + offset) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
++i;
|
||||
}
|
||||
@ -249,91 +249,105 @@ void Bignum::SubtractBignum(const Bignum& other) {
|
||||
}
|
||||
|
||||
|
||||
void Bignum::ShiftLeft(int shift_amount) {
|
||||
if (used_digits_ == 0) return;
|
||||
exponent_ += shift_amount / kBigitSize;
|
||||
int local_shift = shift_amount % kBigitSize;
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
void Bignum::ShiftLeft(const int shift_amount) {
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
exponent_ += (shift_amount / kBigitSize);
|
||||
const int local_shift = shift_amount % kBigitSize;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
BigitsShiftLeft(local_shift);
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByUInt32(uint32_t factor) {
|
||||
if (factor == 1) return;
|
||||
void Bignum::MultiplyByUInt32(const uint32_t factor) {
|
||||
if (factor == 1) {
|
||||
return;
|
||||
}
|
||||
if (factor == 0) {
|
||||
Zero();
|
||||
return;
|
||||
}
|
||||
if (used_digits_ == 0) return;
|
||||
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
// The product of a bigit with the factor is of size kBigitSize + 32.
|
||||
// Assert that this number + 1 (for the carry) fits into double chunk.
|
||||
ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
|
||||
DoubleChunk carry = 0;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry;
|
||||
bigits_[i] = static_cast<Chunk>(product & kBigitMask);
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const DoubleChunk product = static_cast<DoubleChunk>(factor) * RawBigit(i) + carry;
|
||||
RawBigit(i) = static_cast<Chunk>(product & kBigitMask);
|
||||
carry = (product >> kBigitSize);
|
||||
}
|
||||
while (carry != 0) {
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
bigits_[used_digits_] = carry & kBigitMask;
|
||||
used_digits_++;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
RawBigit(used_bigits_) = carry & kBigitMask;
|
||||
used_bigits_++;
|
||||
carry >>= kBigitSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByUInt64(uint64_t factor) {
|
||||
if (factor == 1) return;
|
||||
void Bignum::MultiplyByUInt64(const uint64_t factor) {
|
||||
if (factor == 1) {
|
||||
return;
|
||||
}
|
||||
if (factor == 0) {
|
||||
Zero();
|
||||
return;
|
||||
}
|
||||
ASSERT(kBigitSize < 32);
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize < 32);
|
||||
uint64_t carry = 0;
|
||||
uint64_t low = factor & 0xFFFFFFFF;
|
||||
uint64_t high = factor >> 32;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
uint64_t product_low = low * bigits_[i];
|
||||
uint64_t product_high = high * bigits_[i];
|
||||
uint64_t tmp = (carry & kBigitMask) + product_low;
|
||||
bigits_[i] = tmp & kBigitMask;
|
||||
const uint64_t low = factor & 0xFFFFFFFF;
|
||||
const uint64_t high = factor >> 32;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const uint64_t product_low = low * RawBigit(i);
|
||||
const uint64_t product_high = high * RawBigit(i);
|
||||
const uint64_t tmp = (carry & kBigitMask) + product_low;
|
||||
RawBigit(i) = tmp & kBigitMask;
|
||||
carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
|
||||
(product_high << (32 - kBigitSize));
|
||||
}
|
||||
while (carry != 0) {
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
bigits_[used_digits_] = carry & kBigitMask;
|
||||
used_digits_++;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
RawBigit(used_bigits_) = carry & kBigitMask;
|
||||
used_bigits_++;
|
||||
carry >>= kBigitSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByPowerOfTen(int exponent) {
|
||||
const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d);
|
||||
const uint16_t kFive1 = 5;
|
||||
const uint16_t kFive2 = kFive1 * 5;
|
||||
const uint16_t kFive3 = kFive2 * 5;
|
||||
const uint16_t kFive4 = kFive3 * 5;
|
||||
const uint16_t kFive5 = kFive4 * 5;
|
||||
const uint16_t kFive6 = kFive5 * 5;
|
||||
const uint32_t kFive7 = kFive6 * 5;
|
||||
const uint32_t kFive8 = kFive7 * 5;
|
||||
const uint32_t kFive9 = kFive8 * 5;
|
||||
const uint32_t kFive10 = kFive9 * 5;
|
||||
const uint32_t kFive11 = kFive10 * 5;
|
||||
const uint32_t kFive12 = kFive11 * 5;
|
||||
const uint32_t kFive13 = kFive12 * 5;
|
||||
const uint32_t kFive1_to_12[] =
|
||||
void Bignum::MultiplyByPowerOfTen(const int exponent) {
|
||||
static const uint64_t kFive27 = DOUBLE_CONVERSION_UINT64_2PART_C(0x6765c793, fa10079d);
|
||||
static const uint16_t kFive1 = 5;
|
||||
static const uint16_t kFive2 = kFive1 * 5;
|
||||
static const uint16_t kFive3 = kFive2 * 5;
|
||||
static const uint16_t kFive4 = kFive3 * 5;
|
||||
static const uint16_t kFive5 = kFive4 * 5;
|
||||
static const uint16_t kFive6 = kFive5 * 5;
|
||||
static const uint32_t kFive7 = kFive6 * 5;
|
||||
static const uint32_t kFive8 = kFive7 * 5;
|
||||
static const uint32_t kFive9 = kFive8 * 5;
|
||||
static const uint32_t kFive10 = kFive9 * 5;
|
||||
static const uint32_t kFive11 = kFive10 * 5;
|
||||
static const uint32_t kFive12 = kFive11 * 5;
|
||||
static const uint32_t kFive13 = kFive12 * 5;
|
||||
static const uint32_t kFive1_to_12[] =
|
||||
{ kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
|
||||
kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
|
||||
|
||||
ASSERT(exponent >= 0);
|
||||
if (exponent == 0) return;
|
||||
if (used_digits_ == 0) return;
|
||||
DOUBLE_CONVERSION_ASSERT(exponent >= 0);
|
||||
|
||||
if (exponent == 0) {
|
||||
return;
|
||||
}
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
// We shift by exponent at the end just before returning.
|
||||
int remaining_exponent = exponent;
|
||||
while (remaining_exponent >= 27) {
|
||||
@ -352,8 +366,8 @@ void Bignum::MultiplyByPowerOfTen(int exponent) {
|
||||
|
||||
|
||||
void Bignum::Square() {
|
||||
ASSERT(IsClamped());
|
||||
int product_length = 2 * used_digits_;
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
const int product_length = 2 * used_bigits_;
|
||||
EnsureCapacity(product_length);
|
||||
|
||||
// Comba multiplication: compute each column separately.
|
||||
@ -368,64 +382,64 @@ void Bignum::Square() {
|
||||
//
|
||||
// Assert that the additional number of bits in a DoubleChunk are enough to
|
||||
// sum up used_digits of Bigit*Bigit.
|
||||
if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) {
|
||||
UNIMPLEMENTED();
|
||||
if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_bigits_) {
|
||||
DOUBLE_CONVERSION_UNIMPLEMENTED();
|
||||
}
|
||||
DoubleChunk accumulator = 0;
|
||||
// First shift the digits so we don't overwrite them.
|
||||
int copy_offset = used_digits_;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
bigits_[copy_offset + i] = bigits_[i];
|
||||
const int copy_offset = used_bigits_;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
RawBigit(copy_offset + i) = RawBigit(i);
|
||||
}
|
||||
// We have two loops to avoid some 'if's in the loop.
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
// Process temporary digit i with power i.
|
||||
// The sum of the two indices must be equal to i.
|
||||
int bigit_index1 = i;
|
||||
int bigit_index2 = 0;
|
||||
// Sum all of the sub-products.
|
||||
while (bigit_index1 >= 0) {
|
||||
Chunk chunk1 = bigits_[copy_offset + bigit_index1];
|
||||
Chunk chunk2 = bigits_[copy_offset + bigit_index2];
|
||||
const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
|
||||
const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
|
||||
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
|
||||
bigit_index1--;
|
||||
bigit_index2++;
|
||||
}
|
||||
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
accumulator >>= kBigitSize;
|
||||
}
|
||||
for (int i = used_digits_; i < product_length; ++i) {
|
||||
int bigit_index1 = used_digits_ - 1;
|
||||
for (int i = used_bigits_; i < product_length; ++i) {
|
||||
int bigit_index1 = used_bigits_ - 1;
|
||||
int bigit_index2 = i - bigit_index1;
|
||||
// Invariant: sum of both indices is again equal to i.
|
||||
// Inner loop runs 0 times on last iteration, emptying accumulator.
|
||||
while (bigit_index2 < used_digits_) {
|
||||
Chunk chunk1 = bigits_[copy_offset + bigit_index1];
|
||||
Chunk chunk2 = bigits_[copy_offset + bigit_index2];
|
||||
while (bigit_index2 < used_bigits_) {
|
||||
const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
|
||||
const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
|
||||
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
|
||||
bigit_index1--;
|
||||
bigit_index2++;
|
||||
}
|
||||
// The overwritten bigits_[i] will never be read in further loop iterations,
|
||||
// The overwritten RawBigit(i) will never be read in further loop iterations,
|
||||
// because bigit_index1 and bigit_index2 are always greater
|
||||
// than i - used_digits_.
|
||||
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
// than i - used_bigits_.
|
||||
RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
accumulator >>= kBigitSize;
|
||||
}
|
||||
// Since the result was guaranteed to lie inside the number the
|
||||
// accumulator must be 0 now.
|
||||
ASSERT(accumulator == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(accumulator == 0);
|
||||
|
||||
// Don't forget to update the used_digits and the exponent.
|
||||
used_digits_ = product_length;
|
||||
used_bigits_ = product_length;
|
||||
exponent_ *= 2;
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
ASSERT(base != 0);
|
||||
ASSERT(power_exponent >= 0);
|
||||
void Bignum::AssignPowerUInt16(uint16_t base, const int power_exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(base != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(power_exponent >= 0);
|
||||
if (power_exponent == 0) {
|
||||
AssignUInt16(1);
|
||||
return;
|
||||
@ -445,7 +459,7 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
tmp_base >>= 1;
|
||||
bit_size++;
|
||||
}
|
||||
int final_size = bit_size * power_exponent;
|
||||
const int final_size = bit_size * power_exponent;
|
||||
// 1 extra bigit for the shifting, and one for rounded final_size.
|
||||
EnsureCapacity(final_size / kBigitSize + 2);
|
||||
|
||||
@ -466,10 +480,10 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
// Verify that there is enough space in this_value to perform the
|
||||
// multiplication. The first bit_size bits must be 0.
|
||||
if ((power_exponent & mask) != 0) {
|
||||
ASSERT(bit_size > 0);
|
||||
uint64_t base_bits_mask =
|
||||
~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
|
||||
bool high_bits_zero = (this_value & base_bits_mask) == 0;
|
||||
DOUBLE_CONVERSION_ASSERT(bit_size > 0);
|
||||
const uint64_t base_bits_mask =
|
||||
~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
|
||||
const bool high_bits_zero = (this_value & base_bits_mask) == 0;
|
||||
if (high_bits_zero) {
|
||||
this_value *= base;
|
||||
} else {
|
||||
@ -499,9 +513,9 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
|
||||
// Precondition: this/other < 16bit.
|
||||
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
ASSERT(other.used_digits_ > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.used_bigits_ > 0);
|
||||
|
||||
// Easy case: if we have less digits than the divisor than the result is 0.
|
||||
// Note: this handles the case where this == 0, too.
|
||||
@ -519,34 +533,34 @@ uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
// This naive approach is extremely inefficient if `this` divided by other
|
||||
// is big. This function is implemented for doubleToString where
|
||||
// the result should be small (less than 10).
|
||||
ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16));
|
||||
ASSERT(bigits_[used_digits_ - 1] < 0x10000);
|
||||
DOUBLE_CONVERSION_ASSERT(other.RawBigit(other.used_bigits_ - 1) >= ((1 << kBigitSize) / 16));
|
||||
DOUBLE_CONVERSION_ASSERT(RawBigit(used_bigits_ - 1) < 0x10000);
|
||||
// Remove the multiples of the first digit.
|
||||
// Example this = 23 and other equals 9. -> Remove 2 multiples.
|
||||
result += static_cast<uint16_t>(bigits_[used_digits_ - 1]);
|
||||
SubtractTimes(other, bigits_[used_digits_ - 1]);
|
||||
result += static_cast<uint16_t>(RawBigit(used_bigits_ - 1));
|
||||
SubtractTimes(other, RawBigit(used_bigits_ - 1));
|
||||
}
|
||||
|
||||
ASSERT(BigitLength() == other.BigitLength());
|
||||
DOUBLE_CONVERSION_ASSERT(BigitLength() == other.BigitLength());
|
||||
|
||||
// Both bignums are at the same length now.
|
||||
// Since other has more than 0 digits we know that the access to
|
||||
// bigits_[used_digits_ - 1] is safe.
|
||||
Chunk this_bigit = bigits_[used_digits_ - 1];
|
||||
Chunk other_bigit = other.bigits_[other.used_digits_ - 1];
|
||||
// RawBigit(used_bigits_ - 1) is safe.
|
||||
const Chunk this_bigit = RawBigit(used_bigits_ - 1);
|
||||
const Chunk other_bigit = other.RawBigit(other.used_bigits_ - 1);
|
||||
|
||||
if (other.used_digits_ == 1) {
|
||||
if (other.used_bigits_ == 1) {
|
||||
// Shortcut for easy (and common) case.
|
||||
int quotient = this_bigit / other_bigit;
|
||||
bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient;
|
||||
ASSERT(quotient < 0x10000);
|
||||
RawBigit(used_bigits_ - 1) = this_bigit - other_bigit * quotient;
|
||||
DOUBLE_CONVERSION_ASSERT(quotient < 0x10000);
|
||||
result += static_cast<uint16_t>(quotient);
|
||||
Clamp();
|
||||
return result;
|
||||
}
|
||||
|
||||
int division_estimate = this_bigit / (other_bigit + 1);
|
||||
ASSERT(division_estimate < 0x10000);
|
||||
const int division_estimate = this_bigit / (other_bigit + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(division_estimate < 0x10000);
|
||||
result += static_cast<uint16_t>(division_estimate);
|
||||
SubtractTimes(other, division_estimate);
|
||||
|
||||
@ -566,7 +580,7 @@ uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
|
||||
template<typename S>
|
||||
static int SizeInHexChars(S number) {
|
||||
ASSERT(number > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(number > 0);
|
||||
int result = 0;
|
||||
while (number != 0) {
|
||||
number >>= 4;
|
||||
@ -576,29 +590,35 @@ static int SizeInHexChars(S number) {
|
||||
}
|
||||
|
||||
|
||||
static char HexCharOfValue(int value) {
|
||||
ASSERT(0 <= value && value <= 16);
|
||||
if (value < 10) return static_cast<char>(value + '0');
|
||||
static char HexCharOfValue(const int value) {
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= value && value <= 16);
|
||||
if (value < 10) {
|
||||
return static_cast<char>(value + '0');
|
||||
}
|
||||
return static_cast<char>(value - 10 + 'A');
|
||||
}
|
||||
|
||||
|
||||
bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
ASSERT(IsClamped());
|
||||
bool Bignum::ToHexString(char* buffer, const int buffer_size) const {
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
// Each bigit must be printable as separate hex-character.
|
||||
ASSERT(kBigitSize % 4 == 0);
|
||||
const int kHexCharsPerBigit = kBigitSize / 4;
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize % 4 == 0);
|
||||
static const int kHexCharsPerBigit = kBigitSize / 4;
|
||||
|
||||
if (used_digits_ == 0) {
|
||||
if (buffer_size < 2) return false;
|
||||
if (used_bigits_ == 0) {
|
||||
if (buffer_size < 2) {
|
||||
return false;
|
||||
}
|
||||
buffer[0] = '0';
|
||||
buffer[1] = '\0';
|
||||
return true;
|
||||
}
|
||||
// We add 1 for the terminating '\0' character.
|
||||
int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
|
||||
SizeInHexChars(bigits_[used_digits_ - 1]) + 1;
|
||||
if (needed_chars > buffer_size) return false;
|
||||
const int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
|
||||
SizeInHexChars(RawBigit(used_bigits_ - 1)) + 1;
|
||||
if (needed_chars > buffer_size) {
|
||||
return false;
|
||||
}
|
||||
int string_index = needed_chars - 1;
|
||||
buffer[string_index--] = '\0';
|
||||
for (int i = 0; i < exponent_; ++i) {
|
||||
@ -606,15 +626,15 @@ bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
buffer[string_index--] = '0';
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < used_digits_ - 1; ++i) {
|
||||
Chunk current_bigit = bigits_[i];
|
||||
for (int i = 0; i < used_bigits_ - 1; ++i) {
|
||||
Chunk current_bigit = RawBigit(i);
|
||||
for (int j = 0; j < kHexCharsPerBigit; ++j) {
|
||||
buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
|
||||
current_bigit >>= 4;
|
||||
}
|
||||
}
|
||||
// And finally the last bigit.
|
||||
Chunk most_significant_bigit = bigits_[used_digits_ - 1];
|
||||
Chunk most_significant_bigit = RawBigit(used_bigits_ - 1);
|
||||
while (most_significant_bigit != 0) {
|
||||
buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
|
||||
most_significant_bigit >>= 4;
|
||||
@ -623,25 +643,37 @@ bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
}
|
||||
|
||||
|
||||
Bignum::Chunk Bignum::BigitAt(int index) const {
|
||||
if (index >= BigitLength()) return 0;
|
||||
if (index < exponent_) return 0;
|
||||
return bigits_[index - exponent_];
|
||||
Bignum::Chunk Bignum::BigitOrZero(const int index) const {
|
||||
if (index >= BigitLength()) {
|
||||
return 0;
|
||||
}
|
||||
if (index < exponent_) {
|
||||
return 0;
|
||||
}
|
||||
return RawBigit(index - exponent_);
|
||||
}
|
||||
|
||||
|
||||
int Bignum::Compare(const Bignum& a, const Bignum& b) {
|
||||
ASSERT(a.IsClamped());
|
||||
ASSERT(b.IsClamped());
|
||||
int bigit_length_a = a.BigitLength();
|
||||
int bigit_length_b = b.BigitLength();
|
||||
if (bigit_length_a < bigit_length_b) return -1;
|
||||
if (bigit_length_a > bigit_length_b) return +1;
|
||||
for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) {
|
||||
Chunk bigit_a = a.BigitAt(i);
|
||||
Chunk bigit_b = b.BigitAt(i);
|
||||
if (bigit_a < bigit_b) return -1;
|
||||
if (bigit_a > bigit_b) return +1;
|
||||
DOUBLE_CONVERSION_ASSERT(a.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(b.IsClamped());
|
||||
const int bigit_length_a = a.BigitLength();
|
||||
const int bigit_length_b = b.BigitLength();
|
||||
if (bigit_length_a < bigit_length_b) {
|
||||
return -1;
|
||||
}
|
||||
if (bigit_length_a > bigit_length_b) {
|
||||
return +1;
|
||||
}
|
||||
for (int i = bigit_length_a - 1; i >= (std::min)(a.exponent_, b.exponent_); --i) {
|
||||
const Chunk bigit_a = a.BigitOrZero(i);
|
||||
const Chunk bigit_b = b.BigitOrZero(i);
|
||||
if (bigit_a < bigit_b) {
|
||||
return -1;
|
||||
}
|
||||
if (bigit_a > bigit_b) {
|
||||
return +1;
|
||||
}
|
||||
// Otherwise they are equal up to this digit. Try the next digit.
|
||||
}
|
||||
return 0;
|
||||
@ -649,14 +681,18 @@ int Bignum::Compare(const Bignum& a, const Bignum& b) {
|
||||
|
||||
|
||||
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
|
||||
ASSERT(a.IsClamped());
|
||||
ASSERT(b.IsClamped());
|
||||
ASSERT(c.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(a.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(b.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(c.IsClamped());
|
||||
if (a.BigitLength() < b.BigitLength()) {
|
||||
return PlusCompare(b, a, c);
|
||||
}
|
||||
if (a.BigitLength() + 1 < c.BigitLength()) return -1;
|
||||
if (a.BigitLength() > c.BigitLength()) return +1;
|
||||
if (a.BigitLength() + 1 < c.BigitLength()) {
|
||||
return -1;
|
||||
}
|
||||
if (a.BigitLength() > c.BigitLength()) {
|
||||
return +1;
|
||||
}
|
||||
// The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
|
||||
// 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
|
||||
// of 'a'.
|
||||
@ -666,92 +702,83 @@ int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
|
||||
|
||||
Chunk borrow = 0;
|
||||
// Starting at min_exponent all digits are == 0. So no need to compare them.
|
||||
int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_);
|
||||
const int min_exponent = (std::min)((std::min)(a.exponent_, b.exponent_), c.exponent_);
|
||||
for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
|
||||
Chunk chunk_a = a.BigitAt(i);
|
||||
Chunk chunk_b = b.BigitAt(i);
|
||||
Chunk chunk_c = c.BigitAt(i);
|
||||
Chunk sum = chunk_a + chunk_b;
|
||||
const Chunk chunk_a = a.BigitOrZero(i);
|
||||
const Chunk chunk_b = b.BigitOrZero(i);
|
||||
const Chunk chunk_c = c.BigitOrZero(i);
|
||||
const Chunk sum = chunk_a + chunk_b;
|
||||
if (sum > chunk_c + borrow) {
|
||||
return +1;
|
||||
} else {
|
||||
borrow = chunk_c + borrow - sum;
|
||||
if (borrow > 1) return -1;
|
||||
if (borrow > 1) {
|
||||
return -1;
|
||||
}
|
||||
borrow <<= kBigitSize;
|
||||
}
|
||||
}
|
||||
if (borrow == 0) return 0;
|
||||
if (borrow == 0) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Clamp() {
|
||||
while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) {
|
||||
used_digits_--;
|
||||
while (used_bigits_ > 0 && RawBigit(used_bigits_ - 1) == 0) {
|
||||
used_bigits_--;
|
||||
}
|
||||
if (used_digits_ == 0) {
|
||||
if (used_bigits_ == 0) {
|
||||
// Zero.
|
||||
exponent_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Bignum::IsClamped() const {
|
||||
return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Zero() {
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
used_digits_ = 0;
|
||||
exponent_ = 0;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Align(const Bignum& other) {
|
||||
if (exponent_ > other.exponent_) {
|
||||
// If "X" represents a "hidden" digit (by the exponent) then we are in the
|
||||
// If "X" represents a "hidden" bigit (by the exponent) then we are in the
|
||||
// following case (a == this, b == other):
|
||||
// a: aaaaaaXXXX or a: aaaaaXXX
|
||||
// b: bbbbbbX b: bbbbbbbbXX
|
||||
// We replace some of the hidden digits (X) of a with 0 digits.
|
||||
// a: aaaaaa000X or a: aaaaa0XX
|
||||
int zero_digits = exponent_ - other.exponent_;
|
||||
EnsureCapacity(used_digits_ + zero_digits);
|
||||
for (int i = used_digits_ - 1; i >= 0; --i) {
|
||||
bigits_[i + zero_digits] = bigits_[i];
|
||||
const int zero_bigits = exponent_ - other.exponent_;
|
||||
EnsureCapacity(used_bigits_ + zero_bigits);
|
||||
for (int i = used_bigits_ - 1; i >= 0; --i) {
|
||||
RawBigit(i + zero_bigits) = RawBigit(i);
|
||||
}
|
||||
for (int i = 0; i < zero_digits; ++i) {
|
||||
bigits_[i] = 0;
|
||||
for (int i = 0; i < zero_bigits; ++i) {
|
||||
RawBigit(i) = 0;
|
||||
}
|
||||
used_digits_ += zero_digits;
|
||||
exponent_ -= zero_digits;
|
||||
ASSERT(used_digits_ >= 0);
|
||||
ASSERT(exponent_ >= 0);
|
||||
used_bigits_ += zero_bigits;
|
||||
exponent_ -= zero_bigits;
|
||||
|
||||
DOUBLE_CONVERSION_ASSERT(used_bigits_ >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent_ >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::BigitsShiftLeft(int shift_amount) {
|
||||
ASSERT(shift_amount < kBigitSize);
|
||||
ASSERT(shift_amount >= 0);
|
||||
void Bignum::BigitsShiftLeft(const int shift_amount) {
|
||||
DOUBLE_CONVERSION_ASSERT(shift_amount < kBigitSize);
|
||||
DOUBLE_CONVERSION_ASSERT(shift_amount >= 0);
|
||||
Chunk carry = 0;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount);
|
||||
bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const Chunk new_carry = RawBigit(i) >> (kBigitSize - shift_amount);
|
||||
RawBigit(i) = ((RawBigit(i) << shift_amount) + carry) & kBigitMask;
|
||||
carry = new_carry;
|
||||
}
|
||||
if (carry != 0) {
|
||||
bigits_[used_digits_] = carry;
|
||||
used_digits_++;
|
||||
RawBigit(used_bigits_) = carry;
|
||||
used_bigits_++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::SubtractTimes(const Bignum& other, int factor) {
|
||||
ASSERT(exponent_ <= other.exponent_);
|
||||
void Bignum::SubtractTimes(const Bignum& other, const int factor) {
|
||||
DOUBLE_CONVERSION_ASSERT(exponent_ <= other.exponent_);
|
||||
if (factor < 3) {
|
||||
for (int i = 0; i < factor; ++i) {
|
||||
SubtractBignum(other);
|
||||
@ -759,19 +786,21 @@ void Bignum::SubtractTimes(const Bignum& other, int factor) {
|
||||
return;
|
||||
}
|
||||
Chunk borrow = 0;
|
||||
int exponent_diff = other.exponent_ - exponent_;
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i];
|
||||
DoubleChunk remove = borrow + product;
|
||||
Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask);
|
||||
bigits_[i + exponent_diff] = difference & kBigitMask;
|
||||
const int exponent_diff = other.exponent_ - exponent_;
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
const DoubleChunk product = static_cast<DoubleChunk>(factor) * other.RawBigit(i);
|
||||
const DoubleChunk remove = borrow + product;
|
||||
const Chunk difference = RawBigit(i + exponent_diff) - (remove & kBigitMask);
|
||||
RawBigit(i + exponent_diff) = difference & kBigitMask;
|
||||
borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
|
||||
(remove >> kBigitSize));
|
||||
}
|
||||
for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) {
|
||||
if (borrow == 0) return;
|
||||
Chunk difference = bigits_[i] - borrow;
|
||||
bigits_[i] = difference & kBigitMask;
|
||||
for (int i = other.used_bigits_ + exponent_diff; i < used_bigits_; ++i) {
|
||||
if (borrow == 0) {
|
||||
return;
|
||||
}
|
||||
const Chunk difference = RawBigit(i) - borrow;
|
||||
RawBigit(i) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
}
|
||||
Clamp();
|
||||
|
@ -53,26 +53,27 @@ class Bignum {
|
||||
// exponent.
|
||||
static const int kMaxSignificantBits = 3584;
|
||||
|
||||
Bignum();
|
||||
void AssignUInt16(uint16_t value);
|
||||
Bignum() : used_bigits_(0), exponent_(0) {}
|
||||
|
||||
void AssignUInt16(const uint16_t value);
|
||||
void AssignUInt64(uint64_t value);
|
||||
void AssignBignum(const Bignum& other);
|
||||
|
||||
void AssignDecimalString(Vector<const char> value);
|
||||
void AssignHexString(Vector<const char> value);
|
||||
void AssignDecimalString(const Vector<const char> value);
|
||||
void AssignHexString(const Vector<const char> value);
|
||||
|
||||
void AssignPowerUInt16(uint16_t base, int exponent);
|
||||
void AssignPowerUInt16(uint16_t base, const int exponent);
|
||||
|
||||
void AddUInt64(uint64_t operand);
|
||||
void AddUInt64(const uint64_t operand);
|
||||
void AddBignum(const Bignum& other);
|
||||
// Precondition: this >= other.
|
||||
void SubtractBignum(const Bignum& other);
|
||||
|
||||
void Square();
|
||||
void ShiftLeft(int shift_amount);
|
||||
void MultiplyByUInt32(uint32_t factor);
|
||||
void MultiplyByUInt64(uint64_t factor);
|
||||
void MultiplyByPowerOfTen(int exponent);
|
||||
void ShiftLeft(const int shift_amount);
|
||||
void MultiplyByUInt32(const uint32_t factor);
|
||||
void MultiplyByUInt64(const uint64_t factor);
|
||||
void MultiplyByPowerOfTen(const int exponent);
|
||||
void Times10() { return MultiplyByUInt32(10); }
|
||||
// Pseudocode:
|
||||
// int result = this / other;
|
||||
@ -80,7 +81,7 @@ class Bignum {
|
||||
// In the worst case this function is in O(this/other).
|
||||
uint16_t DivideModuloIntBignum(const Bignum& other);
|
||||
|
||||
bool ToHexString(char* buffer, int buffer_size) const;
|
||||
bool ToHexString(char* buffer, const int buffer_size) const;
|
||||
|
||||
// Returns
|
||||
// -1 if a < b,
|
||||
@ -124,33 +125,40 @@ class Bignum {
|
||||
// grow. There are no checks if the stack-allocated space is sufficient.
|
||||
static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
|
||||
|
||||
void EnsureCapacity(int size) {
|
||||
static void EnsureCapacity(const int size) {
|
||||
if (size > kBigitCapacity) {
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
void Align(const Bignum& other);
|
||||
void Clamp();
|
||||
bool IsClamped() const;
|
||||
void Zero();
|
||||
bool IsClamped() const {
|
||||
return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
|
||||
}
|
||||
void Zero() {
|
||||
used_bigits_ = 0;
|
||||
exponent_ = 0;
|
||||
}
|
||||
// Requires this to have enough capacity (no tests done).
|
||||
// Updates used_digits_ if necessary.
|
||||
// Updates used_bigits_ if necessary.
|
||||
// shift_amount must be < kBigitSize.
|
||||
void BigitsShiftLeft(int shift_amount);
|
||||
// BigitLength includes the "hidden" digits encoded in the exponent.
|
||||
int BigitLength() const { return used_digits_ + exponent_; }
|
||||
Chunk BigitAt(int index) const;
|
||||
void SubtractTimes(const Bignum& other, int factor);
|
||||
void BigitsShiftLeft(const int shift_amount);
|
||||
// BigitLength includes the "hidden" bigits encoded in the exponent.
|
||||
int BigitLength() const { return used_bigits_ + exponent_; }
|
||||
Chunk& RawBigit(const int index);
|
||||
const Chunk& RawBigit(const int index) const;
|
||||
Chunk BigitOrZero(const int index) const;
|
||||
void SubtractTimes(const Bignum& other, const int factor);
|
||||
|
||||
// The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
|
||||
// where the value of the buffer consists of the lower kBigitSize bits of
|
||||
// the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
|
||||
// significant bits.
|
||||
int16_t used_bigits_;
|
||||
int16_t exponent_;
|
||||
Chunk bigits_buffer_[kBigitCapacity];
|
||||
// A vector backed by bigits_buffer_. This way accesses to the array are
|
||||
// checked for out-of-bounds errors.
|
||||
Vector<Chunk> bigits_;
|
||||
int used_digits_;
|
||||
// The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
|
||||
int exponent_;
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Bignum);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -49,6 +49,8 @@ U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
namespace PowersOfTenCache {
|
||||
|
||||
struct CachedPower {
|
||||
uint64_t significand;
|
||||
int16_t binary_exponent;
|
||||
@ -56,103 +58,99 @@ struct CachedPower {
|
||||
};
|
||||
|
||||
static const CachedPower kCachedPowers[] = {
|
||||
{UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348},
|
||||
{UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340},
|
||||
{UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332},
|
||||
{UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324},
|
||||
{UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316},
|
||||
{UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308},
|
||||
{UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300},
|
||||
{UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292},
|
||||
{UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284},
|
||||
{UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276},
|
||||
{UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268},
|
||||
{UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260},
|
||||
{UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252},
|
||||
{UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244},
|
||||
{UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236},
|
||||
{UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228},
|
||||
{UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220},
|
||||
{UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212},
|
||||
{UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204},
|
||||
{UINT64_2PART_C(0xef340a98, 172aace5), -715, -196},
|
||||
{UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188},
|
||||
{UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180},
|
||||
{UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172},
|
||||
{UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164},
|
||||
{UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156},
|
||||
{UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148},
|
||||
{UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140},
|
||||
{UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132},
|
||||
{UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124},
|
||||
{UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116},
|
||||
{UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108},
|
||||
{UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100},
|
||||
{UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92},
|
||||
{UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84},
|
||||
{UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76},
|
||||
{UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68},
|
||||
{UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60},
|
||||
{UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52},
|
||||
{UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44},
|
||||
{UINT64_2PART_C(0xaa242499, 697392d3), -183, -36},
|
||||
{UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28},
|
||||
{UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20},
|
||||
{UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12},
|
||||
{UINT64_2PART_C(0xd1b71758, e219652c), -77, -4},
|
||||
{UINT64_2PART_C(0x9c400000, 00000000), -50, 4},
|
||||
{UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12},
|
||||
{UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20},
|
||||
{UINT64_2PART_C(0x813f3978, f8940984), 30, 28},
|
||||
{UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36},
|
||||
{UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44},
|
||||
{UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52},
|
||||
{UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60},
|
||||
{UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68},
|
||||
{UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76},
|
||||
{UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84},
|
||||
{UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92},
|
||||
{UINT64_2PART_C(0x924d692c, a61be758), 269, 100},
|
||||
{UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108},
|
||||
{UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116},
|
||||
{UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124},
|
||||
{UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132},
|
||||
{UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140},
|
||||
{UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148},
|
||||
{UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156},
|
||||
{UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164},
|
||||
{UINT64_2PART_C(0xa59bc234, db398c25), 508, 172},
|
||||
{UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180},
|
||||
{UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188},
|
||||
{UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196},
|
||||
{UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204},
|
||||
{UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212},
|
||||
{UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220},
|
||||
{UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228},
|
||||
{UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236},
|
||||
{UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244},
|
||||
{UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252},
|
||||
{UINT64_2PART_C(0xd01fef10, a657842c), 800, 260},
|
||||
{UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268},
|
||||
{UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276},
|
||||
{UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284},
|
||||
{UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292},
|
||||
{UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300},
|
||||
{UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308},
|
||||
{UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316},
|
||||
{UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324},
|
||||
{UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332},
|
||||
{UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xef340a98, 172aace5), -715, -196},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaa242499, 697392d3), -183, -36},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd1b71758, e219652c), -77, -4},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50, 4},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x813f3978, f8940984), 30, 28},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x924d692c, a61be758), 269, 100},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa59bc234, db398c25), 508, 172},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd01fef10, a657842c), 800, 260},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340},
|
||||
};
|
||||
|
||||
static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
|
||||
static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
|
||||
// Difference between the decimal exponents in the table above.
|
||||
const int PowersOfTenCache::kDecimalExponentDistance = 8;
|
||||
const int PowersOfTenCache::kMinDecimalExponent = -348;
|
||||
const int PowersOfTenCache::kMaxDecimalExponent = 340;
|
||||
|
||||
void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
|
||||
void GetCachedPowerForBinaryExponentRange(
|
||||
int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
@ -162,30 +160,32 @@ void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
|
||||
int foo = kCachedPowersOffset;
|
||||
int index =
|
||||
(foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
|
||||
ASSERT(0 <= index && index < static_cast<int>(ARRAY_SIZE(kCachedPowers)));
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= index && index < static_cast<int>(DOUBLE_CONVERSION_ARRAY_SIZE(kCachedPowers)));
|
||||
CachedPower cached_power = kCachedPowers[index];
|
||||
ASSERT(min_exponent <= cached_power.binary_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(min_exponent <= cached_power.binary_exponent);
|
||||
(void) max_exponent; // Mark variable as used.
|
||||
ASSERT(cached_power.binary_exponent <= max_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(cached_power.binary_exponent <= max_exponent);
|
||||
*decimal_exponent = cached_power.decimal_exponent;
|
||||
*power = DiyFp(cached_power.significand, cached_power.binary_exponent);
|
||||
}
|
||||
|
||||
|
||||
void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent) {
|
||||
ASSERT(kMinDecimalExponent <= requested_exponent);
|
||||
ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
|
||||
void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(kMinDecimalExponent <= requested_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
|
||||
int index =
|
||||
(requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance;
|
||||
CachedPower cached_power = kCachedPowers[index];
|
||||
*power = DiyFp(cached_power.significand, cached_power.binary_exponent);
|
||||
*found_exponent = cached_power.decimal_exponent;
|
||||
ASSERT(*found_exponent <= requested_exponent);
|
||||
ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance);
|
||||
DOUBLE_CONVERSION_ASSERT(*found_exponent <= requested_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance);
|
||||
}
|
||||
|
||||
} // namespace PowersOfTenCache
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
|
@ -46,32 +46,32 @@ U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class PowersOfTenCache {
|
||||
public:
|
||||
namespace PowersOfTenCache {
|
||||
|
||||
// Not all powers of ten are cached. The decimal exponent of two neighboring
|
||||
// cached numbers will differ by kDecimalExponentDistance.
|
||||
static const int kDecimalExponentDistance;
|
||||
static const int kDecimalExponentDistance = 8;
|
||||
|
||||
static const int kMinDecimalExponent;
|
||||
static const int kMaxDecimalExponent;
|
||||
static const int kMinDecimalExponent = -348;
|
||||
static const int kMaxDecimalExponent = 340;
|
||||
|
||||
// Returns a cached power-of-ten with a binary exponent in the range
|
||||
// [min_exponent; max_exponent] (boundaries included).
|
||||
static void GetCachedPowerForBinaryExponentRange(int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
int* decimal_exponent);
|
||||
void GetCachedPowerForBinaryExponentRange(int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
int* decimal_exponent);
|
||||
|
||||
// Returns a cached power of ten x ~= 10^k such that
|
||||
// k <= decimal_exponent < k + kCachedPowersDecimalDistance.
|
||||
// The given decimal_exponent must satisfy
|
||||
// kMinDecimalExponent <= requested_exponent, and
|
||||
// requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance.
|
||||
static void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent);
|
||||
};
|
||||
void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent);
|
||||
|
||||
} // namespace PowersOfTenCache
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
|
@ -1,74 +0,0 @@
|
||||
// © 2018 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
//
|
||||
// From the double-conversion library. Original license:
|
||||
//
|
||||
// Copyright 2010 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
|
||||
#include "double-conversion-diy-fp.h"
|
||||
#include "double-conversion-utils.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
void DiyFp::Multiply(const DiyFp& other) {
|
||||
// Simply "emulates" a 128 bit multiplication.
|
||||
// However: the resulting number only contains 64 bits. The least
|
||||
// significant 64 bits are only used for rounding the most significant 64
|
||||
// bits.
|
||||
const uint64_t kM32 = 0xFFFFFFFFU;
|
||||
uint64_t a = f_ >> 32;
|
||||
uint64_t b = f_ & kM32;
|
||||
uint64_t c = other.f_ >> 32;
|
||||
uint64_t d = other.f_ & kM32;
|
||||
uint64_t ac = a * c;
|
||||
uint64_t bc = b * c;
|
||||
uint64_t ad = a * d;
|
||||
uint64_t bd = b * d;
|
||||
uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
|
||||
// By adding 1U << 31 to tmp we round the final result.
|
||||
// Halfway cases will be round up.
|
||||
tmp += 1U << 31;
|
||||
uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
|
||||
e_ += other.e_ + 64;
|
||||
f_ = result_f;
|
||||
}
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
U_NAMESPACE_END
|
||||
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
|
@ -50,36 +50,55 @@ namespace double_conversion {
|
||||
// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
|
||||
// have the most significant bit of the significand set.
|
||||
// Multiplication and Subtraction do not normalize their results.
|
||||
// DiyFp are not designed to contain special doubles (NaN and Infinity).
|
||||
// DiyFp store only non-negative numbers and are not designed to contain special
|
||||
// doubles (NaN and Infinity).
|
||||
class DiyFp {
|
||||
public:
|
||||
static const int kSignificandSize = 64;
|
||||
|
||||
DiyFp() : f_(0), e_(0) {}
|
||||
DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {}
|
||||
DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {}
|
||||
|
||||
// this = this - other.
|
||||
// this -= other.
|
||||
// The exponents of both numbers must be the same and the significand of this
|
||||
// must be bigger than the significand of other.
|
||||
// must be greater or equal than the significand of other.
|
||||
// The result will not be normalized.
|
||||
void Subtract(const DiyFp& other) {
|
||||
ASSERT(e_ == other.e_);
|
||||
ASSERT(f_ >= other.f_);
|
||||
DOUBLE_CONVERSION_ASSERT(e_ == other.e_);
|
||||
DOUBLE_CONVERSION_ASSERT(f_ >= other.f_);
|
||||
f_ -= other.f_;
|
||||
}
|
||||
|
||||
// Returns a - b.
|
||||
// The exponents of both numbers must be the same and this must be bigger
|
||||
// than other. The result will not be normalized.
|
||||
// The exponents of both numbers must be the same and a must be greater
|
||||
// or equal than b. The result will not be normalized.
|
||||
static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
|
||||
DiyFp result = a;
|
||||
result.Subtract(b);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// this = this * other.
|
||||
void Multiply(const DiyFp& other);
|
||||
// this *= other.
|
||||
void Multiply(const DiyFp& other) {
|
||||
// Simply "emulates" a 128 bit multiplication.
|
||||
// However: the resulting number only contains 64 bits. The least
|
||||
// significant 64 bits are only used for rounding the most significant 64
|
||||
// bits.
|
||||
const uint64_t kM32 = 0xFFFFFFFFU;
|
||||
const uint64_t a = f_ >> 32;
|
||||
const uint64_t b = f_ & kM32;
|
||||
const uint64_t c = other.f_ >> 32;
|
||||
const uint64_t d = other.f_ & kM32;
|
||||
const uint64_t ac = a * c;
|
||||
const uint64_t bc = b * c;
|
||||
const uint64_t ad = a * d;
|
||||
const uint64_t bd = b * d;
|
||||
// By adding 1U << 31 to tmp we round the final result.
|
||||
// Halfway cases will be rounded up.
|
||||
const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31);
|
||||
e_ += other.e_ + 64;
|
||||
f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
|
||||
}
|
||||
|
||||
// returns a * b;
|
||||
static DiyFp Times(const DiyFp& a, const DiyFp& b) {
|
||||
@ -89,13 +108,13 @@ class DiyFp {
|
||||
}
|
||||
|
||||
void Normalize() {
|
||||
ASSERT(f_ != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(f_ != 0);
|
||||
uint64_t significand = f_;
|
||||
int exponent = e_;
|
||||
int32_t exponent = e_;
|
||||
|
||||
// This method is mainly called for normalizing boundaries. In general
|
||||
// boundaries need to be shifted by 10 bits. We thus optimize for this case.
|
||||
const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
|
||||
// This method is mainly called for normalizing boundaries. In general,
|
||||
// boundaries need to be shifted by 10 bits, and we optimize for this case.
|
||||
const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000);
|
||||
while ((significand & k10MSBits) == 0) {
|
||||
significand <<= 10;
|
||||
exponent -= 10;
|
||||
@ -115,16 +134,16 @@ class DiyFp {
|
||||
}
|
||||
|
||||
uint64_t f() const { return f_; }
|
||||
int e() const { return e_; }
|
||||
int32_t e() const { return e_; }
|
||||
|
||||
void set_f(uint64_t new_value) { f_ = new_value; }
|
||||
void set_e(int new_value) { e_ = new_value; }
|
||||
void set_e(int32_t new_value) { e_ = new_value; }
|
||||
|
||||
private:
|
||||
static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
|
||||
|
||||
uint64_t f_;
|
||||
int e_;
|
||||
int32_t e_;
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
450
icu4c/source/i18n/double-conversion-double-to-string.cpp
Normal file
450
icu4c/source/i18n/double-conversion-double-to-string.cpp
Normal file
@ -0,0 +1,450 @@
|
||||
// © 2018 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
//
|
||||
// From the double-conversion library. Original license:
|
||||
//
|
||||
// Copyright 2010 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
// The file fixed-dtoa.h is not needed.
|
||||
|
||||
#include "double-conversion-double-to-string.h"
|
||||
|
||||
#include "double-conversion-bignum-dtoa.h"
|
||||
#include "double-conversion-fast-dtoa.h"
|
||||
#include "double-conversion-ieee.h"
|
||||
#include "double-conversion-utils.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
#if 0 // not needed for ICU
|
||||
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
|
||||
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
|
||||
static DoubleToStringConverter converter(flags,
|
||||
"Infinity",
|
||||
"NaN",
|
||||
'e',
|
||||
-6, 21,
|
||||
6, 0);
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::HandleSpecialValues(
|
||||
double value,
|
||||
StringBuilder* result_builder) const {
|
||||
Double double_inspect(value);
|
||||
if (double_inspect.IsInfinite()) {
|
||||
if (infinity_symbol_ == NULL) return false;
|
||||
if (value < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
result_builder->AddString(infinity_symbol_);
|
||||
return true;
|
||||
}
|
||||
if (double_inspect.IsNan()) {
|
||||
if (nan_symbol_ == NULL) return false;
|
||||
result_builder->AddString(nan_symbol_);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateExponentialRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const {
|
||||
DOUBLE_CONVERSION_ASSERT(length != 0);
|
||||
result_builder->AddCharacter(decimal_digits[0]);
|
||||
if (length != 1) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddSubstring(&decimal_digits[1], length-1);
|
||||
}
|
||||
result_builder->AddCharacter(exponent_character_);
|
||||
if (exponent < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
exponent = -exponent;
|
||||
} else {
|
||||
if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
|
||||
result_builder->AddCharacter('+');
|
||||
}
|
||||
}
|
||||
if (exponent == 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
return;
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT(exponent < 1e4);
|
||||
// Changing this constant requires updating the comment of DoubleToStringConverter constructor
|
||||
const int kMaxExponentLength = 5;
|
||||
char buffer[kMaxExponentLength + 1];
|
||||
buffer[kMaxExponentLength] = '\0';
|
||||
int first_char_pos = kMaxExponentLength;
|
||||
while (exponent > 0) {
|
||||
buffer[--first_char_pos] = '0' + (exponent % 10);
|
||||
exponent /= 10;
|
||||
}
|
||||
// Add prefix '0' to make exponent width >= min(min_exponent_with_, kMaxExponentLength)
|
||||
// For example: convert 1e+9 -> 1e+09, if min_exponent_with_ is set to 2
|
||||
while(kMaxExponentLength - first_char_pos < std::min(min_exponent_width_, kMaxExponentLength)) {
|
||||
buffer[--first_char_pos] = '0';
|
||||
}
|
||||
result_builder->AddSubstring(&buffer[first_char_pos],
|
||||
kMaxExponentLength - first_char_pos);
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateDecimalRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const {
|
||||
// Create a representation that is padded with zeros if needed.
|
||||
if (decimal_point <= 0) {
|
||||
// "0.00000decimal_rep" or "0.000decimal_rep00".
|
||||
result_builder->AddCharacter('0');
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', -decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(length <= digits_after_point - (-decimal_point));
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
int remaining_digits = digits_after_point - (-decimal_point) - length;
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
} else if (decimal_point >= length) {
|
||||
// "decimal_rep0000.00000" or "decimal_rep.0000".
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
result_builder->AddPadding('0', decimal_point - length);
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', digits_after_point);
|
||||
}
|
||||
} else {
|
||||
// "decima.l_rep000".
|
||||
DOUBLE_CONVERSION_ASSERT(digits_after_point > 0);
|
||||
result_builder->AddSubstring(decimal_digits, decimal_point);
|
||||
result_builder->AddCharacter('.');
|
||||
DOUBLE_CONVERSION_ASSERT(length - decimal_point <= digits_after_point);
|
||||
result_builder->AddSubstring(&decimal_digits[decimal_point],
|
||||
length - decimal_point);
|
||||
int remaining_digits = digits_after_point - (length - decimal_point);
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
if (digits_after_point == 0) {
|
||||
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
}
|
||||
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToShortestIeeeNumber(
|
||||
double value,
|
||||
StringBuilder* result_builder,
|
||||
DoubleToStringConverter::DtoaMode mode) const {
|
||||
DOUBLE_CONVERSION_ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE);
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
const int kDecimalRepCapacity = kBase10MaximalLength + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
if ((decimal_in_shortest_low_ <= exponent) &&
|
||||
(exponent < decimal_in_shortest_high_)) {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
|
||||
decimal_point,
|
||||
(std::max)(0, decimal_rep_length - decimal_point),
|
||||
result_builder);
|
||||
} else {
|
||||
CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
DOUBLE_CONVERSION_ASSERT(kMaxFixedDigitsBeforePoint == 60);
|
||||
const double kFirstNonFixed = 1e60;
|
||||
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
|
||||
if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for the '\0' byte.
|
||||
const int kDecimalRepCapacity =
|
||||
kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
DoubleToAscii(value, FIXED, requested_digits,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
requested_digits, result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToExponential(
|
||||
double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits < -1) return false;
|
||||
if (requested_digits > kMaxExponentialDigits) return false;
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for digit before the decimal point and the '\0' character.
|
||||
const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
|
||||
DOUBLE_CONVERSION_ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
#ifndef NDEBUG
|
||||
// Problem: there is an assert in StringBuilder::AddSubstring() that
|
||||
// will pass this buffer to strlen(), and this buffer is not generally
|
||||
// null-terminated.
|
||||
memset(decimal_rep, 0, sizeof(decimal_rep));
|
||||
#endif
|
||||
int decimal_rep_length;
|
||||
|
||||
if (requested_digits == -1) {
|
||||
DoubleToAscii(value, SHORTEST, 0,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
} else {
|
||||
DoubleToAscii(value, PRECISION, requested_digits + 1,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= requested_digits + 1);
|
||||
|
||||
for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
decimal_rep_length = requested_digits + 1;
|
||||
}
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
decimal_rep_length,
|
||||
exponent,
|
||||
result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add one for the terminating null character.
|
||||
const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, PRECISION, precision,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= precision);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
// The exponent if we print the number as x.xxeyyy. That is with the
|
||||
// decimal point after the first digit.
|
||||
int exponent = decimal_point - 1;
|
||||
|
||||
int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
|
||||
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
|
||||
(decimal_point - precision + extra_zero >
|
||||
max_trailing_padding_zeroes_in_precision_mode_)) {
|
||||
// Fill buffer to contain 'precision' digits.
|
||||
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
|
||||
// is allowed to return less characters.
|
||||
for (int i = decimal_rep_length; i < precision; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
precision,
|
||||
exponent,
|
||||
result_builder);
|
||||
} else {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
(std::max)(0, precision - decimal_point),
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // not needed for ICU
|
||||
|
||||
|
||||
static BignumDtoaMode DtoaToBignumDtoaMode(
|
||||
DoubleToStringConverter::DtoaMode dtoa_mode) {
|
||||
switch (dtoa_mode) {
|
||||
case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
|
||||
case DoubleToStringConverter::SHORTEST_SINGLE:
|
||||
return BIGNUM_DTOA_SHORTEST_SINGLE;
|
||||
case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
|
||||
case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
|
||||
default:
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point) {
|
||||
Vector<char> vector(buffer, buffer_length);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
|
||||
|
||||
if (Double(v).Sign() < 0) {
|
||||
*sign = true;
|
||||
v = -v;
|
||||
} else {
|
||||
*sign = false;
|
||||
}
|
||||
|
||||
if (mode == PRECISION && requested_digits == 0) {
|
||||
vector[0] = '\0';
|
||||
*length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (v == 0) {
|
||||
vector[0] = '0';
|
||||
vector[1] = '\0';
|
||||
*length = 1;
|
||||
*point = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
bool fast_worked;
|
||||
switch (mode) {
|
||||
case SHORTEST:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
|
||||
break;
|
||||
#if 0 // not needed for ICU
|
||||
case SHORTEST_SINGLE:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
|
||||
vector, length, point);
|
||||
break;
|
||||
case FIXED:
|
||||
fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
|
||||
break;
|
||||
case PRECISION:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
||||
vector, length, point);
|
||||
break;
|
||||
#endif // not needed for ICU
|
||||
default:
|
||||
fast_worked = false;
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
if (fast_worked) return;
|
||||
|
||||
// If the fast dtoa didn't succeed use the slower bignum version.
|
||||
BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
|
||||
BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
|
||||
vector[*length] = '\0';
|
||||
}
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
U_NAMESPACE_END
|
||||
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
|
419
icu4c/source/i18n/double-conversion-double-to-string.h
Normal file
419
icu4c/source/i18n/double-conversion-double-to-string.h
Normal file
@ -0,0 +1,419 @@
|
||||
// © 2018 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
//
|
||||
// From the double-conversion library. Original license:
|
||||
//
|
||||
// Copyright 2012 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
||||
#define DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
|
||||
#include "double-conversion-utils.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class DoubleToStringConverter {
|
||||
public:
|
||||
#if 0 // not needed for ICU
|
||||
// When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
|
||||
// or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
|
||||
// function returns false.
|
||||
static const int kMaxFixedDigitsBeforePoint = 60;
|
||||
static const int kMaxFixedDigitsAfterPoint = 60;
|
||||
|
||||
// When calling ToExponential with a requested_digits
|
||||
// parameter > kMaxExponentialDigits then the function returns false.
|
||||
static const int kMaxExponentialDigits = 120;
|
||||
|
||||
// When calling ToPrecision with a requested_digits
|
||||
// parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
|
||||
// then the function returns false.
|
||||
static const int kMinPrecisionDigits = 1;
|
||||
static const int kMaxPrecisionDigits = 120;
|
||||
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
EMIT_POSITIVE_EXPONENT_SIGN = 1,
|
||||
EMIT_TRAILING_DECIMAL_POINT = 2,
|
||||
EMIT_TRAILING_ZERO_AFTER_POINT = 4,
|
||||
UNIQUE_ZERO = 8
|
||||
};
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
|
||||
// form, emits a '+' for positive exponents. Example: 1.2e+2.
|
||||
// - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
|
||||
// converted into decimal format then a trailing decimal point is appended.
|
||||
// Example: 2345.0 is converted to "2345.".
|
||||
// - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
|
||||
// emits a trailing '0'-character. This flag requires the
|
||||
// EXMIT_TRAILING_DECIMAL_POINT flag.
|
||||
// Example: 2345.0 is converted to "2345.0".
|
||||
// - UNIQUE_ZERO: "-0.0" is converted to "0.0".
|
||||
//
|
||||
// Infinity symbol and nan_symbol provide the string representation for these
|
||||
// special values. If the string is NULL and the special value is encountered
|
||||
// then the conversion functions return false.
|
||||
//
|
||||
// The exponent_character is used in exponential representations. It is
|
||||
// usually 'e' or 'E'.
|
||||
//
|
||||
// When converting to the shortest representation the converter will
|
||||
// represent input numbers in decimal format if they are in the interval
|
||||
// [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
|
||||
// (lower boundary included, greater boundary excluded).
|
||||
// Example: with decimal_in_shortest_low = -6 and
|
||||
// decimal_in_shortest_high = 21:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// When converting to precision mode the converter may add
|
||||
// max_leading_padding_zeroes before returning the number in exponential
|
||||
// format.
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
//
|
||||
// The min_exponent_width is used for exponential representations.
|
||||
// The converter adds leading '0's to the exponent until the exponent
|
||||
// is at least min_exponent_width digits long.
|
||||
// The min_exponent_width is clamped to 5.
|
||||
// As such, the exponent may never have more than 5 digits in total.
|
||||
DoubleToStringConverter(int flags,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
char exponent_character,
|
||||
int decimal_in_shortest_low,
|
||||
int decimal_in_shortest_high,
|
||||
int max_leading_padding_zeroes_in_precision_mode,
|
||||
int max_trailing_padding_zeroes_in_precision_mode,
|
||||
int min_exponent_width = 0)
|
||||
: flags_(flags),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
exponent_character_(exponent_character),
|
||||
decimal_in_shortest_low_(decimal_in_shortest_low),
|
||||
decimal_in_shortest_high_(decimal_in_shortest_high),
|
||||
max_leading_padding_zeroes_in_precision_mode_(
|
||||
max_leading_padding_zeroes_in_precision_mode),
|
||||
max_trailing_padding_zeroes_in_precision_mode_(
|
||||
max_trailing_padding_zeroes_in_precision_mode),
|
||||
min_exponent_width_(min_exponent_width) {
|
||||
// When 'trailing zero after the point' is set, then 'trailing point'
|
||||
// must be set too.
|
||||
DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
|
||||
!((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
|
||||
}
|
||||
|
||||
// Returns a converter following the EcmaScript specification.
|
||||
static const DoubleToStringConverter& EcmaScriptConverter();
|
||||
|
||||
// Computes the shortest string of digits that correctly represent the input
|
||||
// number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
|
||||
// (see constructor) it then either returns a decimal representation, or an
|
||||
// exponential representation.
|
||||
// Example with decimal_in_shortest_low = -6,
|
||||
// decimal_in_shortest_high = 21,
|
||||
// EMIT_POSITIVE_EXPONENT_SIGN activated, and
|
||||
// EMIT_TRAILING_DECIMAL_POINT deactived:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// Note: the conversion may round the output if the returned string
|
||||
// is accurate enough to uniquely identify the input-number.
|
||||
// For example the most precise representation of the double 9e59 equals
|
||||
// "899999999999999918767229449717619953810131273674690656206848", but
|
||||
// the converter will return the shorter (but still correct) "9e59".
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except when the input value is special and no infinity_symbol or
|
||||
// nan_symbol has been given to the constructor.
|
||||
bool ToShortest(double value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
|
||||
}
|
||||
|
||||
// Same as ToShortest, but for single-precision floats.
|
||||
bool ToShortestSingle(float value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
|
||||
}
|
||||
|
||||
|
||||
// Computes a decimal representation with a fixed number of digits after the
|
||||
// decimal point. The last emitted digit is rounded.
|
||||
//
|
||||
// Examples:
|
||||
// ToFixed(3.12, 1) -> "3.1"
|
||||
// ToFixed(3.1415, 3) -> "3.142"
|
||||
// ToFixed(1234.56789, 4) -> "1234.5679"
|
||||
// ToFixed(1.23, 5) -> "1.23000"
|
||||
// ToFixed(0.1, 4) -> "0.1000"
|
||||
// ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
|
||||
// ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
|
||||
// ToFixed(0.1, 17) -> "0.10000000000000001"
|
||||
//
|
||||
// If requested_digits equals 0, then the tail of the result depends on
|
||||
// the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples, for requested_digits == 0,
|
||||
// let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
|
||||
// - false and false: then 123.45 -> 123
|
||||
// 0.678 -> 1
|
||||
// - true and false: then 123.45 -> 123.
|
||||
// 0.678 -> 1.
|
||||
// - true and true: then 123.45 -> 123.0
|
||||
// 0.678 -> 1.0
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'value' > 10^kMaxFixedDigitsBeforePoint, or
|
||||
// - 'requested_digits' > kMaxFixedDigitsAfterPoint.
|
||||
// The last two conditions imply that the result will never contain more than
|
||||
// 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
|
||||
// (one additional character for the sign, and one for the decimal point).
|
||||
bool ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes a representation in exponential format with requested_digits
|
||||
// after the decimal point. The last emitted digit is rounded.
|
||||
// If requested_digits equals -1, then the shortest exponential representation
|
||||
// is computed.
|
||||
//
|
||||
// Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
|
||||
// exponent_character set to 'e'.
|
||||
// ToExponential(3.12, 1) -> "3.1e0"
|
||||
// ToExponential(5.0, 3) -> "5.000e0"
|
||||
// ToExponential(0.001, 2) -> "1.00e-3"
|
||||
// ToExponential(3.1415, -1) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 4) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 3) -> "3.142e0"
|
||||
// ToExponential(123456789000000, 3) -> "1.235e14"
|
||||
// ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
|
||||
// ToExponential(1000000000000000019884624838656.0, 32) ->
|
||||
// "1.00000000000000001988462483865600e30"
|
||||
// ToExponential(1234, 0) -> "1e3"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'requested_digits' > kMaxExponentialDigits.
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxExponentialDigits + 8 characters (the sign, the digit before the
|
||||
// decimal point, the decimal point, the exponent character, the
|
||||
// exponent's sign, and at most 3 exponent digits).
|
||||
bool ToExponential(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes 'precision' leading digits of the given 'value' and returns them
|
||||
// either in exponential or decimal format, depending on
|
||||
// max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
|
||||
// constructor).
|
||||
// The last computed digit is rounded.
|
||||
//
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT:
|
||||
// ToPrecision(123450.0, 6) -> "123450"
|
||||
// ToPrecision(123450.0, 5) -> "123450"
|
||||
// ToPrecision(123450.0, 4) -> "123500"
|
||||
// ToPrecision(123450.0, 3) -> "123000"
|
||||
// ToPrecision(123450.0, 2) -> "1.2e5"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - precision < kMinPericisionDigits
|
||||
// - precision > kMaxPrecisionDigits
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
|
||||
// exponent character, the exponent's sign, and at most 3 exponent digits).
|
||||
bool ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const;
|
||||
#endif // not needed for ICU
|
||||
|
||||
enum DtoaMode {
|
||||
// Produce the shortest correct representation.
|
||||
// For example the output of 0.299999999999999988897 is (the less accurate
|
||||
// but correct) 0.3.
|
||||
SHORTEST,
|
||||
// Same as SHORTEST, but for single-precision floats.
|
||||
SHORTEST_SINGLE,
|
||||
// Produce a fixed number of digits after the decimal point.
|
||||
// For instance fixed(0.1, 4) becomes 0.1000
|
||||
// If the input number is big, the output will be big.
|
||||
FIXED,
|
||||
// Fixed number of digits (independent of the decimal point).
|
||||
PRECISION
|
||||
};
|
||||
|
||||
// The maximal number of digits that are needed to emit a double in base 10.
|
||||
// A higher precision can be achieved by using more digits, but the shortest
|
||||
// accurate representation of any double will never use more digits than
|
||||
// kBase10MaximalLength.
|
||||
// Note that DoubleToAscii null-terminates its input. So the given buffer
|
||||
// should be at least kBase10MaximalLength + 1 characters long.
|
||||
static const int kBase10MaximalLength = 17;
|
||||
|
||||
// Converts the given double 'v' to digit characters. 'v' must not be NaN,
|
||||
// +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
|
||||
// applies to 'v' after it has been casted to a single-precision float. That
|
||||
// is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
|
||||
// -Infinity.
|
||||
//
|
||||
// The result should be interpreted as buffer * 10^(point-length).
|
||||
//
|
||||
// The digits are written to the buffer in the platform's charset, which is
|
||||
// often UTF-8 (with ASCII-range digits) but may be another charset, such
|
||||
// as EBCDIC.
|
||||
//
|
||||
// The output depends on the given mode:
|
||||
// - SHORTEST: produce the least amount of digits for which the internal
|
||||
// identity requirement is still satisfied. If the digits are printed
|
||||
// (together with the correct exponent) then reading this number will give
|
||||
// 'v' again. The buffer will choose the representation that is closest to
|
||||
// 'v'. If there are two at the same distance, than the one farther away
|
||||
// from 0 is chosen (halfway cases - ending with 5 - are rounded up).
|
||||
// In this mode the 'requested_digits' parameter is ignored.
|
||||
// - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
|
||||
// - FIXED: produces digits necessary to print a given number with
|
||||
// 'requested_digits' digits after the decimal point. The produced digits
|
||||
// might be too short in which case the caller has to fill the remainder
|
||||
// with '0's.
|
||||
// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
|
||||
// Halfway cases are rounded towards +/-Infinity (away from 0). The call
|
||||
// toFixed(0.15, 2) thus returns buffer="2", point=0.
|
||||
// The returned buffer may contain digits that would be truncated from the
|
||||
// shortest representation of the input.
|
||||
// - PRECISION: produces 'requested_digits' where the first digit is not '0'.
|
||||
// Even though the length of produced digits usually equals
|
||||
// 'requested_digits', the function is allowed to return fewer digits, in
|
||||
// which case the caller has to fill the missing digits with '0's.
|
||||
// Halfway cases are again rounded away from 0.
|
||||
// DoubleToAscii expects the given buffer to be big enough to hold all
|
||||
// digits and a terminating null-character. In SHORTEST-mode it expects a
|
||||
// buffer of at least kBase10MaximalLength + 1. In all other modes the
|
||||
// requested_digits parameter and the padding-zeroes limit the size of the
|
||||
// output. Don't forget the decimal point, the exponent character and the
|
||||
// terminating null-character when computing the maximal output size.
|
||||
// The given length is only used in debug mode to ensure the buffer is big
|
||||
// enough.
|
||||
// ICU PATCH: Export this as U_I18N_API for unit tests.
|
||||
static void U_I18N_API DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point);
|
||||
|
||||
#if 0 // not needed for ICU
|
||||
private:
|
||||
// Implementation for ToShortest and ToShortestSingle.
|
||||
bool ToShortestIeeeNumber(double value,
|
||||
StringBuilder* result_builder,
|
||||
DtoaMode mode) const;
|
||||
|
||||
// If the value is a special value (NaN or Infinity) constructs the
|
||||
// corresponding string using the configured infinity/nan-symbol.
|
||||
// If either of them is NULL or the value is not special then the
|
||||
// function returns false.
|
||||
bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
|
||||
// Constructs an exponential representation (i.e. 1.234e56).
|
||||
// The given exponent assumes a decimal point after the first decimal digit.
|
||||
void CreateExponentialRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const;
|
||||
// Creates a decimal representation (i.e 1234.5678).
|
||||
void CreateDecimalRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
const int flags_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const char exponent_character_;
|
||||
const int decimal_in_shortest_low_;
|
||||
const int decimal_in_shortest_high_;
|
||||
const int max_leading_padding_zeroes_in_precision_mode_;
|
||||
const int max_trailing_padding_zeroes_in_precision_mode_;
|
||||
const int min_exponent_width_;
|
||||
#endif // not needed for ICU
|
||||
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
U_NAMESPACE_END
|
||||
|
||||
#endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
||||
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
|
@ -152,7 +152,7 @@ static bool RoundWeed(Vector<char> buffer,
|
||||
// Conceptually rest ~= too_high - buffer
|
||||
// We need to do the following tests in this order to avoid over- and
|
||||
// underflows.
|
||||
ASSERT(rest <= unsafe_interval);
|
||||
DOUBLE_CONVERSION_ASSERT(rest <= unsafe_interval);
|
||||
while (rest < small_distance && // Negated condition 1
|
||||
unsafe_interval - rest >= ten_kappa && // Negated condition 2
|
||||
(rest + ten_kappa < small_distance || // buffer{-1} > w_high
|
||||
@ -198,7 +198,7 @@ static bool RoundWeedCounted(Vector<char> buffer,
|
||||
uint64_t ten_kappa,
|
||||
uint64_t unit,
|
||||
int* kappa) {
|
||||
ASSERT(rest < ten_kappa);
|
||||
DOUBLE_CONVERSION_ASSERT(rest < ten_kappa);
|
||||
// The following tests are done in a specific order to avoid overflows. They
|
||||
// will work correctly with any uint64 values of rest < ten_kappa and unit.
|
||||
//
|
||||
@ -255,7 +255,7 @@ static void BiggestPowerTen(uint32_t number,
|
||||
int number_bits,
|
||||
uint32_t* power,
|
||||
int* exponent_plus_one) {
|
||||
ASSERT(number < (1u << (number_bits + 1)));
|
||||
DOUBLE_CONVERSION_ASSERT(number < (1u << (number_bits + 1)));
|
||||
// 1233/4096 is approximately 1/lg(10).
|
||||
int exponent_plus_one_guess = ((number_bits + 1) * 1233 >> 12);
|
||||
// We increment to skip over the first entry in the kPowersOf10 table.
|
||||
@ -317,9 +317,9 @@ static bool DigitGen(DiyFp low,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* kappa) {
|
||||
ASSERT(low.e() == w.e() && w.e() == high.e());
|
||||
ASSERT(low.f() + 1 <= high.f() - 1);
|
||||
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(low.e() == w.e() && w.e() == high.e());
|
||||
DOUBLE_CONVERSION_ASSERT(low.f() + 1 <= high.f() - 1);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
// low, w and high are imprecise, but by less than one ulp (unit in the last
|
||||
// place).
|
||||
// If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
|
||||
@ -361,7 +361,7 @@ static bool DigitGen(DiyFp low,
|
||||
// that is smaller than integrals.
|
||||
while (*kappa > 0) {
|
||||
int digit = integrals / divisor;
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
integrals %= divisor;
|
||||
@ -388,16 +388,16 @@ static bool DigitGen(DiyFp low,
|
||||
// data (like the interval or 'unit'), too.
|
||||
// Note that the multiplication by 10 does not overflow, because w.e >= -60
|
||||
// and thus one.e >= -60.
|
||||
ASSERT(one.e() >= -60);
|
||||
ASSERT(fractionals < one.f());
|
||||
ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(one.e() >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals < one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
for (;;) {
|
||||
fractionals *= 10;
|
||||
unit *= 10;
|
||||
unsafe_interval.set_f(unsafe_interval.f() * 10);
|
||||
// Integer division by one.
|
||||
int digit = static_cast<int>(fractionals >> -one.e());
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
fractionals &= one.f() - 1; // Modulo by one.
|
||||
@ -444,9 +444,9 @@ static bool DigitGenCounted(DiyFp w,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* kappa) {
|
||||
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
ASSERT(kMinimalTargetExponent >= -60);
|
||||
ASSERT(kMaximalTargetExponent <= -32);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(kMaximalTargetExponent <= -32);
|
||||
// w is assumed to have an error less than 1 unit. Whenever w is scaled we
|
||||
// also scale its error.
|
||||
uint64_t w_error = 1;
|
||||
@ -472,7 +472,7 @@ static bool DigitGenCounted(DiyFp w,
|
||||
// that is smaller than 'integrals'.
|
||||
while (*kappa > 0) {
|
||||
int digit = integrals / divisor;
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
requested_digits--;
|
||||
@ -498,15 +498,15 @@ static bool DigitGenCounted(DiyFp w,
|
||||
// data (the 'unit'), too.
|
||||
// Note that the multiplication by 10 does not overflow, because w.e >= -60
|
||||
// and thus one.e >= -60.
|
||||
ASSERT(one.e() >= -60);
|
||||
ASSERT(fractionals < one.f());
|
||||
ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(one.e() >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals < one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
while (requested_digits > 0 && fractionals > w_error) {
|
||||
fractionals *= 10;
|
||||
w_error *= 10;
|
||||
// Integer division by one.
|
||||
int digit = static_cast<int>(fractionals >> -one.e());
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
requested_digits--;
|
||||
@ -544,11 +544,11 @@ static bool Grisu3(double v,
|
||||
if (mode == FAST_DTOA_SHORTEST) {
|
||||
Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
} else {
|
||||
ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
|
||||
DOUBLE_CONVERSION_ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
|
||||
float single_v = static_cast<float>(v);
|
||||
Single(single_v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
}
|
||||
ASSERT(boundary_plus.e() == w.e());
|
||||
DOUBLE_CONVERSION_ASSERT(boundary_plus.e() == w.e());
|
||||
DiyFp ten_mk; // Cached power of ten: 10^-k
|
||||
int mk; // -k
|
||||
int ten_mk_minimal_binary_exponent =
|
||||
@ -559,7 +559,7 @@ static bool Grisu3(double v,
|
||||
ten_mk_minimal_binary_exponent,
|
||||
ten_mk_maximal_binary_exponent,
|
||||
&ten_mk, &mk);
|
||||
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DOUBLE_CONVERSION_ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize) &&
|
||||
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize));
|
||||
@ -573,7 +573,7 @@ static bool Grisu3(double v,
|
||||
// In other words: let f = scaled_w.f() and e = scaled_w.e(), then
|
||||
// (f-1) * 2^e < w*10^k < (f+1) * 2^e
|
||||
DiyFp scaled_w = DiyFp::Times(w, ten_mk);
|
||||
ASSERT(scaled_w.e() ==
|
||||
DOUBLE_CONVERSION_ASSERT(scaled_w.e() ==
|
||||
boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
|
||||
// In theory it would be possible to avoid some recomputations by computing
|
||||
// the difference between w and boundary_minus/plus (a power of 2) and to
|
||||
@ -618,7 +618,7 @@ static bool Grisu3Counted(double v,
|
||||
ten_mk_minimal_binary_exponent,
|
||||
ten_mk_maximal_binary_exponent,
|
||||
&ten_mk, &mk);
|
||||
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DOUBLE_CONVERSION_ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize) &&
|
||||
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize));
|
||||
@ -652,8 +652,8 @@ bool FastDtoa(double v,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* decimal_point) {
|
||||
ASSERT(v > 0);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(v > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
|
||||
bool result = false;
|
||||
int decimal_exponent = 0;
|
||||
@ -667,7 +667,7 @@ bool FastDtoa(double v,
|
||||
buffer, length, &decimal_exponent);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
if (result) {
|
||||
*decimal_point = *length + decimal_exponent;
|
||||
|
@ -55,12 +55,14 @@ static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
|
||||
// Helper functions for doubles.
|
||||
class Double {
|
||||
public:
|
||||
static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
|
||||
static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
|
||||
static const int kSignificandSize = 53;
|
||||
static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
|
||||
static const int kMaxExponent = 0x7FF - kExponentBias;
|
||||
|
||||
Double() : d64_(0) {}
|
||||
explicit Double(double d) : d64_(double_to_uint64(d)) {}
|
||||
@ -71,14 +73,14 @@ class Double {
|
||||
// The value encoded by this Double must be greater or equal to +0.0.
|
||||
// It must not be special (infinity, or NaN).
|
||||
DiyFp AsDiyFp() const {
|
||||
ASSERT(Sign() > 0);
|
||||
ASSERT(!IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!IsSpecial());
|
||||
return DiyFp(Significand(), Exponent());
|
||||
}
|
||||
|
||||
// The value encoded by this Double must be strictly greater than 0.
|
||||
DiyFp AsNormalizedDiyFp() const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
uint64_t f = Significand();
|
||||
int e = Exponent();
|
||||
|
||||
@ -174,7 +176,7 @@ class Double {
|
||||
// Precondition: the value encoded by this Double must be greater or equal
|
||||
// than +0.0.
|
||||
DiyFp UpperBoundary() const {
|
||||
ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
return DiyFp(Significand() * 2 + 1, Exponent() - 1);
|
||||
}
|
||||
|
||||
@ -183,7 +185,7 @@ class Double {
|
||||
// exponent as m_plus.
|
||||
// Precondition: the value encoded by this Double must be greater than 0.
|
||||
void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
DiyFp v = this->AsDiyFp();
|
||||
DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
|
||||
DiyFp m_minus;
|
||||
@ -236,11 +238,9 @@ class Double {
|
||||
}
|
||||
|
||||
private:
|
||||
static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
|
||||
static const int kDenormalExponent = -kExponentBias + 1;
|
||||
static const int kMaxExponent = 0x7FF - kExponentBias;
|
||||
static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
|
||||
static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
|
||||
|
||||
const uint64_t d64_;
|
||||
|
||||
@ -271,7 +271,7 @@ class Double {
|
||||
(biased_exponent << kPhysicalSignificandSize);
|
||||
}
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Double);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
|
||||
};
|
||||
|
||||
class Single {
|
||||
@ -290,8 +290,8 @@ class Single {
|
||||
// The value encoded by this Single must be greater or equal to +0.0.
|
||||
// It must not be special (infinity, or NaN).
|
||||
DiyFp AsDiyFp() const {
|
||||
ASSERT(Sign() > 0);
|
||||
ASSERT(!IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!IsSpecial());
|
||||
return DiyFp(Significand(), Exponent());
|
||||
}
|
||||
|
||||
@ -354,7 +354,7 @@ class Single {
|
||||
// exponent as m_plus.
|
||||
// Precondition: the value encoded by this Single must be greater than 0.
|
||||
void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
DiyFp v = this->AsDiyFp();
|
||||
DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
|
||||
DiyFp m_minus;
|
||||
@ -372,7 +372,7 @@ class Single {
|
||||
// Precondition: the value encoded by this Single must be greater or equal
|
||||
// than +0.0.
|
||||
DiyFp UpperBoundary() const {
|
||||
ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
return DiyFp(Significand() * 2 + 1, Exponent() - 1);
|
||||
}
|
||||
|
||||
@ -408,7 +408,7 @@ class Single {
|
||||
|
||||
const uint32_t d32_;
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Single);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -37,16 +37,13 @@
|
||||
// ICU PATCH: Do not include std::locale.
|
||||
|
||||
#include <climits>
|
||||
//#include <locale>
|
||||
// #include <locale>
|
||||
#include <cmath>
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
// The file fixed-dtoa.h is not needed.
|
||||
|
||||
#include "double-conversion.h"
|
||||
#include "double-conversion-string-to-double.h"
|
||||
|
||||
#include "double-conversion-bignum-dtoa.h"
|
||||
#include "double-conversion-fast-dtoa.h"
|
||||
#include "double-conversion-ieee.h"
|
||||
#include "double-conversion-strtod.h"
|
||||
#include "double-conversion-utils.h"
|
||||
@ -56,385 +53,6 @@ U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
#if 0 // not needed for ICU
|
||||
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
|
||||
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
|
||||
static DoubleToStringConverter converter(flags,
|
||||
"Infinity",
|
||||
"NaN",
|
||||
'e',
|
||||
-6, 21,
|
||||
6, 0);
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::HandleSpecialValues(
|
||||
double value,
|
||||
StringBuilder* result_builder) const {
|
||||
Double double_inspect(value);
|
||||
if (double_inspect.IsInfinite()) {
|
||||
if (infinity_symbol_ == NULL) return false;
|
||||
if (value < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
result_builder->AddString(infinity_symbol_);
|
||||
return true;
|
||||
}
|
||||
if (double_inspect.IsNan()) {
|
||||
if (nan_symbol_ == NULL) return false;
|
||||
result_builder->AddString(nan_symbol_);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateExponentialRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const {
|
||||
ASSERT(length != 0);
|
||||
result_builder->AddCharacter(decimal_digits[0]);
|
||||
if (length != 1) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddSubstring(&decimal_digits[1], length-1);
|
||||
}
|
||||
result_builder->AddCharacter(exponent_character_);
|
||||
if (exponent < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
exponent = -exponent;
|
||||
} else {
|
||||
if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
|
||||
result_builder->AddCharacter('+');
|
||||
}
|
||||
}
|
||||
if (exponent == 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
return;
|
||||
}
|
||||
ASSERT(exponent < 1e4);
|
||||
const int kMaxExponentLength = 5;
|
||||
char buffer[kMaxExponentLength + 1];
|
||||
buffer[kMaxExponentLength] = '\0';
|
||||
int first_char_pos = kMaxExponentLength;
|
||||
while (exponent > 0) {
|
||||
buffer[--first_char_pos] = '0' + (exponent % 10);
|
||||
exponent /= 10;
|
||||
}
|
||||
result_builder->AddSubstring(&buffer[first_char_pos],
|
||||
kMaxExponentLength - first_char_pos);
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateDecimalRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const {
|
||||
// Create a representation that is padded with zeros if needed.
|
||||
if (decimal_point <= 0) {
|
||||
// "0.00000decimal_rep" or "0.000decimal_rep00".
|
||||
result_builder->AddCharacter('0');
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', -decimal_point);
|
||||
ASSERT(length <= digits_after_point - (-decimal_point));
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
int remaining_digits = digits_after_point - (-decimal_point) - length;
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
} else if (decimal_point >= length) {
|
||||
// "decimal_rep0000.00000" or "decimal_rep.0000".
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
result_builder->AddPadding('0', decimal_point - length);
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', digits_after_point);
|
||||
}
|
||||
} else {
|
||||
// "decima.l_rep000".
|
||||
ASSERT(digits_after_point > 0);
|
||||
result_builder->AddSubstring(decimal_digits, decimal_point);
|
||||
result_builder->AddCharacter('.');
|
||||
ASSERT(length - decimal_point <= digits_after_point);
|
||||
result_builder->AddSubstring(&decimal_digits[decimal_point],
|
||||
length - decimal_point);
|
||||
int remaining_digits = digits_after_point - (length - decimal_point);
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
if (digits_after_point == 0) {
|
||||
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
}
|
||||
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToShortestIeeeNumber(
|
||||
double value,
|
||||
StringBuilder* result_builder,
|
||||
DoubleToStringConverter::DtoaMode mode) const {
|
||||
ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE);
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
const int kDecimalRepCapacity = kBase10MaximalLength + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
if ((decimal_in_shortest_low_ <= exponent) &&
|
||||
(exponent < decimal_in_shortest_high_)) {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
|
||||
decimal_point,
|
||||
Max(0, decimal_rep_length - decimal_point),
|
||||
result_builder);
|
||||
} else {
|
||||
CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
ASSERT(kMaxFixedDigitsBeforePoint == 60);
|
||||
const double kFirstNonFixed = 1e60;
|
||||
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
|
||||
if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for the '\0' byte.
|
||||
const int kDecimalRepCapacity =
|
||||
kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
DoubleToAscii(value, FIXED, requested_digits,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
requested_digits, result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToExponential(
|
||||
double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits < -1) return false;
|
||||
if (requested_digits > kMaxExponentialDigits) return false;
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for digit before the decimal point and the '\0' character.
|
||||
const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
|
||||
ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
if (requested_digits == -1) {
|
||||
DoubleToAscii(value, SHORTEST, 0,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
} else {
|
||||
DoubleToAscii(value, PRECISION, requested_digits + 1,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
ASSERT(decimal_rep_length <= requested_digits + 1);
|
||||
|
||||
for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
decimal_rep_length = requested_digits + 1;
|
||||
}
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
decimal_rep_length,
|
||||
exponent,
|
||||
result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add one for the terminating null character.
|
||||
const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, PRECISION, precision,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
ASSERT(decimal_rep_length <= precision);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
// The exponent if we print the number as x.xxeyyy. That is with the
|
||||
// decimal point after the first digit.
|
||||
int exponent = decimal_point - 1;
|
||||
|
||||
int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
|
||||
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
|
||||
(decimal_point - precision + extra_zero >
|
||||
max_trailing_padding_zeroes_in_precision_mode_)) {
|
||||
// Fill buffer to contain 'precision' digits.
|
||||
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
|
||||
// is allowed to return less characters.
|
||||
for (int i = decimal_rep_length; i < precision; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
precision,
|
||||
exponent,
|
||||
result_builder);
|
||||
} else {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
Max(0, precision - decimal_point),
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // not needed for ICU
|
||||
|
||||
|
||||
static BignumDtoaMode DtoaToBignumDtoaMode(
|
||||
DoubleToStringConverter::DtoaMode dtoa_mode) {
|
||||
switch (dtoa_mode) {
|
||||
case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
|
||||
case DoubleToStringConverter::SHORTEST_SINGLE:
|
||||
return BIGNUM_DTOA_SHORTEST_SINGLE;
|
||||
case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
|
||||
case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point) {
|
||||
Vector<char> vector(buffer, buffer_length);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
|
||||
|
||||
if (Double(v).Sign() < 0) {
|
||||
*sign = true;
|
||||
v = -v;
|
||||
} else {
|
||||
*sign = false;
|
||||
}
|
||||
|
||||
if (mode == PRECISION && requested_digits == 0) {
|
||||
vector[0] = '\0';
|
||||
*length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (v == 0) {
|
||||
vector[0] = '0';
|
||||
vector[1] = '\0';
|
||||
*length = 1;
|
||||
*point = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
bool fast_worked;
|
||||
switch (mode) {
|
||||
case SHORTEST:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
|
||||
break;
|
||||
#if 0 // not needed for ICU
|
||||
case SHORTEST_SINGLE:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
|
||||
vector, length, point);
|
||||
break;
|
||||
case FIXED:
|
||||
fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
|
||||
break;
|
||||
case PRECISION:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
||||
vector, length, point);
|
||||
break;
|
||||
#endif // not needed for ICU
|
||||
default:
|
||||
fast_worked = false;
|
||||
UNREACHABLE();
|
||||
}
|
||||
if (fast_worked) return;
|
||||
|
||||
// If the fast dtoa didn't succeed use the slower bignum version.
|
||||
BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
|
||||
BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
|
||||
vector[*length] = '\0';
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
inline char ToLower(char ch) {
|
||||
@ -444,7 +62,7 @@ inline char ToLower(char ch) {
|
||||
return cType.tolower(ch);
|
||||
#else
|
||||
(void)ch;
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -457,7 +75,7 @@ static inline bool ConsumeSubStringImpl(Iterator* current,
|
||||
Iterator end,
|
||||
const char* substring,
|
||||
Converter converter) {
|
||||
ASSERT(converter(**current) == *substring);
|
||||
DOUBLE_CONVERSION_ASSERT(converter(**current) == *substring);
|
||||
for (substring++; *substring != '\0'; substring++) {
|
||||
++*current;
|
||||
if (*current == end || converter(**current) != *substring) {
|
||||
@ -474,8 +92,8 @@ template <class Iterator>
|
||||
static bool ConsumeSubString(Iterator* current,
|
||||
Iterator end,
|
||||
const char* substring,
|
||||
bool allow_case_insensibility) {
|
||||
if (allow_case_insensibility) {
|
||||
bool allow_case_insensitivity) {
|
||||
if (allow_case_insensitivity) {
|
||||
return ConsumeSubStringImpl(current, end, substring, ToLower);
|
||||
} else {
|
||||
return ConsumeSubStringImpl(current, end, substring, Pass);
|
||||
@ -485,8 +103,8 @@ static bool ConsumeSubString(Iterator* current,
|
||||
// Consumes first character of the str is equal to ch
|
||||
inline bool ConsumeFirstCharacter(char ch,
|
||||
const char* str,
|
||||
bool case_insensibility) {
|
||||
return case_insensibility ? ToLower(ch) == str[0] : ch == str[0];
|
||||
bool case_insensitivity) {
|
||||
return case_insensitivity ? ToLower(ch) == str[0] : ch == str[0];
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@ -501,15 +119,14 @@ const int kMaxSignificantDigits = 772;
|
||||
|
||||
|
||||
static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 };
|
||||
static const int kWhitespaceTable7Length = ARRAY_SIZE(kWhitespaceTable7);
|
||||
static const int kWhitespaceTable7Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable7);
|
||||
|
||||
|
||||
static const uc16 kWhitespaceTable16[] = {
|
||||
160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195,
|
||||
8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279
|
||||
};
|
||||
static const int kWhitespaceTable16Length = ARRAY_SIZE(kWhitespaceTable16);
|
||||
|
||||
static const int kWhitespaceTable16Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable16);
|
||||
|
||||
|
||||
static bool isWhitespace(int x) {
|
||||
@ -609,7 +226,7 @@ static bool IsHexFloatString(Iterator start,
|
||||
Iterator end,
|
||||
uc16 separator,
|
||||
bool allow_trailing_junk) {
|
||||
ASSERT(start != end);
|
||||
DOUBLE_CONVERSION_ASSERT(start != end);
|
||||
|
||||
Iterator current = start;
|
||||
|
||||
@ -624,8 +241,8 @@ static bool IsHexFloatString(Iterator start,
|
||||
saw_digit = true;
|
||||
if (Advance(¤t, separator, 16, end)) return false;
|
||||
}
|
||||
if (!saw_digit) return false; // Only the '.', but no digits.
|
||||
}
|
||||
if (!saw_digit) return false;
|
||||
if (*current != 'p' && *current != 'P') return false;
|
||||
if (Advance(¤t, separator, 16, end)) return false;
|
||||
if (*current == '+' || *current == '-') {
|
||||
@ -654,8 +271,8 @@ static double RadixStringToIeee(Iterator* current,
|
||||
double junk_string_value,
|
||||
bool read_as_double,
|
||||
bool* result_is_junk) {
|
||||
ASSERT(*current != end);
|
||||
ASSERT(!parse_as_hex_float ||
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(!parse_as_hex_float ||
|
||||
IsHexFloatString(*current, end, separator, allow_trailing_junk));
|
||||
|
||||
const int kDoubleSize = Double::kSignificandSize;
|
||||
@ -693,7 +310,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
} else if (parse_as_hex_float && **current == '.') {
|
||||
post_decimal = true;
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
continue;
|
||||
} else if (parse_as_hex_float && (**current == 'p' || **current == 'P')) {
|
||||
break;
|
||||
@ -728,7 +345,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
// Just run over the '.'. We are just trying to see whether there is
|
||||
// a non-zero digit somewhere.
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
post_decimal = true;
|
||||
}
|
||||
if (!isDigit(**current, radix)) break;
|
||||
@ -763,27 +380,31 @@ static double RadixStringToIeee(Iterator* current,
|
||||
if (Advance(current, separator, radix, end)) break;
|
||||
}
|
||||
|
||||
ASSERT(number < ((int64_t)1 << kSignificandSize));
|
||||
ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
|
||||
DOUBLE_CONVERSION_ASSERT(number < ((int64_t)1 << kSignificandSize));
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
|
||||
|
||||
*result_is_junk = false;
|
||||
|
||||
if (parse_as_hex_float) {
|
||||
ASSERT(**current == 'p' || **current == 'P');
|
||||
DOUBLE_CONVERSION_ASSERT(**current == 'p' || **current == 'P');
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
bool is_negative = false;
|
||||
if (**current == '+') {
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
} else if (**current == '-') {
|
||||
is_negative = true;
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
}
|
||||
int written_exponent = 0;
|
||||
while (IsDecimalDigitForRadix(**current, 10)) {
|
||||
written_exponent = 10 * written_exponent + **current - '0';
|
||||
// No need to read exponents if they are too big. That could potentially overflow
|
||||
// the `written_exponent` variable.
|
||||
if (abs(written_exponent) <= 100 * Double::kMaxExponent) {
|
||||
written_exponent = 10 * written_exponent + **current - '0';
|
||||
}
|
||||
if (Advance(current, separator, radix, end)) break;
|
||||
}
|
||||
if (is_negative) written_exponent = -written_exponent;
|
||||
@ -798,7 +419,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
return static_cast<double>(number);
|
||||
}
|
||||
|
||||
ASSERT(number != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(number != 0);
|
||||
double result = Double(DiyFp(number, exponent)).value();
|
||||
return sign ? -result : result;
|
||||
}
|
||||
@ -818,7 +439,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0;
|
||||
const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0;
|
||||
const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0;
|
||||
const bool allow_case_insensibility = (flags_ & ALLOW_CASE_INSENSIBILITY) != 0;
|
||||
const bool allow_case_insensitivity = (flags_ & ALLOW_CASE_INSENSITIVITY) != 0;
|
||||
|
||||
// To make sure that iterator dereferencing is valid the following
|
||||
// convention is used:
|
||||
@ -868,8 +489,8 @@ double StringToDoubleConverter::StringToIeee(
|
||||
}
|
||||
|
||||
if (infinity_symbol_ != NULL) {
|
||||
if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensibility)) {
|
||||
if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensibility)) {
|
||||
if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensitivity)) {
|
||||
if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensitivity)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
@ -880,15 +501,15 @@ double StringToDoubleConverter::StringToIeee(
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return sign ? -Double::Infinity() : Double::Infinity();
|
||||
}
|
||||
}
|
||||
|
||||
if (nan_symbol_ != NULL) {
|
||||
if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensibility)) {
|
||||
if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensibility)) {
|
||||
if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensitivity)) {
|
||||
if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensitivity)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
@ -899,7 +520,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return sign ? -Double::NaN() : Double::NaN();
|
||||
}
|
||||
@ -919,10 +540,11 @@ double StringToDoubleConverter::StringToIeee(
|
||||
(*current == 'x' || *current == 'X')) {
|
||||
++current;
|
||||
|
||||
if (current == end) return junk_string_value_; // "0x"
|
||||
|
||||
bool parse_as_hex_float = (flags_ & ALLOW_HEX_FLOATS) &&
|
||||
IsHexFloatString(current, end, separator_, allow_trailing_junk);
|
||||
|
||||
if (current == end) return junk_string_value_; // "0x"
|
||||
if (!parse_as_hex_float && !isDigit(*current, 16)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
@ -958,7 +580,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
// Copy significant digits of the integer part (if any) to the buffer.
|
||||
while (*current >= '0' && *current <= '9') {
|
||||
if (significant_digits < kMaxSignificantDigits) {
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos++] = static_cast<char>(*current);
|
||||
significant_digits++;
|
||||
// Will later check if it's an octal in the buffer.
|
||||
@ -1003,7 +625,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
// We don't emit a '.', but adjust the exponent instead.
|
||||
while (*current >= '0' && *current <= '9') {
|
||||
if (significant_digits < kMaxSignificantDigits) {
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos++] = static_cast<char>(*current);
|
||||
significant_digits++;
|
||||
exponent--;
|
||||
@ -1061,7 +683,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
}
|
||||
|
||||
const int max_exponent = INT_MAX / 2;
|
||||
ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
|
||||
DOUBLE_CONVERSION_ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
|
||||
int num = 0;
|
||||
do {
|
||||
// Check overflow.
|
||||
@ -1104,7 +726,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
junk_string_value_,
|
||||
read_as_double,
|
||||
&result_is_junk);
|
||||
ASSERT(!result_is_junk);
|
||||
DOUBLE_CONVERSION_ASSERT(!result_is_junk);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return result;
|
||||
}
|
||||
@ -1114,7 +736,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
exponent--;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos] = '\0';
|
||||
|
||||
double converted;
|
244
icu4c/source/i18n/double-conversion-string-to-double.h
Normal file
244
icu4c/source/i18n/double-conversion-string-to-double.h
Normal file
@ -0,0 +1,244 @@
|
||||
// © 2018 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
//
|
||||
// From the double-conversion library. Original license:
|
||||
//
|
||||
// Copyright 2012 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
||||
#define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
|
||||
#include "double-conversion-utils.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class StringToDoubleConverter {
|
||||
public:
|
||||
// Enumeration for allowing octals and ignoring junk when converting
|
||||
// strings to numbers.
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
ALLOW_HEX = 1,
|
||||
ALLOW_OCTALS = 2,
|
||||
ALLOW_TRAILING_JUNK = 4,
|
||||
ALLOW_LEADING_SPACES = 8,
|
||||
ALLOW_TRAILING_SPACES = 16,
|
||||
ALLOW_SPACES_AFTER_SIGN = 32,
|
||||
ALLOW_CASE_INSENSITIVITY = 64,
|
||||
ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
|
||||
ALLOW_HEX_FLOATS = 128,
|
||||
};
|
||||
|
||||
static const uc16 kNoSeparator = '\0';
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
|
||||
// Ex: StringToDouble("0x1234") -> 4660.0
|
||||
// In StringToDouble("0x1234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
|
||||
// the string will not be parsed as "0" followed by junk.
|
||||
//
|
||||
// - ALLOW_OCTALS: recognizes the prefix "0" for octals:
|
||||
// If a sequence of octal digits starts with '0', then the number is
|
||||
// read as octal integer. Octal numbers may only be integers.
|
||||
// Ex: StringToDouble("01234") -> 668.0
|
||||
// StringToDouble("012349") -> 12349.0 // Not a sequence of octal
|
||||
// // digits.
|
||||
// In StringToDouble("01234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// In StringToDouble("01234e56") the characters "e56" are trailing
|
||||
// junk, too.
|
||||
// - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
|
||||
// a double literal.
|
||||
// - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
|
||||
// new-lines, and tabs.
|
||||
// - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
|
||||
// - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
|
||||
// Ex: StringToDouble("- 123.2") -> -123.2.
|
||||
// StringToDouble("+ 123.2") -> 123.2
|
||||
// - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values:
|
||||
// infinity and nan.
|
||||
// - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
|
||||
// This *must* start with "0x" and separate the exponent with "p".
|
||||
// Examples: 0x1.2p3 == 9.0
|
||||
// 0x10.1p0 == 16.0625
|
||||
// ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
|
||||
//
|
||||
// empty_string_value is returned when an empty string is given as input.
|
||||
// If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
|
||||
// containing only spaces is converted to the 'empty_string_value', too.
|
||||
//
|
||||
// junk_string_value is returned when
|
||||
// a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
|
||||
// part of a double-literal) is found.
|
||||
// b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
|
||||
// double literal.
|
||||
//
|
||||
// infinity_symbol and nan_symbol are strings that are used to detect
|
||||
// inputs that represent infinity and NaN. They can be null, in which case
|
||||
// they are ignored.
|
||||
// The conversion routine first reads any possible signs. Then it compares the
|
||||
// following character of the input-string with the first character of
|
||||
// the infinity, and nan-symbol. If either matches, the function assumes, that
|
||||
// a match has been found, and expects the following input characters to match
|
||||
// the remaining characters of the special-value symbol.
|
||||
// This means that the following restrictions apply to special-value symbols:
|
||||
// - they must not start with signs ('+', or '-'),
|
||||
// - they must not have the same first character.
|
||||
// - they must not start with digits.
|
||||
//
|
||||
// If the separator character is not kNoSeparator, then that specific
|
||||
// character is ignored when in between two valid digits of the significant.
|
||||
// It is not allowed to appear in the exponent.
|
||||
// It is not allowed to lead or trail the number.
|
||||
// It is not allowed to appear twice next to each other.
|
||||
//
|
||||
// Examples:
|
||||
// flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = "infinity",
|
||||
// nan_symbol = "nan":
|
||||
// StringToDouble("0x1234") -> 4660.0.
|
||||
// StringToDouble("0x1234K") -> 4660.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> NaN // junk_string_value.
|
||||
// StringToDouble(" 1") -> NaN // junk_string_value.
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("-123.45") -> -123.45.
|
||||
// StringToDouble("--123.45") -> NaN // junk_string_value.
|
||||
// StringToDouble("123e45") -> 123e45.
|
||||
// StringToDouble("123E45") -> 123e45.
|
||||
// StringToDouble("123e+45") -> 123e45.
|
||||
// StringToDouble("123E-45") -> 123e-45.
|
||||
// StringToDouble("123e") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("123e-") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("+NaN") -> NaN // NaN string literal.
|
||||
// StringToDouble("-infinity") -> -inf. // infinity literal.
|
||||
// StringToDouble("Infinity") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = NULL,
|
||||
// nan_symbol = NULL:
|
||||
// StringToDouble("0x1234") -> NaN // junk_string_value.
|
||||
// StringToDouble("01234") -> 668.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" 1") -> 1.0
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("0123e45") -> NaN // junk_string_value.
|
||||
// StringToDouble("01239E45") -> 1239e45.
|
||||
// StringToDouble("-infinity") -> NaN // junk_string_value.
|
||||
// StringToDouble("NaN") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = NO_FLAGS,
|
||||
// separator = ' ':
|
||||
// StringToDouble("1 2 3 4") -> 1234.0
|
||||
// StringToDouble("1 2") -> NaN // junk_string_value
|
||||
// StringToDouble("1 000 000.0") -> 1000000.0
|
||||
// StringToDouble("1.000 000") -> 1.0
|
||||
// StringToDouble("1.0e1 000") -> NaN // junk_string_value
|
||||
StringToDoubleConverter(int flags,
|
||||
double empty_string_value,
|
||||
double junk_string_value,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
uc16 separator = kNoSeparator)
|
||||
: flags_(flags),
|
||||
empty_string_value_(empty_string_value),
|
||||
junk_string_value_(junk_string_value),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
separator_(separator) {
|
||||
}
|
||||
|
||||
// Performs the conversion.
|
||||
// The output parameter 'processed_characters_count' is set to the number
|
||||
// of characters that have been processed to read the number.
|
||||
// Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
|
||||
// in the 'processed_characters_count'. Trailing junk is never included.
|
||||
double StringToDouble(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble above but for 16 bit characters.
|
||||
double StringToDouble(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble but reads a float.
|
||||
// Note that this is not equivalent to static_cast<float>(StringToDouble(...))
|
||||
// due to potential double-rounding.
|
||||
float StringToFloat(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToFloat above but for 16 bit characters.
|
||||
float StringToFloat(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
private:
|
||||
const int flags_;
|
||||
const double empty_string_value_;
|
||||
const double junk_string_value_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const uc16 separator_;
|
||||
|
||||
template <class Iterator>
|
||||
double StringToIeee(Iterator start_pointer,
|
||||
int length,
|
||||
bool read_as_double,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
U_NAMESPACE_END
|
||||
|
||||
#endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
||||
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
|
@ -34,16 +34,15 @@
|
||||
#include "unicode/utypes.h"
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
#include <climits>
|
||||
#include <cstdarg>
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
// The file fixed-dtoa.h is not needed.
|
||||
|
||||
#include "double-conversion-strtod.h"
|
||||
#include "double-conversion-bignum.h"
|
||||
#include "double-conversion-cached-powers.h"
|
||||
#include "double-conversion-ieee.h"
|
||||
#include "double-conversion-strtod.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
@ -67,7 +66,7 @@ static const int kMaxDecimalPower = 309;
|
||||
static const int kMinDecimalPower = -324;
|
||||
|
||||
// 2^64 = 18446744073709551616
|
||||
static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
static const uint64_t kMaxUint64 = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
|
||||
|
||||
static const double exact_powers_of_ten[] = {
|
||||
@ -96,7 +95,7 @@ static const double exact_powers_of_ten[] = {
|
||||
// 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
|
||||
10000000000000000000000.0
|
||||
};
|
||||
static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
|
||||
static const int kExactPowersOfTenSize = DOUBLE_CONVERSION_ARRAY_SIZE(exact_powers_of_ten);
|
||||
|
||||
// Maximum number of significant digits in the decimal representation.
|
||||
// In fact the value is 772 (see conversions.cc), but to give us some margin
|
||||
@ -132,7 +131,7 @@ static void CutToMaxSignificantDigits(Vector<const char> buffer,
|
||||
}
|
||||
// The input buffer has been trimmed. Therefore the last digit must be
|
||||
// different from '0'.
|
||||
ASSERT(buffer[buffer.length() - 1] != '0');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[buffer.length() - 1] != '0');
|
||||
// Set the last digit to be non-zero. This is sufficient to guarantee
|
||||
// correct rounding.
|
||||
significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
|
||||
@ -153,7 +152,7 @@ static void TrimAndCut(Vector<const char> buffer, int exponent,
|
||||
exponent += left_trimmed.length() - right_trimmed.length();
|
||||
if (right_trimmed.length() > kMaxSignificantDecimalDigits) {
|
||||
(void) space_size; // Mark variable as used.
|
||||
ASSERT(space_size >= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(space_size >= kMaxSignificantDecimalDigits);
|
||||
CutToMaxSignificantDigits(right_trimmed, exponent,
|
||||
buffer_copy_space, updated_exponent);
|
||||
*trimmed = Vector<const char>(buffer_copy_space,
|
||||
@ -176,7 +175,7 @@ static uint64_t ReadUint64(Vector<const char> buffer,
|
||||
int i = 0;
|
||||
while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
|
||||
int digit = buffer[i++] - '0';
|
||||
ASSERT(0 <= digit && digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
|
||||
result = 10 * result + digit;
|
||||
}
|
||||
*number_of_read_digits = i;
|
||||
@ -220,7 +219,7 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
// Note that the ARM simulator is compiled for 32bits. It therefore exhibits
|
||||
// the same problem.
|
||||
return false;
|
||||
#endif
|
||||
#else
|
||||
if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
|
||||
int read_digits;
|
||||
// The trimmed input fits into a double.
|
||||
@ -232,14 +231,14 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
|
||||
// 10^-exponent fits into a double.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result /= exact_powers_of_ten[-exponent];
|
||||
return true;
|
||||
}
|
||||
if (0 <= exponent && exponent < kExactPowersOfTenSize) {
|
||||
// 10^exponent fits into a double.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result *= exact_powers_of_ten[exponent];
|
||||
return true;
|
||||
}
|
||||
@ -251,34 +250,35 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
// 10^remaining_digits. As a result the remaining exponent now fits
|
||||
// into a double too.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result *= exact_powers_of_ten[remaining_digits];
|
||||
*result *= exact_powers_of_ten[exponent - remaining_digits];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// Returns 10^exponent as an exact DiyFp.
|
||||
// The given exponent must be in the range [1; kDecimalExponentDistance[.
|
||||
static DiyFp AdjustmentPowerOfTen(int exponent) {
|
||||
ASSERT(0 < exponent);
|
||||
ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
|
||||
DOUBLE_CONVERSION_ASSERT(0 < exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
|
||||
// Simply hardcode the remaining powers for the given decimal exponent
|
||||
// distance.
|
||||
ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
|
||||
DOUBLE_CONVERSION_ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
|
||||
switch (exponent) {
|
||||
case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60);
|
||||
case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57);
|
||||
case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54);
|
||||
case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50);
|
||||
case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47);
|
||||
case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44);
|
||||
case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40);
|
||||
case 1: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xa0000000, 00000000), -60);
|
||||
case 2: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc8000000, 00000000), -57);
|
||||
case 3: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xfa000000, 00000000), -54);
|
||||
case 4: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50);
|
||||
case 5: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc3500000, 00000000), -47);
|
||||
case 6: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xf4240000, 00000000), -44);
|
||||
case 7: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x98968000, 00000000), -40);
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,7 +307,7 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
input.Normalize();
|
||||
error <<= old_e - input.e();
|
||||
|
||||
ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
|
||||
if (exponent < PowersOfTenCache::kMinDecimalExponent) {
|
||||
*result = 0.0;
|
||||
return true;
|
||||
@ -325,7 +325,7 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
|
||||
// The product of input with the adjustment power fits into a 64 bit
|
||||
// integer.
|
||||
ASSERT(DiyFp::kSignificandSize == 64);
|
||||
DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64);
|
||||
} else {
|
||||
// The adjustment power is exact. There is hence only an error of 0.5.
|
||||
error += kDenominator / 2;
|
||||
@ -367,8 +367,8 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
precision_digits_count -= shift_amount;
|
||||
}
|
||||
// We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
|
||||
ASSERT(DiyFp::kSignificandSize == 64);
|
||||
ASSERT(precision_digits_count < 64);
|
||||
DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64);
|
||||
DOUBLE_CONVERSION_ASSERT(precision_digits_count < 64);
|
||||
uint64_t one64 = 1;
|
||||
uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
|
||||
uint64_t precision_bits = input.f() & precision_bits_mask;
|
||||
@ -407,14 +407,14 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
static int CompareBufferWithDiyFp(Vector<const char> buffer,
|
||||
int exponent,
|
||||
DiyFp diy_fp) {
|
||||
ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
|
||||
ASSERT(buffer.length() + exponent > kMinDecimalPower);
|
||||
ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent > kMinDecimalPower);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
|
||||
// Make sure that the Bignum will be able to hold all our numbers.
|
||||
// Our Bignum implementation has a separate field for exponents. Shifts will
|
||||
// consume at most one bigit (< 64 bits).
|
||||
// ln(10) == 3.3219...
|
||||
ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
|
||||
DOUBLE_CONVERSION_ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
|
||||
Bignum buffer_bignum;
|
||||
Bignum diy_fp_bignum;
|
||||
buffer_bignum.AssignDecimalString(buffer);
|
||||
@ -460,18 +460,33 @@ static bool ComputeGuess(Vector<const char> trimmed, int exponent,
|
||||
return false;
|
||||
}
|
||||
|
||||
double Strtod(Vector<const char> buffer, int exponent) {
|
||||
char copy_buffer[kMaxSignificantDecimalDigits];
|
||||
Vector<const char> trimmed;
|
||||
int updated_exponent;
|
||||
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
||||
&trimmed, &updated_exponent);
|
||||
exponent = updated_exponent;
|
||||
#if U_DEBUG // needed for ICU only in debug mode
|
||||
static bool IsDigit(const char d) {
|
||||
return ('0' <= d) && (d <= '9');
|
||||
}
|
||||
|
||||
static bool IsNonZeroDigit(const char d) {
|
||||
return ('1' <= d) && (d <= '9');
|
||||
}
|
||||
|
||||
static bool AssertTrimmedDigits(const Vector<const char>& buffer) {
|
||||
for(int i = 0; i < buffer.length(); ++i) {
|
||||
if(!IsDigit(buffer[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (buffer.length() == 0) || (IsNonZeroDigit(buffer[0]) && IsNonZeroDigit(buffer[buffer.length()-1]));
|
||||
}
|
||||
#endif // needed for ICU only in debug mode
|
||||
|
||||
double StrtodTrimmed(Vector<const char> trimmed, int exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(trimmed.length() <= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(AssertTrimmedDigits(trimmed));
|
||||
double guess;
|
||||
bool is_correct = ComputeGuess(trimmed, exponent, &guess);
|
||||
if (is_correct) return guess;
|
||||
|
||||
const bool is_correct = ComputeGuess(trimmed, exponent, &guess);
|
||||
if (is_correct) {
|
||||
return guess;
|
||||
}
|
||||
DiyFp upper_boundary = Double(guess).UpperBoundary();
|
||||
int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
|
||||
if (comparison < 0) {
|
||||
@ -486,6 +501,39 @@ double Strtod(Vector<const char> buffer, int exponent) {
|
||||
}
|
||||
}
|
||||
|
||||
double Strtod(Vector<const char> buffer, int exponent) {
|
||||
char copy_buffer[kMaxSignificantDecimalDigits];
|
||||
Vector<const char> trimmed;
|
||||
int updated_exponent;
|
||||
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
||||
&trimmed, &updated_exponent);
|
||||
return StrtodTrimmed(trimmed, updated_exponent);
|
||||
}
|
||||
|
||||
static float SanitizedDoubletof(double d) {
|
||||
DOUBLE_CONVERSION_ASSERT(d >= 0.0);
|
||||
// ASAN has a sanitize check that disallows casting doubles to floats if
|
||||
// they are too big.
|
||||
// https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks
|
||||
// The behavior should be covered by IEEE 754, but some projects use this
|
||||
// flag, so work around it.
|
||||
float max_finite = 3.4028234663852885981170418348451692544e+38;
|
||||
// The half-way point between the max-finite and infinity value.
|
||||
// Since infinity has an even significand everything equal or greater than
|
||||
// this value should become infinity.
|
||||
double half_max_finite_infinity =
|
||||
3.40282356779733661637539395458142568448e+38;
|
||||
if (d >= max_finite) {
|
||||
if (d >= half_max_finite_infinity) {
|
||||
return Single::Infinity();
|
||||
} else {
|
||||
return max_finite;
|
||||
}
|
||||
} else {
|
||||
return static_cast<float>(d);
|
||||
}
|
||||
}
|
||||
|
||||
float Strtof(Vector<const char> buffer, int exponent) {
|
||||
char copy_buffer[kMaxSignificantDecimalDigits];
|
||||
Vector<const char> trimmed;
|
||||
@ -497,7 +545,7 @@ float Strtof(Vector<const char> buffer, int exponent) {
|
||||
double double_guess;
|
||||
bool is_correct = ComputeGuess(trimmed, exponent, &double_guess);
|
||||
|
||||
float float_guess = static_cast<float>(double_guess);
|
||||
float float_guess = SanitizedDoubletof(double_guess);
|
||||
if (float_guess == double_guess) {
|
||||
// This shortcut triggers for integer values.
|
||||
return float_guess;
|
||||
@ -520,18 +568,18 @@ float Strtof(Vector<const char> buffer, int exponent) {
|
||||
double double_next = Double(double_guess).NextDouble();
|
||||
double double_previous = Double(double_guess).PreviousDouble();
|
||||
|
||||
float f1 = static_cast<float>(double_previous);
|
||||
float f1 = SanitizedDoubletof(double_previous);
|
||||
float f2 = float_guess;
|
||||
float f3 = static_cast<float>(double_next);
|
||||
float f3 = SanitizedDoubletof(double_next);
|
||||
float f4;
|
||||
if (is_correct) {
|
||||
f4 = f3;
|
||||
} else {
|
||||
double double_next2 = Double(double_next).NextDouble();
|
||||
f4 = static_cast<float>(double_next2);
|
||||
f4 = SanitizedDoubletof(double_next2);
|
||||
}
|
||||
(void) f2; // Mark variable as used.
|
||||
ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
|
||||
DOUBLE_CONVERSION_ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
|
||||
|
||||
// If the guess doesn't lie near a single-precision boundary we can simply
|
||||
// return its float-value.
|
||||
@ -539,11 +587,11 @@ float Strtof(Vector<const char> buffer, int exponent) {
|
||||
return float_guess;
|
||||
}
|
||||
|
||||
ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
|
||||
DOUBLE_CONVERSION_ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
|
||||
(f1 == f2 && f2 != f3 && f3 == f4) ||
|
||||
(f1 == f2 && f2 == f3 && f3 != f4));
|
||||
|
||||
// guess and next are the two possible canditates (in the same way that
|
||||
// guess and next are the two possible candidates (in the same way that
|
||||
// double_guess was the lower candidate for a double-precision guess).
|
||||
float guess = f1;
|
||||
float next = f4;
|
||||
|
@ -54,6 +54,11 @@ double Strtod(Vector<const char> buffer, int exponent);
|
||||
// contain a dot or a sign. It must not start with '0', and must not be empty.
|
||||
float Strtof(Vector<const char> buffer, int exponent);
|
||||
|
||||
// For special use cases, the heart of the Strtod() function is also available
|
||||
// separately, it assumes that 'trimmed' is as produced by TrimAndCut(), i.e.
|
||||
// no leading or trailing zeros, also no lone zero, and not 'too many' digits.
|
||||
double StrtodTrimmed(Vector<const char> trimmed, int exponent);
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
|
@ -42,10 +42,12 @@
|
||||
|
||||
// ICU PATCH: Use U_ASSERT instead of <assert.h>
|
||||
#include "uassert.h"
|
||||
#define ASSERT U_ASSERT
|
||||
|
||||
#ifndef UNIMPLEMENTED
|
||||
#define UNIMPLEMENTED() (abort())
|
||||
#ifndef DOUBLE_CONVERSION_ASSERT
|
||||
#define DOUBLE_CONVERSION_ASSERT(condition) \
|
||||
U_ASSERT(condition);
|
||||
#endif
|
||||
#ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
|
||||
#define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
|
||||
#endif
|
||||
#ifndef DOUBLE_CONVERSION_NO_RETURN
|
||||
#ifdef _MSC_VER
|
||||
@ -54,16 +56,23 @@
|
||||
#define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
|
||||
#endif
|
||||
#endif
|
||||
#ifndef UNREACHABLE
|
||||
#ifndef DOUBLE_CONVERSION_UNREACHABLE
|
||||
#ifdef _MSC_VER
|
||||
void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
|
||||
inline void abort_noreturn() { abort(); }
|
||||
#define UNREACHABLE() (abort_noreturn())
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() (abort_noreturn())
|
||||
#else
|
||||
#define UNREACHABLE() (abort())
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() (abort())
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_UNUSED
|
||||
#ifdef __GNUC__
|
||||
#define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
|
||||
#else
|
||||
#define DOUBLE_CONVERSION_UNUSED
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Double operations detection based on target architecture.
|
||||
// Linux uses a 80bit wide floating point stack on x86. This induces double
|
||||
@ -99,9 +108,9 @@ int main(int argc, char** argv) {
|
||||
defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
|
||||
defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
|
||||
defined(__SH4__) || defined(__alpha__) || \
|
||||
defined(_MIPS_ARCH_MIPS32R2) || \
|
||||
defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
|
||||
defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
|
||||
defined(__riscv) || \
|
||||
defined(__riscv) || defined(__e2k__) || \
|
||||
defined(__or1k__) || defined(__arc__) || \
|
||||
defined(__EMSCRIPTEN__)
|
||||
#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
|
||||
@ -141,24 +150,24 @@ typedef uint16_t uc16;
|
||||
|
||||
// The following macro works on both 32 and 64-bit platforms.
|
||||
// Usage: instead of writing 0x1234567890123456
|
||||
// write UINT64_2PART_C(0x12345678,90123456);
|
||||
#define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
// write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
|
||||
#define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
|
||||
|
||||
// The expression ARRAY_SIZE(a) is a compile-time constant of type
|
||||
// The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
|
||||
// size_t which represents the number of elements of the given
|
||||
// array. You should only use ARRAY_SIZE on statically allocated
|
||||
// array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
|
||||
// arrays.
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(a) \
|
||||
#ifndef DOUBLE_CONVERSION_ARRAY_SIZE
|
||||
#define DOUBLE_CONVERSION_ARRAY_SIZE(a) \
|
||||
((sizeof(a) / sizeof(*(a))) / \
|
||||
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
|
||||
#endif
|
||||
|
||||
// A macro to disallow the evil copy constructor and operator= functions
|
||||
// This should be used in the private: declarations for a class
|
||||
#ifndef DC_DISALLOW_COPY_AND_ASSIGN
|
||||
#define DC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
#ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
|
||||
#define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
#endif
|
||||
@ -169,10 +178,10 @@ typedef uint16_t uc16;
|
||||
// This should be used in the private: declarations for a class
|
||||
// that wants to prevent anyone from instantiating it. This is
|
||||
// especially useful for classes containing only static methods.
|
||||
#ifndef DC_DISALLOW_IMPLICIT_CONSTRUCTORS
|
||||
#define DC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
||||
#ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
|
||||
#define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
||||
TypeName(); \
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
#endif
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
@ -180,25 +189,9 @@ U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
static const int kCharSize = sizeof(char);
|
||||
|
||||
// Returns the maximum of the two parameters.
|
||||
template <typename T>
|
||||
static T Max(T a, T b) {
|
||||
return a < b ? b : a;
|
||||
}
|
||||
|
||||
|
||||
// Returns the minimum of the two parameters.
|
||||
template <typename T>
|
||||
static T Min(T a, T b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
|
||||
inline int StrLength(const char* string) {
|
||||
size_t length = strlen(string);
|
||||
ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
|
||||
DOUBLE_CONVERSION_ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
|
||||
return static_cast<int>(length);
|
||||
}
|
||||
|
||||
@ -208,15 +201,15 @@ class Vector {
|
||||
public:
|
||||
Vector() : start_(NULL), length_(0) {}
|
||||
Vector(T* data, int len) : start_(data), length_(len) {
|
||||
ASSERT(len == 0 || (len > 0 && data != NULL));
|
||||
DOUBLE_CONVERSION_ASSERT(len == 0 || (len > 0 && data != NULL));
|
||||
}
|
||||
|
||||
// Returns a vector using the same backing storage as this one,
|
||||
// spanning from and including 'from', to but not including 'to'.
|
||||
Vector<T> SubVector(int from, int to) {
|
||||
ASSERT(to <= length_);
|
||||
ASSERT(from < to);
|
||||
ASSERT(0 <= from);
|
||||
DOUBLE_CONVERSION_ASSERT(to <= length_);
|
||||
DOUBLE_CONVERSION_ASSERT(from < to);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= from);
|
||||
return Vector<T>(start() + from, to - from);
|
||||
}
|
||||
|
||||
@ -231,7 +224,7 @@ class Vector {
|
||||
|
||||
// Access individual vector elements - checks bounds in debug mode.
|
||||
T& operator[](int index) const {
|
||||
ASSERT(0 <= index && index < length_);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= index && index < length_);
|
||||
return start_[index];
|
||||
}
|
||||
|
||||
@ -239,6 +232,11 @@ class Vector {
|
||||
|
||||
T& last() { return start_[length_ - 1]; }
|
||||
|
||||
void pop_back() {
|
||||
DOUBLE_CONVERSION_ASSERT(!is_empty());
|
||||
--length_;
|
||||
}
|
||||
|
||||
private:
|
||||
T* start_;
|
||||
int length_;
|
||||
@ -259,7 +257,7 @@ class StringBuilder {
|
||||
|
||||
// Get the current position in the builder.
|
||||
int position() const {
|
||||
ASSERT(!is_finalized());
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized());
|
||||
return position_;
|
||||
}
|
||||
|
||||
@ -270,8 +268,8 @@ class StringBuilder {
|
||||
// 0-characters; use the Finalize() method to terminate the string
|
||||
// instead.
|
||||
void AddCharacter(char c) {
|
||||
ASSERT(c != '\0');
|
||||
ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(c != '\0');
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
buffer_[position_++] = c;
|
||||
}
|
||||
|
||||
@ -284,9 +282,9 @@ class StringBuilder {
|
||||
// Add the first 'n' characters of the given string 's' to the
|
||||
// builder. The input string must have enough characters.
|
||||
void AddSubstring(const char* s, int n) {
|
||||
ASSERT(!is_finalized() && position_ + n < buffer_.length());
|
||||
ASSERT(static_cast<size_t>(n) <= strlen(s));
|
||||
memmove(&buffer_[position_], s, n * kCharSize);
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ + n < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n) <= strlen(s));
|
||||
memmove(&buffer_[position_], s, n);
|
||||
position_ += n;
|
||||
}
|
||||
|
||||
@ -301,13 +299,13 @@ class StringBuilder {
|
||||
|
||||
// Finalize the string by 0-terminating it and returning the buffer.
|
||||
char* Finalize() {
|
||||
ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
buffer_[position_] = '\0';
|
||||
// Make sure nobody managed to add a 0-character to the
|
||||
// buffer while building the string.
|
||||
ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
|
||||
DOUBLE_CONVERSION_ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
|
||||
position_ = -1;
|
||||
ASSERT(is_finalized());
|
||||
DOUBLE_CONVERSION_ASSERT(is_finalized());
|
||||
return buffer_.start();
|
||||
}
|
||||
|
||||
@ -317,7 +315,7 @@ class StringBuilder {
|
||||
|
||||
bool is_finalized() const { return position_ < 0; }
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
|
||||
};
|
||||
|
||||
// The type-based aliasing rule allows the compiler to assume that pointers of
|
||||
@ -345,13 +343,14 @@ class StringBuilder {
|
||||
// enough that it can no longer see that you have cast one pointer type to
|
||||
// another thus avoiding the warning.
|
||||
template <class Dest, class Source>
|
||||
inline Dest BitCast(const Source& source) {
|
||||
Dest BitCast(const Source& source) {
|
||||
// Compile time assertion: sizeof(Dest) == sizeof(Source)
|
||||
// A compile error here means your Dest and Source have different sizes.
|
||||
#if __cplusplus >= 201103L
|
||||
static_assert(sizeof(Dest) == sizeof(Source),
|
||||
"source and destination size mismatch");
|
||||
#else
|
||||
DOUBLE_CONVERSION_UNUSED
|
||||
typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
|
||||
#endif
|
||||
|
||||
@ -361,7 +360,7 @@ inline Dest BitCast(const Source& source) {
|
||||
}
|
||||
|
||||
template <class Dest, class Source>
|
||||
inline Dest BitCast(Source* source) {
|
||||
Dest BitCast(Source* source) {
|
||||
return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
|
||||
}
|
||||
|
||||
|
@ -39,561 +39,8 @@
|
||||
|
||||
// ICU PATCH: Customize header file paths for ICU.
|
||||
|
||||
#include "double-conversion-utils.h"
|
||||
|
||||
// ICU PATCH: Wrap in ICU namespace
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class DoubleToStringConverter {
|
||||
public:
|
||||
#if 0 // not needed for ICU
|
||||
// When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
|
||||
// or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
|
||||
// function returns false.
|
||||
static const int kMaxFixedDigitsBeforePoint = 60;
|
||||
static const int kMaxFixedDigitsAfterPoint = 60;
|
||||
|
||||
// When calling ToExponential with a requested_digits
|
||||
// parameter > kMaxExponentialDigits then the function returns false.
|
||||
static const int kMaxExponentialDigits = 120;
|
||||
|
||||
// When calling ToPrecision with a requested_digits
|
||||
// parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
|
||||
// then the function returns false.
|
||||
static const int kMinPrecisionDigits = 1;
|
||||
static const int kMaxPrecisionDigits = 120;
|
||||
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
EMIT_POSITIVE_EXPONENT_SIGN = 1,
|
||||
EMIT_TRAILING_DECIMAL_POINT = 2,
|
||||
EMIT_TRAILING_ZERO_AFTER_POINT = 4,
|
||||
UNIQUE_ZERO = 8
|
||||
};
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
|
||||
// form, emits a '+' for positive exponents. Example: 1.2e+2.
|
||||
// - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
|
||||
// converted into decimal format then a trailing decimal point is appended.
|
||||
// Example: 2345.0 is converted to "2345.".
|
||||
// - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
|
||||
// emits a trailing '0'-character. This flag requires the
|
||||
// EXMIT_TRAILING_DECIMAL_POINT flag.
|
||||
// Example: 2345.0 is converted to "2345.0".
|
||||
// - UNIQUE_ZERO: "-0.0" is converted to "0.0".
|
||||
//
|
||||
// Infinity symbol and nan_symbol provide the string representation for these
|
||||
// special values. If the string is NULL and the special value is encountered
|
||||
// then the conversion functions return false.
|
||||
//
|
||||
// The exponent_character is used in exponential representations. It is
|
||||
// usually 'e' or 'E'.
|
||||
//
|
||||
// When converting to the shortest representation the converter will
|
||||
// represent input numbers in decimal format if they are in the interval
|
||||
// [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
|
||||
// (lower boundary included, greater boundary excluded).
|
||||
// Example: with decimal_in_shortest_low = -6 and
|
||||
// decimal_in_shortest_high = 21:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// When converting to precision mode the converter may add
|
||||
// max_leading_padding_zeroes before returning the number in exponential
|
||||
// format.
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
DoubleToStringConverter(int flags,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
char exponent_character,
|
||||
int decimal_in_shortest_low,
|
||||
int decimal_in_shortest_high,
|
||||
int max_leading_padding_zeroes_in_precision_mode,
|
||||
int max_trailing_padding_zeroes_in_precision_mode)
|
||||
: flags_(flags),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
exponent_character_(exponent_character),
|
||||
decimal_in_shortest_low_(decimal_in_shortest_low),
|
||||
decimal_in_shortest_high_(decimal_in_shortest_high),
|
||||
max_leading_padding_zeroes_in_precision_mode_(
|
||||
max_leading_padding_zeroes_in_precision_mode),
|
||||
max_trailing_padding_zeroes_in_precision_mode_(
|
||||
max_trailing_padding_zeroes_in_precision_mode) {
|
||||
// When 'trailing zero after the point' is set, then 'trailing point'
|
||||
// must be set too.
|
||||
ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
|
||||
!((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
|
||||
}
|
||||
|
||||
// Returns a converter following the EcmaScript specification.
|
||||
static const DoubleToStringConverter& EcmaScriptConverter();
|
||||
|
||||
// Computes the shortest string of digits that correctly represent the input
|
||||
// number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
|
||||
// (see constructor) it then either returns a decimal representation, or an
|
||||
// exponential representation.
|
||||
// Example with decimal_in_shortest_low = -6,
|
||||
// decimal_in_shortest_high = 21,
|
||||
// EMIT_POSITIVE_EXPONENT_SIGN activated, and
|
||||
// EMIT_TRAILING_DECIMAL_POINT deactived:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// Note: the conversion may round the output if the returned string
|
||||
// is accurate enough to uniquely identify the input-number.
|
||||
// For example the most precise representation of the double 9e59 equals
|
||||
// "899999999999999918767229449717619953810131273674690656206848", but
|
||||
// the converter will return the shorter (but still correct) "9e59".
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except when the input value is special and no infinity_symbol or
|
||||
// nan_symbol has been given to the constructor.
|
||||
bool ToShortest(double value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
|
||||
}
|
||||
|
||||
// Same as ToShortest, but for single-precision floats.
|
||||
bool ToShortestSingle(float value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
|
||||
}
|
||||
|
||||
|
||||
// Computes a decimal representation with a fixed number of digits after the
|
||||
// decimal point. The last emitted digit is rounded.
|
||||
//
|
||||
// Examples:
|
||||
// ToFixed(3.12, 1) -> "3.1"
|
||||
// ToFixed(3.1415, 3) -> "3.142"
|
||||
// ToFixed(1234.56789, 4) -> "1234.5679"
|
||||
// ToFixed(1.23, 5) -> "1.23000"
|
||||
// ToFixed(0.1, 4) -> "0.1000"
|
||||
// ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
|
||||
// ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
|
||||
// ToFixed(0.1, 17) -> "0.10000000000000001"
|
||||
//
|
||||
// If requested_digits equals 0, then the tail of the result depends on
|
||||
// the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples, for requested_digits == 0,
|
||||
// let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
|
||||
// - false and false: then 123.45 -> 123
|
||||
// 0.678 -> 1
|
||||
// - true and false: then 123.45 -> 123.
|
||||
// 0.678 -> 1.
|
||||
// - true and true: then 123.45 -> 123.0
|
||||
// 0.678 -> 1.0
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'value' > 10^kMaxFixedDigitsBeforePoint, or
|
||||
// - 'requested_digits' > kMaxFixedDigitsAfterPoint.
|
||||
// The last two conditions imply that the result will never contain more than
|
||||
// 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
|
||||
// (one additional character for the sign, and one for the decimal point).
|
||||
bool ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes a representation in exponential format with requested_digits
|
||||
// after the decimal point. The last emitted digit is rounded.
|
||||
// If requested_digits equals -1, then the shortest exponential representation
|
||||
// is computed.
|
||||
//
|
||||
// Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
|
||||
// exponent_character set to 'e'.
|
||||
// ToExponential(3.12, 1) -> "3.1e0"
|
||||
// ToExponential(5.0, 3) -> "5.000e0"
|
||||
// ToExponential(0.001, 2) -> "1.00e-3"
|
||||
// ToExponential(3.1415, -1) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 4) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 3) -> "3.142e0"
|
||||
// ToExponential(123456789000000, 3) -> "1.235e14"
|
||||
// ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
|
||||
// ToExponential(1000000000000000019884624838656.0, 32) ->
|
||||
// "1.00000000000000001988462483865600e30"
|
||||
// ToExponential(1234, 0) -> "1e3"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'requested_digits' > kMaxExponentialDigits.
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxExponentialDigits + 8 characters (the sign, the digit before the
|
||||
// decimal point, the decimal point, the exponent character, the
|
||||
// exponent's sign, and at most 3 exponent digits).
|
||||
bool ToExponential(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes 'precision' leading digits of the given 'value' and returns them
|
||||
// either in exponential or decimal format, depending on
|
||||
// max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
|
||||
// constructor).
|
||||
// The last computed digit is rounded.
|
||||
//
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT:
|
||||
// ToPrecision(123450.0, 6) -> "123450"
|
||||
// ToPrecision(123450.0, 5) -> "123450"
|
||||
// ToPrecision(123450.0, 4) -> "123500"
|
||||
// ToPrecision(123450.0, 3) -> "123000"
|
||||
// ToPrecision(123450.0, 2) -> "1.2e5"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - precision < kMinPericisionDigits
|
||||
// - precision > kMaxPrecisionDigits
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
|
||||
// exponent character, the exponent's sign, and at most 3 exponent digits).
|
||||
bool ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const;
|
||||
#endif // not needed for ICU
|
||||
|
||||
enum DtoaMode {
|
||||
// Produce the shortest correct representation.
|
||||
// For example the output of 0.299999999999999988897 is (the less accurate
|
||||
// but correct) 0.3.
|
||||
SHORTEST,
|
||||
// Same as SHORTEST, but for single-precision floats.
|
||||
SHORTEST_SINGLE,
|
||||
// Produce a fixed number of digits after the decimal point.
|
||||
// For instance fixed(0.1, 4) becomes 0.1000
|
||||
// If the input number is big, the output will be big.
|
||||
FIXED,
|
||||
// Fixed number of digits (independent of the decimal point).
|
||||
PRECISION
|
||||
};
|
||||
|
||||
// The maximal number of digits that are needed to emit a double in base 10.
|
||||
// A higher precision can be achieved by using more digits, but the shortest
|
||||
// accurate representation of any double will never use more digits than
|
||||
// kBase10MaximalLength.
|
||||
// Note that DoubleToAscii null-terminates its input. So the given buffer
|
||||
// should be at least kBase10MaximalLength + 1 characters long.
|
||||
static const int kBase10MaximalLength = 17;
|
||||
|
||||
// Converts the given double 'v' to digit characters. 'v' must not be NaN,
|
||||
// +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
|
||||
// applies to 'v' after it has been casted to a single-precision float. That
|
||||
// is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
|
||||
// -Infinity.
|
||||
//
|
||||
// The result should be interpreted as buffer * 10^(point-length).
|
||||
//
|
||||
// The digits are written to the buffer in the platform's charset, which is
|
||||
// often UTF-8 (with ASCII-range digits) but may be another charset, such
|
||||
// as EBCDIC.
|
||||
//
|
||||
// The output depends on the given mode:
|
||||
// - SHORTEST: produce the least amount of digits for which the internal
|
||||
// identity requirement is still satisfied. If the digits are printed
|
||||
// (together with the correct exponent) then reading this number will give
|
||||
// 'v' again. The buffer will choose the representation that is closest to
|
||||
// 'v'. If there are two at the same distance, than the one farther away
|
||||
// from 0 is chosen (halfway cases - ending with 5 - are rounded up).
|
||||
// In this mode the 'requested_digits' parameter is ignored.
|
||||
// - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
|
||||
// - FIXED: produces digits necessary to print a given number with
|
||||
// 'requested_digits' digits after the decimal point. The produced digits
|
||||
// might be too short in which case the caller has to fill the remainder
|
||||
// with '0's.
|
||||
// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
|
||||
// Halfway cases are rounded towards +/-Infinity (away from 0). The call
|
||||
// toFixed(0.15, 2) thus returns buffer="2", point=0.
|
||||
// The returned buffer may contain digits that would be truncated from the
|
||||
// shortest representation of the input.
|
||||
// - PRECISION: produces 'requested_digits' where the first digit is not '0'.
|
||||
// Even though the length of produced digits usually equals
|
||||
// 'requested_digits', the function is allowed to return fewer digits, in
|
||||
// which case the caller has to fill the missing digits with '0's.
|
||||
// Halfway cases are again rounded away from 0.
|
||||
// DoubleToAscii expects the given buffer to be big enough to hold all
|
||||
// digits and a terminating null-character. In SHORTEST-mode it expects a
|
||||
// buffer of at least kBase10MaximalLength + 1. In all other modes the
|
||||
// requested_digits parameter and the padding-zeroes limit the size of the
|
||||
// output. Don't forget the decimal point, the exponent character and the
|
||||
// terminating null-character when computing the maximal output size.
|
||||
// The given length is only used in debug mode to ensure the buffer is big
|
||||
// enough.
|
||||
// ICU PATCH: Export this as U_I18N_API for unit tests.
|
||||
static void U_I18N_API DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point);
|
||||
|
||||
#if 0 // not needed for ICU
|
||||
private:
|
||||
// Implementation for ToShortest and ToShortestSingle.
|
||||
bool ToShortestIeeeNumber(double value,
|
||||
StringBuilder* result_builder,
|
||||
DtoaMode mode) const;
|
||||
|
||||
// If the value is a special value (NaN or Infinity) constructs the
|
||||
// corresponding string using the configured infinity/nan-symbol.
|
||||
// If either of them is NULL or the value is not special then the
|
||||
// function returns false.
|
||||
bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
|
||||
// Constructs an exponential representation (i.e. 1.234e56).
|
||||
// The given exponent assumes a decimal point after the first decimal digit.
|
||||
void CreateExponentialRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const;
|
||||
// Creates a decimal representation (i.e 1234.5678).
|
||||
void CreateDecimalRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
const int flags_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const char exponent_character_;
|
||||
const int decimal_in_shortest_low_;
|
||||
const int decimal_in_shortest_high_;
|
||||
const int max_leading_padding_zeroes_in_precision_mode_;
|
||||
const int max_trailing_padding_zeroes_in_precision_mode_;
|
||||
#endif // not needed for ICU
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
|
||||
};
|
||||
|
||||
|
||||
class StringToDoubleConverter {
|
||||
public:
|
||||
// Enumeration for allowing octals and ignoring junk when converting
|
||||
// strings to numbers.
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
ALLOW_HEX = 1,
|
||||
ALLOW_OCTALS = 2,
|
||||
ALLOW_TRAILING_JUNK = 4,
|
||||
ALLOW_LEADING_SPACES = 8,
|
||||
ALLOW_TRAILING_SPACES = 16,
|
||||
ALLOW_SPACES_AFTER_SIGN = 32,
|
||||
ALLOW_CASE_INSENSIBILITY = 64,
|
||||
ALLOW_HEX_FLOATS = 128,
|
||||
};
|
||||
|
||||
static const uc16 kNoSeparator = '\0';
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
|
||||
// Ex: StringToDouble("0x1234") -> 4660.0
|
||||
// In StringToDouble("0x1234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
|
||||
// the string will not be parsed as "0" followed by junk.
|
||||
//
|
||||
// - ALLOW_OCTALS: recognizes the prefix "0" for octals:
|
||||
// If a sequence of octal digits starts with '0', then the number is
|
||||
// read as octal integer. Octal numbers may only be integers.
|
||||
// Ex: StringToDouble("01234") -> 668.0
|
||||
// StringToDouble("012349") -> 12349.0 // Not a sequence of octal
|
||||
// // digits.
|
||||
// In StringToDouble("01234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// In StringToDouble("01234e56") the characters "e56" are trailing
|
||||
// junk, too.
|
||||
// - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
|
||||
// a double literal.
|
||||
// - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
|
||||
// new-lines, and tabs.
|
||||
// - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
|
||||
// - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
|
||||
// Ex: StringToDouble("- 123.2") -> -123.2.
|
||||
// StringToDouble("+ 123.2") -> 123.2
|
||||
// - ALLOW_CASE_INSENSIBILITY: ignore case of characters for special values:
|
||||
// infinity and nan.
|
||||
// - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
|
||||
// This *must* start with "0x" and separate the exponent with "p".
|
||||
// Examples: 0x1.2p3 == 9.0
|
||||
// 0x10.1p0 == 16.0625
|
||||
// ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
|
||||
//
|
||||
// empty_string_value is returned when an empty string is given as input.
|
||||
// If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
|
||||
// containing only spaces is converted to the 'empty_string_value', too.
|
||||
//
|
||||
// junk_string_value is returned when
|
||||
// a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
|
||||
// part of a double-literal) is found.
|
||||
// b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
|
||||
// double literal.
|
||||
//
|
||||
// infinity_symbol and nan_symbol are strings that are used to detect
|
||||
// inputs that represent infinity and NaN. They can be null, in which case
|
||||
// they are ignored.
|
||||
// The conversion routine first reads any possible signs. Then it compares the
|
||||
// following character of the input-string with the first character of
|
||||
// the infinity, and nan-symbol. If either matches, the function assumes, that
|
||||
// a match has been found, and expects the following input characters to match
|
||||
// the remaining characters of the special-value symbol.
|
||||
// This means that the following restrictions apply to special-value symbols:
|
||||
// - they must not start with signs ('+', or '-'),
|
||||
// - they must not have the same first character.
|
||||
// - they must not start with digits.
|
||||
//
|
||||
// If the separator character is not kNoSeparator, then that specific
|
||||
// character is ignored when in between two valid digits of the significant.
|
||||
// It is not allowed to appear in the exponent.
|
||||
// It is not allowed to lead or trail the number.
|
||||
// It is not allowed to appear twice next to each other.
|
||||
//
|
||||
// Examples:
|
||||
// flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = "infinity",
|
||||
// nan_symbol = "nan":
|
||||
// StringToDouble("0x1234") -> 4660.0.
|
||||
// StringToDouble("0x1234K") -> 4660.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> NaN // junk_string_value.
|
||||
// StringToDouble(" 1") -> NaN // junk_string_value.
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("-123.45") -> -123.45.
|
||||
// StringToDouble("--123.45") -> NaN // junk_string_value.
|
||||
// StringToDouble("123e45") -> 123e45.
|
||||
// StringToDouble("123E45") -> 123e45.
|
||||
// StringToDouble("123e+45") -> 123e45.
|
||||
// StringToDouble("123E-45") -> 123e-45.
|
||||
// StringToDouble("123e") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("123e-") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("+NaN") -> NaN // NaN string literal.
|
||||
// StringToDouble("-infinity") -> -inf. // infinity literal.
|
||||
// StringToDouble("Infinity") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = NULL,
|
||||
// nan_symbol = NULL:
|
||||
// StringToDouble("0x1234") -> NaN // junk_string_value.
|
||||
// StringToDouble("01234") -> 668.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" 1") -> 1.0
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("0123e45") -> NaN // junk_string_value.
|
||||
// StringToDouble("01239E45") -> 1239e45.
|
||||
// StringToDouble("-infinity") -> NaN // junk_string_value.
|
||||
// StringToDouble("NaN") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = NO_FLAGS,
|
||||
// separator = ' ':
|
||||
// StringToDouble("1 2 3 4") -> 1234.0
|
||||
// StringToDouble("1 2") -> NaN // junk_string_value
|
||||
// StringToDouble("1 000 000.0") -> 1000000.0
|
||||
// StringToDouble("1.000 000") -> 1.0
|
||||
// StringToDouble("1.0e1 000") -> NaN // junk_string_value
|
||||
StringToDoubleConverter(int flags,
|
||||
double empty_string_value,
|
||||
double junk_string_value,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
uc16 separator = kNoSeparator)
|
||||
: flags_(flags),
|
||||
empty_string_value_(empty_string_value),
|
||||
junk_string_value_(junk_string_value),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
separator_(separator) {
|
||||
}
|
||||
|
||||
// Performs the conversion.
|
||||
// The output parameter 'processed_characters_count' is set to the number
|
||||
// of characters that have been processed to read the number.
|
||||
// Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
|
||||
// in the 'processed_characters_count'. Trailing junk is never included.
|
||||
double StringToDouble(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble above but for 16 bit characters.
|
||||
double StringToDouble(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble but reads a float.
|
||||
// Note that this is not equivalent to static_cast<float>(StringToDouble(...))
|
||||
// due to potential double-rounding.
|
||||
float StringToFloat(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToFloat above but for 16 bit characters.
|
||||
float StringToFloat(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
private:
|
||||
const int flags_;
|
||||
const double empty_string_value_;
|
||||
const double junk_string_value_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const uc16 separator_;
|
||||
|
||||
template <class Iterator>
|
||||
double StringToIeee(Iterator start_pointer,
|
||||
int length,
|
||||
bool read_as_double,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
// ICU PATCH: Close ICU namespace
|
||||
U_NAMESPACE_END
|
||||
#include "double-conversion-string-to-double.h"
|
||||
#include "double-conversion-double-to-string.h"
|
||||
|
||||
#endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
|
||||
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
|
||||
|
@ -158,10 +158,10 @@
|
||||
<ClCompile Include="double-conversion-bignum-dtoa.cpp" />
|
||||
<ClCompile Include="double-conversion-bignum.cpp" />
|
||||
<ClCompile Include="double-conversion-cached-powers.cpp" />
|
||||
<ClCompile Include="double-conversion-diy-fp.cpp" />
|
||||
<ClCompile Include="double-conversion-double-to-string.cpp" />
|
||||
<ClCompile Include="double-conversion-fast-dtoa.cpp" />
|
||||
<ClCompile Include="double-conversion-string-to-double.cpp" />
|
||||
<ClCompile Include="double-conversion-strtod.cpp" />
|
||||
<ClCompile Include="double-conversion.cpp" />
|
||||
<ClCompile Include="dtfmtsym.cpp" />
|
||||
<ClCompile Include="dtitvfmt.cpp" />
|
||||
<ClCompile Include="dtitvinf.cpp" />
|
||||
@ -375,8 +375,11 @@
|
||||
<ClInclude Include="double-conversion-bignum.h" />
|
||||
<ClInclude Include="double-conversion-cached-powers.h" />
|
||||
<ClInclude Include="double-conversion-diy-fp.h" />
|
||||
<ClInclude Include="double-conversion-double-to-string.h" />
|
||||
<ClInclude Include="double-conversion-fast-dtoa.h" />
|
||||
<ClInclude Include="double-conversion-ieee.h" />
|
||||
<ClInclude Include="double-conversion-string-to-double.h" />
|
||||
<ClInclude Include="double-conversion-strtod.h" />
|
||||
<ClInclude Include="double-conversion-utils.h" />
|
||||
<ClInclude Include="double-conversion.h" />
|
||||
<ClInclude Include="dt_impl.h" />
|
||||
|
@ -117,16 +117,16 @@
|
||||
<ClCompile Include="double-conversion-cached-powers.cpp">
|
||||
<Filter>formatting</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="double-conversion-diy-fp.cpp">
|
||||
<ClCompile Include="double-conversion-double-to-string.cpp">
|
||||
<Filter>formatting</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="double-conversion-fast-dtoa.cpp">
|
||||
<Filter>formatting</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="double-conversion-strtod.cpp">
|
||||
<ClCompile Include="double-conversion-string-to-double.cpp">
|
||||
<Filter>formatting</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="double-conversion.cpp">
|
||||
<ClCompile Include="double-conversion-strtod.cpp">
|
||||
<Filter>formatting</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="dtfmtsym.cpp">
|
||||
@ -776,6 +776,9 @@
|
||||
<ClInclude Include="double-conversion-diy-fp.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double-conversion-double-to-string.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double-conversion-fast-dtoa.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
@ -785,6 +788,12 @@
|
||||
<ClInclude Include="double-conversion-utils.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double-conversion-string-to-double.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double-conversion-strtod.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double-conversion.h">
|
||||
<Filter>formatting</Filter>
|
||||
</ClInclude>
|
||||
|
@ -379,10 +379,10 @@
|
||||
<ClCompile Include="double-conversion-bignum-dtoa.cpp" />
|
||||
<ClCompile Include="double-conversion-bignum.cpp" />
|
||||
<ClCompile Include="double-conversion-cached-powers.cpp" />
|
||||
<ClCompile Include="double-conversion-diy-fp.cpp" />
|
||||
<ClCompile Include="double-conversion-double-to-string.cpp" />
|
||||
<ClCompile Include="double-conversion-fast-dtoa.cpp" />
|
||||
<ClCompile Include="double-conversion-string-to-double.cpp" />
|
||||
<ClCompile Include="double-conversion-strtod.cpp" />
|
||||
<ClCompile Include="double-conversion.cpp" />
|
||||
<ClCompile Include="dtfmtsym.cpp" />
|
||||
<ClCompile Include="dtitvfmt.cpp" />
|
||||
<ClCompile Include="dtitvinf.cpp" />
|
||||
@ -594,8 +594,11 @@
|
||||
<ClInclude Include="double-conversion-bignum.h" />
|
||||
<ClInclude Include="double-conversion-cached-powers.h" />
|
||||
<ClInclude Include="double-conversion-diy-fp.h" />
|
||||
<ClInclude Include="double-conversion-double-to-string.h" />
|
||||
<ClInclude Include="double-conversion-fast-dtoa.h" />
|
||||
<ClInclude Include="double-conversion-ieee.h" />
|
||||
<ClInclude Include="double-conversion-string-to-double.h" />
|
||||
<ClInclude Include="double-conversion-strtod.h" />
|
||||
<ClInclude Include="double-conversion-utils.h" />
|
||||
<ClInclude Include="double-conversion.h" />
|
||||
<ClInclude Include="dt_impl.h" />
|
||||
|
@ -948,9 +948,10 @@ group: listformatter
|
||||
resourcebundle simpleformatter format uclean_i18n formatted_value_iterimpl
|
||||
|
||||
group: double_conversion
|
||||
double-conversion.o double-conversion-bignum.o double-conversion-bignum-dtoa.o
|
||||
double-conversion-cached-powers.o double-conversion-diy-fp.o
|
||||
double-conversion-fast-dtoa.o double-conversion-strtod.o
|
||||
double-conversion-bignum.o double-conversion-double-to-string.o
|
||||
double-conversion-bignum-dtoa.o double-conversion-cached-powers.o
|
||||
double-conversion-string-to-double.o double-conversion-fast-dtoa.o
|
||||
double-conversion-strtod.o
|
||||
deps
|
||||
platform
|
||||
|
||||
|
36
vendor/double-conversion/UPDATING.md
vendored
36
vendor/double-conversion/UPDATING.md
vendored
@ -1,7 +1,13 @@
|
||||
<!--
|
||||
© 2016 and later: Unicode, Inc. and others.
|
||||
License & terms of use: http://www.unicode.org/copyright.html
|
||||
-->
|
||||
|
||||
## Updating double-conversion from Github to Vendor
|
||||
# Double Conversion Vendor Library
|
||||
|
||||
This library is used by ICU4C to perform double-to-string and string-to-double conversions.
|
||||
|
||||
## Updating double-conversion from Upstream to ICU
|
||||
|
||||
IMPORTANT: Please start with a clean working directory before continuing (no uncommitted changes).
|
||||
|
||||
@ -11,13 +17,31 @@ Run `pull-from-upstream.sh` as below:
|
||||
|
||||
./pull-from-upstream.sh <tag/branch>
|
||||
|
||||
You will be prompted to download the tarball. If confirmed, the script will overwrite the contents of the upstream directory.
|
||||
The script runs in 4 steps.
|
||||
|
||||
## Updating double-conversion from Vendor to ICU4C
|
||||
### Step 1: Download Tarball
|
||||
|
||||
After completing the first step, the script will stop again and ask you whether to copy the diffs into icu4c. If you say yes, the *diff between the git index and the working copy* (i.e., the output of `git diff`) will be applied to the corresponding files in icu4c.
|
||||
You will be prompted to download the tarball of the upstream tag/branch. If confirmed, the script will download the file to a temp directory and unpack it there.
|
||||
|
||||
Make note of the output of the command. If there are any merge conflicts, you will need to resolve them manually.
|
||||
At this point, the ICU source tree is still pristine.
|
||||
|
||||
### Step 2: Patch ICU4C
|
||||
|
||||
The script computes the ICU patches on double-conversion (diff between the ICU4C source tree and the vendor source tree), then copies in the new version of the files that it downloaded, and apply the ICU patches on top of those new files.
|
||||
|
||||
Look in the command output. If you see a message like `Hunk #6 NOT MERGED`, it means that you have to open the file manually and resolve the merge conflict.
|
||||
|
||||
It is also possible that upstream added, deleted, or renamed files. In this situation, you need to spend extra time touching up these changes in ICU4C.
|
||||
|
||||
At this point, the ICU4C source tree is changed, and the vendor directory is still pristine.
|
||||
|
||||
### Step 3: Copy to Vendor
|
||||
|
||||
Here, the script copies the pristine source from the temp directory to the vendor directory. You should always do this unless you aborted one of the previous steps.
|
||||
|
||||
### Step 4: Cleanup
|
||||
|
||||
The script will ask whether to delete its temp directory. If you want to keep the temp directory to refer to the ICU patches, for example, you can skip this step.
|
||||
|
||||
## Checking ICU Patches
|
||||
|
||||
@ -25,4 +49,4 @@ Look over any ICU patches in the icu4c/i18n version of the code files; they shou
|
||||
|
||||
## Next Steps
|
||||
|
||||
Build and test icu4c, and send the PR for review.
|
||||
Build and test ICU4C, and send the PR for review.
|
||||
|
52
vendor/double-conversion/pull-from-upstream.sh
vendored
52
vendor/double-conversion/pull-from-upstream.sh
vendored
@ -8,32 +8,34 @@ if [[ -z $1 ]]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
filename="$1.tar.gz"
|
||||
url="https://github.com/google/double-conversion/archive/$1.tar.gz";
|
||||
upstream_root="$(dirname "$0")/upstream";
|
||||
icu4c_i18n_root="$(dirname "$0")/../../icu4c/source/i18n";
|
||||
tmpdir=`mktemp -d`;
|
||||
filename="$tmpdir/$1.tar.gz"
|
||||
upstream_root_tmp="$tmpdir/upstream"
|
||||
patch_root_tmp="$tmpdir/patches"
|
||||
|
||||
echo "Will download $url";
|
||||
echo "Will expand into $upstream_root";
|
||||
read -p "Press Enter to continue or s to skip: " ch;
|
||||
|
||||
if [ "$ch" != "s" ]; then
|
||||
rm -f $filename;
|
||||
wget $url;
|
||||
rm -r "$upstream_root"; # in case any files were deleted in the new version
|
||||
mkdir "$upstream_root";
|
||||
tar zxf $filename --strip 1 -C "$upstream_root";
|
||||
rm $filename;
|
||||
echo "upstream updated and $filename removed";
|
||||
wget -O "$filename" "$url";
|
||||
mkdir "$upstream_root_tmp";
|
||||
tar zxf $filename --strip 1 -C "$upstream_root_tmp";
|
||||
fi
|
||||
|
||||
echo "Will apply diffs to $icu4c_i18n_root";
|
||||
read -p "Press Enter to continue or s to skip: " ch;
|
||||
|
||||
do_patch() {
|
||||
vendor_path="$upstream_root/double-conversion/$1";
|
||||
old_vendor_path="$upstream_root/double-conversion/$1";
|
||||
new_vendor_path="$upstream_root_tmp/double-conversion/$1";
|
||||
icu4c_path="$icu4c_i18n_root/$2";
|
||||
git diff --patch "$vendor_path" | patch --merge "$icu4c_path";
|
||||
tmp_path="$patch_root_tmp/$2.patch";
|
||||
diff -u "$old_vendor_path" "$icu4c_path" > "$tmp_path";
|
||||
cp "$new_vendor_path" "$icu4c_path";
|
||||
patch --merge "$icu4c_path" < "$tmp_path";
|
||||
}
|
||||
|
||||
do_patch_prefix_extension() {
|
||||
@ -45,18 +47,38 @@ do_patch_extension() {
|
||||
}
|
||||
|
||||
if [ "$ch" != "s" ]; then
|
||||
do_patch_prefix_extension bignum-dtoa cc cpp;
|
||||
do_patch_prefix_extension bignum-dtoa h h;
|
||||
mkdir "$patch_root_tmp";
|
||||
do_patch_prefix_extension bignum cc cpp;
|
||||
do_patch_prefix_extension bignum h h;
|
||||
do_patch_prefix_extension bignum-dtoa cc cpp;
|
||||
do_patch_prefix_extension bignum-dtoa h h;
|
||||
do_patch_prefix_extension cached-powers cc cpp;
|
||||
do_patch_prefix_extension cached-powers h h;
|
||||
do_patch_prefix_extension diy-fp cc cpp;
|
||||
do_patch_prefix_extension diy-fp h h;
|
||||
do_patch_prefix_extension double-to-string cc cpp;
|
||||
do_patch_prefix_extension double-to-string h h;
|
||||
do_patch_prefix_extension fast-dtoa cc cpp;
|
||||
do_patch_prefix_extension fast-dtoa h h;
|
||||
do_patch_prefix_extension ieee h h;
|
||||
do_patch_prefix_extension string-to-double cc cpp;
|
||||
do_patch_prefix_extension string-to-double h h;
|
||||
do_patch_prefix_extension strtod cc cpp;
|
||||
do_patch_prefix_extension strtod h h;
|
||||
do_patch_prefix_extension utils h h;
|
||||
do_patch_extension double-conversion cc cpp;
|
||||
do_patch_extension double-conversion h h;
|
||||
fi
|
||||
|
||||
echo "Will save pristine copy into $upstream_root";
|
||||
read -p "Press Enter to continue or s to skip: " ch;
|
||||
|
||||
if [ "$ch" != "s" ]; then
|
||||
rm -r "$upstream_root"; # in case any files were deleted in the new version
|
||||
cp -r "$upstream_root_tmp" "$upstream_root";
|
||||
fi
|
||||
|
||||
echo "Temporary files have been saved in $tmpdir";
|
||||
read -p "Press Enter to delete the directory or s to skip: " ch;
|
||||
|
||||
if [ "$ch" != "s" ]; then
|
||||
rm -rf "$tmpdir";
|
||||
fi
|
||||
|
20
vendor/double-conversion/upstream/.gitignore
vendored
20
vendor/double-conversion/upstream/.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.sconsign.dblite
|
||||
*~
|
||||
*.o
|
||||
*.obj
|
||||
msvc/Release/
|
||||
@ -7,3 +8,22 @@ msvc/Debug/
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.user
|
||||
*.a
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
/run_tests
|
||||
Makefile
|
||||
CMakeLists.txt.user
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
Testing
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
*.cmake
|
||||
*.kdev4
|
||||
DartConfiguration.tcl
|
||||
|
2
vendor/double-conversion/upstream/AUTHORS
vendored
2
vendor/double-conversion/upstream/AUTHORS
vendored
@ -12,3 +12,5 @@ Mike Hommey <mhommey@mozilla.com>
|
||||
Martin Olsson <mnemo@minimum.se>
|
||||
Kent Williams <chaircrusher@gmail.com>
|
||||
Elan Ruusamäe <glen@delfi.ee>
|
||||
Colin Hirsch <github@colin-hirsch.net>
|
||||
Zhenyi Peng <zhenyipeng@tencent.com>
|
||||
|
23
vendor/double-conversion/upstream/CMakeLists.txt
vendored
23
vendor/double-conversion/upstream/CMakeLists.txt
vendored
@ -1,14 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(double-conversion VERSION 3.1.4)
|
||||
project(double-conversion VERSION 3.1.5)
|
||||
|
||||
set(headers
|
||||
double-conversion/bignum.h
|
||||
double-conversion/cached-powers.h
|
||||
double-conversion/diy-fp.h
|
||||
double-conversion/double-conversion.h
|
||||
double-conversion/double-to-string.h
|
||||
double-conversion/fast-dtoa.h
|
||||
double-conversion/fixed-dtoa.h
|
||||
double-conversion/ieee.h
|
||||
double-conversion/string-to-double.h
|
||||
double-conversion/strtod.h
|
||||
double-conversion/utils.h)
|
||||
|
||||
@ -16,10 +18,10 @@ add_library(double-conversion
|
||||
double-conversion/bignum.cc
|
||||
double-conversion/bignum-dtoa.cc
|
||||
double-conversion/cached-powers.cc
|
||||
double-conversion/diy-fp.cc
|
||||
double-conversion/double-conversion.cc
|
||||
double-conversion/double-to-string.cc
|
||||
double-conversion/fast-dtoa.cc
|
||||
double-conversion/fixed-dtoa.cc
|
||||
double-conversion/string-to-double.cc
|
||||
double-conversion/strtod.cc
|
||||
${headers})
|
||||
target_include_directories(
|
||||
@ -40,12 +42,13 @@ endif()
|
||||
####
|
||||
# Installation (https://github.com/forexample/package-example)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
# Layout. This works for all platforms:
|
||||
# * <prefix>/lib/cmake/<PROJECT-NAME>
|
||||
# * <prefix>/lib/
|
||||
# * <prefix>/include/
|
||||
set(config_install_dir "lib/cmake/${PROJECT_NAME}")
|
||||
set(include_install_dir "include")
|
||||
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
|
||||
|
||||
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
|
||||
|
||||
@ -81,17 +84,17 @@ configure_package_config_file(
|
||||
install(
|
||||
TARGETS double-conversion
|
||||
EXPORT "${targets_export_name}"
|
||||
LIBRARY DESTINATION "lib"
|
||||
ARCHIVE DESTINATION "lib"
|
||||
RUNTIME DESTINATION "bin"
|
||||
INCLUDES DESTINATION "${include_install_dir}"
|
||||
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
|
||||
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
|
||||
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
|
||||
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
|
||||
)
|
||||
|
||||
# Headers:
|
||||
# * double-conversion/*.h -> <prefix>/include/double-conversion/*.h
|
||||
install(
|
||||
FILES ${headers}
|
||||
DESTINATION "${include_install_dir}/double-conversion"
|
||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/double-conversion"
|
||||
)
|
||||
|
||||
# Config
|
||||
|
30
vendor/double-conversion/upstream/Changelog
vendored
30
vendor/double-conversion/upstream/Changelog
vendored
@ -1,3 +1,33 @@
|
||||
2019-09-02:
|
||||
Add support for e2k architectur. Thanks to Michael Shigorin.
|
||||
|
||||
2019-08-01:
|
||||
Add min exponent width option in double-to-string conversion.
|
||||
|
||||
2019-06-22:
|
||||
Remove redundant parenthesis.
|
||||
|
||||
2019-06-11:
|
||||
Changed all macros to use DOUBLE_CONVERSION_ as prefix.
|
||||
Renamed ALLOW_CASE_INSENSIBILITY to ALLOW_CASE_INSENSITIVITY,
|
||||
the old name is still available but officially deprecated.
|
||||
Created and exposed new intermediate function StrtodTrimmed().
|
||||
|
||||
2019-05-25:
|
||||
Fix `0x` for string->double conversion when Hex Floats are allowed.
|
||||
Avoid integer overflow when exponents for hex floats were too big.
|
||||
Update version number.
|
||||
|
||||
2019-04-22:
|
||||
Fixed warning in gcc4.9. Thanks to Scott McCaskill
|
||||
(https://github.com/usefulcat) for the patch.
|
||||
|
||||
2019-04-16:
|
||||
Merged changes to install libraries in the correct place when
|
||||
using 64-bit libraries.
|
||||
Contributed by Jason Zaman <jasonzaman@gmail.com> and (independently)
|
||||
Dan Church (https://github.com/h3xx)
|
||||
|
||||
2019-03-11:
|
||||
Use relative includes in the library. This shouldn't have any visible effect
|
||||
for consumers of the library.
|
||||
|
5
vendor/double-conversion/upstream/README.md
vendored
5
vendor/double-conversion/upstream/README.md
vendored
@ -7,8 +7,9 @@ The library consists of efficient conversion routines that have been extracted
|
||||
from the V8 JavaScript engine. The code has been refactored and improved so that
|
||||
it can be used more easily in other projects.
|
||||
|
||||
There is extensive documentation in `double-conversion/double-conversion.h`. Other
|
||||
examples can be found in `test/cctest/test-conversions.cc`.
|
||||
There is extensive documentation in `double-conversion/string-to-double.h` and
|
||||
`double-conversion/double-to-string.h`. Other examples can be found in
|
||||
`test/cctest/test-conversions.cc`.
|
||||
|
||||
|
||||
Building
|
||||
|
4
vendor/double-conversion/upstream/SConstruct
vendored
4
vendor/double-conversion/upstream/SConstruct
vendored
@ -20,9 +20,9 @@ env['SHLIBVERSION'] = '3.0.0'
|
||||
|
||||
CCFLAGS = []
|
||||
if int(debug):
|
||||
CCFLAGS.append(ARGUMENTS.get('CXXFLAGS', '-g -Wall -Wshadow -Werror'))
|
||||
CCFLAGS.append(ARGUMENTS.get('CXXFLAGS', '-g -Wall -Wshadow -Werror -UNDEBUG'))
|
||||
if int(optimize):
|
||||
CCFLAGS.append(ARGUMENTS.get('CXXFLAGS', '-O3'))
|
||||
CCFLAGS.append(ARGUMENTS.get('CXXFLAGS', '-O3 -DNDEBUG=1'))
|
||||
|
||||
env.Append(CCFLAGS = " ".join(CCFLAGS))
|
||||
|
||||
|
1
vendor/double-conversion/upstream/double-conversion/.gitignore
vendored
Normal file
1
vendor/double-conversion/upstream/double-conversion/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.os
|
@ -3,10 +3,10 @@ double_conversion_sources = [
|
||||
'bignum.cc',
|
||||
'bignum-dtoa.cc',
|
||||
'cached-powers.cc',
|
||||
'diy-fp.cc',
|
||||
'double-conversion.cc',
|
||||
'double-to-string.cc',
|
||||
'fast-dtoa.cc',
|
||||
'fixed-dtoa.cc',
|
||||
'string-to-double.cc',
|
||||
'strtod.cc'
|
||||
]
|
||||
Return('double_conversion_sources')
|
||||
|
@ -35,7 +35,7 @@
|
||||
namespace double_conversion {
|
||||
|
||||
static int NormalizedExponent(uint64_t significand, int exponent) {
|
||||
ASSERT(significand != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(significand != 0);
|
||||
while ((significand & Double::kHiddenBit) == 0) {
|
||||
significand = significand << 1;
|
||||
exponent = exponent - 1;
|
||||
@ -76,26 +76,26 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// Generates 'requested_digits' after the decimal point.
|
||||
static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length);
|
||||
Vector<char> buffer, int* length);
|
||||
// Generates 'count' digits of numerator/denominator.
|
||||
// Once 'count' digits have been produced rounds the result depending on the
|
||||
// remainder (remainders of exactly .5 round upwards). Might update the
|
||||
// decimal_point when rounding up (for example for 0.9999).
|
||||
static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length);
|
||||
Vector<char> buffer, int* length);
|
||||
|
||||
|
||||
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
Vector<char> buffer, int* length, int* decimal_point) {
|
||||
ASSERT(v > 0);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(v > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
uint64_t significand;
|
||||
int exponent;
|
||||
bool lower_boundary_is_closer;
|
||||
if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) {
|
||||
float f = static_cast<float>(v);
|
||||
ASSERT(f == v);
|
||||
DOUBLE_CONVERSION_ASSERT(f == v);
|
||||
significand = Single(f).Significand();
|
||||
exponent = Single(f).Exponent();
|
||||
lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser();
|
||||
@ -134,7 +134,7 @@ void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
// 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
|
||||
// The maximum double is 1.7976931348623157e308 which needs fewer than
|
||||
// 308*4 binary digits.
|
||||
ASSERT(Bignum::kMaxSignificantBits >= 324*4);
|
||||
DOUBLE_CONVERSION_ASSERT(Bignum::kMaxSignificantBits >= 324*4);
|
||||
InitialScaledStartValues(significand, exponent, lower_boundary_is_closer,
|
||||
estimated_power, need_boundary_deltas,
|
||||
&numerator, &denominator,
|
||||
@ -163,7 +163,7 @@ void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
|
||||
buffer, length);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
buffer[*length] = '\0';
|
||||
}
|
||||
@ -195,7 +195,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
for (;;) {
|
||||
uint16_t digit;
|
||||
digit = numerator->DivideModuloIntBignum(*denominator);
|
||||
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
// digit = numerator / denominator (integer division).
|
||||
// numerator = numerator % denominator.
|
||||
buffer[(*length)++] = static_cast<char>(digit + '0');
|
||||
@ -241,7 +241,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// loop would have stopped earlier.
|
||||
// We still have an assert here in case the preconditions were not
|
||||
// satisfied.
|
||||
ASSERT(buffer[(*length) - 1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) - 1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
} else {
|
||||
// Halfway case.
|
||||
@ -252,7 +252,7 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
if ((buffer[(*length) - 1] - '0') % 2 == 0) {
|
||||
// Round down => Do nothing.
|
||||
} else {
|
||||
ASSERT(buffer[(*length) - 1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) - 1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
}
|
||||
}
|
||||
@ -264,9 +264,9 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
// Round up.
|
||||
// Note again that the last digit could not be '9' since this would have
|
||||
// stopped the loop earlier.
|
||||
// We still have an ASSERT here, in case the preconditions were not
|
||||
// We still have an DOUBLE_CONVERSION_ASSERT here, in case the preconditions were not
|
||||
// satisfied.
|
||||
ASSERT(buffer[(*length) -1] != '9');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[(*length) -1] != '9');
|
||||
buffer[(*length) - 1]++;
|
||||
return;
|
||||
}
|
||||
@ -283,11 +283,11 @@ static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
|
||||
static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char> buffer, int* length) {
|
||||
ASSERT(count >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(count >= 0);
|
||||
for (int i = 0; i < count - 1; ++i) {
|
||||
uint16_t digit;
|
||||
digit = numerator->DivideModuloIntBignum(*denominator);
|
||||
ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
|
||||
// digit = numerator / denominator (integer division).
|
||||
// numerator = numerator % denominator.
|
||||
buffer[i] = static_cast<char>(digit + '0');
|
||||
@ -300,7 +300,7 @@ static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
|
||||
digit++;
|
||||
}
|
||||
ASSERT(digit <= 10);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 10);
|
||||
buffer[count - 1] = static_cast<char>(digit + '0');
|
||||
// Correct bad digits (in case we had a sequence of '9's). Propagate the
|
||||
// carry until we hat a non-'9' or til we reach the first digit.
|
||||
@ -325,7 +325,7 @@ static void GenerateCountedDigits(int count, int* decimal_point,
|
||||
// Input verifies: 1 <= (numerator + delta) / denominator < 10.
|
||||
static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Vector<char>(buffer), int* length) {
|
||||
Vector<char> buffer, int* length) {
|
||||
// Note that we have to look at more than just the requested_digits, since
|
||||
// a number could be rounded up. Example: v=0.5 with requested_digits=0.
|
||||
// Even though the power of v equals 0 we can't just stop here.
|
||||
@ -341,7 +341,7 @@ static void BignumToFixed(int requested_digits, int* decimal_point,
|
||||
} else if (-(*decimal_point) == requested_digits) {
|
||||
// We only need to verify if the number rounds down or up.
|
||||
// Ex: 0.04 and 0.06 with requested_digits == 1.
|
||||
ASSERT(*decimal_point == -requested_digits);
|
||||
DOUBLE_CONVERSION_ASSERT(*decimal_point == -requested_digits);
|
||||
// Initially the fraction lies in range (1, 10]. Multiply the denominator
|
||||
// by 10 so that we can compare more easily.
|
||||
denominator->Times10();
|
||||
@ -420,7 +420,7 @@ static void InitialScaledStartValuesPositiveExponent(
|
||||
Bignum* numerator, Bignum* denominator,
|
||||
Bignum* delta_minus, Bignum* delta_plus) {
|
||||
// A positive exponent implies a positive power.
|
||||
ASSERT(estimated_power >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(estimated_power >= 0);
|
||||
// Since the estimated_power is positive we simply multiply the denominator
|
||||
// by 10^estimated_power.
|
||||
|
||||
@ -506,7 +506,7 @@ static void InitialScaledStartValuesNegativeExponentNegativePower(
|
||||
// numerator = v * 10^-estimated_power * 2 * 2^-exponent.
|
||||
// Remember: numerator has been abused as power_ten. So no need to assign it
|
||||
// to itself.
|
||||
ASSERT(numerator == power_ten);
|
||||
DOUBLE_CONVERSION_ASSERT(numerator == power_ten);
|
||||
numerator->MultiplyByUInt64(significand);
|
||||
|
||||
// denominator = 2 * 2^-exponent with exponent < 0.
|
||||
|
@ -25,141 +25,137 @@
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include "bignum.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
Bignum::Bignum()
|
||||
: bigits_buffer_(), bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) {
|
||||
for (int i = 0; i < kBigitCapacity; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
Bignum::Chunk& Bignum::RawBigit(const int index) {
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
|
||||
return bigits_buffer_[index];
|
||||
}
|
||||
|
||||
|
||||
const Bignum::Chunk& Bignum::RawBigit(const int index) const {
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<unsigned>(index) < kBigitCapacity);
|
||||
return bigits_buffer_[index];
|
||||
}
|
||||
|
||||
|
||||
template<typename S>
|
||||
static int BitSize(S value) {
|
||||
static int BitSize(const S value) {
|
||||
(void) value; // Mark variable as used.
|
||||
return 8 * sizeof(value);
|
||||
}
|
||||
|
||||
// Guaranteed to lie in one Bigit.
|
||||
void Bignum::AssignUInt16(uint16_t value) {
|
||||
ASSERT(kBigitSize >= BitSize(value));
|
||||
void Bignum::AssignUInt16(const uint16_t value) {
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize >= BitSize(value));
|
||||
Zero();
|
||||
if (value == 0) return;
|
||||
|
||||
EnsureCapacity(1);
|
||||
bigits_[0] = value;
|
||||
used_digits_ = 1;
|
||||
if (value > 0) {
|
||||
RawBigit(0) = value;
|
||||
used_bigits_ = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignUInt64(uint64_t value) {
|
||||
const int kUInt64Size = 64;
|
||||
|
||||
Zero();
|
||||
if (value == 0) return;
|
||||
|
||||
int needed_bigits = kUInt64Size / kBigitSize + 1;
|
||||
EnsureCapacity(needed_bigits);
|
||||
for (int i = 0; i < needed_bigits; ++i) {
|
||||
bigits_[i] = value & kBigitMask;
|
||||
value = value >> kBigitSize;
|
||||
for(int i = 0; value > 0; ++i) {
|
||||
RawBigit(i) = value & kBigitMask;
|
||||
value >>= kBigitSize;
|
||||
++used_bigits_;
|
||||
}
|
||||
used_digits_ = needed_bigits;
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignBignum(const Bignum& other) {
|
||||
exponent_ = other.exponent_;
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
bigits_[i] = other.bigits_[i];
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
RawBigit(i) = other.RawBigit(i);
|
||||
}
|
||||
// Clear the excess digits (if there were any).
|
||||
for (int i = other.used_digits_; i < used_digits_; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
used_digits_ = other.used_digits_;
|
||||
used_bigits_ = other.used_bigits_;
|
||||
}
|
||||
|
||||
|
||||
static uint64_t ReadUInt64(Vector<const char> buffer,
|
||||
int from,
|
||||
int digits_to_read) {
|
||||
static uint64_t ReadUInt64(const Vector<const char> buffer,
|
||||
const int from,
|
||||
const int digits_to_read) {
|
||||
uint64_t result = 0;
|
||||
for (int i = from; i < from + digits_to_read; ++i) {
|
||||
int digit = buffer[i] - '0';
|
||||
ASSERT(0 <= digit && digit <= 9);
|
||||
const int digit = buffer[i] - '0';
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
|
||||
result = result * 10 + digit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignDecimalString(Vector<const char> value) {
|
||||
void Bignum::AssignDecimalString(const Vector<const char> value) {
|
||||
// 2^64 = 18446744073709551616 > 10^19
|
||||
const int kMaxUint64DecimalDigits = 19;
|
||||
static const int kMaxUint64DecimalDigits = 19;
|
||||
Zero();
|
||||
int length = value.length();
|
||||
unsigned int pos = 0;
|
||||
unsigned pos = 0;
|
||||
// Let's just say that each digit needs 4 bits.
|
||||
while (length >= kMaxUint64DecimalDigits) {
|
||||
uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
|
||||
const uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits);
|
||||
pos += kMaxUint64DecimalDigits;
|
||||
length -= kMaxUint64DecimalDigits;
|
||||
MultiplyByPowerOfTen(kMaxUint64DecimalDigits);
|
||||
AddUInt64(digits);
|
||||
}
|
||||
uint64_t digits = ReadUInt64(value, pos, length);
|
||||
const uint64_t digits = ReadUInt64(value, pos, length);
|
||||
MultiplyByPowerOfTen(length);
|
||||
AddUInt64(digits);
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
static int HexCharValue(char c) {
|
||||
if ('0' <= c && c <= '9') return c - '0';
|
||||
if ('a' <= c && c <= 'f') return 10 + c - 'a';
|
||||
ASSERT('A' <= c && c <= 'F');
|
||||
static uint64_t HexCharValue(const int c) {
|
||||
if ('0' <= c && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if ('a' <= c && c <= 'f') {
|
||||
return 10 + c - 'a';
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT('A' <= c && c <= 'F');
|
||||
return 10 + c - 'A';
|
||||
}
|
||||
|
||||
|
||||
// Unlike AssignDecimalString(), this function is "only" used
|
||||
// for unit-tests and therefore not performance critical.
|
||||
void Bignum::AssignHexString(Vector<const char> value) {
|
||||
Zero();
|
||||
int length = value.length();
|
||||
|
||||
int needed_bigits = length * 4 / kBigitSize + 1;
|
||||
EnsureCapacity(needed_bigits);
|
||||
int string_index = length - 1;
|
||||
for (int i = 0; i < needed_bigits - 1; ++i) {
|
||||
// These bigits are guaranteed to be "full".
|
||||
Chunk current_bigit = 0;
|
||||
for (int j = 0; j < kBigitSize / 4; j++) {
|
||||
current_bigit += HexCharValue(value[string_index--]) << (j * 4);
|
||||
// Required capacity could be reduced by ignoring leading zeros.
|
||||
EnsureCapacity(((value.length() * 4) + kBigitSize - 1) / kBigitSize);
|
||||
DOUBLE_CONVERSION_ASSERT(sizeof(uint64_t) * 8 >= kBigitSize + 4); // TODO: static_assert
|
||||
// Accumulates converted hex digits until at least kBigitSize bits.
|
||||
// Works with non-factor-of-four kBigitSizes.
|
||||
uint64_t tmp = 0; // Accumulates converted hex digits until at least
|
||||
for (int cnt = 0; !value.is_empty(); value.pop_back()) {
|
||||
tmp |= (HexCharValue(value.last()) << cnt);
|
||||
if ((cnt += 4) >= kBigitSize) {
|
||||
RawBigit(used_bigits_++) = (tmp & kBigitMask);
|
||||
cnt -= kBigitSize;
|
||||
tmp >>= kBigitSize;
|
||||
}
|
||||
bigits_[i] = current_bigit;
|
||||
}
|
||||
used_digits_ = needed_bigits - 1;
|
||||
|
||||
Chunk most_significant_bigit = 0; // Could be = 0;
|
||||
for (int j = 0; j <= string_index; ++j) {
|
||||
most_significant_bigit <<= 4;
|
||||
most_significant_bigit += HexCharValue(value[j]);
|
||||
}
|
||||
if (most_significant_bigit != 0) {
|
||||
bigits_[used_digits_] = most_significant_bigit;
|
||||
used_digits_++;
|
||||
if (tmp > 0) {
|
||||
RawBigit(used_bigits_++) = tmp;
|
||||
}
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AddUInt64(uint64_t operand) {
|
||||
if (operand == 0) return;
|
||||
void Bignum::AddUInt64(const uint64_t operand) {
|
||||
if (operand == 0) {
|
||||
return;
|
||||
}
|
||||
Bignum other;
|
||||
other.AssignUInt64(operand);
|
||||
AddBignum(other);
|
||||
@ -167,8 +163,8 @@ void Bignum::AddUInt64(uint64_t operand) {
|
||||
|
||||
|
||||
void Bignum::AddBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
|
||||
// If this has a greater exponent than other append zero-bigits to this.
|
||||
// After this call exponent_ <= other.exponent_.
|
||||
@ -186,48 +182,52 @@ void Bignum::AddBignum(const Bignum& other) {
|
||||
// cccccccccccc 0000
|
||||
// In both cases we might need a carry bigit.
|
||||
|
||||
EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_);
|
||||
EnsureCapacity(1 + (std::max)(BigitLength(), other.BigitLength()) - exponent_);
|
||||
Chunk carry = 0;
|
||||
int bigit_pos = other.exponent_ - exponent_;
|
||||
ASSERT(bigit_pos >= 0);
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry;
|
||||
bigits_[bigit_pos] = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
bigit_pos++;
|
||||
DOUBLE_CONVERSION_ASSERT(bigit_pos >= 0);
|
||||
for (int i = used_bigits_; i < bigit_pos; ++i) {
|
||||
RawBigit(i) = 0;
|
||||
}
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
|
||||
const Chunk sum = my + other.RawBigit(i) + carry;
|
||||
RawBigit(bigit_pos) = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
++bigit_pos;
|
||||
}
|
||||
|
||||
while (carry != 0) {
|
||||
Chunk sum = bigits_[bigit_pos] + carry;
|
||||
bigits_[bigit_pos] = sum & kBigitMask;
|
||||
const Chunk my = (bigit_pos < used_bigits_) ? RawBigit(bigit_pos) : 0;
|
||||
const Chunk sum = my + carry;
|
||||
RawBigit(bigit_pos) = sum & kBigitMask;
|
||||
carry = sum >> kBigitSize;
|
||||
bigit_pos++;
|
||||
++bigit_pos;
|
||||
}
|
||||
used_digits_ = Max(bigit_pos, used_digits_);
|
||||
ASSERT(IsClamped());
|
||||
used_bigits_ = (std::max)(bigit_pos, static_cast<int>(used_bigits_));
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
}
|
||||
|
||||
|
||||
void Bignum::SubtractBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
// We require this to be bigger than other.
|
||||
ASSERT(LessEqual(other, *this));
|
||||
DOUBLE_CONVERSION_ASSERT(LessEqual(other, *this));
|
||||
|
||||
Align(other);
|
||||
|
||||
int offset = other.exponent_ - exponent_;
|
||||
const int offset = other.exponent_ - exponent_;
|
||||
Chunk borrow = 0;
|
||||
int i;
|
||||
for (i = 0; i < other.used_digits_; ++i) {
|
||||
ASSERT((borrow == 0) || (borrow == 1));
|
||||
Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow;
|
||||
bigits_[i + offset] = difference & kBigitMask;
|
||||
for (i = 0; i < other.used_bigits_; ++i) {
|
||||
DOUBLE_CONVERSION_ASSERT((borrow == 0) || (borrow == 1));
|
||||
const Chunk difference = RawBigit(i + offset) - other.RawBigit(i) - borrow;
|
||||
RawBigit(i + offset) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
}
|
||||
while (borrow != 0) {
|
||||
Chunk difference = bigits_[i + offset] - borrow;
|
||||
bigits_[i + offset] = difference & kBigitMask;
|
||||
const Chunk difference = RawBigit(i + offset) - borrow;
|
||||
RawBigit(i + offset) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
++i;
|
||||
}
|
||||
@ -235,91 +235,105 @@ void Bignum::SubtractBignum(const Bignum& other) {
|
||||
}
|
||||
|
||||
|
||||
void Bignum::ShiftLeft(int shift_amount) {
|
||||
if (used_digits_ == 0) return;
|
||||
exponent_ += shift_amount / kBigitSize;
|
||||
int local_shift = shift_amount % kBigitSize;
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
void Bignum::ShiftLeft(const int shift_amount) {
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
exponent_ += (shift_amount / kBigitSize);
|
||||
const int local_shift = shift_amount % kBigitSize;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
BigitsShiftLeft(local_shift);
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByUInt32(uint32_t factor) {
|
||||
if (factor == 1) return;
|
||||
void Bignum::MultiplyByUInt32(const uint32_t factor) {
|
||||
if (factor == 1) {
|
||||
return;
|
||||
}
|
||||
if (factor == 0) {
|
||||
Zero();
|
||||
return;
|
||||
}
|
||||
if (used_digits_ == 0) return;
|
||||
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
// The product of a bigit with the factor is of size kBigitSize + 32.
|
||||
// Assert that this number + 1 (for the carry) fits into double chunk.
|
||||
ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1);
|
||||
DoubleChunk carry = 0;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry;
|
||||
bigits_[i] = static_cast<Chunk>(product & kBigitMask);
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const DoubleChunk product = static_cast<DoubleChunk>(factor) * RawBigit(i) + carry;
|
||||
RawBigit(i) = static_cast<Chunk>(product & kBigitMask);
|
||||
carry = (product >> kBigitSize);
|
||||
}
|
||||
while (carry != 0) {
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
bigits_[used_digits_] = carry & kBigitMask;
|
||||
used_digits_++;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
RawBigit(used_bigits_) = carry & kBigitMask;
|
||||
used_bigits_++;
|
||||
carry >>= kBigitSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByUInt64(uint64_t factor) {
|
||||
if (factor == 1) return;
|
||||
void Bignum::MultiplyByUInt64(const uint64_t factor) {
|
||||
if (factor == 1) {
|
||||
return;
|
||||
}
|
||||
if (factor == 0) {
|
||||
Zero();
|
||||
return;
|
||||
}
|
||||
ASSERT(kBigitSize < 32);
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize < 32);
|
||||
uint64_t carry = 0;
|
||||
uint64_t low = factor & 0xFFFFFFFF;
|
||||
uint64_t high = factor >> 32;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
uint64_t product_low = low * bigits_[i];
|
||||
uint64_t product_high = high * bigits_[i];
|
||||
uint64_t tmp = (carry & kBigitMask) + product_low;
|
||||
bigits_[i] = tmp & kBigitMask;
|
||||
const uint64_t low = factor & 0xFFFFFFFF;
|
||||
const uint64_t high = factor >> 32;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const uint64_t product_low = low * RawBigit(i);
|
||||
const uint64_t product_high = high * RawBigit(i);
|
||||
const uint64_t tmp = (carry & kBigitMask) + product_low;
|
||||
RawBigit(i) = tmp & kBigitMask;
|
||||
carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
|
||||
(product_high << (32 - kBigitSize));
|
||||
}
|
||||
while (carry != 0) {
|
||||
EnsureCapacity(used_digits_ + 1);
|
||||
bigits_[used_digits_] = carry & kBigitMask;
|
||||
used_digits_++;
|
||||
EnsureCapacity(used_bigits_ + 1);
|
||||
RawBigit(used_bigits_) = carry & kBigitMask;
|
||||
used_bigits_++;
|
||||
carry >>= kBigitSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::MultiplyByPowerOfTen(int exponent) {
|
||||
const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d);
|
||||
const uint16_t kFive1 = 5;
|
||||
const uint16_t kFive2 = kFive1 * 5;
|
||||
const uint16_t kFive3 = kFive2 * 5;
|
||||
const uint16_t kFive4 = kFive3 * 5;
|
||||
const uint16_t kFive5 = kFive4 * 5;
|
||||
const uint16_t kFive6 = kFive5 * 5;
|
||||
const uint32_t kFive7 = kFive6 * 5;
|
||||
const uint32_t kFive8 = kFive7 * 5;
|
||||
const uint32_t kFive9 = kFive8 * 5;
|
||||
const uint32_t kFive10 = kFive9 * 5;
|
||||
const uint32_t kFive11 = kFive10 * 5;
|
||||
const uint32_t kFive12 = kFive11 * 5;
|
||||
const uint32_t kFive13 = kFive12 * 5;
|
||||
const uint32_t kFive1_to_12[] =
|
||||
void Bignum::MultiplyByPowerOfTen(const int exponent) {
|
||||
static const uint64_t kFive27 = DOUBLE_CONVERSION_UINT64_2PART_C(0x6765c793, fa10079d);
|
||||
static const uint16_t kFive1 = 5;
|
||||
static const uint16_t kFive2 = kFive1 * 5;
|
||||
static const uint16_t kFive3 = kFive2 * 5;
|
||||
static const uint16_t kFive4 = kFive3 * 5;
|
||||
static const uint16_t kFive5 = kFive4 * 5;
|
||||
static const uint16_t kFive6 = kFive5 * 5;
|
||||
static const uint32_t kFive7 = kFive6 * 5;
|
||||
static const uint32_t kFive8 = kFive7 * 5;
|
||||
static const uint32_t kFive9 = kFive8 * 5;
|
||||
static const uint32_t kFive10 = kFive9 * 5;
|
||||
static const uint32_t kFive11 = kFive10 * 5;
|
||||
static const uint32_t kFive12 = kFive11 * 5;
|
||||
static const uint32_t kFive13 = kFive12 * 5;
|
||||
static const uint32_t kFive1_to_12[] =
|
||||
{ kFive1, kFive2, kFive3, kFive4, kFive5, kFive6,
|
||||
kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 };
|
||||
|
||||
ASSERT(exponent >= 0);
|
||||
if (exponent == 0) return;
|
||||
if (used_digits_ == 0) return;
|
||||
DOUBLE_CONVERSION_ASSERT(exponent >= 0);
|
||||
|
||||
if (exponent == 0) {
|
||||
return;
|
||||
}
|
||||
if (used_bigits_ == 0) {
|
||||
return;
|
||||
}
|
||||
// We shift by exponent at the end just before returning.
|
||||
int remaining_exponent = exponent;
|
||||
while (remaining_exponent >= 27) {
|
||||
@ -338,8 +352,8 @@ void Bignum::MultiplyByPowerOfTen(int exponent) {
|
||||
|
||||
|
||||
void Bignum::Square() {
|
||||
ASSERT(IsClamped());
|
||||
int product_length = 2 * used_digits_;
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
const int product_length = 2 * used_bigits_;
|
||||
EnsureCapacity(product_length);
|
||||
|
||||
// Comba multiplication: compute each column separately.
|
||||
@ -354,64 +368,64 @@ void Bignum::Square() {
|
||||
//
|
||||
// Assert that the additional number of bits in a DoubleChunk are enough to
|
||||
// sum up used_digits of Bigit*Bigit.
|
||||
if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) {
|
||||
UNIMPLEMENTED();
|
||||
if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_bigits_) {
|
||||
DOUBLE_CONVERSION_UNIMPLEMENTED();
|
||||
}
|
||||
DoubleChunk accumulator = 0;
|
||||
// First shift the digits so we don't overwrite them.
|
||||
int copy_offset = used_digits_;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
bigits_[copy_offset + i] = bigits_[i];
|
||||
const int copy_offset = used_bigits_;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
RawBigit(copy_offset + i) = RawBigit(i);
|
||||
}
|
||||
// We have two loops to avoid some 'if's in the loop.
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
// Process temporary digit i with power i.
|
||||
// The sum of the two indices must be equal to i.
|
||||
int bigit_index1 = i;
|
||||
int bigit_index2 = 0;
|
||||
// Sum all of the sub-products.
|
||||
while (bigit_index1 >= 0) {
|
||||
Chunk chunk1 = bigits_[copy_offset + bigit_index1];
|
||||
Chunk chunk2 = bigits_[copy_offset + bigit_index2];
|
||||
const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
|
||||
const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
|
||||
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
|
||||
bigit_index1--;
|
||||
bigit_index2++;
|
||||
}
|
||||
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
accumulator >>= kBigitSize;
|
||||
}
|
||||
for (int i = used_digits_; i < product_length; ++i) {
|
||||
int bigit_index1 = used_digits_ - 1;
|
||||
for (int i = used_bigits_; i < product_length; ++i) {
|
||||
int bigit_index1 = used_bigits_ - 1;
|
||||
int bigit_index2 = i - bigit_index1;
|
||||
// Invariant: sum of both indices is again equal to i.
|
||||
// Inner loop runs 0 times on last iteration, emptying accumulator.
|
||||
while (bigit_index2 < used_digits_) {
|
||||
Chunk chunk1 = bigits_[copy_offset + bigit_index1];
|
||||
Chunk chunk2 = bigits_[copy_offset + bigit_index2];
|
||||
while (bigit_index2 < used_bigits_) {
|
||||
const Chunk chunk1 = RawBigit(copy_offset + bigit_index1);
|
||||
const Chunk chunk2 = RawBigit(copy_offset + bigit_index2);
|
||||
accumulator += static_cast<DoubleChunk>(chunk1) * chunk2;
|
||||
bigit_index1--;
|
||||
bigit_index2++;
|
||||
}
|
||||
// The overwritten bigits_[i] will never be read in further loop iterations,
|
||||
// The overwritten RawBigit(i) will never be read in further loop iterations,
|
||||
// because bigit_index1 and bigit_index2 are always greater
|
||||
// than i - used_digits_.
|
||||
bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
// than i - used_bigits_.
|
||||
RawBigit(i) = static_cast<Chunk>(accumulator) & kBigitMask;
|
||||
accumulator >>= kBigitSize;
|
||||
}
|
||||
// Since the result was guaranteed to lie inside the number the
|
||||
// accumulator must be 0 now.
|
||||
ASSERT(accumulator == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(accumulator == 0);
|
||||
|
||||
// Don't forget to update the used_digits and the exponent.
|
||||
used_digits_ = product_length;
|
||||
used_bigits_ = product_length;
|
||||
exponent_ *= 2;
|
||||
Clamp();
|
||||
}
|
||||
|
||||
|
||||
void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
ASSERT(base != 0);
|
||||
ASSERT(power_exponent >= 0);
|
||||
void Bignum::AssignPowerUInt16(uint16_t base, const int power_exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(base != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(power_exponent >= 0);
|
||||
if (power_exponent == 0) {
|
||||
AssignUInt16(1);
|
||||
return;
|
||||
@ -431,7 +445,7 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
tmp_base >>= 1;
|
||||
bit_size++;
|
||||
}
|
||||
int final_size = bit_size * power_exponent;
|
||||
const int final_size = bit_size * power_exponent;
|
||||
// 1 extra bigit for the shifting, and one for rounded final_size.
|
||||
EnsureCapacity(final_size / kBigitSize + 2);
|
||||
|
||||
@ -452,10 +466,10 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
// Verify that there is enough space in this_value to perform the
|
||||
// multiplication. The first bit_size bits must be 0.
|
||||
if ((power_exponent & mask) != 0) {
|
||||
ASSERT(bit_size > 0);
|
||||
uint64_t base_bits_mask =
|
||||
~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
|
||||
bool high_bits_zero = (this_value & base_bits_mask) == 0;
|
||||
DOUBLE_CONVERSION_ASSERT(bit_size > 0);
|
||||
const uint64_t base_bits_mask =
|
||||
~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1);
|
||||
const bool high_bits_zero = (this_value & base_bits_mask) == 0;
|
||||
if (high_bits_zero) {
|
||||
this_value *= base;
|
||||
} else {
|
||||
@ -485,9 +499,9 @@ void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) {
|
||||
|
||||
// Precondition: this/other < 16bit.
|
||||
uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
ASSERT(IsClamped());
|
||||
ASSERT(other.IsClamped());
|
||||
ASSERT(other.used_digits_ > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(other.used_bigits_ > 0);
|
||||
|
||||
// Easy case: if we have less digits than the divisor than the result is 0.
|
||||
// Note: this handles the case where this == 0, too.
|
||||
@ -505,34 +519,34 @@ uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
// This naive approach is extremely inefficient if `this` divided by other
|
||||
// is big. This function is implemented for doubleToString where
|
||||
// the result should be small (less than 10).
|
||||
ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16));
|
||||
ASSERT(bigits_[used_digits_ - 1] < 0x10000);
|
||||
DOUBLE_CONVERSION_ASSERT(other.RawBigit(other.used_bigits_ - 1) >= ((1 << kBigitSize) / 16));
|
||||
DOUBLE_CONVERSION_ASSERT(RawBigit(used_bigits_ - 1) < 0x10000);
|
||||
// Remove the multiples of the first digit.
|
||||
// Example this = 23 and other equals 9. -> Remove 2 multiples.
|
||||
result += static_cast<uint16_t>(bigits_[used_digits_ - 1]);
|
||||
SubtractTimes(other, bigits_[used_digits_ - 1]);
|
||||
result += static_cast<uint16_t>(RawBigit(used_bigits_ - 1));
|
||||
SubtractTimes(other, RawBigit(used_bigits_ - 1));
|
||||
}
|
||||
|
||||
ASSERT(BigitLength() == other.BigitLength());
|
||||
DOUBLE_CONVERSION_ASSERT(BigitLength() == other.BigitLength());
|
||||
|
||||
// Both bignums are at the same length now.
|
||||
// Since other has more than 0 digits we know that the access to
|
||||
// bigits_[used_digits_ - 1] is safe.
|
||||
Chunk this_bigit = bigits_[used_digits_ - 1];
|
||||
Chunk other_bigit = other.bigits_[other.used_digits_ - 1];
|
||||
// RawBigit(used_bigits_ - 1) is safe.
|
||||
const Chunk this_bigit = RawBigit(used_bigits_ - 1);
|
||||
const Chunk other_bigit = other.RawBigit(other.used_bigits_ - 1);
|
||||
|
||||
if (other.used_digits_ == 1) {
|
||||
if (other.used_bigits_ == 1) {
|
||||
// Shortcut for easy (and common) case.
|
||||
int quotient = this_bigit / other_bigit;
|
||||
bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient;
|
||||
ASSERT(quotient < 0x10000);
|
||||
RawBigit(used_bigits_ - 1) = this_bigit - other_bigit * quotient;
|
||||
DOUBLE_CONVERSION_ASSERT(quotient < 0x10000);
|
||||
result += static_cast<uint16_t>(quotient);
|
||||
Clamp();
|
||||
return result;
|
||||
}
|
||||
|
||||
int division_estimate = this_bigit / (other_bigit + 1);
|
||||
ASSERT(division_estimate < 0x10000);
|
||||
const int division_estimate = this_bigit / (other_bigit + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(division_estimate < 0x10000);
|
||||
result += static_cast<uint16_t>(division_estimate);
|
||||
SubtractTimes(other, division_estimate);
|
||||
|
||||
@ -552,7 +566,7 @@ uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) {
|
||||
|
||||
template<typename S>
|
||||
static int SizeInHexChars(S number) {
|
||||
ASSERT(number > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(number > 0);
|
||||
int result = 0;
|
||||
while (number != 0) {
|
||||
number >>= 4;
|
||||
@ -562,29 +576,35 @@ static int SizeInHexChars(S number) {
|
||||
}
|
||||
|
||||
|
||||
static char HexCharOfValue(int value) {
|
||||
ASSERT(0 <= value && value <= 16);
|
||||
if (value < 10) return static_cast<char>(value + '0');
|
||||
static char HexCharOfValue(const int value) {
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= value && value <= 16);
|
||||
if (value < 10) {
|
||||
return static_cast<char>(value + '0');
|
||||
}
|
||||
return static_cast<char>(value - 10 + 'A');
|
||||
}
|
||||
|
||||
|
||||
bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
ASSERT(IsClamped());
|
||||
bool Bignum::ToHexString(char* buffer, const int buffer_size) const {
|
||||
DOUBLE_CONVERSION_ASSERT(IsClamped());
|
||||
// Each bigit must be printable as separate hex-character.
|
||||
ASSERT(kBigitSize % 4 == 0);
|
||||
const int kHexCharsPerBigit = kBigitSize / 4;
|
||||
DOUBLE_CONVERSION_ASSERT(kBigitSize % 4 == 0);
|
||||
static const int kHexCharsPerBigit = kBigitSize / 4;
|
||||
|
||||
if (used_digits_ == 0) {
|
||||
if (buffer_size < 2) return false;
|
||||
if (used_bigits_ == 0) {
|
||||
if (buffer_size < 2) {
|
||||
return false;
|
||||
}
|
||||
buffer[0] = '0';
|
||||
buffer[1] = '\0';
|
||||
return true;
|
||||
}
|
||||
// We add 1 for the terminating '\0' character.
|
||||
int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
|
||||
SizeInHexChars(bigits_[used_digits_ - 1]) + 1;
|
||||
if (needed_chars > buffer_size) return false;
|
||||
const int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit +
|
||||
SizeInHexChars(RawBigit(used_bigits_ - 1)) + 1;
|
||||
if (needed_chars > buffer_size) {
|
||||
return false;
|
||||
}
|
||||
int string_index = needed_chars - 1;
|
||||
buffer[string_index--] = '\0';
|
||||
for (int i = 0; i < exponent_; ++i) {
|
||||
@ -592,15 +612,15 @@ bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
buffer[string_index--] = '0';
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < used_digits_ - 1; ++i) {
|
||||
Chunk current_bigit = bigits_[i];
|
||||
for (int i = 0; i < used_bigits_ - 1; ++i) {
|
||||
Chunk current_bigit = RawBigit(i);
|
||||
for (int j = 0; j < kHexCharsPerBigit; ++j) {
|
||||
buffer[string_index--] = HexCharOfValue(current_bigit & 0xF);
|
||||
current_bigit >>= 4;
|
||||
}
|
||||
}
|
||||
// And finally the last bigit.
|
||||
Chunk most_significant_bigit = bigits_[used_digits_ - 1];
|
||||
Chunk most_significant_bigit = RawBigit(used_bigits_ - 1);
|
||||
while (most_significant_bigit != 0) {
|
||||
buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF);
|
||||
most_significant_bigit >>= 4;
|
||||
@ -609,25 +629,37 @@ bool Bignum::ToHexString(char* buffer, int buffer_size) const {
|
||||
}
|
||||
|
||||
|
||||
Bignum::Chunk Bignum::BigitAt(int index) const {
|
||||
if (index >= BigitLength()) return 0;
|
||||
if (index < exponent_) return 0;
|
||||
return bigits_[index - exponent_];
|
||||
Bignum::Chunk Bignum::BigitOrZero(const int index) const {
|
||||
if (index >= BigitLength()) {
|
||||
return 0;
|
||||
}
|
||||
if (index < exponent_) {
|
||||
return 0;
|
||||
}
|
||||
return RawBigit(index - exponent_);
|
||||
}
|
||||
|
||||
|
||||
int Bignum::Compare(const Bignum& a, const Bignum& b) {
|
||||
ASSERT(a.IsClamped());
|
||||
ASSERT(b.IsClamped());
|
||||
int bigit_length_a = a.BigitLength();
|
||||
int bigit_length_b = b.BigitLength();
|
||||
if (bigit_length_a < bigit_length_b) return -1;
|
||||
if (bigit_length_a > bigit_length_b) return +1;
|
||||
for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) {
|
||||
Chunk bigit_a = a.BigitAt(i);
|
||||
Chunk bigit_b = b.BigitAt(i);
|
||||
if (bigit_a < bigit_b) return -1;
|
||||
if (bigit_a > bigit_b) return +1;
|
||||
DOUBLE_CONVERSION_ASSERT(a.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(b.IsClamped());
|
||||
const int bigit_length_a = a.BigitLength();
|
||||
const int bigit_length_b = b.BigitLength();
|
||||
if (bigit_length_a < bigit_length_b) {
|
||||
return -1;
|
||||
}
|
||||
if (bigit_length_a > bigit_length_b) {
|
||||
return +1;
|
||||
}
|
||||
for (int i = bigit_length_a - 1; i >= (std::min)(a.exponent_, b.exponent_); --i) {
|
||||
const Chunk bigit_a = a.BigitOrZero(i);
|
||||
const Chunk bigit_b = b.BigitOrZero(i);
|
||||
if (bigit_a < bigit_b) {
|
||||
return -1;
|
||||
}
|
||||
if (bigit_a > bigit_b) {
|
||||
return +1;
|
||||
}
|
||||
// Otherwise they are equal up to this digit. Try the next digit.
|
||||
}
|
||||
return 0;
|
||||
@ -635,14 +667,18 @@ int Bignum::Compare(const Bignum& a, const Bignum& b) {
|
||||
|
||||
|
||||
int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
|
||||
ASSERT(a.IsClamped());
|
||||
ASSERT(b.IsClamped());
|
||||
ASSERT(c.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(a.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(b.IsClamped());
|
||||
DOUBLE_CONVERSION_ASSERT(c.IsClamped());
|
||||
if (a.BigitLength() < b.BigitLength()) {
|
||||
return PlusCompare(b, a, c);
|
||||
}
|
||||
if (a.BigitLength() + 1 < c.BigitLength()) return -1;
|
||||
if (a.BigitLength() > c.BigitLength()) return +1;
|
||||
if (a.BigitLength() + 1 < c.BigitLength()) {
|
||||
return -1;
|
||||
}
|
||||
if (a.BigitLength() > c.BigitLength()) {
|
||||
return +1;
|
||||
}
|
||||
// The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than
|
||||
// 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one
|
||||
// of 'a'.
|
||||
@ -652,92 +688,83 @@ int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) {
|
||||
|
||||
Chunk borrow = 0;
|
||||
// Starting at min_exponent all digits are == 0. So no need to compare them.
|
||||
int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_);
|
||||
const int min_exponent = (std::min)((std::min)(a.exponent_, b.exponent_), c.exponent_);
|
||||
for (int i = c.BigitLength() - 1; i >= min_exponent; --i) {
|
||||
Chunk chunk_a = a.BigitAt(i);
|
||||
Chunk chunk_b = b.BigitAt(i);
|
||||
Chunk chunk_c = c.BigitAt(i);
|
||||
Chunk sum = chunk_a + chunk_b;
|
||||
const Chunk chunk_a = a.BigitOrZero(i);
|
||||
const Chunk chunk_b = b.BigitOrZero(i);
|
||||
const Chunk chunk_c = c.BigitOrZero(i);
|
||||
const Chunk sum = chunk_a + chunk_b;
|
||||
if (sum > chunk_c + borrow) {
|
||||
return +1;
|
||||
} else {
|
||||
borrow = chunk_c + borrow - sum;
|
||||
if (borrow > 1) return -1;
|
||||
if (borrow > 1) {
|
||||
return -1;
|
||||
}
|
||||
borrow <<= kBigitSize;
|
||||
}
|
||||
}
|
||||
if (borrow == 0) return 0;
|
||||
if (borrow == 0) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Clamp() {
|
||||
while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) {
|
||||
used_digits_--;
|
||||
while (used_bigits_ > 0 && RawBigit(used_bigits_ - 1) == 0) {
|
||||
used_bigits_--;
|
||||
}
|
||||
if (used_digits_ == 0) {
|
||||
if (used_bigits_ == 0) {
|
||||
// Zero.
|
||||
exponent_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Bignum::IsClamped() const {
|
||||
return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Zero() {
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
bigits_[i] = 0;
|
||||
}
|
||||
used_digits_ = 0;
|
||||
exponent_ = 0;
|
||||
}
|
||||
|
||||
|
||||
void Bignum::Align(const Bignum& other) {
|
||||
if (exponent_ > other.exponent_) {
|
||||
// If "X" represents a "hidden" digit (by the exponent) then we are in the
|
||||
// If "X" represents a "hidden" bigit (by the exponent) then we are in the
|
||||
// following case (a == this, b == other):
|
||||
// a: aaaaaaXXXX or a: aaaaaXXX
|
||||
// b: bbbbbbX b: bbbbbbbbXX
|
||||
// We replace some of the hidden digits (X) of a with 0 digits.
|
||||
// a: aaaaaa000X or a: aaaaa0XX
|
||||
int zero_digits = exponent_ - other.exponent_;
|
||||
EnsureCapacity(used_digits_ + zero_digits);
|
||||
for (int i = used_digits_ - 1; i >= 0; --i) {
|
||||
bigits_[i + zero_digits] = bigits_[i];
|
||||
const int zero_bigits = exponent_ - other.exponent_;
|
||||
EnsureCapacity(used_bigits_ + zero_bigits);
|
||||
for (int i = used_bigits_ - 1; i >= 0; --i) {
|
||||
RawBigit(i + zero_bigits) = RawBigit(i);
|
||||
}
|
||||
for (int i = 0; i < zero_digits; ++i) {
|
||||
bigits_[i] = 0;
|
||||
for (int i = 0; i < zero_bigits; ++i) {
|
||||
RawBigit(i) = 0;
|
||||
}
|
||||
used_digits_ += zero_digits;
|
||||
exponent_ -= zero_digits;
|
||||
ASSERT(used_digits_ >= 0);
|
||||
ASSERT(exponent_ >= 0);
|
||||
used_bigits_ += zero_bigits;
|
||||
exponent_ -= zero_bigits;
|
||||
|
||||
DOUBLE_CONVERSION_ASSERT(used_bigits_ >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent_ >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::BigitsShiftLeft(int shift_amount) {
|
||||
ASSERT(shift_amount < kBigitSize);
|
||||
ASSERT(shift_amount >= 0);
|
||||
void Bignum::BigitsShiftLeft(const int shift_amount) {
|
||||
DOUBLE_CONVERSION_ASSERT(shift_amount < kBigitSize);
|
||||
DOUBLE_CONVERSION_ASSERT(shift_amount >= 0);
|
||||
Chunk carry = 0;
|
||||
for (int i = 0; i < used_digits_; ++i) {
|
||||
Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount);
|
||||
bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask;
|
||||
for (int i = 0; i < used_bigits_; ++i) {
|
||||
const Chunk new_carry = RawBigit(i) >> (kBigitSize - shift_amount);
|
||||
RawBigit(i) = ((RawBigit(i) << shift_amount) + carry) & kBigitMask;
|
||||
carry = new_carry;
|
||||
}
|
||||
if (carry != 0) {
|
||||
bigits_[used_digits_] = carry;
|
||||
used_digits_++;
|
||||
RawBigit(used_bigits_) = carry;
|
||||
used_bigits_++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Bignum::SubtractTimes(const Bignum& other, int factor) {
|
||||
ASSERT(exponent_ <= other.exponent_);
|
||||
void Bignum::SubtractTimes(const Bignum& other, const int factor) {
|
||||
DOUBLE_CONVERSION_ASSERT(exponent_ <= other.exponent_);
|
||||
if (factor < 3) {
|
||||
for (int i = 0; i < factor; ++i) {
|
||||
SubtractBignum(other);
|
||||
@ -745,19 +772,21 @@ void Bignum::SubtractTimes(const Bignum& other, int factor) {
|
||||
return;
|
||||
}
|
||||
Chunk borrow = 0;
|
||||
int exponent_diff = other.exponent_ - exponent_;
|
||||
for (int i = 0; i < other.used_digits_; ++i) {
|
||||
DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i];
|
||||
DoubleChunk remove = borrow + product;
|
||||
Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask);
|
||||
bigits_[i + exponent_diff] = difference & kBigitMask;
|
||||
const int exponent_diff = other.exponent_ - exponent_;
|
||||
for (int i = 0; i < other.used_bigits_; ++i) {
|
||||
const DoubleChunk product = static_cast<DoubleChunk>(factor) * other.RawBigit(i);
|
||||
const DoubleChunk remove = borrow + product;
|
||||
const Chunk difference = RawBigit(i + exponent_diff) - (remove & kBigitMask);
|
||||
RawBigit(i + exponent_diff) = difference & kBigitMask;
|
||||
borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) +
|
||||
(remove >> kBigitSize));
|
||||
}
|
||||
for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) {
|
||||
if (borrow == 0) return;
|
||||
Chunk difference = bigits_[i] - borrow;
|
||||
bigits_[i] = difference & kBigitMask;
|
||||
for (int i = other.used_bigits_ + exponent_diff; i < used_bigits_; ++i) {
|
||||
if (borrow == 0) {
|
||||
return;
|
||||
}
|
||||
const Chunk difference = RawBigit(i) - borrow;
|
||||
RawBigit(i) = difference & kBigitMask;
|
||||
borrow = difference >> (kChunkSize - 1);
|
||||
}
|
||||
Clamp();
|
||||
|
@ -39,26 +39,27 @@ class Bignum {
|
||||
// exponent.
|
||||
static const int kMaxSignificantBits = 3584;
|
||||
|
||||
Bignum();
|
||||
void AssignUInt16(uint16_t value);
|
||||
Bignum() : used_bigits_(0), exponent_(0) {}
|
||||
|
||||
void AssignUInt16(const uint16_t value);
|
||||
void AssignUInt64(uint64_t value);
|
||||
void AssignBignum(const Bignum& other);
|
||||
|
||||
void AssignDecimalString(Vector<const char> value);
|
||||
void AssignHexString(Vector<const char> value);
|
||||
void AssignDecimalString(const Vector<const char> value);
|
||||
void AssignHexString(const Vector<const char> value);
|
||||
|
||||
void AssignPowerUInt16(uint16_t base, int exponent);
|
||||
void AssignPowerUInt16(uint16_t base, const int exponent);
|
||||
|
||||
void AddUInt64(uint64_t operand);
|
||||
void AddUInt64(const uint64_t operand);
|
||||
void AddBignum(const Bignum& other);
|
||||
// Precondition: this >= other.
|
||||
void SubtractBignum(const Bignum& other);
|
||||
|
||||
void Square();
|
||||
void ShiftLeft(int shift_amount);
|
||||
void MultiplyByUInt32(uint32_t factor);
|
||||
void MultiplyByUInt64(uint64_t factor);
|
||||
void MultiplyByPowerOfTen(int exponent);
|
||||
void ShiftLeft(const int shift_amount);
|
||||
void MultiplyByUInt32(const uint32_t factor);
|
||||
void MultiplyByUInt64(const uint64_t factor);
|
||||
void MultiplyByPowerOfTen(const int exponent);
|
||||
void Times10() { return MultiplyByUInt32(10); }
|
||||
// Pseudocode:
|
||||
// int result = this / other;
|
||||
@ -66,7 +67,7 @@ class Bignum {
|
||||
// In the worst case this function is in O(this/other).
|
||||
uint16_t DivideModuloIntBignum(const Bignum& other);
|
||||
|
||||
bool ToHexString(char* buffer, int buffer_size) const;
|
||||
bool ToHexString(char* buffer, const int buffer_size) const;
|
||||
|
||||
// Returns
|
||||
// -1 if a < b,
|
||||
@ -110,33 +111,40 @@ class Bignum {
|
||||
// grow. There are no checks if the stack-allocated space is sufficient.
|
||||
static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
|
||||
|
||||
void EnsureCapacity(int size) {
|
||||
static void EnsureCapacity(const int size) {
|
||||
if (size > kBigitCapacity) {
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
void Align(const Bignum& other);
|
||||
void Clamp();
|
||||
bool IsClamped() const;
|
||||
void Zero();
|
||||
bool IsClamped() const {
|
||||
return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
|
||||
}
|
||||
void Zero() {
|
||||
used_bigits_ = 0;
|
||||
exponent_ = 0;
|
||||
}
|
||||
// Requires this to have enough capacity (no tests done).
|
||||
// Updates used_digits_ if necessary.
|
||||
// Updates used_bigits_ if necessary.
|
||||
// shift_amount must be < kBigitSize.
|
||||
void BigitsShiftLeft(int shift_amount);
|
||||
// BigitLength includes the "hidden" digits encoded in the exponent.
|
||||
int BigitLength() const { return used_digits_ + exponent_; }
|
||||
Chunk BigitAt(int index) const;
|
||||
void SubtractTimes(const Bignum& other, int factor);
|
||||
void BigitsShiftLeft(const int shift_amount);
|
||||
// BigitLength includes the "hidden" bigits encoded in the exponent.
|
||||
int BigitLength() const { return used_bigits_ + exponent_; }
|
||||
Chunk& RawBigit(const int index);
|
||||
const Chunk& RawBigit(const int index) const;
|
||||
Chunk BigitOrZero(const int index) const;
|
||||
void SubtractTimes(const Bignum& other, const int factor);
|
||||
|
||||
// The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
|
||||
// where the value of the buffer consists of the lower kBigitSize bits of
|
||||
// the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
|
||||
// significant bits.
|
||||
int16_t used_bigits_;
|
||||
int16_t exponent_;
|
||||
Chunk bigits_buffer_[kBigitCapacity];
|
||||
// A vector backed by bigits_buffer_. This way accesses to the array are
|
||||
// checked for out-of-bounds errors.
|
||||
Vector<Chunk> bigits_;
|
||||
int used_digits_;
|
||||
// The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
|
||||
int exponent_;
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Bignum);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -35,6 +35,8 @@
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
namespace PowersOfTenCache {
|
||||
|
||||
struct CachedPower {
|
||||
uint64_t significand;
|
||||
int16_t binary_exponent;
|
||||
@ -42,103 +44,99 @@ struct CachedPower {
|
||||
};
|
||||
|
||||
static const CachedPower kCachedPowers[] = {
|
||||
{UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348},
|
||||
{UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340},
|
||||
{UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332},
|
||||
{UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324},
|
||||
{UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316},
|
||||
{UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308},
|
||||
{UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300},
|
||||
{UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292},
|
||||
{UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284},
|
||||
{UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276},
|
||||
{UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268},
|
||||
{UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260},
|
||||
{UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252},
|
||||
{UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244},
|
||||
{UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236},
|
||||
{UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228},
|
||||
{UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220},
|
||||
{UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212},
|
||||
{UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204},
|
||||
{UINT64_2PART_C(0xef340a98, 172aace5), -715, -196},
|
||||
{UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188},
|
||||
{UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180},
|
||||
{UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172},
|
||||
{UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164},
|
||||
{UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156},
|
||||
{UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148},
|
||||
{UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140},
|
||||
{UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132},
|
||||
{UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124},
|
||||
{UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116},
|
||||
{UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108},
|
||||
{UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100},
|
||||
{UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92},
|
||||
{UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84},
|
||||
{UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76},
|
||||
{UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68},
|
||||
{UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60},
|
||||
{UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52},
|
||||
{UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44},
|
||||
{UINT64_2PART_C(0xaa242499, 697392d3), -183, -36},
|
||||
{UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28},
|
||||
{UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20},
|
||||
{UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12},
|
||||
{UINT64_2PART_C(0xd1b71758, e219652c), -77, -4},
|
||||
{UINT64_2PART_C(0x9c400000, 00000000), -50, 4},
|
||||
{UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12},
|
||||
{UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20},
|
||||
{UINT64_2PART_C(0x813f3978, f8940984), 30, 28},
|
||||
{UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36},
|
||||
{UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44},
|
||||
{UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52},
|
||||
{UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60},
|
||||
{UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68},
|
||||
{UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76},
|
||||
{UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84},
|
||||
{UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92},
|
||||
{UINT64_2PART_C(0x924d692c, a61be758), 269, 100},
|
||||
{UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108},
|
||||
{UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116},
|
||||
{UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124},
|
||||
{UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132},
|
||||
{UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140},
|
||||
{UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148},
|
||||
{UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156},
|
||||
{UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164},
|
||||
{UINT64_2PART_C(0xa59bc234, db398c25), 508, 172},
|
||||
{UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180},
|
||||
{UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188},
|
||||
{UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196},
|
||||
{UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204},
|
||||
{UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212},
|
||||
{UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220},
|
||||
{UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228},
|
||||
{UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236},
|
||||
{UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244},
|
||||
{UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252},
|
||||
{UINT64_2PART_C(0xd01fef10, a657842c), 800, 260},
|
||||
{UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268},
|
||||
{UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276},
|
||||
{UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284},
|
||||
{UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292},
|
||||
{UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300},
|
||||
{UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308},
|
||||
{UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316},
|
||||
{UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324},
|
||||
{UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332},
|
||||
{UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xef340a98, 172aace5), -715, -196},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaa242499, 697392d3), -183, -36},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd1b71758, e219652c), -77, -4},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50, 4},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x813f3978, f8940984), 30, 28},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x924d692c, a61be758), 269, 100},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa59bc234, db398c25), 508, 172},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd01fef10, a657842c), 800, 260},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332},
|
||||
{DOUBLE_CONVERSION_UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340},
|
||||
};
|
||||
|
||||
static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
|
||||
static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
|
||||
// Difference between the decimal exponents in the table above.
|
||||
const int PowersOfTenCache::kDecimalExponentDistance = 8;
|
||||
const int PowersOfTenCache::kMinDecimalExponent = -348;
|
||||
const int PowersOfTenCache::kMaxDecimalExponent = 340;
|
||||
|
||||
void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
|
||||
void GetCachedPowerForBinaryExponentRange(
|
||||
int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
@ -148,28 +146,30 @@ void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
|
||||
int foo = kCachedPowersOffset;
|
||||
int index =
|
||||
(foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
|
||||
ASSERT(0 <= index && index < static_cast<int>(ARRAY_SIZE(kCachedPowers)));
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= index && index < static_cast<int>(DOUBLE_CONVERSION_ARRAY_SIZE(kCachedPowers)));
|
||||
CachedPower cached_power = kCachedPowers[index];
|
||||
ASSERT(min_exponent <= cached_power.binary_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(min_exponent <= cached_power.binary_exponent);
|
||||
(void) max_exponent; // Mark variable as used.
|
||||
ASSERT(cached_power.binary_exponent <= max_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(cached_power.binary_exponent <= max_exponent);
|
||||
*decimal_exponent = cached_power.decimal_exponent;
|
||||
*power = DiyFp(cached_power.significand, cached_power.binary_exponent);
|
||||
}
|
||||
|
||||
|
||||
void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent) {
|
||||
ASSERT(kMinDecimalExponent <= requested_exponent);
|
||||
ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
|
||||
void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(kMinDecimalExponent <= requested_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
|
||||
int index =
|
||||
(requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance;
|
||||
CachedPower cached_power = kCachedPowers[index];
|
||||
*power = DiyFp(cached_power.significand, cached_power.binary_exponent);
|
||||
*found_exponent = cached_power.decimal_exponent;
|
||||
ASSERT(*found_exponent <= requested_exponent);
|
||||
ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance);
|
||||
DOUBLE_CONVERSION_ASSERT(*found_exponent <= requested_exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance);
|
||||
}
|
||||
|
||||
} // namespace PowersOfTenCache
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -32,32 +32,32 @@
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class PowersOfTenCache {
|
||||
public:
|
||||
namespace PowersOfTenCache {
|
||||
|
||||
// Not all powers of ten are cached. The decimal exponent of two neighboring
|
||||
// cached numbers will differ by kDecimalExponentDistance.
|
||||
static const int kDecimalExponentDistance;
|
||||
static const int kDecimalExponentDistance = 8;
|
||||
|
||||
static const int kMinDecimalExponent;
|
||||
static const int kMaxDecimalExponent;
|
||||
static const int kMinDecimalExponent = -348;
|
||||
static const int kMaxDecimalExponent = 340;
|
||||
|
||||
// Returns a cached power-of-ten with a binary exponent in the range
|
||||
// [min_exponent; max_exponent] (boundaries included).
|
||||
static void GetCachedPowerForBinaryExponentRange(int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
int* decimal_exponent);
|
||||
void GetCachedPowerForBinaryExponentRange(int min_exponent,
|
||||
int max_exponent,
|
||||
DiyFp* power,
|
||||
int* decimal_exponent);
|
||||
|
||||
// Returns a cached power of ten x ~= 10^k such that
|
||||
// k <= decimal_exponent < k + kCachedPowersDecimalDistance.
|
||||
// The given decimal_exponent must satisfy
|
||||
// kMinDecimalExponent <= requested_exponent, and
|
||||
// requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance.
|
||||
static void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent);
|
||||
};
|
||||
void GetCachedPowerForDecimalExponent(int requested_exponent,
|
||||
DiyFp* power,
|
||||
int* found_exponent);
|
||||
|
||||
} // namespace PowersOfTenCache
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
|
@ -1,57 +0,0 @@
|
||||
// Copyright 2010 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
#include "diy-fp.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
void DiyFp::Multiply(const DiyFp& other) {
|
||||
// Simply "emulates" a 128 bit multiplication.
|
||||
// However: the resulting number only contains 64 bits. The least
|
||||
// significant 64 bits are only used for rounding the most significant 64
|
||||
// bits.
|
||||
const uint64_t kM32 = 0xFFFFFFFFU;
|
||||
uint64_t a = f_ >> 32;
|
||||
uint64_t b = f_ & kM32;
|
||||
uint64_t c = other.f_ >> 32;
|
||||
uint64_t d = other.f_ & kM32;
|
||||
uint64_t ac = a * c;
|
||||
uint64_t bc = b * c;
|
||||
uint64_t ad = a * d;
|
||||
uint64_t bd = b * d;
|
||||
uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
|
||||
// By adding 1U << 31 to tmp we round the final result.
|
||||
// Halfway cases will be round up.
|
||||
tmp += 1U << 31;
|
||||
uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
|
||||
e_ += other.e_ + 64;
|
||||
f_ = result_f;
|
||||
}
|
||||
|
||||
} // namespace double_conversion
|
@ -36,36 +36,55 @@ namespace double_conversion {
|
||||
// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
|
||||
// have the most significant bit of the significand set.
|
||||
// Multiplication and Subtraction do not normalize their results.
|
||||
// DiyFp are not designed to contain special doubles (NaN and Infinity).
|
||||
// DiyFp store only non-negative numbers and are not designed to contain special
|
||||
// doubles (NaN and Infinity).
|
||||
class DiyFp {
|
||||
public:
|
||||
static const int kSignificandSize = 64;
|
||||
|
||||
DiyFp() : f_(0), e_(0) {}
|
||||
DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {}
|
||||
DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {}
|
||||
|
||||
// this = this - other.
|
||||
// this -= other.
|
||||
// The exponents of both numbers must be the same and the significand of this
|
||||
// must be bigger than the significand of other.
|
||||
// must be greater or equal than the significand of other.
|
||||
// The result will not be normalized.
|
||||
void Subtract(const DiyFp& other) {
|
||||
ASSERT(e_ == other.e_);
|
||||
ASSERT(f_ >= other.f_);
|
||||
DOUBLE_CONVERSION_ASSERT(e_ == other.e_);
|
||||
DOUBLE_CONVERSION_ASSERT(f_ >= other.f_);
|
||||
f_ -= other.f_;
|
||||
}
|
||||
|
||||
// Returns a - b.
|
||||
// The exponents of both numbers must be the same and this must be bigger
|
||||
// than other. The result will not be normalized.
|
||||
// The exponents of both numbers must be the same and a must be greater
|
||||
// or equal than b. The result will not be normalized.
|
||||
static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
|
||||
DiyFp result = a;
|
||||
result.Subtract(b);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// this = this * other.
|
||||
void Multiply(const DiyFp& other);
|
||||
// this *= other.
|
||||
void Multiply(const DiyFp& other) {
|
||||
// Simply "emulates" a 128 bit multiplication.
|
||||
// However: the resulting number only contains 64 bits. The least
|
||||
// significant 64 bits are only used for rounding the most significant 64
|
||||
// bits.
|
||||
const uint64_t kM32 = 0xFFFFFFFFU;
|
||||
const uint64_t a = f_ >> 32;
|
||||
const uint64_t b = f_ & kM32;
|
||||
const uint64_t c = other.f_ >> 32;
|
||||
const uint64_t d = other.f_ & kM32;
|
||||
const uint64_t ac = a * c;
|
||||
const uint64_t bc = b * c;
|
||||
const uint64_t ad = a * d;
|
||||
const uint64_t bd = b * d;
|
||||
// By adding 1U << 31 to tmp we round the final result.
|
||||
// Halfway cases will be rounded up.
|
||||
const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31);
|
||||
e_ += other.e_ + 64;
|
||||
f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
|
||||
}
|
||||
|
||||
// returns a * b;
|
||||
static DiyFp Times(const DiyFp& a, const DiyFp& b) {
|
||||
@ -75,13 +94,13 @@ class DiyFp {
|
||||
}
|
||||
|
||||
void Normalize() {
|
||||
ASSERT(f_ != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(f_ != 0);
|
||||
uint64_t significand = f_;
|
||||
int exponent = e_;
|
||||
int32_t exponent = e_;
|
||||
|
||||
// This method is mainly called for normalizing boundaries. In general
|
||||
// boundaries need to be shifted by 10 bits. We thus optimize for this case.
|
||||
const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
|
||||
// This method is mainly called for normalizing boundaries. In general,
|
||||
// boundaries need to be shifted by 10 bits, and we optimize for this case.
|
||||
const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000);
|
||||
while ((significand & k10MSBits) == 0) {
|
||||
significand <<= 10;
|
||||
exponent -= 10;
|
||||
@ -101,16 +120,16 @@ class DiyFp {
|
||||
}
|
||||
|
||||
uint64_t f() const { return f_; }
|
||||
int e() const { return e_; }
|
||||
int32_t e() const { return e_; }
|
||||
|
||||
void set_f(uint64_t new_value) { f_ = new_value; }
|
||||
void set_e(int new_value) { e_ = new_value; }
|
||||
void set_e(int32_t new_value) { e_ = new_value; }
|
||||
|
||||
private:
|
||||
static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
|
||||
|
||||
uint64_t f_;
|
||||
int e_;
|
||||
int32_t e_;
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -28,549 +28,7 @@
|
||||
#ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
|
||||
#define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class DoubleToStringConverter {
|
||||
public:
|
||||
// When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
|
||||
// or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
|
||||
// function returns false.
|
||||
static const int kMaxFixedDigitsBeforePoint = 60;
|
||||
static const int kMaxFixedDigitsAfterPoint = 60;
|
||||
|
||||
// When calling ToExponential with a requested_digits
|
||||
// parameter > kMaxExponentialDigits then the function returns false.
|
||||
static const int kMaxExponentialDigits = 120;
|
||||
|
||||
// When calling ToPrecision with a requested_digits
|
||||
// parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
|
||||
// then the function returns false.
|
||||
static const int kMinPrecisionDigits = 1;
|
||||
static const int kMaxPrecisionDigits = 120;
|
||||
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
EMIT_POSITIVE_EXPONENT_SIGN = 1,
|
||||
EMIT_TRAILING_DECIMAL_POINT = 2,
|
||||
EMIT_TRAILING_ZERO_AFTER_POINT = 4,
|
||||
UNIQUE_ZERO = 8
|
||||
};
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
|
||||
// form, emits a '+' for positive exponents. Example: 1.2e+2.
|
||||
// - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
|
||||
// converted into decimal format then a trailing decimal point is appended.
|
||||
// Example: 2345.0 is converted to "2345.".
|
||||
// - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
|
||||
// emits a trailing '0'-character. This flag requires the
|
||||
// EXMIT_TRAILING_DECIMAL_POINT flag.
|
||||
// Example: 2345.0 is converted to "2345.0".
|
||||
// - UNIQUE_ZERO: "-0.0" is converted to "0.0".
|
||||
//
|
||||
// Infinity symbol and nan_symbol provide the string representation for these
|
||||
// special values. If the string is NULL and the special value is encountered
|
||||
// then the conversion functions return false.
|
||||
//
|
||||
// The exponent_character is used in exponential representations. It is
|
||||
// usually 'e' or 'E'.
|
||||
//
|
||||
// When converting to the shortest representation the converter will
|
||||
// represent input numbers in decimal format if they are in the interval
|
||||
// [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
|
||||
// (lower boundary included, greater boundary excluded).
|
||||
// Example: with decimal_in_shortest_low = -6 and
|
||||
// decimal_in_shortest_high = 21:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// When converting to precision mode the converter may add
|
||||
// max_leading_padding_zeroes before returning the number in exponential
|
||||
// format.
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
DoubleToStringConverter(int flags,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
char exponent_character,
|
||||
int decimal_in_shortest_low,
|
||||
int decimal_in_shortest_high,
|
||||
int max_leading_padding_zeroes_in_precision_mode,
|
||||
int max_trailing_padding_zeroes_in_precision_mode)
|
||||
: flags_(flags),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
exponent_character_(exponent_character),
|
||||
decimal_in_shortest_low_(decimal_in_shortest_low),
|
||||
decimal_in_shortest_high_(decimal_in_shortest_high),
|
||||
max_leading_padding_zeroes_in_precision_mode_(
|
||||
max_leading_padding_zeroes_in_precision_mode),
|
||||
max_trailing_padding_zeroes_in_precision_mode_(
|
||||
max_trailing_padding_zeroes_in_precision_mode) {
|
||||
// When 'trailing zero after the point' is set, then 'trailing point'
|
||||
// must be set too.
|
||||
ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
|
||||
!((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
|
||||
}
|
||||
|
||||
// Returns a converter following the EcmaScript specification.
|
||||
static const DoubleToStringConverter& EcmaScriptConverter();
|
||||
|
||||
// Computes the shortest string of digits that correctly represent the input
|
||||
// number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
|
||||
// (see constructor) it then either returns a decimal representation, or an
|
||||
// exponential representation.
|
||||
// Example with decimal_in_shortest_low = -6,
|
||||
// decimal_in_shortest_high = 21,
|
||||
// EMIT_POSITIVE_EXPONENT_SIGN activated, and
|
||||
// EMIT_TRAILING_DECIMAL_POINT deactived:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// Note: the conversion may round the output if the returned string
|
||||
// is accurate enough to uniquely identify the input-number.
|
||||
// For example the most precise representation of the double 9e59 equals
|
||||
// "899999999999999918767229449717619953810131273674690656206848", but
|
||||
// the converter will return the shorter (but still correct) "9e59".
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except when the input value is special and no infinity_symbol or
|
||||
// nan_symbol has been given to the constructor.
|
||||
bool ToShortest(double value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
|
||||
}
|
||||
|
||||
// Same as ToShortest, but for single-precision floats.
|
||||
bool ToShortestSingle(float value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
|
||||
}
|
||||
|
||||
|
||||
// Computes a decimal representation with a fixed number of digits after the
|
||||
// decimal point. The last emitted digit is rounded.
|
||||
//
|
||||
// Examples:
|
||||
// ToFixed(3.12, 1) -> "3.1"
|
||||
// ToFixed(3.1415, 3) -> "3.142"
|
||||
// ToFixed(1234.56789, 4) -> "1234.5679"
|
||||
// ToFixed(1.23, 5) -> "1.23000"
|
||||
// ToFixed(0.1, 4) -> "0.1000"
|
||||
// ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
|
||||
// ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
|
||||
// ToFixed(0.1, 17) -> "0.10000000000000001"
|
||||
//
|
||||
// If requested_digits equals 0, then the tail of the result depends on
|
||||
// the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples, for requested_digits == 0,
|
||||
// let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
|
||||
// - false and false: then 123.45 -> 123
|
||||
// 0.678 -> 1
|
||||
// - true and false: then 123.45 -> 123.
|
||||
// 0.678 -> 1.
|
||||
// - true and true: then 123.45 -> 123.0
|
||||
// 0.678 -> 1.0
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'value' > 10^kMaxFixedDigitsBeforePoint, or
|
||||
// - 'requested_digits' > kMaxFixedDigitsAfterPoint.
|
||||
// The last two conditions imply that the result will never contain more than
|
||||
// 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
|
||||
// (one additional character for the sign, and one for the decimal point).
|
||||
bool ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes a representation in exponential format with requested_digits
|
||||
// after the decimal point. The last emitted digit is rounded.
|
||||
// If requested_digits equals -1, then the shortest exponential representation
|
||||
// is computed.
|
||||
//
|
||||
// Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
|
||||
// exponent_character set to 'e'.
|
||||
// ToExponential(3.12, 1) -> "3.1e0"
|
||||
// ToExponential(5.0, 3) -> "5.000e0"
|
||||
// ToExponential(0.001, 2) -> "1.00e-3"
|
||||
// ToExponential(3.1415, -1) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 4) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 3) -> "3.142e0"
|
||||
// ToExponential(123456789000000, 3) -> "1.235e14"
|
||||
// ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
|
||||
// ToExponential(1000000000000000019884624838656.0, 32) ->
|
||||
// "1.00000000000000001988462483865600e30"
|
||||
// ToExponential(1234, 0) -> "1e3"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'requested_digits' > kMaxExponentialDigits.
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxExponentialDigits + 8 characters (the sign, the digit before the
|
||||
// decimal point, the decimal point, the exponent character, the
|
||||
// exponent's sign, and at most 3 exponent digits).
|
||||
bool ToExponential(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes 'precision' leading digits of the given 'value' and returns them
|
||||
// either in exponential or decimal format, depending on
|
||||
// max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
|
||||
// constructor).
|
||||
// The last computed digit is rounded.
|
||||
//
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT:
|
||||
// ToPrecision(123450.0, 6) -> "123450"
|
||||
// ToPrecision(123450.0, 5) -> "123450"
|
||||
// ToPrecision(123450.0, 4) -> "123500"
|
||||
// ToPrecision(123450.0, 3) -> "123000"
|
||||
// ToPrecision(123450.0, 2) -> "1.2e5"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - precision < kMinPericisionDigits
|
||||
// - precision > kMaxPrecisionDigits
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
|
||||
// exponent character, the exponent's sign, and at most 3 exponent digits).
|
||||
bool ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
enum DtoaMode {
|
||||
// Produce the shortest correct representation.
|
||||
// For example the output of 0.299999999999999988897 is (the less accurate
|
||||
// but correct) 0.3.
|
||||
SHORTEST,
|
||||
// Same as SHORTEST, but for single-precision floats.
|
||||
SHORTEST_SINGLE,
|
||||
// Produce a fixed number of digits after the decimal point.
|
||||
// For instance fixed(0.1, 4) becomes 0.1000
|
||||
// If the input number is big, the output will be big.
|
||||
FIXED,
|
||||
// Fixed number of digits (independent of the decimal point).
|
||||
PRECISION
|
||||
};
|
||||
|
||||
// The maximal number of digits that are needed to emit a double in base 10.
|
||||
// A higher precision can be achieved by using more digits, but the shortest
|
||||
// accurate representation of any double will never use more digits than
|
||||
// kBase10MaximalLength.
|
||||
// Note that DoubleToAscii null-terminates its input. So the given buffer
|
||||
// should be at least kBase10MaximalLength + 1 characters long.
|
||||
static const int kBase10MaximalLength = 17;
|
||||
|
||||
// Converts the given double 'v' to digit characters. 'v' must not be NaN,
|
||||
// +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
|
||||
// applies to 'v' after it has been casted to a single-precision float. That
|
||||
// is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
|
||||
// -Infinity.
|
||||
//
|
||||
// The result should be interpreted as buffer * 10^(point-length).
|
||||
//
|
||||
// The digits are written to the buffer in the platform's charset, which is
|
||||
// often UTF-8 (with ASCII-range digits) but may be another charset, such
|
||||
// as EBCDIC.
|
||||
//
|
||||
// The output depends on the given mode:
|
||||
// - SHORTEST: produce the least amount of digits for which the internal
|
||||
// identity requirement is still satisfied. If the digits are printed
|
||||
// (together with the correct exponent) then reading this number will give
|
||||
// 'v' again. The buffer will choose the representation that is closest to
|
||||
// 'v'. If there are two at the same distance, than the one farther away
|
||||
// from 0 is chosen (halfway cases - ending with 5 - are rounded up).
|
||||
// In this mode the 'requested_digits' parameter is ignored.
|
||||
// - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
|
||||
// - FIXED: produces digits necessary to print a given number with
|
||||
// 'requested_digits' digits after the decimal point. The produced digits
|
||||
// might be too short in which case the caller has to fill the remainder
|
||||
// with '0's.
|
||||
// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
|
||||
// Halfway cases are rounded towards +/-Infinity (away from 0). The call
|
||||
// toFixed(0.15, 2) thus returns buffer="2", point=0.
|
||||
// The returned buffer may contain digits that would be truncated from the
|
||||
// shortest representation of the input.
|
||||
// - PRECISION: produces 'requested_digits' where the first digit is not '0'.
|
||||
// Even though the length of produced digits usually equals
|
||||
// 'requested_digits', the function is allowed to return fewer digits, in
|
||||
// which case the caller has to fill the missing digits with '0's.
|
||||
// Halfway cases are again rounded away from 0.
|
||||
// DoubleToAscii expects the given buffer to be big enough to hold all
|
||||
// digits and a terminating null-character. In SHORTEST-mode it expects a
|
||||
// buffer of at least kBase10MaximalLength + 1. In all other modes the
|
||||
// requested_digits parameter and the padding-zeroes limit the size of the
|
||||
// output. Don't forget the decimal point, the exponent character and the
|
||||
// terminating null-character when computing the maximal output size.
|
||||
// The given length is only used in debug mode to ensure the buffer is big
|
||||
// enough.
|
||||
static void DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point);
|
||||
|
||||
private:
|
||||
// Implementation for ToShortest and ToShortestSingle.
|
||||
bool ToShortestIeeeNumber(double value,
|
||||
StringBuilder* result_builder,
|
||||
DtoaMode mode) const;
|
||||
|
||||
// If the value is a special value (NaN or Infinity) constructs the
|
||||
// corresponding string using the configured infinity/nan-symbol.
|
||||
// If either of them is NULL or the value is not special then the
|
||||
// function returns false.
|
||||
bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
|
||||
// Constructs an exponential representation (i.e. 1.234e56).
|
||||
// The given exponent assumes a decimal point after the first decimal digit.
|
||||
void CreateExponentialRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const;
|
||||
// Creates a decimal representation (i.e 1234.5678).
|
||||
void CreateDecimalRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
const int flags_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const char exponent_character_;
|
||||
const int decimal_in_shortest_low_;
|
||||
const int decimal_in_shortest_high_;
|
||||
const int max_leading_padding_zeroes_in_precision_mode_;
|
||||
const int max_trailing_padding_zeroes_in_precision_mode_;
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
|
||||
};
|
||||
|
||||
|
||||
class StringToDoubleConverter {
|
||||
public:
|
||||
// Enumeration for allowing octals and ignoring junk when converting
|
||||
// strings to numbers.
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
ALLOW_HEX = 1,
|
||||
ALLOW_OCTALS = 2,
|
||||
ALLOW_TRAILING_JUNK = 4,
|
||||
ALLOW_LEADING_SPACES = 8,
|
||||
ALLOW_TRAILING_SPACES = 16,
|
||||
ALLOW_SPACES_AFTER_SIGN = 32,
|
||||
ALLOW_CASE_INSENSIBILITY = 64,
|
||||
ALLOW_HEX_FLOATS = 128,
|
||||
};
|
||||
|
||||
static const uc16 kNoSeparator = '\0';
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
|
||||
// Ex: StringToDouble("0x1234") -> 4660.0
|
||||
// In StringToDouble("0x1234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
|
||||
// the string will not be parsed as "0" followed by junk.
|
||||
//
|
||||
// - ALLOW_OCTALS: recognizes the prefix "0" for octals:
|
||||
// If a sequence of octal digits starts with '0', then the number is
|
||||
// read as octal integer. Octal numbers may only be integers.
|
||||
// Ex: StringToDouble("01234") -> 668.0
|
||||
// StringToDouble("012349") -> 12349.0 // Not a sequence of octal
|
||||
// // digits.
|
||||
// In StringToDouble("01234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// In StringToDouble("01234e56") the characters "e56" are trailing
|
||||
// junk, too.
|
||||
// - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
|
||||
// a double literal.
|
||||
// - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
|
||||
// new-lines, and tabs.
|
||||
// - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
|
||||
// - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
|
||||
// Ex: StringToDouble("- 123.2") -> -123.2.
|
||||
// StringToDouble("+ 123.2") -> 123.2
|
||||
// - ALLOW_CASE_INSENSIBILITY: ignore case of characters for special values:
|
||||
// infinity and nan.
|
||||
// - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
|
||||
// This *must* start with "0x" and separate the exponent with "p".
|
||||
// Examples: 0x1.2p3 == 9.0
|
||||
// 0x10.1p0 == 16.0625
|
||||
// ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
|
||||
//
|
||||
// empty_string_value is returned when an empty string is given as input.
|
||||
// If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
|
||||
// containing only spaces is converted to the 'empty_string_value', too.
|
||||
//
|
||||
// junk_string_value is returned when
|
||||
// a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
|
||||
// part of a double-literal) is found.
|
||||
// b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
|
||||
// double literal.
|
||||
//
|
||||
// infinity_symbol and nan_symbol are strings that are used to detect
|
||||
// inputs that represent infinity and NaN. They can be null, in which case
|
||||
// they are ignored.
|
||||
// The conversion routine first reads any possible signs. Then it compares the
|
||||
// following character of the input-string with the first character of
|
||||
// the infinity, and nan-symbol. If either matches, the function assumes, that
|
||||
// a match has been found, and expects the following input characters to match
|
||||
// the remaining characters of the special-value symbol.
|
||||
// This means that the following restrictions apply to special-value symbols:
|
||||
// - they must not start with signs ('+', or '-'),
|
||||
// - they must not have the same first character.
|
||||
// - they must not start with digits.
|
||||
//
|
||||
// If the separator character is not kNoSeparator, then that specific
|
||||
// character is ignored when in between two valid digits of the significant.
|
||||
// It is not allowed to appear in the exponent.
|
||||
// It is not allowed to lead or trail the number.
|
||||
// It is not allowed to appear twice next to each other.
|
||||
//
|
||||
// Examples:
|
||||
// flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = "infinity",
|
||||
// nan_symbol = "nan":
|
||||
// StringToDouble("0x1234") -> 4660.0.
|
||||
// StringToDouble("0x1234K") -> 4660.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> NaN // junk_string_value.
|
||||
// StringToDouble(" 1") -> NaN // junk_string_value.
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("-123.45") -> -123.45.
|
||||
// StringToDouble("--123.45") -> NaN // junk_string_value.
|
||||
// StringToDouble("123e45") -> 123e45.
|
||||
// StringToDouble("123E45") -> 123e45.
|
||||
// StringToDouble("123e+45") -> 123e45.
|
||||
// StringToDouble("123E-45") -> 123e-45.
|
||||
// StringToDouble("123e") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("123e-") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("+NaN") -> NaN // NaN string literal.
|
||||
// StringToDouble("-infinity") -> -inf. // infinity literal.
|
||||
// StringToDouble("Infinity") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = NULL,
|
||||
// nan_symbol = NULL:
|
||||
// StringToDouble("0x1234") -> NaN // junk_string_value.
|
||||
// StringToDouble("01234") -> 668.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" 1") -> 1.0
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("0123e45") -> NaN // junk_string_value.
|
||||
// StringToDouble("01239E45") -> 1239e45.
|
||||
// StringToDouble("-infinity") -> NaN // junk_string_value.
|
||||
// StringToDouble("NaN") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = NO_FLAGS,
|
||||
// separator = ' ':
|
||||
// StringToDouble("1 2 3 4") -> 1234.0
|
||||
// StringToDouble("1 2") -> NaN // junk_string_value
|
||||
// StringToDouble("1 000 000.0") -> 1000000.0
|
||||
// StringToDouble("1.000 000") -> 1.0
|
||||
// StringToDouble("1.0e1 000") -> NaN // junk_string_value
|
||||
StringToDoubleConverter(int flags,
|
||||
double empty_string_value,
|
||||
double junk_string_value,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
uc16 separator = kNoSeparator)
|
||||
: flags_(flags),
|
||||
empty_string_value_(empty_string_value),
|
||||
junk_string_value_(junk_string_value),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
separator_(separator) {
|
||||
}
|
||||
|
||||
// Performs the conversion.
|
||||
// The output parameter 'processed_characters_count' is set to the number
|
||||
// of characters that have been processed to read the number.
|
||||
// Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
|
||||
// in the 'processed_characters_count'. Trailing junk is never included.
|
||||
double StringToDouble(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble above but for 16 bit characters.
|
||||
double StringToDouble(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble but reads a float.
|
||||
// Note that this is not equivalent to static_cast<float>(StringToDouble(...))
|
||||
// due to potential double-rounding.
|
||||
float StringToFloat(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToFloat above but for 16 bit characters.
|
||||
float StringToFloat(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
private:
|
||||
const int flags_;
|
||||
const double empty_string_value_;
|
||||
const double junk_string_value_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const uc16 separator_;
|
||||
|
||||
template <class Iterator>
|
||||
double StringToIeee(Iterator start_pointer,
|
||||
int length,
|
||||
bool read_as_double,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
#include "string-to-double.h"
|
||||
#include "double-to-string.h"
|
||||
|
||||
#endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
|
||||
|
428
vendor/double-conversion/upstream/double-conversion/double-to-string.cc
vendored
Normal file
428
vendor/double-conversion/upstream/double-conversion/double-to-string.cc
vendored
Normal file
@ -0,0 +1,428 @@
|
||||
// Copyright 2010 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
|
||||
#include "double-to-string.h"
|
||||
|
||||
#include "bignum-dtoa.h"
|
||||
#include "fast-dtoa.h"
|
||||
#include "fixed-dtoa.h"
|
||||
#include "ieee.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
|
||||
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
|
||||
static DoubleToStringConverter converter(flags,
|
||||
"Infinity",
|
||||
"NaN",
|
||||
'e',
|
||||
-6, 21,
|
||||
6, 0);
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::HandleSpecialValues(
|
||||
double value,
|
||||
StringBuilder* result_builder) const {
|
||||
Double double_inspect(value);
|
||||
if (double_inspect.IsInfinite()) {
|
||||
if (infinity_symbol_ == NULL) return false;
|
||||
if (value < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
result_builder->AddString(infinity_symbol_);
|
||||
return true;
|
||||
}
|
||||
if (double_inspect.IsNan()) {
|
||||
if (nan_symbol_ == NULL) return false;
|
||||
result_builder->AddString(nan_symbol_);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateExponentialRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const {
|
||||
DOUBLE_CONVERSION_ASSERT(length != 0);
|
||||
result_builder->AddCharacter(decimal_digits[0]);
|
||||
if (length != 1) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddSubstring(&decimal_digits[1], length-1);
|
||||
}
|
||||
result_builder->AddCharacter(exponent_character_);
|
||||
if (exponent < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
exponent = -exponent;
|
||||
} else {
|
||||
if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
|
||||
result_builder->AddCharacter('+');
|
||||
}
|
||||
}
|
||||
if (exponent == 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
return;
|
||||
}
|
||||
DOUBLE_CONVERSION_ASSERT(exponent < 1e4);
|
||||
// Changing this constant requires updating the comment of DoubleToStringConverter constructor
|
||||
const int kMaxExponentLength = 5;
|
||||
char buffer[kMaxExponentLength + 1];
|
||||
buffer[kMaxExponentLength] = '\0';
|
||||
int first_char_pos = kMaxExponentLength;
|
||||
while (exponent > 0) {
|
||||
buffer[--first_char_pos] = '0' + (exponent % 10);
|
||||
exponent /= 10;
|
||||
}
|
||||
// Add prefix '0' to make exponent width >= min(min_exponent_with_, kMaxExponentLength)
|
||||
// For example: convert 1e+9 -> 1e+09, if min_exponent_with_ is set to 2
|
||||
while(kMaxExponentLength - first_char_pos < std::min(min_exponent_width_, kMaxExponentLength)) {
|
||||
buffer[--first_char_pos] = '0';
|
||||
}
|
||||
result_builder->AddSubstring(&buffer[first_char_pos],
|
||||
kMaxExponentLength - first_char_pos);
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateDecimalRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const {
|
||||
// Create a representation that is padded with zeros if needed.
|
||||
if (decimal_point <= 0) {
|
||||
// "0.00000decimal_rep" or "0.000decimal_rep00".
|
||||
result_builder->AddCharacter('0');
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', -decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(length <= digits_after_point - (-decimal_point));
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
int remaining_digits = digits_after_point - (-decimal_point) - length;
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
} else if (decimal_point >= length) {
|
||||
// "decimal_rep0000.00000" or "decimal_rep.0000".
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
result_builder->AddPadding('0', decimal_point - length);
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', digits_after_point);
|
||||
}
|
||||
} else {
|
||||
// "decima.l_rep000".
|
||||
DOUBLE_CONVERSION_ASSERT(digits_after_point > 0);
|
||||
result_builder->AddSubstring(decimal_digits, decimal_point);
|
||||
result_builder->AddCharacter('.');
|
||||
DOUBLE_CONVERSION_ASSERT(length - decimal_point <= digits_after_point);
|
||||
result_builder->AddSubstring(&decimal_digits[decimal_point],
|
||||
length - decimal_point);
|
||||
int remaining_digits = digits_after_point - (length - decimal_point);
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
if (digits_after_point == 0) {
|
||||
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
}
|
||||
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToShortestIeeeNumber(
|
||||
double value,
|
||||
StringBuilder* result_builder,
|
||||
DoubleToStringConverter::DtoaMode mode) const {
|
||||
DOUBLE_CONVERSION_ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE);
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
const int kDecimalRepCapacity = kBase10MaximalLength + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
if ((decimal_in_shortest_low_ <= exponent) &&
|
||||
(exponent < decimal_in_shortest_high_)) {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
|
||||
decimal_point,
|
||||
(std::max)(0, decimal_rep_length - decimal_point),
|
||||
result_builder);
|
||||
} else {
|
||||
CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
DOUBLE_CONVERSION_ASSERT(kMaxFixedDigitsBeforePoint == 60);
|
||||
const double kFirstNonFixed = 1e60;
|
||||
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
|
||||
if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for the '\0' byte.
|
||||
const int kDecimalRepCapacity =
|
||||
kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
DoubleToAscii(value, FIXED, requested_digits,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
requested_digits, result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToExponential(
|
||||
double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits < -1) return false;
|
||||
if (requested_digits > kMaxExponentialDigits) return false;
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for digit before the decimal point and the '\0' character.
|
||||
const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
|
||||
DOUBLE_CONVERSION_ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
#ifndef NDEBUG
|
||||
// Problem: there is an assert in StringBuilder::AddSubstring() that
|
||||
// will pass this buffer to strlen(), and this buffer is not generally
|
||||
// null-terminated.
|
||||
memset(decimal_rep, 0, sizeof(decimal_rep));
|
||||
#endif
|
||||
int decimal_rep_length;
|
||||
|
||||
if (requested_digits == -1) {
|
||||
DoubleToAscii(value, SHORTEST, 0,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
} else {
|
||||
DoubleToAscii(value, PRECISION, requested_digits + 1,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= requested_digits + 1);
|
||||
|
||||
for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
decimal_rep_length = requested_digits + 1;
|
||||
}
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
decimal_rep_length,
|
||||
exponent,
|
||||
result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add one for the terminating null character.
|
||||
const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, PRECISION, precision,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
DOUBLE_CONVERSION_ASSERT(decimal_rep_length <= precision);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
// The exponent if we print the number as x.xxeyyy. That is with the
|
||||
// decimal point after the first digit.
|
||||
int exponent = decimal_point - 1;
|
||||
|
||||
int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
|
||||
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
|
||||
(decimal_point - precision + extra_zero >
|
||||
max_trailing_padding_zeroes_in_precision_mode_)) {
|
||||
// Fill buffer to contain 'precision' digits.
|
||||
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
|
||||
// is allowed to return less characters.
|
||||
for (int i = decimal_rep_length; i < precision; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
precision,
|
||||
exponent,
|
||||
result_builder);
|
||||
} else {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
(std::max)(0, precision - decimal_point),
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static BignumDtoaMode DtoaToBignumDtoaMode(
|
||||
DoubleToStringConverter::DtoaMode dtoa_mode) {
|
||||
switch (dtoa_mode) {
|
||||
case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
|
||||
case DoubleToStringConverter::SHORTEST_SINGLE:
|
||||
return BIGNUM_DTOA_SHORTEST_SINGLE;
|
||||
case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
|
||||
case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
|
||||
default:
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point) {
|
||||
Vector<char> vector(buffer, buffer_length);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
|
||||
|
||||
if (Double(v).Sign() < 0) {
|
||||
*sign = true;
|
||||
v = -v;
|
||||
} else {
|
||||
*sign = false;
|
||||
}
|
||||
|
||||
if (mode == PRECISION && requested_digits == 0) {
|
||||
vector[0] = '\0';
|
||||
*length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (v == 0) {
|
||||
vector[0] = '0';
|
||||
vector[1] = '\0';
|
||||
*length = 1;
|
||||
*point = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
bool fast_worked;
|
||||
switch (mode) {
|
||||
case SHORTEST:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
|
||||
break;
|
||||
case SHORTEST_SINGLE:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
|
||||
vector, length, point);
|
||||
break;
|
||||
case FIXED:
|
||||
fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
|
||||
break;
|
||||
case PRECISION:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
||||
vector, length, point);
|
||||
break;
|
||||
default:
|
||||
fast_worked = false;
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
if (fast_worked) return;
|
||||
|
||||
// If the fast dtoa didn't succeed use the slower bignum version.
|
||||
BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
|
||||
BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
|
||||
vector[*length] = '\0';
|
||||
}
|
||||
|
||||
} // namespace double_conversion
|
396
vendor/double-conversion/upstream/double-conversion/double-to-string.h
vendored
Normal file
396
vendor/double-conversion/upstream/double-conversion/double-to-string.h
vendored
Normal file
@ -0,0 +1,396 @@
|
||||
// Copyright 2012 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
||||
#define DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class DoubleToStringConverter {
|
||||
public:
|
||||
// When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
|
||||
// or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
|
||||
// function returns false.
|
||||
static const int kMaxFixedDigitsBeforePoint = 60;
|
||||
static const int kMaxFixedDigitsAfterPoint = 60;
|
||||
|
||||
// When calling ToExponential with a requested_digits
|
||||
// parameter > kMaxExponentialDigits then the function returns false.
|
||||
static const int kMaxExponentialDigits = 120;
|
||||
|
||||
// When calling ToPrecision with a requested_digits
|
||||
// parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
|
||||
// then the function returns false.
|
||||
static const int kMinPrecisionDigits = 1;
|
||||
static const int kMaxPrecisionDigits = 120;
|
||||
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
EMIT_POSITIVE_EXPONENT_SIGN = 1,
|
||||
EMIT_TRAILING_DECIMAL_POINT = 2,
|
||||
EMIT_TRAILING_ZERO_AFTER_POINT = 4,
|
||||
UNIQUE_ZERO = 8
|
||||
};
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
|
||||
// form, emits a '+' for positive exponents. Example: 1.2e+2.
|
||||
// - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
|
||||
// converted into decimal format then a trailing decimal point is appended.
|
||||
// Example: 2345.0 is converted to "2345.".
|
||||
// - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
|
||||
// emits a trailing '0'-character. This flag requires the
|
||||
// EXMIT_TRAILING_DECIMAL_POINT flag.
|
||||
// Example: 2345.0 is converted to "2345.0".
|
||||
// - UNIQUE_ZERO: "-0.0" is converted to "0.0".
|
||||
//
|
||||
// Infinity symbol and nan_symbol provide the string representation for these
|
||||
// special values. If the string is NULL and the special value is encountered
|
||||
// then the conversion functions return false.
|
||||
//
|
||||
// The exponent_character is used in exponential representations. It is
|
||||
// usually 'e' or 'E'.
|
||||
//
|
||||
// When converting to the shortest representation the converter will
|
||||
// represent input numbers in decimal format if they are in the interval
|
||||
// [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
|
||||
// (lower boundary included, greater boundary excluded).
|
||||
// Example: with decimal_in_shortest_low = -6 and
|
||||
// decimal_in_shortest_high = 21:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// When converting to precision mode the converter may add
|
||||
// max_leading_padding_zeroes before returning the number in exponential
|
||||
// format.
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
//
|
||||
// The min_exponent_width is used for exponential representations.
|
||||
// The converter adds leading '0's to the exponent until the exponent
|
||||
// is at least min_exponent_width digits long.
|
||||
// The min_exponent_width is clamped to 5.
|
||||
// As such, the exponent may never have more than 5 digits in total.
|
||||
DoubleToStringConverter(int flags,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
char exponent_character,
|
||||
int decimal_in_shortest_low,
|
||||
int decimal_in_shortest_high,
|
||||
int max_leading_padding_zeroes_in_precision_mode,
|
||||
int max_trailing_padding_zeroes_in_precision_mode,
|
||||
int min_exponent_width = 0)
|
||||
: flags_(flags),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
exponent_character_(exponent_character),
|
||||
decimal_in_shortest_low_(decimal_in_shortest_low),
|
||||
decimal_in_shortest_high_(decimal_in_shortest_high),
|
||||
max_leading_padding_zeroes_in_precision_mode_(
|
||||
max_leading_padding_zeroes_in_precision_mode),
|
||||
max_trailing_padding_zeroes_in_precision_mode_(
|
||||
max_trailing_padding_zeroes_in_precision_mode),
|
||||
min_exponent_width_(min_exponent_width) {
|
||||
// When 'trailing zero after the point' is set, then 'trailing point'
|
||||
// must be set too.
|
||||
DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
|
||||
!((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
|
||||
}
|
||||
|
||||
// Returns a converter following the EcmaScript specification.
|
||||
static const DoubleToStringConverter& EcmaScriptConverter();
|
||||
|
||||
// Computes the shortest string of digits that correctly represent the input
|
||||
// number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
|
||||
// (see constructor) it then either returns a decimal representation, or an
|
||||
// exponential representation.
|
||||
// Example with decimal_in_shortest_low = -6,
|
||||
// decimal_in_shortest_high = 21,
|
||||
// EMIT_POSITIVE_EXPONENT_SIGN activated, and
|
||||
// EMIT_TRAILING_DECIMAL_POINT deactived:
|
||||
// ToShortest(0.000001) -> "0.000001"
|
||||
// ToShortest(0.0000001) -> "1e-7"
|
||||
// ToShortest(111111111111111111111.0) -> "111111111111111110000"
|
||||
// ToShortest(100000000000000000000.0) -> "100000000000000000000"
|
||||
// ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
|
||||
//
|
||||
// Note: the conversion may round the output if the returned string
|
||||
// is accurate enough to uniquely identify the input-number.
|
||||
// For example the most precise representation of the double 9e59 equals
|
||||
// "899999999999999918767229449717619953810131273674690656206848", but
|
||||
// the converter will return the shorter (but still correct) "9e59".
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except when the input value is special and no infinity_symbol or
|
||||
// nan_symbol has been given to the constructor.
|
||||
bool ToShortest(double value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
|
||||
}
|
||||
|
||||
// Same as ToShortest, but for single-precision floats.
|
||||
bool ToShortestSingle(float value, StringBuilder* result_builder) const {
|
||||
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
|
||||
}
|
||||
|
||||
|
||||
// Computes a decimal representation with a fixed number of digits after the
|
||||
// decimal point. The last emitted digit is rounded.
|
||||
//
|
||||
// Examples:
|
||||
// ToFixed(3.12, 1) -> "3.1"
|
||||
// ToFixed(3.1415, 3) -> "3.142"
|
||||
// ToFixed(1234.56789, 4) -> "1234.5679"
|
||||
// ToFixed(1.23, 5) -> "1.23000"
|
||||
// ToFixed(0.1, 4) -> "0.1000"
|
||||
// ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
|
||||
// ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
|
||||
// ToFixed(0.1, 17) -> "0.10000000000000001"
|
||||
//
|
||||
// If requested_digits equals 0, then the tail of the result depends on
|
||||
// the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples, for requested_digits == 0,
|
||||
// let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
|
||||
// - false and false: then 123.45 -> 123
|
||||
// 0.678 -> 1
|
||||
// - true and false: then 123.45 -> 123.
|
||||
// 0.678 -> 1.
|
||||
// - true and true: then 123.45 -> 123.0
|
||||
// 0.678 -> 1.0
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'value' > 10^kMaxFixedDigitsBeforePoint, or
|
||||
// - 'requested_digits' > kMaxFixedDigitsAfterPoint.
|
||||
// The last two conditions imply that the result will never contain more than
|
||||
// 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
|
||||
// (one additional character for the sign, and one for the decimal point).
|
||||
bool ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes a representation in exponential format with requested_digits
|
||||
// after the decimal point. The last emitted digit is rounded.
|
||||
// If requested_digits equals -1, then the shortest exponential representation
|
||||
// is computed.
|
||||
//
|
||||
// Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
|
||||
// exponent_character set to 'e'.
|
||||
// ToExponential(3.12, 1) -> "3.1e0"
|
||||
// ToExponential(5.0, 3) -> "5.000e0"
|
||||
// ToExponential(0.001, 2) -> "1.00e-3"
|
||||
// ToExponential(3.1415, -1) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 4) -> "3.1415e0"
|
||||
// ToExponential(3.1415, 3) -> "3.142e0"
|
||||
// ToExponential(123456789000000, 3) -> "1.235e14"
|
||||
// ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
|
||||
// ToExponential(1000000000000000019884624838656.0, 32) ->
|
||||
// "1.00000000000000001988462483865600e30"
|
||||
// ToExponential(1234, 0) -> "1e3"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - 'requested_digits' > kMaxExponentialDigits.
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxExponentialDigits + 8 characters (the sign, the digit before the
|
||||
// decimal point, the decimal point, the exponent character, the
|
||||
// exponent's sign, and at most 3 exponent digits).
|
||||
bool ToExponential(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
// Computes 'precision' leading digits of the given 'value' and returns them
|
||||
// either in exponential or decimal format, depending on
|
||||
// max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
|
||||
// constructor).
|
||||
// The last computed digit is rounded.
|
||||
//
|
||||
// Example with max_leading_padding_zeroes_in_precision_mode = 6.
|
||||
// ToPrecision(0.0000012345, 2) -> "0.0000012"
|
||||
// ToPrecision(0.00000012345, 2) -> "1.2e-7"
|
||||
// Similarily the converter may add up to
|
||||
// max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
|
||||
// returning an exponential representation. A zero added by the
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
|
||||
// ToPrecision(230.0, 2) -> "230"
|
||||
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
|
||||
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
|
||||
// Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
|
||||
// EMIT_TRAILING_ZERO_AFTER_POINT:
|
||||
// ToPrecision(123450.0, 6) -> "123450"
|
||||
// ToPrecision(123450.0, 5) -> "123450"
|
||||
// ToPrecision(123450.0, 4) -> "123500"
|
||||
// ToPrecision(123450.0, 3) -> "123000"
|
||||
// ToPrecision(123450.0, 2) -> "1.2e5"
|
||||
//
|
||||
// Returns true if the conversion succeeds. The conversion always succeeds
|
||||
// except for the following cases:
|
||||
// - the input value is special and no infinity_symbol or nan_symbol has
|
||||
// been provided to the constructor,
|
||||
// - precision < kMinPericisionDigits
|
||||
// - precision > kMaxPrecisionDigits
|
||||
// The last condition implies that the result will never contain more than
|
||||
// kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
|
||||
// exponent character, the exponent's sign, and at most 3 exponent digits).
|
||||
bool ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
enum DtoaMode {
|
||||
// Produce the shortest correct representation.
|
||||
// For example the output of 0.299999999999999988897 is (the less accurate
|
||||
// but correct) 0.3.
|
||||
SHORTEST,
|
||||
// Same as SHORTEST, but for single-precision floats.
|
||||
SHORTEST_SINGLE,
|
||||
// Produce a fixed number of digits after the decimal point.
|
||||
// For instance fixed(0.1, 4) becomes 0.1000
|
||||
// If the input number is big, the output will be big.
|
||||
FIXED,
|
||||
// Fixed number of digits (independent of the decimal point).
|
||||
PRECISION
|
||||
};
|
||||
|
||||
// The maximal number of digits that are needed to emit a double in base 10.
|
||||
// A higher precision can be achieved by using more digits, but the shortest
|
||||
// accurate representation of any double will never use more digits than
|
||||
// kBase10MaximalLength.
|
||||
// Note that DoubleToAscii null-terminates its input. So the given buffer
|
||||
// should be at least kBase10MaximalLength + 1 characters long.
|
||||
static const int kBase10MaximalLength = 17;
|
||||
|
||||
// Converts the given double 'v' to digit characters. 'v' must not be NaN,
|
||||
// +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
|
||||
// applies to 'v' after it has been casted to a single-precision float. That
|
||||
// is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
|
||||
// -Infinity.
|
||||
//
|
||||
// The result should be interpreted as buffer * 10^(point-length).
|
||||
//
|
||||
// The digits are written to the buffer in the platform's charset, which is
|
||||
// often UTF-8 (with ASCII-range digits) but may be another charset, such
|
||||
// as EBCDIC.
|
||||
//
|
||||
// The output depends on the given mode:
|
||||
// - SHORTEST: produce the least amount of digits for which the internal
|
||||
// identity requirement is still satisfied. If the digits are printed
|
||||
// (together with the correct exponent) then reading this number will give
|
||||
// 'v' again. The buffer will choose the representation that is closest to
|
||||
// 'v'. If there are two at the same distance, than the one farther away
|
||||
// from 0 is chosen (halfway cases - ending with 5 - are rounded up).
|
||||
// In this mode the 'requested_digits' parameter is ignored.
|
||||
// - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
|
||||
// - FIXED: produces digits necessary to print a given number with
|
||||
// 'requested_digits' digits after the decimal point. The produced digits
|
||||
// might be too short in which case the caller has to fill the remainder
|
||||
// with '0's.
|
||||
// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
|
||||
// Halfway cases are rounded towards +/-Infinity (away from 0). The call
|
||||
// toFixed(0.15, 2) thus returns buffer="2", point=0.
|
||||
// The returned buffer may contain digits that would be truncated from the
|
||||
// shortest representation of the input.
|
||||
// - PRECISION: produces 'requested_digits' where the first digit is not '0'.
|
||||
// Even though the length of produced digits usually equals
|
||||
// 'requested_digits', the function is allowed to return fewer digits, in
|
||||
// which case the caller has to fill the missing digits with '0's.
|
||||
// Halfway cases are again rounded away from 0.
|
||||
// DoubleToAscii expects the given buffer to be big enough to hold all
|
||||
// digits and a terminating null-character. In SHORTEST-mode it expects a
|
||||
// buffer of at least kBase10MaximalLength + 1. In all other modes the
|
||||
// requested_digits parameter and the padding-zeroes limit the size of the
|
||||
// output. Don't forget the decimal point, the exponent character and the
|
||||
// terminating null-character when computing the maximal output size.
|
||||
// The given length is only used in debug mode to ensure the buffer is big
|
||||
// enough.
|
||||
static void DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point);
|
||||
|
||||
private:
|
||||
// Implementation for ToShortest and ToShortestSingle.
|
||||
bool ToShortestIeeeNumber(double value,
|
||||
StringBuilder* result_builder,
|
||||
DtoaMode mode) const;
|
||||
|
||||
// If the value is a special value (NaN or Infinity) constructs the
|
||||
// corresponding string using the configured infinity/nan-symbol.
|
||||
// If either of them is NULL or the value is not special then the
|
||||
// function returns false.
|
||||
bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
|
||||
// Constructs an exponential representation (i.e. 1.234e56).
|
||||
// The given exponent assumes a decimal point after the first decimal digit.
|
||||
void CreateExponentialRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const;
|
||||
// Creates a decimal representation (i.e 1234.5678).
|
||||
void CreateDecimalRepresentation(const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const;
|
||||
|
||||
const int flags_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const char exponent_character_;
|
||||
const int decimal_in_shortest_low_;
|
||||
const int decimal_in_shortest_high_;
|
||||
const int max_leading_padding_zeroes_in_precision_mode_;
|
||||
const int max_trailing_padding_zeroes_in_precision_mode_;
|
||||
const int min_exponent_width_;
|
||||
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
#endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
|
@ -138,7 +138,7 @@ static bool RoundWeed(Vector<char> buffer,
|
||||
// Conceptually rest ~= too_high - buffer
|
||||
// We need to do the following tests in this order to avoid over- and
|
||||
// underflows.
|
||||
ASSERT(rest <= unsafe_interval);
|
||||
DOUBLE_CONVERSION_ASSERT(rest <= unsafe_interval);
|
||||
while (rest < small_distance && // Negated condition 1
|
||||
unsafe_interval - rest >= ten_kappa && // Negated condition 2
|
||||
(rest + ten_kappa < small_distance || // buffer{-1} > w_high
|
||||
@ -184,7 +184,7 @@ static bool RoundWeedCounted(Vector<char> buffer,
|
||||
uint64_t ten_kappa,
|
||||
uint64_t unit,
|
||||
int* kappa) {
|
||||
ASSERT(rest < ten_kappa);
|
||||
DOUBLE_CONVERSION_ASSERT(rest < ten_kappa);
|
||||
// The following tests are done in a specific order to avoid overflows. They
|
||||
// will work correctly with any uint64 values of rest < ten_kappa and unit.
|
||||
//
|
||||
@ -241,7 +241,7 @@ static void BiggestPowerTen(uint32_t number,
|
||||
int number_bits,
|
||||
uint32_t* power,
|
||||
int* exponent_plus_one) {
|
||||
ASSERT(number < (1u << (number_bits + 1)));
|
||||
DOUBLE_CONVERSION_ASSERT(number < (1u << (number_bits + 1)));
|
||||
// 1233/4096 is approximately 1/lg(10).
|
||||
int exponent_plus_one_guess = ((number_bits + 1) * 1233 >> 12);
|
||||
// We increment to skip over the first entry in the kPowersOf10 table.
|
||||
@ -303,9 +303,9 @@ static bool DigitGen(DiyFp low,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* kappa) {
|
||||
ASSERT(low.e() == w.e() && w.e() == high.e());
|
||||
ASSERT(low.f() + 1 <= high.f() - 1);
|
||||
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(low.e() == w.e() && w.e() == high.e());
|
||||
DOUBLE_CONVERSION_ASSERT(low.f() + 1 <= high.f() - 1);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
// low, w and high are imprecise, but by less than one ulp (unit in the last
|
||||
// place).
|
||||
// If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
|
||||
@ -347,7 +347,7 @@ static bool DigitGen(DiyFp low,
|
||||
// that is smaller than integrals.
|
||||
while (*kappa > 0) {
|
||||
int digit = integrals / divisor;
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
integrals %= divisor;
|
||||
@ -374,16 +374,16 @@ static bool DigitGen(DiyFp low,
|
||||
// data (like the interval or 'unit'), too.
|
||||
// Note that the multiplication by 10 does not overflow, because w.e >= -60
|
||||
// and thus one.e >= -60.
|
||||
ASSERT(one.e() >= -60);
|
||||
ASSERT(fractionals < one.f());
|
||||
ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(one.e() >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals < one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
for (;;) {
|
||||
fractionals *= 10;
|
||||
unit *= 10;
|
||||
unsafe_interval.set_f(unsafe_interval.f() * 10);
|
||||
// Integer division by one.
|
||||
int digit = static_cast<int>(fractionals >> -one.e());
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
fractionals &= one.f() - 1; // Modulo by one.
|
||||
@ -430,9 +430,9 @@ static bool DigitGenCounted(DiyFp w,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* kappa) {
|
||||
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
ASSERT(kMinimalTargetExponent >= -60);
|
||||
ASSERT(kMaximalTargetExponent <= -32);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(kMinimalTargetExponent >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(kMaximalTargetExponent <= -32);
|
||||
// w is assumed to have an error less than 1 unit. Whenever w is scaled we
|
||||
// also scale its error.
|
||||
uint64_t w_error = 1;
|
||||
@ -458,7 +458,7 @@ static bool DigitGenCounted(DiyFp w,
|
||||
// that is smaller than 'integrals'.
|
||||
while (*kappa > 0) {
|
||||
int digit = integrals / divisor;
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
requested_digits--;
|
||||
@ -484,15 +484,15 @@ static bool DigitGenCounted(DiyFp w,
|
||||
// data (the 'unit'), too.
|
||||
// Note that the multiplication by 10 does not overflow, because w.e >= -60
|
||||
// and thus one.e >= -60.
|
||||
ASSERT(one.e() >= -60);
|
||||
ASSERT(fractionals < one.f());
|
||||
ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(one.e() >= -60);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals < one.f());
|
||||
DOUBLE_CONVERSION_ASSERT(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||
while (requested_digits > 0 && fractionals > w_error) {
|
||||
fractionals *= 10;
|
||||
w_error *= 10;
|
||||
// Integer division by one.
|
||||
int digit = static_cast<int>(fractionals >> -one.e());
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
requested_digits--;
|
||||
@ -530,11 +530,11 @@ static bool Grisu3(double v,
|
||||
if (mode == FAST_DTOA_SHORTEST) {
|
||||
Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
} else {
|
||||
ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
|
||||
DOUBLE_CONVERSION_ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE);
|
||||
float single_v = static_cast<float>(v);
|
||||
Single(single_v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
}
|
||||
ASSERT(boundary_plus.e() == w.e());
|
||||
DOUBLE_CONVERSION_ASSERT(boundary_plus.e() == w.e());
|
||||
DiyFp ten_mk; // Cached power of ten: 10^-k
|
||||
int mk; // -k
|
||||
int ten_mk_minimal_binary_exponent =
|
||||
@ -545,7 +545,7 @@ static bool Grisu3(double v,
|
||||
ten_mk_minimal_binary_exponent,
|
||||
ten_mk_maximal_binary_exponent,
|
||||
&ten_mk, &mk);
|
||||
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DOUBLE_CONVERSION_ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize) &&
|
||||
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize));
|
||||
@ -559,7 +559,7 @@ static bool Grisu3(double v,
|
||||
// In other words: let f = scaled_w.f() and e = scaled_w.e(), then
|
||||
// (f-1) * 2^e < w*10^k < (f+1) * 2^e
|
||||
DiyFp scaled_w = DiyFp::Times(w, ten_mk);
|
||||
ASSERT(scaled_w.e() ==
|
||||
DOUBLE_CONVERSION_ASSERT(scaled_w.e() ==
|
||||
boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
|
||||
// In theory it would be possible to avoid some recomputations by computing
|
||||
// the difference between w and boundary_minus/plus (a power of 2) and to
|
||||
@ -604,7 +604,7 @@ static bool Grisu3Counted(double v,
|
||||
ten_mk_minimal_binary_exponent,
|
||||
ten_mk_maximal_binary_exponent,
|
||||
&ten_mk, &mk);
|
||||
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DOUBLE_CONVERSION_ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize) &&
|
||||
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||
DiyFp::kSignificandSize));
|
||||
@ -638,8 +638,8 @@ bool FastDtoa(double v,
|
||||
Vector<char> buffer,
|
||||
int* length,
|
||||
int* decimal_point) {
|
||||
ASSERT(v > 0);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(v > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!Double(v).IsSpecial());
|
||||
|
||||
bool result = false;
|
||||
int decimal_exponent = 0;
|
||||
@ -653,7 +653,7 @@ bool FastDtoa(double v,
|
||||
buffer, length, &decimal_exponent);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
if (result) {
|
||||
*decimal_point = *length + decimal_exponent;
|
||||
|
@ -53,11 +53,11 @@ class UInt128 {
|
||||
accumulator >>= 32;
|
||||
accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
|
||||
high_bits_ = (accumulator << 32) + part;
|
||||
ASSERT((accumulator >> 32) == 0);
|
||||
DOUBLE_CONVERSION_ASSERT((accumulator >> 32) == 0);
|
||||
}
|
||||
|
||||
void Shift(int shift_amount) {
|
||||
ASSERT(-64 <= shift_amount && shift_amount <= 64);
|
||||
DOUBLE_CONVERSION_ASSERT(-64 <= shift_amount && shift_amount <= 64);
|
||||
if (shift_amount == 0) {
|
||||
return;
|
||||
} else if (shift_amount == -64) {
|
||||
@ -230,13 +230,13 @@ static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
|
||||
static void FillFractionals(uint64_t fractionals, int exponent,
|
||||
int fractional_count, Vector<char> buffer,
|
||||
int* length, int* decimal_point) {
|
||||
ASSERT(-128 <= exponent && exponent <= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(-128 <= exponent && exponent <= 0);
|
||||
// 'fractionals' is a fixed-point number, with binary point at bit
|
||||
// (-exponent). Inside the function the non-converted remainder of fractionals
|
||||
// is a fixed-point number, with binary point at bit 'point'.
|
||||
if (-exponent <= 64) {
|
||||
// One 64 bit number is sufficient.
|
||||
ASSERT(fractionals >> 56 == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals >> 56 == 0);
|
||||
int point = -exponent;
|
||||
for (int i = 0; i < fractional_count; ++i) {
|
||||
if (fractionals == 0) break;
|
||||
@ -253,18 +253,18 @@ static void FillFractionals(uint64_t fractionals, int exponent,
|
||||
fractionals *= 5;
|
||||
point--;
|
||||
int digit = static_cast<int>(fractionals >> point);
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
fractionals -= static_cast<uint64_t>(digit) << point;
|
||||
}
|
||||
// If the first bit after the point is set we have to round up.
|
||||
ASSERT(fractionals == 0 || point - 1 >= 0);
|
||||
DOUBLE_CONVERSION_ASSERT(fractionals == 0 || point - 1 >= 0);
|
||||
if ((fractionals != 0) && ((fractionals >> (point - 1)) & 1) == 1) {
|
||||
RoundUp(buffer, length, decimal_point);
|
||||
}
|
||||
} else { // We need 128 bits.
|
||||
ASSERT(64 < -exponent && -exponent <= 128);
|
||||
DOUBLE_CONVERSION_ASSERT(64 < -exponent && -exponent <= 128);
|
||||
UInt128 fractionals128 = UInt128(fractionals, 0);
|
||||
fractionals128.Shift(-exponent - 64);
|
||||
int point = 128;
|
||||
@ -276,7 +276,7 @@ static void FillFractionals(uint64_t fractionals, int exponent,
|
||||
fractionals128.Multiply(5);
|
||||
point--;
|
||||
int digit = fractionals128.DivModPowerOf2(point);
|
||||
ASSERT(digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(digit <= 9);
|
||||
buffer[*length] = static_cast<char>('0' + digit);
|
||||
(*length)++;
|
||||
}
|
||||
@ -335,7 +335,7 @@ bool FastFixedDtoa(double v,
|
||||
// The quotient delivers the first digits, and the remainder fits into a 64
|
||||
// bit number.
|
||||
// Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
|
||||
const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17
|
||||
const uint64_t kFive17 = DOUBLE_CONVERSION_UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17
|
||||
uint64_t divisor = kFive17;
|
||||
int divisor_power = 17;
|
||||
uint64_t dividend = significand;
|
||||
@ -383,7 +383,7 @@ bool FastFixedDtoa(double v,
|
||||
} else if (exponent < -128) {
|
||||
// This configuration (with at most 20 digits) means that all digits must be
|
||||
// 0.
|
||||
ASSERT(fractional_count <= 20);
|
||||
DOUBLE_CONVERSION_ASSERT(fractional_count <= 20);
|
||||
buffer[0] = '\0';
|
||||
*length = 0;
|
||||
*decimal_point = -fractional_count;
|
||||
|
@ -41,12 +41,14 @@ static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
|
||||
// Helper functions for doubles.
|
||||
class Double {
|
||||
public:
|
||||
static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
|
||||
static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
|
||||
static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
|
||||
static const int kSignificandSize = 53;
|
||||
static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
|
||||
static const int kMaxExponent = 0x7FF - kExponentBias;
|
||||
|
||||
Double() : d64_(0) {}
|
||||
explicit Double(double d) : d64_(double_to_uint64(d)) {}
|
||||
@ -57,14 +59,14 @@ class Double {
|
||||
// The value encoded by this Double must be greater or equal to +0.0.
|
||||
// It must not be special (infinity, or NaN).
|
||||
DiyFp AsDiyFp() const {
|
||||
ASSERT(Sign() > 0);
|
||||
ASSERT(!IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!IsSpecial());
|
||||
return DiyFp(Significand(), Exponent());
|
||||
}
|
||||
|
||||
// The value encoded by this Double must be strictly greater than 0.
|
||||
DiyFp AsNormalizedDiyFp() const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
uint64_t f = Significand();
|
||||
int e = Exponent();
|
||||
|
||||
@ -160,7 +162,7 @@ class Double {
|
||||
// Precondition: the value encoded by this Double must be greater or equal
|
||||
// than +0.0.
|
||||
DiyFp UpperBoundary() const {
|
||||
ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
return DiyFp(Significand() * 2 + 1, Exponent() - 1);
|
||||
}
|
||||
|
||||
@ -169,7 +171,7 @@ class Double {
|
||||
// exponent as m_plus.
|
||||
// Precondition: the value encoded by this Double must be greater than 0.
|
||||
void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
DiyFp v = this->AsDiyFp();
|
||||
DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
|
||||
DiyFp m_minus;
|
||||
@ -222,11 +224,9 @@ class Double {
|
||||
}
|
||||
|
||||
private:
|
||||
static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
|
||||
static const int kDenormalExponent = -kExponentBias + 1;
|
||||
static const int kMaxExponent = 0x7FF - kExponentBias;
|
||||
static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
|
||||
static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
|
||||
static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
|
||||
|
||||
const uint64_t d64_;
|
||||
|
||||
@ -257,7 +257,7 @@ class Double {
|
||||
(biased_exponent << kPhysicalSignificandSize);
|
||||
}
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Double);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
|
||||
};
|
||||
|
||||
class Single {
|
||||
@ -276,8 +276,8 @@ class Single {
|
||||
// The value encoded by this Single must be greater or equal to +0.0.
|
||||
// It must not be special (infinity, or NaN).
|
||||
DiyFp AsDiyFp() const {
|
||||
ASSERT(Sign() > 0);
|
||||
ASSERT(!IsSpecial());
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(!IsSpecial());
|
||||
return DiyFp(Significand(), Exponent());
|
||||
}
|
||||
|
||||
@ -340,7 +340,7 @@ class Single {
|
||||
// exponent as m_plus.
|
||||
// Precondition: the value encoded by this Single must be greater than 0.
|
||||
void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
|
||||
ASSERT(value() > 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(value() > 0.0);
|
||||
DiyFp v = this->AsDiyFp();
|
||||
DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
|
||||
DiyFp m_minus;
|
||||
@ -358,7 +358,7 @@ class Single {
|
||||
// Precondition: the value encoded by this Single must be greater or equal
|
||||
// than +0.0.
|
||||
DiyFp UpperBoundary() const {
|
||||
ASSERT(Sign() > 0);
|
||||
DOUBLE_CONVERSION_ASSERT(Sign() > 0);
|
||||
return DiyFp(Significand() * 2 + 1, Exponent() - 1);
|
||||
}
|
||||
|
||||
@ -394,7 +394,7 @@ class Single {
|
||||
|
||||
const uint32_t d32_;
|
||||
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(Single);
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
@ -29,392 +29,14 @@
|
||||
#include <locale>
|
||||
#include <cmath>
|
||||
|
||||
#include "double-conversion.h"
|
||||
#include "string-to-double.h"
|
||||
|
||||
#include "bignum-dtoa.h"
|
||||
#include "fast-dtoa.h"
|
||||
#include "fixed-dtoa.h"
|
||||
#include "ieee.h"
|
||||
#include "strtod.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() {
|
||||
int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN;
|
||||
static DoubleToStringConverter converter(flags,
|
||||
"Infinity",
|
||||
"NaN",
|
||||
'e',
|
||||
-6, 21,
|
||||
6, 0);
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::HandleSpecialValues(
|
||||
double value,
|
||||
StringBuilder* result_builder) const {
|
||||
Double double_inspect(value);
|
||||
if (double_inspect.IsInfinite()) {
|
||||
if (infinity_symbol_ == NULL) return false;
|
||||
if (value < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
result_builder->AddString(infinity_symbol_);
|
||||
return true;
|
||||
}
|
||||
if (double_inspect.IsNan()) {
|
||||
if (nan_symbol_ == NULL) return false;
|
||||
result_builder->AddString(nan_symbol_);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateExponentialRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int exponent,
|
||||
StringBuilder* result_builder) const {
|
||||
ASSERT(length != 0);
|
||||
result_builder->AddCharacter(decimal_digits[0]);
|
||||
if (length != 1) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddSubstring(&decimal_digits[1], length-1);
|
||||
}
|
||||
result_builder->AddCharacter(exponent_character_);
|
||||
if (exponent < 0) {
|
||||
result_builder->AddCharacter('-');
|
||||
exponent = -exponent;
|
||||
} else {
|
||||
if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) {
|
||||
result_builder->AddCharacter('+');
|
||||
}
|
||||
}
|
||||
if (exponent == 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
return;
|
||||
}
|
||||
ASSERT(exponent < 1e4);
|
||||
const int kMaxExponentLength = 5;
|
||||
char buffer[kMaxExponentLength + 1];
|
||||
buffer[kMaxExponentLength] = '\0';
|
||||
int first_char_pos = kMaxExponentLength;
|
||||
while (exponent > 0) {
|
||||
buffer[--first_char_pos] = '0' + (exponent % 10);
|
||||
exponent /= 10;
|
||||
}
|
||||
result_builder->AddSubstring(&buffer[first_char_pos],
|
||||
kMaxExponentLength - first_char_pos);
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::CreateDecimalRepresentation(
|
||||
const char* decimal_digits,
|
||||
int length,
|
||||
int decimal_point,
|
||||
int digits_after_point,
|
||||
StringBuilder* result_builder) const {
|
||||
// Create a representation that is padded with zeros if needed.
|
||||
if (decimal_point <= 0) {
|
||||
// "0.00000decimal_rep" or "0.000decimal_rep00".
|
||||
result_builder->AddCharacter('0');
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', -decimal_point);
|
||||
ASSERT(length <= digits_after_point - (-decimal_point));
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
int remaining_digits = digits_after_point - (-decimal_point) - length;
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
} else if (decimal_point >= length) {
|
||||
// "decimal_rep0000.00000" or "decimal_rep.0000".
|
||||
result_builder->AddSubstring(decimal_digits, length);
|
||||
result_builder->AddPadding('0', decimal_point - length);
|
||||
if (digits_after_point > 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
result_builder->AddPadding('0', digits_after_point);
|
||||
}
|
||||
} else {
|
||||
// "decima.l_rep000".
|
||||
ASSERT(digits_after_point > 0);
|
||||
result_builder->AddSubstring(decimal_digits, decimal_point);
|
||||
result_builder->AddCharacter('.');
|
||||
ASSERT(length - decimal_point <= digits_after_point);
|
||||
result_builder->AddSubstring(&decimal_digits[decimal_point],
|
||||
length - decimal_point);
|
||||
int remaining_digits = digits_after_point - (length - decimal_point);
|
||||
result_builder->AddPadding('0', remaining_digits);
|
||||
}
|
||||
if (digits_after_point == 0) {
|
||||
if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) {
|
||||
result_builder->AddCharacter('.');
|
||||
}
|
||||
if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) {
|
||||
result_builder->AddCharacter('0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToShortestIeeeNumber(
|
||||
double value,
|
||||
StringBuilder* result_builder,
|
||||
DoubleToStringConverter::DtoaMode mode) const {
|
||||
ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE);
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
const int kDecimalRepCapacity = kBase10MaximalLength + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = (flags_ & UNIQUE_ZERO) != 0;
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
if ((decimal_in_shortest_low_ <= exponent) &&
|
||||
(exponent < decimal_in_shortest_high_)) {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
|
||||
decimal_point,
|
||||
Max(0, decimal_rep_length - decimal_point),
|
||||
result_builder);
|
||||
} else {
|
||||
CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToFixed(double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
ASSERT(kMaxFixedDigitsBeforePoint == 60);
|
||||
const double kFirstNonFixed = 1e60;
|
||||
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits > kMaxFixedDigitsAfterPoint) return false;
|
||||
if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false;
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for the '\0' byte.
|
||||
const int kDecimalRepCapacity =
|
||||
kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
DoubleToAscii(value, FIXED, requested_digits,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
requested_digits, result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToExponential(
|
||||
double value,
|
||||
int requested_digits,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (requested_digits < -1) return false;
|
||||
if (requested_digits > kMaxExponentialDigits) return false;
|
||||
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add space for digit before the decimal point and the '\0' character.
|
||||
const int kDecimalRepCapacity = kMaxExponentialDigits + 2;
|
||||
ASSERT(kDecimalRepCapacity > kBase10MaximalLength);
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
if (requested_digits == -1) {
|
||||
DoubleToAscii(value, SHORTEST, 0,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
} else {
|
||||
DoubleToAscii(value, PRECISION, requested_digits + 1,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
ASSERT(decimal_rep_length <= requested_digits + 1);
|
||||
|
||||
for (int i = decimal_rep_length; i < requested_digits + 1; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
decimal_rep_length = requested_digits + 1;
|
||||
}
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
int exponent = decimal_point - 1;
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
decimal_rep_length,
|
||||
exponent,
|
||||
result_builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool DoubleToStringConverter::ToPrecision(double value,
|
||||
int precision,
|
||||
StringBuilder* result_builder) const {
|
||||
if (Double(value).IsSpecial()) {
|
||||
return HandleSpecialValues(value, result_builder);
|
||||
}
|
||||
|
||||
if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find a sufficiently precise decimal representation of n.
|
||||
int decimal_point;
|
||||
bool sign;
|
||||
// Add one for the terminating null character.
|
||||
const int kDecimalRepCapacity = kMaxPrecisionDigits + 1;
|
||||
char decimal_rep[kDecimalRepCapacity];
|
||||
int decimal_rep_length;
|
||||
|
||||
DoubleToAscii(value, PRECISION, precision,
|
||||
decimal_rep, kDecimalRepCapacity,
|
||||
&sign, &decimal_rep_length, &decimal_point);
|
||||
ASSERT(decimal_rep_length <= precision);
|
||||
|
||||
bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0);
|
||||
if (sign && (value != 0.0 || !unique_zero)) {
|
||||
result_builder->AddCharacter('-');
|
||||
}
|
||||
|
||||
// The exponent if we print the number as x.xxeyyy. That is with the
|
||||
// decimal point after the first digit.
|
||||
int exponent = decimal_point - 1;
|
||||
|
||||
int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0;
|
||||
if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) ||
|
||||
(decimal_point - precision + extra_zero >
|
||||
max_trailing_padding_zeroes_in_precision_mode_)) {
|
||||
// Fill buffer to contain 'precision' digits.
|
||||
// Usually the buffer is already at the correct length, but 'DoubleToAscii'
|
||||
// is allowed to return less characters.
|
||||
for (int i = decimal_rep_length; i < precision; ++i) {
|
||||
decimal_rep[i] = '0';
|
||||
}
|
||||
|
||||
CreateExponentialRepresentation(decimal_rep,
|
||||
precision,
|
||||
exponent,
|
||||
result_builder);
|
||||
} else {
|
||||
CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point,
|
||||
Max(0, precision - decimal_point),
|
||||
result_builder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static BignumDtoaMode DtoaToBignumDtoaMode(
|
||||
DoubleToStringConverter::DtoaMode dtoa_mode) {
|
||||
switch (dtoa_mode) {
|
||||
case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST;
|
||||
case DoubleToStringConverter::SHORTEST_SINGLE:
|
||||
return BIGNUM_DTOA_SHORTEST_SINGLE;
|
||||
case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED;
|
||||
case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DoubleToStringConverter::DoubleToAscii(double v,
|
||||
DtoaMode mode,
|
||||
int requested_digits,
|
||||
char* buffer,
|
||||
int buffer_length,
|
||||
bool* sign,
|
||||
int* length,
|
||||
int* point) {
|
||||
Vector<char> vector(buffer, buffer_length);
|
||||
ASSERT(!Double(v).IsSpecial());
|
||||
ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0);
|
||||
|
||||
if (Double(v).Sign() < 0) {
|
||||
*sign = true;
|
||||
v = -v;
|
||||
} else {
|
||||
*sign = false;
|
||||
}
|
||||
|
||||
if (mode == PRECISION && requested_digits == 0) {
|
||||
vector[0] = '\0';
|
||||
*length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (v == 0) {
|
||||
vector[0] = '0';
|
||||
vector[1] = '\0';
|
||||
*length = 1;
|
||||
*point = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
bool fast_worked;
|
||||
switch (mode) {
|
||||
case SHORTEST:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point);
|
||||
break;
|
||||
case SHORTEST_SINGLE:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0,
|
||||
vector, length, point);
|
||||
break;
|
||||
case FIXED:
|
||||
fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point);
|
||||
break;
|
||||
case PRECISION:
|
||||
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
||||
vector, length, point);
|
||||
break;
|
||||
default:
|
||||
fast_worked = false;
|
||||
UNREACHABLE();
|
||||
}
|
||||
if (fast_worked) return;
|
||||
|
||||
// If the fast dtoa didn't succeed use the slower bignum version.
|
||||
BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
|
||||
BignumDtoa(v, bignum_mode, requested_digits, vector, length, point);
|
||||
vector[*length] = '\0';
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
inline char ToLower(char ch) {
|
||||
@ -432,7 +54,7 @@ static inline bool ConsumeSubStringImpl(Iterator* current,
|
||||
Iterator end,
|
||||
const char* substring,
|
||||
Converter converter) {
|
||||
ASSERT(converter(**current) == *substring);
|
||||
DOUBLE_CONVERSION_ASSERT(converter(**current) == *substring);
|
||||
for (substring++; *substring != '\0'; substring++) {
|
||||
++*current;
|
||||
if (*current == end || converter(**current) != *substring) {
|
||||
@ -449,8 +71,8 @@ template <class Iterator>
|
||||
static bool ConsumeSubString(Iterator* current,
|
||||
Iterator end,
|
||||
const char* substring,
|
||||
bool allow_case_insensibility) {
|
||||
if (allow_case_insensibility) {
|
||||
bool allow_case_insensitivity) {
|
||||
if (allow_case_insensitivity) {
|
||||
return ConsumeSubStringImpl(current, end, substring, ToLower);
|
||||
} else {
|
||||
return ConsumeSubStringImpl(current, end, substring, Pass);
|
||||
@ -460,8 +82,8 @@ static bool ConsumeSubString(Iterator* current,
|
||||
// Consumes first character of the str is equal to ch
|
||||
inline bool ConsumeFirstCharacter(char ch,
|
||||
const char* str,
|
||||
bool case_insensibility) {
|
||||
return case_insensibility ? ToLower(ch) == str[0] : ch == str[0];
|
||||
bool case_insensitivity) {
|
||||
return case_insensitivity ? ToLower(ch) == str[0] : ch == str[0];
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@ -476,14 +98,14 @@ const int kMaxSignificantDigits = 772;
|
||||
|
||||
|
||||
static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 };
|
||||
static const int kWhitespaceTable7Length = ARRAY_SIZE(kWhitespaceTable7);
|
||||
static const int kWhitespaceTable7Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable7);
|
||||
|
||||
|
||||
static const uc16 kWhitespaceTable16[] = {
|
||||
160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195,
|
||||
8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279
|
||||
};
|
||||
static const int kWhitespaceTable16Length = ARRAY_SIZE(kWhitespaceTable16);
|
||||
static const int kWhitespaceTable16Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable16);
|
||||
|
||||
|
||||
static bool isWhitespace(int x) {
|
||||
@ -583,7 +205,7 @@ static bool IsHexFloatString(Iterator start,
|
||||
Iterator end,
|
||||
uc16 separator,
|
||||
bool allow_trailing_junk) {
|
||||
ASSERT(start != end);
|
||||
DOUBLE_CONVERSION_ASSERT(start != end);
|
||||
|
||||
Iterator current = start;
|
||||
|
||||
@ -598,8 +220,8 @@ static bool IsHexFloatString(Iterator start,
|
||||
saw_digit = true;
|
||||
if (Advance(¤t, separator, 16, end)) return false;
|
||||
}
|
||||
if (!saw_digit) return false; // Only the '.', but no digits.
|
||||
}
|
||||
if (!saw_digit) return false;
|
||||
if (*current != 'p' && *current != 'P') return false;
|
||||
if (Advance(¤t, separator, 16, end)) return false;
|
||||
if (*current == '+' || *current == '-') {
|
||||
@ -628,8 +250,8 @@ static double RadixStringToIeee(Iterator* current,
|
||||
double junk_string_value,
|
||||
bool read_as_double,
|
||||
bool* result_is_junk) {
|
||||
ASSERT(*current != end);
|
||||
ASSERT(!parse_as_hex_float ||
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(!parse_as_hex_float ||
|
||||
IsHexFloatString(*current, end, separator, allow_trailing_junk));
|
||||
|
||||
const int kDoubleSize = Double::kSignificandSize;
|
||||
@ -667,7 +289,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
} else if (parse_as_hex_float && **current == '.') {
|
||||
post_decimal = true;
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
continue;
|
||||
} else if (parse_as_hex_float && (**current == 'p' || **current == 'P')) {
|
||||
break;
|
||||
@ -702,7 +324,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
// Just run over the '.'. We are just trying to see whether there is
|
||||
// a non-zero digit somewhere.
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
post_decimal = true;
|
||||
}
|
||||
if (!isDigit(**current, radix)) break;
|
||||
@ -737,27 +359,31 @@ static double RadixStringToIeee(Iterator* current,
|
||||
if (Advance(current, separator, radix, end)) break;
|
||||
}
|
||||
|
||||
ASSERT(number < ((int64_t)1 << kSignificandSize));
|
||||
ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
|
||||
DOUBLE_CONVERSION_ASSERT(number < ((int64_t)1 << kSignificandSize));
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
|
||||
|
||||
*result_is_junk = false;
|
||||
|
||||
if (parse_as_hex_float) {
|
||||
ASSERT(**current == 'p' || **current == 'P');
|
||||
DOUBLE_CONVERSION_ASSERT(**current == 'p' || **current == 'P');
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
bool is_negative = false;
|
||||
if (**current == '+') {
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
} else if (**current == '-') {
|
||||
is_negative = true;
|
||||
Advance(current, separator, radix, end);
|
||||
ASSERT(*current != end);
|
||||
DOUBLE_CONVERSION_ASSERT(*current != end);
|
||||
}
|
||||
int written_exponent = 0;
|
||||
while (IsDecimalDigitForRadix(**current, 10)) {
|
||||
written_exponent = 10 * written_exponent + **current - '0';
|
||||
// No need to read exponents if they are too big. That could potentially overflow
|
||||
// the `written_exponent` variable.
|
||||
if (abs(written_exponent) <= 100 * Double::kMaxExponent) {
|
||||
written_exponent = 10 * written_exponent + **current - '0';
|
||||
}
|
||||
if (Advance(current, separator, radix, end)) break;
|
||||
}
|
||||
if (is_negative) written_exponent = -written_exponent;
|
||||
@ -772,7 +398,7 @@ static double RadixStringToIeee(Iterator* current,
|
||||
return static_cast<double>(number);
|
||||
}
|
||||
|
||||
ASSERT(number != 0);
|
||||
DOUBLE_CONVERSION_ASSERT(number != 0);
|
||||
double result = Double(DiyFp(number, exponent)).value();
|
||||
return sign ? -result : result;
|
||||
}
|
||||
@ -792,7 +418,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0;
|
||||
const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0;
|
||||
const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0;
|
||||
const bool allow_case_insensibility = (flags_ & ALLOW_CASE_INSENSIBILITY) != 0;
|
||||
const bool allow_case_insensitivity = (flags_ & ALLOW_CASE_INSENSITIVITY) != 0;
|
||||
|
||||
// To make sure that iterator dereferencing is valid the following
|
||||
// convention is used:
|
||||
@ -842,8 +468,8 @@ double StringToDoubleConverter::StringToIeee(
|
||||
}
|
||||
|
||||
if (infinity_symbol_ != NULL) {
|
||||
if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensibility)) {
|
||||
if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensibility)) {
|
||||
if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensitivity)) {
|
||||
if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensitivity)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
@ -854,15 +480,15 @@ double StringToDoubleConverter::StringToIeee(
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return sign ? -Double::Infinity() : Double::Infinity();
|
||||
}
|
||||
}
|
||||
|
||||
if (nan_symbol_ != NULL) {
|
||||
if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensibility)) {
|
||||
if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensibility)) {
|
||||
if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensitivity)) {
|
||||
if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensitivity)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
@ -873,7 +499,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
return junk_string_value_;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos == 0);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos == 0);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return sign ? -Double::NaN() : Double::NaN();
|
||||
}
|
||||
@ -893,10 +519,11 @@ double StringToDoubleConverter::StringToIeee(
|
||||
(*current == 'x' || *current == 'X')) {
|
||||
++current;
|
||||
|
||||
if (current == end) return junk_string_value_; // "0x"
|
||||
|
||||
bool parse_as_hex_float = (flags_ & ALLOW_HEX_FLOATS) &&
|
||||
IsHexFloatString(current, end, separator_, allow_trailing_junk);
|
||||
|
||||
if (current == end) return junk_string_value_; // "0x"
|
||||
if (!parse_as_hex_float && !isDigit(*current, 16)) {
|
||||
return junk_string_value_;
|
||||
}
|
||||
@ -932,7 +559,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
// Copy significant digits of the integer part (if any) to the buffer.
|
||||
while (*current >= '0' && *current <= '9') {
|
||||
if (significant_digits < kMaxSignificantDigits) {
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos++] = static_cast<char>(*current);
|
||||
significant_digits++;
|
||||
// Will later check if it's an octal in the buffer.
|
||||
@ -977,7 +604,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
// We don't emit a '.', but adjust the exponent instead.
|
||||
while (*current >= '0' && *current <= '9') {
|
||||
if (significant_digits < kMaxSignificantDigits) {
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos++] = static_cast<char>(*current);
|
||||
significant_digits++;
|
||||
exponent--;
|
||||
@ -1035,7 +662,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
}
|
||||
|
||||
const int max_exponent = INT_MAX / 2;
|
||||
ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
|
||||
DOUBLE_CONVERSION_ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2);
|
||||
int num = 0;
|
||||
do {
|
||||
// Check overflow.
|
||||
@ -1078,7 +705,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
junk_string_value_,
|
||||
read_as_double,
|
||||
&result_is_junk);
|
||||
ASSERT(!result_is_junk);
|
||||
DOUBLE_CONVERSION_ASSERT(!result_is_junk);
|
||||
*processed_characters_count = static_cast<int>(current - input);
|
||||
return result;
|
||||
}
|
||||
@ -1088,7 +715,7 @@ double StringToDoubleConverter::StringToIeee(
|
||||
exponent--;
|
||||
}
|
||||
|
||||
ASSERT(buffer_pos < kBufferSize);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize);
|
||||
buffer[buffer_pos] = '\0';
|
||||
|
||||
double converted;
|
226
vendor/double-conversion/upstream/double-conversion/string-to-double.h
vendored
Normal file
226
vendor/double-conversion/upstream/double-conversion/string-to-double.h
vendored
Normal file
@ -0,0 +1,226 @@
|
||||
// Copyright 2012 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
||||
#define DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
class StringToDoubleConverter {
|
||||
public:
|
||||
// Enumeration for allowing octals and ignoring junk when converting
|
||||
// strings to numbers.
|
||||
enum Flags {
|
||||
NO_FLAGS = 0,
|
||||
ALLOW_HEX = 1,
|
||||
ALLOW_OCTALS = 2,
|
||||
ALLOW_TRAILING_JUNK = 4,
|
||||
ALLOW_LEADING_SPACES = 8,
|
||||
ALLOW_TRAILING_SPACES = 16,
|
||||
ALLOW_SPACES_AFTER_SIGN = 32,
|
||||
ALLOW_CASE_INSENSITIVITY = 64,
|
||||
ALLOW_CASE_INSENSIBILITY = 64, // Deprecated
|
||||
ALLOW_HEX_FLOATS = 128,
|
||||
};
|
||||
|
||||
static const uc16 kNoSeparator = '\0';
|
||||
|
||||
// Flags should be a bit-or combination of the possible Flags-enum.
|
||||
// - NO_FLAGS: no special flags.
|
||||
// - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers.
|
||||
// Ex: StringToDouble("0x1234") -> 4660.0
|
||||
// In StringToDouble("0x1234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK,
|
||||
// the string will not be parsed as "0" followed by junk.
|
||||
//
|
||||
// - ALLOW_OCTALS: recognizes the prefix "0" for octals:
|
||||
// If a sequence of octal digits starts with '0', then the number is
|
||||
// read as octal integer. Octal numbers may only be integers.
|
||||
// Ex: StringToDouble("01234") -> 668.0
|
||||
// StringToDouble("012349") -> 12349.0 // Not a sequence of octal
|
||||
// // digits.
|
||||
// In StringToDouble("01234.56") the characters ".56" are trailing
|
||||
// junk. The result of the call is hence dependent on
|
||||
// the ALLOW_TRAILING_JUNK flag and/or the junk value.
|
||||
// In StringToDouble("01234e56") the characters "e56" are trailing
|
||||
// junk, too.
|
||||
// - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of
|
||||
// a double literal.
|
||||
// - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces,
|
||||
// new-lines, and tabs.
|
||||
// - ALLOW_TRAILING_SPACES: ignore trailing whitespace.
|
||||
// - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign.
|
||||
// Ex: StringToDouble("- 123.2") -> -123.2.
|
||||
// StringToDouble("+ 123.2") -> 123.2
|
||||
// - ALLOW_CASE_INSENSITIVITY: ignore case of characters for special values:
|
||||
// infinity and nan.
|
||||
// - ALLOW_HEX_FLOATS: allows hexadecimal float literals.
|
||||
// This *must* start with "0x" and separate the exponent with "p".
|
||||
// Examples: 0x1.2p3 == 9.0
|
||||
// 0x10.1p0 == 16.0625
|
||||
// ALLOW_HEX and ALLOW_HEX_FLOATS are indendent.
|
||||
//
|
||||
// empty_string_value is returned when an empty string is given as input.
|
||||
// If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string
|
||||
// containing only spaces is converted to the 'empty_string_value', too.
|
||||
//
|
||||
// junk_string_value is returned when
|
||||
// a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not
|
||||
// part of a double-literal) is found.
|
||||
// b) ALLOW_TRAILING_JUNK is set, but the string does not start with a
|
||||
// double literal.
|
||||
//
|
||||
// infinity_symbol and nan_symbol are strings that are used to detect
|
||||
// inputs that represent infinity and NaN. They can be null, in which case
|
||||
// they are ignored.
|
||||
// The conversion routine first reads any possible signs. Then it compares the
|
||||
// following character of the input-string with the first character of
|
||||
// the infinity, and nan-symbol. If either matches, the function assumes, that
|
||||
// a match has been found, and expects the following input characters to match
|
||||
// the remaining characters of the special-value symbol.
|
||||
// This means that the following restrictions apply to special-value symbols:
|
||||
// - they must not start with signs ('+', or '-'),
|
||||
// - they must not have the same first character.
|
||||
// - they must not start with digits.
|
||||
//
|
||||
// If the separator character is not kNoSeparator, then that specific
|
||||
// character is ignored when in between two valid digits of the significant.
|
||||
// It is not allowed to appear in the exponent.
|
||||
// It is not allowed to lead or trail the number.
|
||||
// It is not allowed to appear twice next to each other.
|
||||
//
|
||||
// Examples:
|
||||
// flags = ALLOW_HEX | ALLOW_TRAILING_JUNK,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = "infinity",
|
||||
// nan_symbol = "nan":
|
||||
// StringToDouble("0x1234") -> 4660.0.
|
||||
// StringToDouble("0x1234K") -> 4660.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> NaN // junk_string_value.
|
||||
// StringToDouble(" 1") -> NaN // junk_string_value.
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("-123.45") -> -123.45.
|
||||
// StringToDouble("--123.45") -> NaN // junk_string_value.
|
||||
// StringToDouble("123e45") -> 123e45.
|
||||
// StringToDouble("123E45") -> 123e45.
|
||||
// StringToDouble("123e+45") -> 123e45.
|
||||
// StringToDouble("123E-45") -> 123e-45.
|
||||
// StringToDouble("123e") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("123e-") -> 123.0 // trailing junk ignored.
|
||||
// StringToDouble("+NaN") -> NaN // NaN string literal.
|
||||
// StringToDouble("-infinity") -> -inf. // infinity literal.
|
||||
// StringToDouble("Infinity") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES,
|
||||
// empty_string_value = 0.0,
|
||||
// junk_string_value = NaN,
|
||||
// infinity_symbol = NULL,
|
||||
// nan_symbol = NULL:
|
||||
// StringToDouble("0x1234") -> NaN // junk_string_value.
|
||||
// StringToDouble("01234") -> 668.0.
|
||||
// StringToDouble("") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" ") -> 0.0 // empty_string_value.
|
||||
// StringToDouble(" 1") -> 1.0
|
||||
// StringToDouble("0x") -> NaN // junk_string_value.
|
||||
// StringToDouble("0123e45") -> NaN // junk_string_value.
|
||||
// StringToDouble("01239E45") -> 1239e45.
|
||||
// StringToDouble("-infinity") -> NaN // junk_string_value.
|
||||
// StringToDouble("NaN") -> NaN // junk_string_value.
|
||||
//
|
||||
// flags = NO_FLAGS,
|
||||
// separator = ' ':
|
||||
// StringToDouble("1 2 3 4") -> 1234.0
|
||||
// StringToDouble("1 2") -> NaN // junk_string_value
|
||||
// StringToDouble("1 000 000.0") -> 1000000.0
|
||||
// StringToDouble("1.000 000") -> 1.0
|
||||
// StringToDouble("1.0e1 000") -> NaN // junk_string_value
|
||||
StringToDoubleConverter(int flags,
|
||||
double empty_string_value,
|
||||
double junk_string_value,
|
||||
const char* infinity_symbol,
|
||||
const char* nan_symbol,
|
||||
uc16 separator = kNoSeparator)
|
||||
: flags_(flags),
|
||||
empty_string_value_(empty_string_value),
|
||||
junk_string_value_(junk_string_value),
|
||||
infinity_symbol_(infinity_symbol),
|
||||
nan_symbol_(nan_symbol),
|
||||
separator_(separator) {
|
||||
}
|
||||
|
||||
// Performs the conversion.
|
||||
// The output parameter 'processed_characters_count' is set to the number
|
||||
// of characters that have been processed to read the number.
|
||||
// Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included
|
||||
// in the 'processed_characters_count'. Trailing junk is never included.
|
||||
double StringToDouble(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble above but for 16 bit characters.
|
||||
double StringToDouble(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToDouble but reads a float.
|
||||
// Note that this is not equivalent to static_cast<float>(StringToDouble(...))
|
||||
// due to potential double-rounding.
|
||||
float StringToFloat(const char* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
// Same as StringToFloat above but for 16 bit characters.
|
||||
float StringToFloat(const uc16* buffer,
|
||||
int length,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
private:
|
||||
const int flags_;
|
||||
const double empty_string_value_;
|
||||
const double junk_string_value_;
|
||||
const char* const infinity_symbol_;
|
||||
const char* const nan_symbol_;
|
||||
const uc16 separator_;
|
||||
|
||||
template <class Iterator>
|
||||
double StringToIeee(Iterator start_pointer,
|
||||
int length,
|
||||
bool read_as_double,
|
||||
int* processed_characters_count) const;
|
||||
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
|
||||
};
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
#endif // DOUBLE_CONVERSION_STRING_TO_DOUBLE_H_
|
@ -52,7 +52,7 @@ static const int kMaxDecimalPower = 309;
|
||||
static const int kMinDecimalPower = -324;
|
||||
|
||||
// 2^64 = 18446744073709551616
|
||||
static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
static const uint64_t kMaxUint64 = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
|
||||
|
||||
static const double exact_powers_of_ten[] = {
|
||||
@ -81,7 +81,7 @@ static const double exact_powers_of_ten[] = {
|
||||
// 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
|
||||
10000000000000000000000.0
|
||||
};
|
||||
static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
|
||||
static const int kExactPowersOfTenSize = DOUBLE_CONVERSION_ARRAY_SIZE(exact_powers_of_ten);
|
||||
|
||||
// Maximum number of significant digits in the decimal representation.
|
||||
// In fact the value is 772 (see conversions.cc), but to give us some margin
|
||||
@ -117,7 +117,7 @@ static void CutToMaxSignificantDigits(Vector<const char> buffer,
|
||||
}
|
||||
// The input buffer has been trimmed. Therefore the last digit must be
|
||||
// different from '0'.
|
||||
ASSERT(buffer[buffer.length() - 1] != '0');
|
||||
DOUBLE_CONVERSION_ASSERT(buffer[buffer.length() - 1] != '0');
|
||||
// Set the last digit to be non-zero. This is sufficient to guarantee
|
||||
// correct rounding.
|
||||
significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
|
||||
@ -138,7 +138,7 @@ static void TrimAndCut(Vector<const char> buffer, int exponent,
|
||||
exponent += left_trimmed.length() - right_trimmed.length();
|
||||
if (right_trimmed.length() > kMaxSignificantDecimalDigits) {
|
||||
(void) space_size; // Mark variable as used.
|
||||
ASSERT(space_size >= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(space_size >= kMaxSignificantDecimalDigits);
|
||||
CutToMaxSignificantDigits(right_trimmed, exponent,
|
||||
buffer_copy_space, updated_exponent);
|
||||
*trimmed = Vector<const char>(buffer_copy_space,
|
||||
@ -161,7 +161,7 @@ static uint64_t ReadUint64(Vector<const char> buffer,
|
||||
int i = 0;
|
||||
while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
|
||||
int digit = buffer[i++] - '0';
|
||||
ASSERT(0 <= digit && digit <= 9);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9);
|
||||
result = 10 * result + digit;
|
||||
}
|
||||
*number_of_read_digits = i;
|
||||
@ -217,14 +217,14 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
|
||||
// 10^-exponent fits into a double.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result /= exact_powers_of_ten[-exponent];
|
||||
return true;
|
||||
}
|
||||
if (0 <= exponent && exponent < kExactPowersOfTenSize) {
|
||||
// 10^exponent fits into a double.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result *= exact_powers_of_ten[exponent];
|
||||
return true;
|
||||
}
|
||||
@ -236,7 +236,7 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
// 10^remaining_digits. As a result the remaining exponent now fits
|
||||
// into a double too.
|
||||
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
||||
ASSERT(read_digits == trimmed.length());
|
||||
DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length());
|
||||
*result *= exact_powers_of_ten[remaining_digits];
|
||||
*result *= exact_powers_of_ten[exponent - remaining_digits];
|
||||
return true;
|
||||
@ -250,21 +250,21 @@ static bool DoubleStrtod(Vector<const char> trimmed,
|
||||
// Returns 10^exponent as an exact DiyFp.
|
||||
// The given exponent must be in the range [1; kDecimalExponentDistance[.
|
||||
static DiyFp AdjustmentPowerOfTen(int exponent) {
|
||||
ASSERT(0 < exponent);
|
||||
ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
|
||||
DOUBLE_CONVERSION_ASSERT(0 < exponent);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
|
||||
// Simply hardcode the remaining powers for the given decimal exponent
|
||||
// distance.
|
||||
ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
|
||||
DOUBLE_CONVERSION_ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
|
||||
switch (exponent) {
|
||||
case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60);
|
||||
case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57);
|
||||
case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54);
|
||||
case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50);
|
||||
case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47);
|
||||
case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44);
|
||||
case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40);
|
||||
case 1: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xa0000000, 00000000), -60);
|
||||
case 2: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc8000000, 00000000), -57);
|
||||
case 3: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xfa000000, 00000000), -54);
|
||||
case 4: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50);
|
||||
case 5: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc3500000, 00000000), -47);
|
||||
case 6: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xf4240000, 00000000), -44);
|
||||
case 7: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x98968000, 00000000), -40);
|
||||
default:
|
||||
UNREACHABLE();
|
||||
DOUBLE_CONVERSION_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
@ -293,7 +293,7 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
input.Normalize();
|
||||
error <<= old_e - input.e();
|
||||
|
||||
ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
|
||||
DOUBLE_CONVERSION_ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
|
||||
if (exponent < PowersOfTenCache::kMinDecimalExponent) {
|
||||
*result = 0.0;
|
||||
return true;
|
||||
@ -311,7 +311,7 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
|
||||
// The product of input with the adjustment power fits into a 64 bit
|
||||
// integer.
|
||||
ASSERT(DiyFp::kSignificandSize == 64);
|
||||
DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64);
|
||||
} else {
|
||||
// The adjustment power is exact. There is hence only an error of 0.5.
|
||||
error += kDenominator / 2;
|
||||
@ -353,8 +353,8 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
precision_digits_count -= shift_amount;
|
||||
}
|
||||
// We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
|
||||
ASSERT(DiyFp::kSignificandSize == 64);
|
||||
ASSERT(precision_digits_count < 64);
|
||||
DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64);
|
||||
DOUBLE_CONVERSION_ASSERT(precision_digits_count < 64);
|
||||
uint64_t one64 = 1;
|
||||
uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
|
||||
uint64_t precision_bits = input.f() & precision_bits_mask;
|
||||
@ -393,14 +393,14 @@ static bool DiyFpStrtod(Vector<const char> buffer,
|
||||
static int CompareBufferWithDiyFp(Vector<const char> buffer,
|
||||
int exponent,
|
||||
DiyFp diy_fp) {
|
||||
ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
|
||||
ASSERT(buffer.length() + exponent > kMinDecimalPower);
|
||||
ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent > kMinDecimalPower);
|
||||
DOUBLE_CONVERSION_ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
|
||||
// Make sure that the Bignum will be able to hold all our numbers.
|
||||
// Our Bignum implementation has a separate field for exponents. Shifts will
|
||||
// consume at most one bigit (< 64 bits).
|
||||
// ln(10) == 3.3219...
|
||||
ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
|
||||
DOUBLE_CONVERSION_ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
|
||||
Bignum buffer_bignum;
|
||||
Bignum diy_fp_bignum;
|
||||
buffer_bignum.AssignDecimalString(buffer);
|
||||
@ -446,18 +446,31 @@ static bool ComputeGuess(Vector<const char> trimmed, int exponent,
|
||||
return false;
|
||||
}
|
||||
|
||||
double Strtod(Vector<const char> buffer, int exponent) {
|
||||
char copy_buffer[kMaxSignificantDecimalDigits];
|
||||
Vector<const char> trimmed;
|
||||
int updated_exponent;
|
||||
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
||||
&trimmed, &updated_exponent);
|
||||
exponent = updated_exponent;
|
||||
static bool IsDigit(const char d) {
|
||||
return ('0' <= d) && (d <= '9');
|
||||
}
|
||||
|
||||
static bool IsNonZeroDigit(const char d) {
|
||||
return ('1' <= d) && (d <= '9');
|
||||
}
|
||||
|
||||
static bool AssertTrimmedDigits(const Vector<const char>& buffer) {
|
||||
for(int i = 0; i < buffer.length(); ++i) {
|
||||
if(!IsDigit(buffer[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (buffer.length() == 0) || (IsNonZeroDigit(buffer[0]) && IsNonZeroDigit(buffer[buffer.length()-1]));
|
||||
}
|
||||
|
||||
double StrtodTrimmed(Vector<const char> trimmed, int exponent) {
|
||||
DOUBLE_CONVERSION_ASSERT(trimmed.length() <= kMaxSignificantDecimalDigits);
|
||||
DOUBLE_CONVERSION_ASSERT(AssertTrimmedDigits(trimmed));
|
||||
double guess;
|
||||
bool is_correct = ComputeGuess(trimmed, exponent, &guess);
|
||||
if (is_correct) return guess;
|
||||
|
||||
const bool is_correct = ComputeGuess(trimmed, exponent, &guess);
|
||||
if (is_correct) {
|
||||
return guess;
|
||||
}
|
||||
DiyFp upper_boundary = Double(guess).UpperBoundary();
|
||||
int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
|
||||
if (comparison < 0) {
|
||||
@ -472,8 +485,17 @@ double Strtod(Vector<const char> buffer, int exponent) {
|
||||
}
|
||||
}
|
||||
|
||||
double Strtod(Vector<const char> buffer, int exponent) {
|
||||
char copy_buffer[kMaxSignificantDecimalDigits];
|
||||
Vector<const char> trimmed;
|
||||
int updated_exponent;
|
||||
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
||||
&trimmed, &updated_exponent);
|
||||
return StrtodTrimmed(trimmed, updated_exponent);
|
||||
}
|
||||
|
||||
static float SanitizedDoubletof(double d) {
|
||||
ASSERT(d >= 0.0);
|
||||
DOUBLE_CONVERSION_ASSERT(d >= 0.0);
|
||||
// ASAN has a sanitize check that disallows casting doubles to floats if
|
||||
// they are too big.
|
||||
// https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks
|
||||
@ -541,7 +563,7 @@ float Strtof(Vector<const char> buffer, int exponent) {
|
||||
f4 = SanitizedDoubletof(double_next2);
|
||||
}
|
||||
(void) f2; // Mark variable as used.
|
||||
ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
|
||||
DOUBLE_CONVERSION_ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
|
||||
|
||||
// If the guess doesn't lie near a single-precision boundary we can simply
|
||||
// return its float-value.
|
||||
@ -549,7 +571,7 @@ float Strtof(Vector<const char> buffer, int exponent) {
|
||||
return float_guess;
|
||||
}
|
||||
|
||||
ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
|
||||
DOUBLE_CONVERSION_ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
|
||||
(f1 == f2 && f2 != f3 && f3 == f4) ||
|
||||
(f1 == f2 && f2 == f3 && f3 != f4));
|
||||
|
||||
|
@ -40,6 +40,11 @@ double Strtod(Vector<const char> buffer, int exponent);
|
||||
// contain a dot or a sign. It must not start with '0', and must not be empty.
|
||||
float Strtof(Vector<const char> buffer, int exponent);
|
||||
|
||||
// For special use cases, the heart of the Strtod() function is also available
|
||||
// separately, it assumes that 'trimmed' is as produced by TrimAndCut(), i.e.
|
||||
// no leading or trailing zeros, also no lone zero, and not 'too many' digits.
|
||||
double StrtodTrimmed(Vector<const char> trimmed, int exponent);
|
||||
|
||||
} // namespace double_conversion
|
||||
|
||||
#endif // DOUBLE_CONVERSION_STRTOD_H_
|
||||
|
@ -32,12 +32,12 @@
|
||||
#include <cstring>
|
||||
|
||||
#include <cassert>
|
||||
#ifndef ASSERT
|
||||
#define ASSERT(condition) \
|
||||
#ifndef DOUBLE_CONVERSION_ASSERT
|
||||
#define DOUBLE_CONVERSION_ASSERT(condition) \
|
||||
assert(condition);
|
||||
#endif
|
||||
#ifndef UNIMPLEMENTED
|
||||
#define UNIMPLEMENTED() (abort())
|
||||
#ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
|
||||
#define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
|
||||
#endif
|
||||
#ifndef DOUBLE_CONVERSION_NO_RETURN
|
||||
#ifdef _MSC_VER
|
||||
@ -46,16 +46,23 @@
|
||||
#define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
|
||||
#endif
|
||||
#endif
|
||||
#ifndef UNREACHABLE
|
||||
#ifndef DOUBLE_CONVERSION_UNREACHABLE
|
||||
#ifdef _MSC_VER
|
||||
void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
|
||||
inline void abort_noreturn() { abort(); }
|
||||
#define UNREACHABLE() (abort_noreturn())
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() (abort_noreturn())
|
||||
#else
|
||||
#define UNREACHABLE() (abort())
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() (abort())
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef DOUBLE_CONVERSION_UNUSED
|
||||
#ifdef __GNUC__
|
||||
#define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
|
||||
#else
|
||||
#define DOUBLE_CONVERSION_UNUSED
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Double operations detection based on target architecture.
|
||||
// Linux uses a 80bit wide floating point stack on x86. This induces double
|
||||
@ -91,9 +98,9 @@ int main(int argc, char** argv) {
|
||||
defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
|
||||
defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
|
||||
defined(__SH4__) || defined(__alpha__) || \
|
||||
defined(_MIPS_ARCH_MIPS32R2) || \
|
||||
defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
|
||||
defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
|
||||
defined(__riscv) || \
|
||||
defined(__riscv) || defined(__e2k__) || \
|
||||
defined(__or1k__) || defined(__arc__) || \
|
||||
defined(__EMSCRIPTEN__)
|
||||
#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
|
||||
@ -133,24 +140,24 @@ typedef uint16_t uc16;
|
||||
|
||||
// The following macro works on both 32 and 64-bit platforms.
|
||||
// Usage: instead of writing 0x1234567890123456
|
||||
// write UINT64_2PART_C(0x12345678,90123456);
|
||||
#define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
// write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
|
||||
#define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
|
||||
|
||||
// The expression ARRAY_SIZE(a) is a compile-time constant of type
|
||||
// The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
|
||||
// size_t which represents the number of elements of the given
|
||||
// array. You should only use ARRAY_SIZE on statically allocated
|
||||
// array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
|
||||
// arrays.
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(a) \
|
||||
#ifndef DOUBLE_CONVERSION_ARRAY_SIZE
|
||||
#define DOUBLE_CONVERSION_ARRAY_SIZE(a) \
|
||||
((sizeof(a) / sizeof(*(a))) / \
|
||||
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
|
||||
#endif
|
||||
|
||||
// A macro to disallow the evil copy constructor and operator= functions
|
||||
// This should be used in the private: declarations for a class
|
||||
#ifndef DC_DISALLOW_COPY_AND_ASSIGN
|
||||
#define DC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
#ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
|
||||
#define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
#endif
|
||||
@ -161,33 +168,17 @@ typedef uint16_t uc16;
|
||||
// This should be used in the private: declarations for a class
|
||||
// that wants to prevent anyone from instantiating it. This is
|
||||
// especially useful for classes containing only static methods.
|
||||
#ifndef DC_DISALLOW_IMPLICIT_CONSTRUCTORS
|
||||
#define DC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
||||
#ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
|
||||
#define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
||||
TypeName(); \
|
||||
DC_DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
#endif
|
||||
|
||||
namespace double_conversion {
|
||||
|
||||
static const int kCharSize = sizeof(char);
|
||||
|
||||
// Returns the maximum of the two parameters.
|
||||
template <typename T>
|
||||
static T Max(T a, T b) {
|
||||
return a < b ? b : a;
|
||||
}
|
||||
|
||||
|
||||
// Returns the minimum of the two parameters.
|
||||
template <typename T>
|
||||
static T Min(T a, T b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
|
||||
inline int StrLength(const char* string) {
|
||||
size_t length = strlen(string);
|
||||
ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
|
||||
DOUBLE_CONVERSION_ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
|
||||
return static_cast<int>(length);
|
||||
}
|
||||
|
||||
@ -197,15 +188,15 @@ class Vector {
|
||||
public:
|
||||
Vector() : start_(NULL), length_(0) {}
|
||||
Vector(T* data, int len) : start_(data), length_(len) {
|
||||
ASSERT(len == 0 || (len > 0 && data != NULL));
|
||||
DOUBLE_CONVERSION_ASSERT(len == 0 || (len > 0 && data != NULL));
|
||||
}
|
||||
|
||||
// Returns a vector using the same backing storage as this one,
|
||||
// spanning from and including 'from', to but not including 'to'.
|
||||
Vector<T> SubVector(int from, int to) {
|
||||
ASSERT(to <= length_);
|
||||
ASSERT(from < to);
|
||||
ASSERT(0 <= from);
|
||||
DOUBLE_CONVERSION_ASSERT(to <= length_);
|
||||
DOUBLE_CONVERSION_ASSERT(from < to);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= from);
|
||||
return Vector<T>(start() + from, to - from);
|
||||
}
|
||||
|
||||
@ -220,7 +211,7 @@ class Vector {
|
||||
|
||||
// Access individual vector elements - checks bounds in debug mode.
|
||||
T& operator[](int index) const {
|
||||
ASSERT(0 <= index && index < length_);
|
||||
DOUBLE_CONVERSION_ASSERT(0 <= index && index < length_);
|
||||
return start_[index];
|
||||
}
|
||||
|
||||
@ -228,6 +219,11 @@ class Vector {
|
||||
|
||||
T& last() { return start_[length_ - 1]; }
|
||||
|
||||
void pop_back() {
|
||||
DOUBLE_CONVERSION_ASSERT(!is_empty());
|
||||
--length_;
|
||||
}
|
||||
|
||||
private:
|
||||
T* start_;
|
||||
int length_;
|
||||
@ -248,7 +244,7 @@ class StringBuilder {
|
||||
|
||||
// Get the current position in the builder.
|
||||
int position() const {
|
||||
ASSERT(!is_finalized());
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized());
|
||||
return position_;
|
||||
}
|
||||
|
||||
@ -259,8 +255,8 @@ class StringBuilder {
|
||||
// 0-characters; use the Finalize() method to terminate the string
|
||||
// instead.
|
||||
void AddCharacter(char c) {
|
||||
ASSERT(c != '\0');
|
||||
ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(c != '\0');
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
buffer_[position_++] = c;
|
||||
}
|
||||
|
||||
@ -273,9 +269,9 @@ class StringBuilder {
|
||||
// Add the first 'n' characters of the given string 's' to the
|
||||
// builder. The input string must have enough characters.
|
||||
void AddSubstring(const char* s, int n) {
|
||||
ASSERT(!is_finalized() && position_ + n < buffer_.length());
|
||||
ASSERT(static_cast<size_t>(n) <= strlen(s));
|
||||
memmove(&buffer_[position_], s, n * kCharSize);
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ + n < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n) <= strlen(s));
|
||||
memmove(&buffer_[position_], s, n);
|
||||
position_ += n;
|
||||
}
|
||||
|
||||
@ -290,13 +286,13 @@ class StringBuilder {
|
||||
|
||||
// Finalize the string by 0-terminating it and returning the buffer.
|
||||
char* Finalize() {
|
||||
ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
|
||||
buffer_[position_] = '\0';
|
||||
// Make sure nobody managed to add a 0-character to the
|
||||
// buffer while building the string.
|
||||
ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
|
||||
DOUBLE_CONVERSION_ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
|
||||
position_ = -1;
|
||||
ASSERT(is_finalized());
|
||||
DOUBLE_CONVERSION_ASSERT(is_finalized());
|
||||
return buffer_.start();
|
||||
}
|
||||
|
||||
@ -306,7 +302,7 @@ class StringBuilder {
|
||||
|
||||
bool is_finalized() const { return position_ < 0; }
|
||||
|
||||
DC_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
|
||||
DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
|
||||
};
|
||||
|
||||
// The type-based aliasing rule allows the compiler to assume that pointers of
|
||||
@ -334,13 +330,14 @@ class StringBuilder {
|
||||
// enough that it can no longer see that you have cast one pointer type to
|
||||
// another thus avoiding the warning.
|
||||
template <class Dest, class Source>
|
||||
inline Dest BitCast(const Source& source) {
|
||||
Dest BitCast(const Source& source) {
|
||||
// Compile time assertion: sizeof(Dest) == sizeof(Source)
|
||||
// A compile error here means your Dest and Source have different sizes.
|
||||
#if __cplusplus >= 201103L
|
||||
static_assert(sizeof(Dest) == sizeof(Source),
|
||||
"source and destination size mismatch");
|
||||
#else
|
||||
DOUBLE_CONVERSION_UNUSED
|
||||
typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
|
||||
#endif
|
||||
|
||||
@ -350,7 +347,7 @@ inline Dest BitCast(const Source& source) {
|
||||
}
|
||||
|
||||
template <class Dest, class Source>
|
||||
inline Dest BitCast(Source* source) {
|
||||
Dest BitCast(Source* source) {
|
||||
return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,7 @@ int main(int argc, char* argv[]) {
|
||||
test = test->prev();
|
||||
}
|
||||
}
|
||||
delete[] arg_copy;
|
||||
free(arg_copy);
|
||||
}
|
||||
}
|
||||
if (print_run_count && tests_run != 1)
|
||||
|
@ -35,21 +35,21 @@
|
||||
extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
|
||||
void API_Fatal(const char* location, const char* format, ...);
|
||||
|
||||
// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
|
||||
// The FATAL, DOUBLE_CONVERSION_UNREACHABLE and DOUBLE_CONVERSION_UNIMPLEMENTED macros are useful during
|
||||
// development, but they should not be relied on in the final product.
|
||||
#ifdef DEBUG
|
||||
#define FATAL(msg) \
|
||||
V8_Fatal(__FILE__, __LINE__, "%s", (msg))
|
||||
#define UNIMPLEMENTED() \
|
||||
#define DOUBLE_CONVERSION_UNIMPLEMENTED() \
|
||||
V8_Fatal(__FILE__, __LINE__, "unimplemented code")
|
||||
#define UNREACHABLE() \
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() \
|
||||
V8_Fatal(__FILE__, __LINE__, "unreachable code")
|
||||
#else
|
||||
#define FATAL(msg) \
|
||||
V8_Fatal("", 0, "%s", (msg))
|
||||
#define UNIMPLEMENTED() \
|
||||
#define DOUBLE_CONVERSION_UNIMPLEMENTED() \
|
||||
V8_Fatal("", 0, "unimplemented code")
|
||||
#define UNREACHABLE() ((void) 0)
|
||||
#define DOUBLE_CONVERSION_UNREACHABLE() ((void) 0)
|
||||
#endif
|
||||
|
||||
|
||||
@ -279,36 +279,36 @@ template <int> class StaticAssertionHelper { };
|
||||
SEMI_STATIC_JOIN(__StaticAssertTypedef__, __LINE__)
|
||||
|
||||
|
||||
// The ASSERT macro is equivalent to CHECK except that it only
|
||||
// The DOUBLE_CONVERSION_ASSERT macro is equivalent to CHECK except that it only
|
||||
// generates code in debug builds.
|
||||
#ifdef DEBUG
|
||||
#define ASSERT_RESULT(expr) CHECK(expr)
|
||||
#define ASSERT(condition) CHECK(condition)
|
||||
#define ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
|
||||
#define ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
|
||||
#define ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
|
||||
#define SLOW_ASSERT(condition) if (FLAG_enable_slow_asserts) CHECK(condition)
|
||||
#define DOUBLE_CONVERSION_ASSERT_RESULT(expr) CHECK(expr)
|
||||
#define DOUBLE_CONVERSION_ASSERT(condition) CHECK(condition)
|
||||
#define DOUBLE_CONVERSION_ASSERT_EQ(v1, v2) CHECK_EQ(v1, v2)
|
||||
#define DOUBLE_CONVERSION_ASSERT_NE(v1, v2) CHECK_NE(v1, v2)
|
||||
#define DOUBLE_CONVERSION_ASSERT_GE(v1, v2) CHECK_GE(v1, v2)
|
||||
#define SLOW_DOUBLE_CONVERSION_ASSERT(condition) if (FLAG_enable_slow_asserts) CHECK(condition)
|
||||
#else
|
||||
#define ASSERT_RESULT(expr) (expr)
|
||||
#define ASSERT(condition) ((void) 0)
|
||||
#define ASSERT_EQ(v1, v2) ((void) 0)
|
||||
#define ASSERT_NE(v1, v2) ((void) 0)
|
||||
#define ASSERT_GE(v1, v2) ((void) 0)
|
||||
#define SLOW_ASSERT(condition) ((void) 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_RESULT(expr) (expr)
|
||||
#define DOUBLE_CONVERSION_ASSERT(condition) ((void) 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_EQ(v1, v2) ((void) 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_NE(v1, v2) ((void) 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_GE(v1, v2) ((void) 0)
|
||||
#define SLOW_DOUBLE_CONVERSION_ASSERT(condition) ((void) 0)
|
||||
#endif
|
||||
// Static asserts has no impact on runtime performance, so they can be
|
||||
// safely enabled in release mode. Moreover, the ((void) 0) expression
|
||||
// obeys different syntax rules than typedef's, e.g. it can't appear
|
||||
// inside class declaration, this leads to inconsistency between debug
|
||||
// and release compilation modes behaviour.
|
||||
#define STATIC_ASSERT(test) STATIC_CHECK(test)
|
||||
#define STATIC_DOUBLE_CONVERSION_ASSERT(test) STATIC_CHECK(test)
|
||||
|
||||
|
||||
#define ASSERT_TAG_ALIGNED(address) \
|
||||
ASSERT((reinterpret_cast<intptr_t>(address) & kHeapObjectTagMask) == 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_TAG_ALIGNED(address) \
|
||||
DOUBLE_CONVERSION_ASSERT((reinterpret_cast<intptr_t>(address) & kHeapObjectTagMask) == 0)
|
||||
|
||||
#define ASSERT_SIZE_TAG_ALIGNED(size) ASSERT((size & kHeapObjectTagMask) == 0)
|
||||
#define DOUBLE_CONVERSION_ASSERT_SIZE_TAG_ALIGNED(size) DOUBLE_CONVERSION_ASSERT((size & kHeapObjectTagMask) == 0)
|
||||
|
||||
#define ASSERT_NOT_NULL(p) ASSERT_NE(NULL, p)
|
||||
#define DOUBLE_CONVERSION_ASSERT_NOT_NULL(p) DOUBLE_CONVERSION_ASSERT_NE(NULL, p)
|
||||
|
||||
#endif // V8_CHECKS_H_
|
||||
|
@ -196,7 +196,7 @@ TEST(BignumDtoaVariousDoubles) {
|
||||
CHECK_EQ("1", buffer.start());
|
||||
CHECK_EQ(-22, point);
|
||||
|
||||
uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
|
||||
uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
double v = Double(smallest_normal64).value();
|
||||
BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||
CHECK_EQ("22250738585072014", buffer.start());
|
||||
@ -208,7 +208,7 @@ TEST(BignumDtoaVariousDoubles) {
|
||||
CHECK_EQ("22250738585072013831", buffer.start());
|
||||
CHECK_EQ(-307, point);
|
||||
|
||||
uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
v = Double(largest_denormal64).value();
|
||||
BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||
CHECK_EQ("2225073858507201", buffer.start());
|
||||
|
@ -81,12 +81,12 @@ TEST(Assign) {
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("12345678", buffer);
|
||||
|
||||
uint64_t big = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
uint64_t big = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
||||
bignum.AssignUInt64(big);
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("FFFFFFFFFFFFFFFF", buffer);
|
||||
|
||||
big = UINT64_2PART_C(0x12345678, 9ABCDEF0);
|
||||
big = DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678, 9ABCDEF0);
|
||||
bignum.AssignUInt64(big);
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("123456789ABCDEF0", buffer);
|
||||
@ -206,49 +206,49 @@ TEST(AddUInt64) {
|
||||
CHECK_EQ("1000000000000000000000FFFF", buffer);
|
||||
|
||||
AssignHexString(&bignum, "0");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0xA, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xA, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("A00000000", buffer);
|
||||
|
||||
AssignHexString(&bignum, "1");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0xA, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xA, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("A00000001", buffer);
|
||||
|
||||
AssignHexString(&bignum, "1");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0x100, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0x100, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000001", buffer);
|
||||
|
||||
AssignHexString(&bignum, "1");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0xFFFF, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFF, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("FFFF00000001", buffer);
|
||||
|
||||
AssignHexString(&bignum, "FFFFFFF");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0x1, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0x1, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10FFFFFFF", buffer);
|
||||
|
||||
AssignHexString(&bignum, "10000000000000000000000000000000000000000000");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0xFFFF, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFF, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000000000000000000000000000FFFF00000000", buffer);
|
||||
|
||||
AssignHexString(&bignum, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
||||
bignum.AddUInt64(UINT64_2PART_C(0x1, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0x1, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("1000000000000000000000000000000000000FFFFFFFF", buffer);
|
||||
|
||||
bignum.AssignUInt16(0x1);
|
||||
bignum.ShiftLeft(100);
|
||||
bignum.AddUInt64(UINT64_2PART_C(0x1, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0x1, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000000000000100000000", buffer);
|
||||
|
||||
bignum.AssignUInt16(0x1);
|
||||
bignum.ShiftLeft(100);
|
||||
bignum.AddUInt64(UINT64_2PART_C(0xFFFF, 00000000));
|
||||
bignum.AddUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFF, 00000000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000000000FFFF00000000", buffer);
|
||||
}
|
||||
@ -314,9 +314,13 @@ TEST(AddBignum) {
|
||||
CHECK_EQ("10000000000001000000000000", buffer);
|
||||
|
||||
other.ShiftLeft(64);
|
||||
// other == "10000000000000000000000000000"
|
||||
CHECK(other.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000000000000000000000000", buffer);
|
||||
|
||||
bignum.AssignUInt16(0x1);
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("1", buffer);
|
||||
|
||||
bignum.AddBignum(other);
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("10000000000000000000000000001", buffer);
|
||||
@ -570,7 +574,7 @@ TEST(MultiplyUInt64) {
|
||||
CHECK_EQ("FFFF00000000000000", buffer);
|
||||
|
||||
AssignHexString(&bignum, "100000000000000");
|
||||
bignum.MultiplyByUInt64(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
bignum.MultiplyByUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("FFFFFFFFFFFFFFFF00000000000000", buffer);
|
||||
|
||||
@ -580,7 +584,7 @@ TEST(MultiplyUInt64) {
|
||||
CHECK_EQ("12333335552433", buffer);
|
||||
|
||||
AssignHexString(&bignum, "1234567ABCD");
|
||||
bignum.MultiplyByUInt64(UINT64_2PART_C(0xFF, FFFFFFFF));
|
||||
bignum.MultiplyByUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFF, FFFFFFFF));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("1234567ABCBDCBA985433", buffer);
|
||||
|
||||
@ -600,7 +604,7 @@ TEST(MultiplyUInt64) {
|
||||
CHECK_EQ("EFFFFFFFFFFFFFFF1", buffer);
|
||||
|
||||
AssignHexString(&bignum, "FFFFFFFFFFFFFFFF");
|
||||
bignum.MultiplyByUInt64(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
bignum.MultiplyByUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("FFFFFFFFFFFFFFFE0000000000000001", buffer);
|
||||
|
||||
@ -635,12 +639,12 @@ TEST(MultiplyUInt64) {
|
||||
bignum.AssignUInt16(0xFFFF);
|
||||
bignum.ShiftLeft(100);
|
||||
// "FFFF0 0000 0000 0000 0000 0000 0000"
|
||||
bignum.MultiplyByUInt64(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
bignum.MultiplyByUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("FFFEFFFFFFFFFFFF00010000000000000000000000000", buffer);
|
||||
|
||||
AssignDecimalString(&bignum, "15611230384529777");
|
||||
bignum.MultiplyByUInt64(UINT64_2PART_C(0x8ac72304, 89e80000));
|
||||
bignum.MultiplyByUInt64(DOUBLE_CONVERSION_UINT64_2PART_C(0x8ac72304, 89e80000));
|
||||
CHECK(bignum.ToHexString(buffer, kBufferSize));
|
||||
CHECK_EQ("1E10EE4B11D15A7F3DE7F3C7680000", buffer);
|
||||
}
|
||||
|
@ -71,6 +71,90 @@ TEST(DoubleToShortest) {
|
||||
CHECK(dc.ToShortest(-0.0, &builder));
|
||||
CHECK_EQ("0", builder.Finalize());
|
||||
|
||||
// Test min_exponent_width
|
||||
flags = DoubleToStringConverter::UNIQUE_ZERO |
|
||||
DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN;
|
||||
DoubleToStringConverter dcExpWidth2(flags, NULL, NULL, 'e', -4, 6, 0, 0, 2);
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(11111111111.0, &builder));
|
||||
CHECK_EQ("1.1111111111e+10", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(1111111111.0, &builder));
|
||||
CHECK_EQ("1.111111111e+09", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(1111111.0, &builder));
|
||||
CHECK_EQ("1.111111e+06", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(111111.0, &builder));
|
||||
CHECK_EQ("111111", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(10000000000.0, &builder));
|
||||
CHECK_EQ("1e+10", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth2.ToShortest(1000000000.0, &builder));
|
||||
CHECK_EQ("1e+09", builder.Finalize());
|
||||
|
||||
DoubleToStringConverter dcExpWidth0(flags, NULL, NULL, 'e', -4, 6, 0, 0, 0);
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(11111111111.0, &builder));
|
||||
CHECK_EQ("1.1111111111e+10", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(1111111111.0, &builder));
|
||||
CHECK_EQ("1.111111111e+9", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(1111111.0, &builder));
|
||||
CHECK_EQ("1.111111e+6", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(111111.0, &builder));
|
||||
CHECK_EQ("111111", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(10000000000.0, &builder));
|
||||
CHECK_EQ("1e+10", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth0.ToShortest(1000000000.0, &builder));
|
||||
CHECK_EQ("1e+9", builder.Finalize());
|
||||
|
||||
// Set min_exponent_width to 100 is equal to 5,
|
||||
// as kMaxExponentLength is defined to 5 in double-to-string.cc
|
||||
DoubleToStringConverter dcExpWidth100(flags, NULL, NULL, 'e', -4, 6, 0, 0, 100);
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(11111111111.0, &builder));
|
||||
CHECK_EQ("1.1111111111e+00010", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(1111111111.0, &builder));
|
||||
CHECK_EQ("1.111111111e+00009", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(1111111.0, &builder));
|
||||
CHECK_EQ("1.111111e+00006", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(111111.0, &builder));
|
||||
CHECK_EQ("111111", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(10000000000.0, &builder));
|
||||
CHECK_EQ("1e+00010", builder.Finalize());
|
||||
|
||||
builder.Reset();
|
||||
CHECK(dcExpWidth100.ToShortest(1000000000.0, &builder));
|
||||
CHECK_EQ("1e+00009", builder.Finalize());
|
||||
// End of min_exponent_width testing
|
||||
|
||||
flags = DoubleToStringConverter::NO_FLAGS;
|
||||
DoubleToStringConverter dc2(flags, NULL, NULL, 'e', -1, 1, 0, 0);
|
||||
builder.Reset();
|
||||
@ -352,8 +436,8 @@ TEST(DoubleToFixed) {
|
||||
CHECK(dc.ToFixed(-0.0, 1, &builder));
|
||||
CHECK_EQ("0.0", builder.Finalize());
|
||||
|
||||
ASSERT(DoubleToStringConverter::kMaxFixedDigitsBeforePoint == 60);
|
||||
ASSERT(DoubleToStringConverter::kMaxFixedDigitsAfterPoint == 60);
|
||||
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxFixedDigitsBeforePoint == 60);
|
||||
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxFixedDigitsAfterPoint == 60);
|
||||
builder.Reset();
|
||||
CHECK(dc.ToFixed(
|
||||
0.0, DoubleToStringConverter::kMaxFixedDigitsAfterPoint, &builder));
|
||||
@ -638,7 +722,7 @@ TEST(DoubleToExponential) {
|
||||
CHECK(dc.ToExponential(-0.0, 2, &builder));
|
||||
CHECK_EQ("0.00e+0", builder.Finalize());
|
||||
|
||||
ASSERT(DoubleToStringConverter::kMaxExponentialDigits == 120);
|
||||
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxExponentialDigits == 120);
|
||||
builder.Reset();
|
||||
CHECK(dc.ToExponential(
|
||||
0.0, DoubleToStringConverter::kMaxExponentialDigits, &builder));
|
||||
@ -765,7 +849,7 @@ TEST(DoubleToPrecision) {
|
||||
0, 0, // Padding zeroes for shortest mode.
|
||||
6, 0); // Padding zeroes for precision mode.
|
||||
|
||||
ASSERT(DoubleToStringConverter::kMinPrecisionDigits == 1);
|
||||
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMinPrecisionDigits == 1);
|
||||
CHECK(dc.ToPrecision(0.0, 1, &builder));
|
||||
CHECK_EQ("0", builder.Finalize());
|
||||
|
||||
@ -781,7 +865,7 @@ TEST(DoubleToPrecision) {
|
||||
CHECK(dc.ToPrecision(-0.0, 2, &builder));
|
||||
CHECK_EQ("0.0", builder.Finalize());
|
||||
|
||||
ASSERT(DoubleToStringConverter::kMaxPrecisionDigits == 120);
|
||||
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxPrecisionDigits == 120);
|
||||
builder.Reset();
|
||||
CHECK(dc.ToPrecision(
|
||||
0.0, DoubleToStringConverter::kMaxPrecisionDigits, &builder));
|
||||
@ -1752,7 +1836,7 @@ static double StrToD16(const char* str, int flags,
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT(length < 256);
|
||||
DOUBLE_CONVERSION_ASSERT(length < 256);
|
||||
StringToDoubleConverter converter(flags, empty_string_value, Double::NaN(),
|
||||
NULL, NULL, separator);
|
||||
double result =
|
||||
@ -1773,7 +1857,7 @@ static double StrToD(const char* str, int flags, double empty_string_value,
|
||||
((strlen(str) == static_cast<unsigned>(*processed_characters_count)));
|
||||
|
||||
uc16 buffer16[256];
|
||||
ASSERT(strlen(str) < ARRAY_SIZE(buffer16));
|
||||
DOUBLE_CONVERSION_ASSERT(strlen(str) < DOUBLE_CONVERSION_ARRAY_SIZE(buffer16));
|
||||
int len = strlen(str);
|
||||
for (int i = 0; i < len; i++) {
|
||||
buffer16[i] = str[i];
|
||||
@ -2682,6 +2766,71 @@ TEST(StringToDoubleHexString) {
|
||||
|
||||
CHECK_EQ(-0.0, StrToD("-0x1p-2000", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD(" ", flags, Double::NaN(),
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("0x", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD(" 0x ", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD(" 0x 3", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("0x3g", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("x3", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD(" 0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("+ 0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("+", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("-", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("- -0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("- +0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("+ +0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("0xp1", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::NaN(), StrToD("0x.p1", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Double::Infinity(), StrToD("0x1.p10000000000000000", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(0.0, StrToD("0x1.p-10000000000000000", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
}
|
||||
|
||||
|
||||
@ -3833,7 +3982,7 @@ static double StrToF(const char* str, int flags, double empty_string_value,
|
||||
((strlen(str) == static_cast<unsigned>(*processed_characters_count)));
|
||||
|
||||
uc16 buffer16[256];
|
||||
ASSERT(strlen(str) < ARRAY_SIZE(buffer16));
|
||||
DOUBLE_CONVERSION_ASSERT(strlen(str) < DOUBLE_CONVERSION_ARRAY_SIZE(buffer16));
|
||||
int len = strlen(str);
|
||||
for (int i = 0; i < len; i++) {
|
||||
buffer16[i] = str[i];
|
||||
@ -4669,6 +4818,156 @@ TEST(StringToFloatHexString) {
|
||||
CHECK_EQ(Single::NaN(), StrToF("x3", flags, 0.0f,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
flags = StringToDoubleConverter::ALLOW_HEX_FLOATS;
|
||||
|
||||
CHECK_EQ(3.0f, StrToF("0x3p0", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(0.0f, StrToF("0x.0p0", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(3.0f, StrToF("0x3.0p0", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(3.0f, StrToF("0x3.p0", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(-5634002804104940178441764864.0f, StrToF("-0x123456789012345678901234p0",
|
||||
flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(134217728.0f, StrToF("0x8000001p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(134217728.0f, StrToF("0x8000000p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755813888.0f, StrToF("0x8000000001p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755813888.0f, StrToF("0x8000000000p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755879424.0f, StrToF("0x8000008001p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755813888.0f, StrToF("0x8000008000p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755944960.0f, StrToF("0x8000018001p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(549755944960.0f, StrToF("0x8000018000p0", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(8796093022208.0f, StrToF("0x8000000001p4", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(8796093022208.0f, StrToF("0x8000000000p+4", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(8796094070784.0f, StrToF("0x8000008001p04", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(34359738368.0f, StrToF("0x8000008000p-4", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(34359746560.0f, StrToF("0x8000018001p-04", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(8796095119360.0f, StrToF("0x8000018000p4", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(Single::Infinity(), StrToF("0x1p2000", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(0.0f, StrToF("0x1p-2000", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(-0.0f, StrToF("-0x1p-2000", flags, 0.0, &processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF(" ", flags, Single::NaN(),
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("0x", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF(" 0x ", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF(" 0x 3", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("0x3g", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("x3", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF(" 0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("+ 0x3 foo", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("+", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("-", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("- -0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("- +0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("+ +0x5", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("0xp1", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::NaN(), StrToF("0x.p1", flags, 0.0, &processed, &all_used));
|
||||
CHECK_EQ(0, processed);
|
||||
|
||||
CHECK_EQ(Single::Infinity(), StrToF("0x1.p10000000000000000", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
|
||||
CHECK_EQ(0.0f, StrToF("0x1.p-10000000000000000", flags, 0.0,
|
||||
&processed, &all_used));
|
||||
CHECK(all_used);
|
||||
}
|
||||
|
||||
|
||||
@ -5316,7 +5615,7 @@ TEST(StringToDoubleFloatWhitespace) {
|
||||
kFigureSpace, kPunctuationSpace, kThinSpace, kHairSpace,
|
||||
kNarrowNoBreakSpace, kMediumMathematicalSpace, kIdeographicSpace,
|
||||
};
|
||||
const int kWhitespace16Length = ARRAY_SIZE(kWhitespace16);
|
||||
const int kWhitespace16Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespace16);
|
||||
CHECK_EQ(-1.2, StrToD16(kWhitespace16, kWhitespace16Length, flags,
|
||||
Double::NaN(),
|
||||
&processed, &all_used));
|
||||
@ -5331,7 +5630,7 @@ TEST(StringToDoubleFloatWhitespace) {
|
||||
TEST(StringToDoubleCaseInsensitiveSpecialValues) {
|
||||
int processed = 0;
|
||||
|
||||
int flags = StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY |
|
||||
int flags = StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY |
|
||||
StringToDoubleConverter::ALLOW_LEADING_SPACES |
|
||||
StringToDoubleConverter::ALLOW_TRAILING_JUNK |
|
||||
StringToDoubleConverter::ALLOW_TRAILING_SPACES;
|
||||
|
@ -34,20 +34,20 @@ TEST(Multiply) {
|
||||
CHECK(0 == diy_fp1.f()); // NOLINT
|
||||
CHECK_EQ(64, diy_fp1.e());
|
||||
|
||||
diy_fp1 = DiyFp(UINT64_2PART_C(0x80000000, 00000000), 11);
|
||||
diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000), 11);
|
||||
diy_fp2 = DiyFp(2, 13);
|
||||
product = DiyFp::Times(diy_fp1, diy_fp2);
|
||||
CHECK(1 == product.f()); // NOLINT
|
||||
CHECK_EQ(11 + 13 + 64, product.e());
|
||||
|
||||
// Test rounding.
|
||||
diy_fp1 = DiyFp(UINT64_2PART_C(0x80000000, 00000001), 11);
|
||||
diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000001), 11);
|
||||
diy_fp2 = DiyFp(1, 13);
|
||||
product = DiyFp::Times(diy_fp1, diy_fp2);
|
||||
CHECK(1 == product.f()); // NOLINT
|
||||
CHECK_EQ(11 + 13 + 64, product.e());
|
||||
|
||||
diy_fp1 = DiyFp(UINT64_2PART_C(0x7fffffff, ffffffff), 11);
|
||||
diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x7fffffff, ffffffff), 11);
|
||||
diy_fp2 = DiyFp(1, 13);
|
||||
product = DiyFp::Times(diy_fp1, diy_fp2);
|
||||
CHECK(0 == product.f()); // NOLINT
|
||||
@ -56,10 +56,10 @@ TEST(Multiply) {
|
||||
// Halfway cases are allowed to round either way. So don't check for it.
|
||||
|
||||
// Big numbers.
|
||||
diy_fp1 = DiyFp(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 11);
|
||||
diy_fp2 = DiyFp(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 13);
|
||||
diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 11);
|
||||
diy_fp2 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 13);
|
||||
// 128bit result: 0xfffffffffffffffe0000000000000001
|
||||
product = DiyFp::Times(diy_fp1, diy_fp2);
|
||||
CHECK(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFe) == product.f());
|
||||
CHECK(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFe) == product.f());
|
||||
CHECK_EQ(11 + 13 + 64, product.e());
|
||||
}
|
||||
|
@ -269,7 +269,7 @@ TEST(DtoaVariousDoubles) {
|
||||
CHECK_EQ("35844466", buffer.start());
|
||||
CHECK_EQ(299, point);
|
||||
|
||||
uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
|
||||
uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
double v = Double(smallest_normal64).value();
|
||||
DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
|
||||
CHECK_EQ("22250738585072014", buffer.start());
|
||||
@ -287,7 +287,7 @@ TEST(DtoaVariousDoubles) {
|
||||
CHECK_EQ("22250738585072013831", buffer.start());
|
||||
CHECK_EQ(-307, point);
|
||||
|
||||
uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
v = Double(largest_denormal64).value();
|
||||
DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
|
||||
CHECK_EQ("2225073858507201", buffer.start());
|
||||
|
@ -79,7 +79,7 @@ TEST(FastDtoaShortestVariousDoubles) {
|
||||
CHECK_EQ(299, point);
|
||||
}
|
||||
|
||||
uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
|
||||
uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
double v = Double(smallest_normal64).value();
|
||||
status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||
if (status) {
|
||||
@ -87,7 +87,7 @@ TEST(FastDtoaShortestVariousDoubles) {
|
||||
CHECK_EQ(-307, point);
|
||||
}
|
||||
|
||||
uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
v = Double(largest_denormal64).value();
|
||||
status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||
if (status) {
|
||||
@ -244,14 +244,14 @@ TEST(FastDtoaPrecisionVariousDoubles) {
|
||||
CHECK_EQ("35844466", buffer.start());
|
||||
CHECK_EQ(299, point);
|
||||
|
||||
uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
|
||||
uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
double v = Double(smallest_normal64).value();
|
||||
status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
|
||||
CHECK(status);
|
||||
CHECK_EQ("22250738585072014", buffer.start());
|
||||
CHECK_EQ(-307, point);
|
||||
|
||||
uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
v = Double(largest_denormal64).value();
|
||||
status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
|
||||
CHECK(status);
|
||||
|
@ -13,13 +13,13 @@ using namespace double_conversion;
|
||||
|
||||
TEST(Uint64Conversions) {
|
||||
// Start by checking the byte-order.
|
||||
uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
uint64_t ordered = DOUBLE_CONVERSION_UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
CHECK_EQ(3512700564088504e-318, Double(ordered).value());
|
||||
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
CHECK_EQ(5e-324, Double(min_double64).value());
|
||||
|
||||
uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
uint64_t max_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
CHECK_EQ(1.7976931348623157e308, Double(max_double64).value());
|
||||
}
|
||||
|
||||
@ -38,22 +38,22 @@ TEST(Uint32Conversions) {
|
||||
|
||||
|
||||
TEST(Double_AsDiyFp) {
|
||||
uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
uint64_t ordered = DOUBLE_CONVERSION_UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
DiyFp diy_fp = Double(ordered).AsDiyFp();
|
||||
CHECK_EQ(0x12 - 0x3FF - 52, diy_fp.e());
|
||||
// The 52 mantissa bits, plus the implicit 1 in bit 52 as a UINT64.
|
||||
CHECK(UINT64_2PART_C(0x00134567, 89ABCDEF) == diy_fp.f()); // NOLINT
|
||||
CHECK(DOUBLE_CONVERSION_UINT64_2PART_C(0x00134567, 89ABCDEF) == diy_fp.f()); // NOLINT
|
||||
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
diy_fp = Double(min_double64).AsDiyFp();
|
||||
CHECK_EQ(-0x3FF - 52 + 1, diy_fp.e());
|
||||
// This is a denormal; so no hidden bit.
|
||||
CHECK(1 == diy_fp.f()); // NOLINT
|
||||
|
||||
uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
uint64_t max_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
diy_fp = Double(max_double64).AsDiyFp();
|
||||
CHECK_EQ(0x7FE - 0x3FF - 52, diy_fp.e());
|
||||
CHECK(UINT64_2PART_C(0x001fffff, ffffffff) == diy_fp.f()); // NOLINT
|
||||
CHECK(DOUBLE_CONVERSION_UINT64_2PART_C(0x001fffff, ffffffff) == diy_fp.f()); // NOLINT
|
||||
}
|
||||
|
||||
|
||||
@ -78,32 +78,32 @@ TEST(Single_AsDiyFp) {
|
||||
|
||||
|
||||
TEST(AsNormalizedDiyFp) {
|
||||
uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
uint64_t ordered = DOUBLE_CONVERSION_UINT64_2PART_C(0x01234567, 89ABCDEF);
|
||||
DiyFp diy_fp = Double(ordered).AsNormalizedDiyFp();
|
||||
CHECK_EQ(0x12 - 0x3FF - 52 - 11, diy_fp.e());
|
||||
CHECK((UINT64_2PART_C(0x00134567, 89ABCDEF) << 11) ==
|
||||
CHECK((DOUBLE_CONVERSION_UINT64_2PART_C(0x00134567, 89ABCDEF) << 11) ==
|
||||
diy_fp.f()); // NOLINT
|
||||
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
diy_fp = Double(min_double64).AsNormalizedDiyFp();
|
||||
CHECK_EQ(-0x3FF - 52 + 1 - 63, diy_fp.e());
|
||||
// This is a denormal; so no hidden bit.
|
||||
CHECK(UINT64_2PART_C(0x80000000, 00000000) == diy_fp.f()); // NOLINT
|
||||
CHECK(DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000) == diy_fp.f()); // NOLINT
|
||||
|
||||
uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
uint64_t max_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
diy_fp = Double(max_double64).AsNormalizedDiyFp();
|
||||
CHECK_EQ(0x7FE - 0x3FF - 52 - 11, diy_fp.e());
|
||||
CHECK((UINT64_2PART_C(0x001fffff, ffffffff) << 11) ==
|
||||
CHECK((DOUBLE_CONVERSION_UINT64_2PART_C(0x001fffff, ffffffff) << 11) ==
|
||||
diy_fp.f()); // NOLINT
|
||||
}
|
||||
|
||||
|
||||
TEST(Double_IsDenormal) {
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
CHECK(Double(min_double64).IsDenormal());
|
||||
uint64_t bits = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t bits = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
CHECK(Double(bits).IsDenormal());
|
||||
bits = UINT64_2PART_C(0x00100000, 00000000);
|
||||
bits = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
CHECK(!Double(bits).IsDenormal());
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ TEST(Double_IsSpecial) {
|
||||
CHECK(Double(Double::Infinity()).IsSpecial());
|
||||
CHECK(Double(-Double::Infinity()).IsSpecial());
|
||||
CHECK(Double(Double::NaN()).IsSpecial());
|
||||
uint64_t bits = UINT64_2PART_C(0xFFF12345, 00000000);
|
||||
uint64_t bits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFF12345, 00000000);
|
||||
CHECK(Double(bits).IsSpecial());
|
||||
// Denormals are not special:
|
||||
CHECK(!Double(5e-324).IsSpecial());
|
||||
@ -172,7 +172,7 @@ TEST(Double_IsInfinite) {
|
||||
CHECK(!Double(-0.0).IsInfinite());
|
||||
CHECK(!Double(1.0).IsInfinite());
|
||||
CHECK(!Double(-1.0).IsInfinite());
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
CHECK(!Double(min_double64).IsInfinite());
|
||||
}
|
||||
|
||||
@ -192,7 +192,7 @@ TEST(Single_IsInfinite) {
|
||||
|
||||
TEST(Double_IsNan) {
|
||||
CHECK(Double(Double::NaN()).IsNan());
|
||||
uint64_t other_nan = UINT64_2PART_C(0xFFFFFFFF, 00000001);
|
||||
uint64_t other_nan = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, 00000001);
|
||||
CHECK(Double(other_nan).IsNan());
|
||||
CHECK(!Double(Double::Infinity()).IsNan());
|
||||
CHECK(!Double(-Double::Infinity()).IsNan());
|
||||
@ -200,7 +200,7 @@ TEST(Double_IsNan) {
|
||||
CHECK(!Double(-0.0).IsNan());
|
||||
CHECK(!Double(1.0).IsNan());
|
||||
CHECK(!Double(-1.0).IsNan());
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
CHECK(!Double(min_double64).IsNan());
|
||||
}
|
||||
|
||||
@ -226,7 +226,7 @@ TEST(Double_Sign) {
|
||||
CHECK_EQ(-1, Double(-Double::Infinity()).Sign());
|
||||
CHECK_EQ(1, Double(0.0).Sign());
|
||||
CHECK_EQ(-1, Double(-0.0).Sign());
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
CHECK_EQ(1, Double(min_double64).Sign());
|
||||
}
|
||||
|
||||
@ -264,7 +264,7 @@ TEST(Double_NormalizedBoundaries) {
|
||||
CHECK((1 << 9) == diy_fp.f() - boundary_minus.f()); // NOLINT
|
||||
CHECK((1 << 10) == boundary_plus.f() - diy_fp.f()); // NOLINT
|
||||
|
||||
uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
|
||||
uint64_t min_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00000000, 00000001);
|
||||
diy_fp = Double(min_double64).AsNormalizedDiyFp();
|
||||
Double(min_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
CHECK_EQ(diy_fp.e(), boundary_minus.e());
|
||||
@ -276,7 +276,7 @@ TEST(Double_NormalizedBoundaries) {
|
||||
CHECK((static_cast<uint64_t>(1) << 62) ==
|
||||
diy_fp.f() - boundary_minus.f()); // NOLINT
|
||||
|
||||
uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
|
||||
uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
|
||||
diy_fp = Double(smallest_normal64).AsNormalizedDiyFp();
|
||||
Double(smallest_normal64).NormalizedBoundaries(&boundary_minus,
|
||||
&boundary_plus);
|
||||
@ -287,7 +287,7 @@ TEST(Double_NormalizedBoundaries) {
|
||||
CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
|
||||
CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
|
||||
|
||||
uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
|
||||
diy_fp = Double(largest_denormal64).AsNormalizedDiyFp();
|
||||
Double(largest_denormal64).NormalizedBoundaries(&boundary_minus,
|
||||
&boundary_plus);
|
||||
@ -296,7 +296,7 @@ TEST(Double_NormalizedBoundaries) {
|
||||
CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
|
||||
CHECK((1 << 11) == diy_fp.f() - boundary_minus.f()); // NOLINT
|
||||
|
||||
uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
uint64_t max_double64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x7fefffff, ffffffff);
|
||||
diy_fp = Double(max_double64).AsNormalizedDiyFp();
|
||||
Double(max_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
|
||||
CHECK_EQ(diy_fp.e(), boundary_minus.e());
|
||||
@ -398,7 +398,7 @@ TEST(NextDouble) {
|
||||
CHECK_EQ(4e-324, d2.NextDouble());
|
||||
CHECK_EQ(-1.7976931348623157e308, Double(-Double::Infinity()).NextDouble());
|
||||
CHECK_EQ(Double::Infinity(),
|
||||
Double(UINT64_2PART_C(0x7fefffff, ffffffff)).NextDouble());
|
||||
Double(DOUBLE_CONVERSION_UINT64_2PART_C(0x7fefffff, ffffffff)).NextDouble());
|
||||
}
|
||||
|
||||
|
||||
@ -417,5 +417,5 @@ TEST(PreviousDouble) {
|
||||
CHECK_EQ(-4e-324, d2.PreviousDouble());
|
||||
CHECK_EQ(1.7976931348623157e308, Double(Double::Infinity()).PreviousDouble());
|
||||
CHECK_EQ(-Double::Infinity(),
|
||||
Double(UINT64_2PART_C(0xffefffff, ffffffff)).PreviousDouble());
|
||||
Double(DOUBLE_CONVERSION_UINT64_2PART_C(0xffefffff, ffffffff)).PreviousDouble());
|
||||
}
|
||||
|
@ -21,6 +21,11 @@ static double StrtodChar(const char* str, int exponent) {
|
||||
}
|
||||
|
||||
|
||||
static double StrtodTrimmedChar(const char* str, int exponent) {
|
||||
return StrtodTrimmed(StringToVector(str), exponent);
|
||||
}
|
||||
|
||||
|
||||
static float StrtofChar(const char* str, int exponent) {
|
||||
return Strtof(StringToVector(str), exponent);
|
||||
}
|
||||
@ -350,6 +355,276 @@ TEST(Strtod) {
|
||||
}
|
||||
|
||||
|
||||
TEST(StrtodTrimmed) {
|
||||
Vector<const char> vector;
|
||||
|
||||
vector = StringToVector("1");
|
||||
CHECK_EQ(1.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(10.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(100.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(1e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(1e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(1e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(1e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(1e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(1e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(1e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(1e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(1e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(1e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(1e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(1e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(1e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(1e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
vector = StringToVector("2");
|
||||
CHECK_EQ(2.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(20.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(200.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(2e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(2e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(2e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(2e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(2e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(2e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(2e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(2e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(2e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(2e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(2e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(2e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(2e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(2e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
vector = StringToVector("9");
|
||||
CHECK_EQ(9.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(90.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(900.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(9e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(9e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(9e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(9e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(9e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(9e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(9e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(9e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(9e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(9e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(9e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(9e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(9e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(9e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
vector = StringToVector("12345");
|
||||
CHECK_EQ(12345.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(123450.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(1234500.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(12345e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(12345e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(12345e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(12345e30, StrtodTrimmed(vector, 30));
|
||||
CHECK_EQ(12345e31, StrtodTrimmed(vector, 31));
|
||||
CHECK_EQ(12345e32, StrtodTrimmed(vector, 32));
|
||||
CHECK_EQ(12345e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(12345e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(12345e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(12345e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(12345e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(12345e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(12345e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(12345e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(12345e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(12345e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(12345e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
vector = StringToVector("12345678901234");
|
||||
CHECK_EQ(12345678901234.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(123456789012340.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(1234567890123400.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(12345678901234e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(12345678901234e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(12345678901234e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(12345678901234e30, StrtodTrimmed(vector, 30));
|
||||
CHECK_EQ(12345678901234e31, StrtodTrimmed(vector, 31));
|
||||
CHECK_EQ(12345678901234e32, StrtodTrimmed(vector, 32));
|
||||
CHECK_EQ(12345678901234e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(12345678901234e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(12345678901234e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(12345678901234e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(12345678901234e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(12345678901234e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(12345678901234e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(12345678901234e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(12345678901234e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(12345678901234e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(12345678901234e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
vector = StringToVector("123456789012345");
|
||||
CHECK_EQ(123456789012345.0, StrtodTrimmed(vector, 0));
|
||||
CHECK_EQ(1234567890123450.0, StrtodTrimmed(vector, 1));
|
||||
CHECK_EQ(12345678901234500.0, StrtodTrimmed(vector, 2));
|
||||
CHECK_EQ(123456789012345e20, StrtodTrimmed(vector, 20));
|
||||
CHECK_EQ(123456789012345e22, StrtodTrimmed(vector, 22));
|
||||
CHECK_EQ(123456789012345e23, StrtodTrimmed(vector, 23));
|
||||
CHECK_EQ(123456789012345e35, StrtodTrimmed(vector, 35));
|
||||
CHECK_EQ(123456789012345e36, StrtodTrimmed(vector, 36));
|
||||
CHECK_EQ(123456789012345e37, StrtodTrimmed(vector, 37));
|
||||
CHECK_EQ(123456789012345e39, StrtodTrimmed(vector, 39));
|
||||
CHECK_EQ(123456789012345e-1, StrtodTrimmed(vector, -1));
|
||||
CHECK_EQ(123456789012345e-2, StrtodTrimmed(vector, -2));
|
||||
CHECK_EQ(123456789012345e-5, StrtodTrimmed(vector, -5));
|
||||
CHECK_EQ(123456789012345e-20, StrtodTrimmed(vector, -20));
|
||||
CHECK_EQ(123456789012345e-22, StrtodTrimmed(vector, -22));
|
||||
CHECK_EQ(123456789012345e-23, StrtodTrimmed(vector, -23));
|
||||
CHECK_EQ(123456789012345e-25, StrtodTrimmed(vector, -25));
|
||||
CHECK_EQ(123456789012345e-39, StrtodTrimmed(vector, -39));
|
||||
|
||||
CHECK_EQ(0.0, StrtodTrimmedChar("", 1324));
|
||||
CHECK_EQ(0.0, StrtodTrimmedChar("2", -324));
|
||||
CHECK_EQ(4e-324, StrtodTrimmedChar("3", -324));
|
||||
// It would be more readable to put non-zero literals on the left side (i.e.
|
||||
// CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
|
||||
// they are truncated to zero.
|
||||
CHECK_EQ(0.0, StrtodTrimmedChar("1", -325));
|
||||
CHECK_EQ(0.0, StrtodTrimmedChar("1", -325));
|
||||
|
||||
// It would be more readable to put the literals (and not Double::Infinity())
|
||||
// on the left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
|
||||
// complains that the floating constant exceeds range of 'double'.
|
||||
CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("1", 309));
|
||||
CHECK_EQ(1e308, StrtodTrimmedChar("1", 308));
|
||||
CHECK_EQ(1234e305, StrtodTrimmedChar("1234", 305));
|
||||
CHECK_EQ(1234e304, StrtodTrimmedChar("1234", 304));
|
||||
CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("18", 307));
|
||||
CHECK_EQ(17e307, StrtodTrimmedChar("17", 307));
|
||||
|
||||
CHECK_EQ(1.7976931348623157E+308, StrtodTrimmedChar("17976931348623157", 292));
|
||||
CHECK_EQ(1.7976931348623158E+308, StrtodTrimmedChar("17976931348623158", 292));
|
||||
CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("17976931348623159", 292));
|
||||
|
||||
// The following number is the result of 89255.0/1e-22. Both floating-point
|
||||
// numbers can be accurately represented with doubles. However on Linux,x86
|
||||
// the floating-point stack is set to 80bits and the double-rounding
|
||||
// introduces an error.
|
||||
CHECK_EQ(89255e-22, StrtodTrimmedChar("89255", -22));
|
||||
|
||||
// Some random values.
|
||||
CHECK_EQ(358416272e-33, StrtodTrimmedChar("358416272", -33));
|
||||
CHECK_EQ(104110013277974872254e-225,
|
||||
StrtodTrimmedChar("104110013277974872254", -225));
|
||||
|
||||
CHECK_EQ(123456789e108, StrtodTrimmedChar("123456789", 108));
|
||||
CHECK_EQ(123456789e109, StrtodTrimmedChar("123456789", 109));
|
||||
CHECK_EQ(123456789e110, StrtodTrimmedChar("123456789", 110));
|
||||
CHECK_EQ(123456789e111, StrtodTrimmedChar("123456789", 111));
|
||||
CHECK_EQ(123456789e112, StrtodTrimmedChar("123456789", 112));
|
||||
CHECK_EQ(123456789e113, StrtodTrimmedChar("123456789", 113));
|
||||
CHECK_EQ(123456789e114, StrtodTrimmedChar("123456789", 114));
|
||||
CHECK_EQ(123456789e115, StrtodTrimmedChar("123456789", 115));
|
||||
CHECK_EQ(1234567890123456789012345e108,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 108));
|
||||
CHECK_EQ(1234567890123456789012345e109,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 109));
|
||||
CHECK_EQ(1234567890123456789012345e110,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 110));
|
||||
CHECK_EQ(1234567890123456789012345e111,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 111));
|
||||
CHECK_EQ(1234567890123456789012345e112,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 112));
|
||||
CHECK_EQ(1234567890123456789012345e113,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 113));
|
||||
CHECK_EQ(1234567890123456789012345e114,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 114));
|
||||
CHECK_EQ(1234567890123456789012345e115,
|
||||
StrtodTrimmedChar("1234567890123456789012345", 115));
|
||||
|
||||
CHECK_EQ(1234567890123456789052345e108,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 108));
|
||||
CHECK_EQ(1234567890123456789052345e109,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 109));
|
||||
CHECK_EQ(1234567890123456789052345e110,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 110));
|
||||
CHECK_EQ(1234567890123456789052345e111,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 111));
|
||||
CHECK_EQ(1234567890123456789052345e112,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 112));
|
||||
CHECK_EQ(1234567890123456789052345e113,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 113));
|
||||
CHECK_EQ(1234567890123456789052345e114,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 114));
|
||||
CHECK_EQ(1234567890123456789052345e115,
|
||||
StrtodTrimmedChar("1234567890123456789052345", 115));
|
||||
|
||||
// Boundary cases. Boundaries themselves should round to even.
|
||||
//
|
||||
// 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928
|
||||
// next: 72057594037927936
|
||||
// boundary: 72057594037927932 should round up.
|
||||
CHECK_EQ(72057594037927928.0, StrtodTrimmedChar("72057594037927928", 0));
|
||||
CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("72057594037927936", 0));
|
||||
CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("72057594037927932", 0));
|
||||
CHECK_EQ(72057594037927928.0, StrtodTrimmedChar("7205759403792793199999", -5));
|
||||
CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("7205759403792793200001", -5));
|
||||
|
||||
// 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784
|
||||
// next: 9223372036854775808
|
||||
// boundary: 9223372036854775296 should round up.
|
||||
CHECK_EQ(9223372036854774784.0, StrtodTrimmedChar("9223372036854774784", 0));
|
||||
CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("9223372036854775808", 0));
|
||||
CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("9223372036854775296", 0));
|
||||
CHECK_EQ(9223372036854774784.0, StrtodTrimmedChar("922337203685477529599999", -5));
|
||||
CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("922337203685477529600001", -5));
|
||||
|
||||
// 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384
|
||||
// next: 10141204801825835211973625643008
|
||||
// boundary: 10141204801825834649023672221696 should round up.
|
||||
CHECK_EQ(10141204801825834086073718800384.0,
|
||||
StrtodTrimmedChar("10141204801825834086073718800384", 0));
|
||||
CHECK_EQ(10141204801825835211973625643008.0,
|
||||
StrtodTrimmedChar("10141204801825835211973625643008", 0));
|
||||
CHECK_EQ(10141204801825835211973625643008.0,
|
||||
StrtodTrimmedChar("10141204801825834649023672221696", 0));
|
||||
CHECK_EQ(10141204801825834086073718800384.0,
|
||||
StrtodTrimmedChar("1014120480182583464902367222169599999", -5));
|
||||
CHECK_EQ(10141204801825835211973625643008.0,
|
||||
StrtodTrimmedChar("1014120480182583464902367222169600001", -5));
|
||||
|
||||
// 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808
|
||||
// next: 5708990770823839524233143877797980545530986496
|
||||
// boundary: 5708990770823839207320493820740630171355185152
|
||||
// The boundary should round up.
|
||||
CHECK_EQ(5708990770823838890407843763683279797179383808.0,
|
||||
StrtodTrimmedChar("5708990770823838890407843763683279797179383808", 0));
|
||||
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
|
||||
StrtodTrimmedChar("5708990770823839524233143877797980545530986496", 0));
|
||||
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
|
||||
StrtodTrimmedChar("5708990770823839207320493820740630171355185152", 0));
|
||||
CHECK_EQ(5708990770823838890407843763683279797179383808.0,
|
||||
StrtodTrimmedChar("5708990770823839207320493820740630171355185151999", -3));
|
||||
CHECK_EQ(5708990770823839524233143877797980545530986496.0,
|
||||
StrtodTrimmedChar("5708990770823839207320493820740630171355185152001", -3));
|
||||
|
||||
// The following test-cases got some public attention in early 2011 when they
|
||||
// sent Java and PHP into an infinite loop.
|
||||
CHECK_EQ(2.225073858507201e-308, StrtodTrimmedChar("22250738585072011", -324));
|
||||
CHECK_EQ(2.22507385850720138309e-308,
|
||||
StrtodTrimmedChar("22250738585072011360574097967091319759348195463516456480"
|
||||
"23426109724822222021076945516529523908135087914149158913"
|
||||
"03962110687008643869459464552765720740782062174337998814"
|
||||
"10632673292535522868813721490129811224514518898490572223"
|
||||
"07285255133155755015914397476397983411801999323962548289"
|
||||
"01710708185069063066665599493827577257201576306269066333"
|
||||
"26475653000092458883164330377797918696120494973903778297"
|
||||
"04905051080609940730262937128958950003583799967207254304"
|
||||
"36028407889577179615094551674824347103070260914462157228"
|
||||
"98802581825451803257070188608721131280795122334262883686"
|
||||
"22321503775666622503982534335974568884423900265498198385"
|
||||
"48794829220689472168983109969836584681402285424333066033"
|
||||
"98508864458040010349339704275671864433837704860378616227"
|
||||
"71738545623065874679014086723327636718751", -1076));
|
||||
}
|
||||
|
||||
|
||||
TEST(Strtof) {
|
||||
Vector<const char> vector;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user