Added precision mode to fast-dtoa.
Review URL: http://codereview.chromium.org/2000004 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5491 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
a98baf9666
commit
fc9915b770
@ -956,8 +956,9 @@ static char* CreateExponentialRepresentation(char* decimal_rep,
|
|||||||
|
|
||||||
|
|
||||||
char* DoubleToExponentialCString(double value, int f) {
|
char* DoubleToExponentialCString(double value, int f) {
|
||||||
|
const int kMaxDigitsAfterPoint = 20;
|
||||||
// f might be -1 to signal that f was undefined in JavaScript.
|
// f might be -1 to signal that f was undefined in JavaScript.
|
||||||
ASSERT(f >= -1 && f <= 20);
|
ASSERT(f >= -1 && f <= kMaxDigitsAfterPoint);
|
||||||
|
|
||||||
bool negative = false;
|
bool negative = false;
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
@ -969,29 +970,60 @@ char* DoubleToExponentialCString(double value, int f) {
|
|||||||
int decimal_point;
|
int decimal_point;
|
||||||
int sign;
|
int sign;
|
||||||
char* decimal_rep = NULL;
|
char* decimal_rep = NULL;
|
||||||
|
bool used_gay_dtoa = false;
|
||||||
|
// f corresponds to the digits after the point. There is always one digit
|
||||||
|
// before the point. The number of requested_digits equals hence f + 1.
|
||||||
|
// And we have to add one character for the null-terminator.
|
||||||
|
const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1;
|
||||||
|
// Make sure that the buffer is big enough, even if we fall back to the
|
||||||
|
// shortest representation (which happens when f equals -1).
|
||||||
|
ASSERT(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1);
|
||||||
|
char v8_dtoa_buffer[kV8DtoaBufferCapacity];
|
||||||
|
int decimal_rep_length;
|
||||||
|
|
||||||
if (f == -1) {
|
if (f == -1) {
|
||||||
|
if (DoubleToAscii(value, DTOA_SHORTEST, 0,
|
||||||
|
Vector<char>(v8_dtoa_buffer, kV8DtoaBufferCapacity),
|
||||||
|
&sign, &decimal_rep_length, &decimal_point)) {
|
||||||
|
f = decimal_rep_length - 1;
|
||||||
|
decimal_rep = v8_dtoa_buffer;
|
||||||
|
} else {
|
||||||
decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL);
|
decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL);
|
||||||
f = StrLength(decimal_rep) - 1;
|
decimal_rep_length = StrLength(decimal_rep);
|
||||||
|
f = decimal_rep_length - 1;
|
||||||
|
used_gay_dtoa = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (DoubleToAscii(value, DTOA_PRECISION, f + 1,
|
||||||
|
Vector<char>(v8_dtoa_buffer, kV8DtoaBufferCapacity),
|
||||||
|
&sign, &decimal_rep_length, &decimal_point)) {
|
||||||
|
decimal_rep = v8_dtoa_buffer;
|
||||||
} else {
|
} else {
|
||||||
decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL);
|
decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL);
|
||||||
|
decimal_rep_length = StrLength(decimal_rep);
|
||||||
|
used_gay_dtoa = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int decimal_rep_length = StrLength(decimal_rep);
|
|
||||||
ASSERT(decimal_rep_length > 0);
|
ASSERT(decimal_rep_length > 0);
|
||||||
ASSERT(decimal_rep_length <= f + 1);
|
ASSERT(decimal_rep_length <= f + 1);
|
||||||
USE(decimal_rep_length);
|
|
||||||
|
|
||||||
int exponent = decimal_point - 1;
|
int exponent = decimal_point - 1;
|
||||||
char* result =
|
char* result =
|
||||||
CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
|
CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1);
|
||||||
|
|
||||||
|
if (used_gay_dtoa) {
|
||||||
freedtoa(decimal_rep);
|
freedtoa(decimal_rep);
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char* DoubleToPrecisionCString(double value, int p) {
|
char* DoubleToPrecisionCString(double value, int p) {
|
||||||
ASSERT(p >= 1 && p <= 21);
|
const int kMinimalDigits = 1;
|
||||||
|
const int kMaximalDigits = 21;
|
||||||
|
ASSERT(p >= kMinimalDigits && p <= kMaximalDigits);
|
||||||
|
USE(kMinimalDigits);
|
||||||
|
|
||||||
bool negative = false;
|
bool negative = false;
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
@ -1002,8 +1034,22 @@ char* DoubleToPrecisionCString(double value, int p) {
|
|||||||
// Find a sufficiently precise decimal representation of n.
|
// Find a sufficiently precise decimal representation of n.
|
||||||
int decimal_point;
|
int decimal_point;
|
||||||
int sign;
|
int sign;
|
||||||
char* decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL);
|
char* decimal_rep = NULL;
|
||||||
int decimal_rep_length = StrLength(decimal_rep);
|
bool used_gay_dtoa = false;
|
||||||
|
// Add one for the terminating null character.
|
||||||
|
const int kV8DtoaBufferCapacity = kMaximalDigits + 1;
|
||||||
|
char v8_dtoa_buffer[kV8DtoaBufferCapacity];
|
||||||
|
int decimal_rep_length;
|
||||||
|
|
||||||
|
if (DoubleToAscii(value, DTOA_PRECISION, p,
|
||||||
|
Vector<char>(v8_dtoa_buffer, kV8DtoaBufferCapacity),
|
||||||
|
&sign, &decimal_rep_length, &decimal_point)) {
|
||||||
|
decimal_rep = v8_dtoa_buffer;
|
||||||
|
} else {
|
||||||
|
decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL);
|
||||||
|
decimal_rep_length = StrLength(decimal_rep);
|
||||||
|
used_gay_dtoa = true;
|
||||||
|
}
|
||||||
ASSERT(decimal_rep_length <= p);
|
ASSERT(decimal_rep_length <= p);
|
||||||
|
|
||||||
int exponent = decimal_point - 1;
|
int exponent = decimal_point - 1;
|
||||||
@ -1047,7 +1093,9 @@ char* DoubleToPrecisionCString(double value, int p) {
|
|||||||
result = builder.Finalize();
|
result = builder.Finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (used_gay_dtoa) {
|
||||||
freedtoa(decimal_rep);
|
freedtoa(decimal_rep);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,11 +65,12 @@ bool DoubleToAscii(double v, DtoaMode mode, int requested_digits,
|
|||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case DTOA_SHORTEST:
|
case DTOA_SHORTEST:
|
||||||
return FastDtoa(v, buffer, length, point);
|
return FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
|
||||||
case DTOA_FIXED:
|
case DTOA_FIXED:
|
||||||
return FastFixedDtoa(v, requested_digits, buffer, length, point);
|
return FastFixedDtoa(v, requested_digits, buffer, length, point);
|
||||||
default:
|
case DTOA_PRECISION:
|
||||||
break;
|
return FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
||||||
|
buffer, length, point);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
321
src/fast-dtoa.cc
321
src/fast-dtoa.cc
@ -42,8 +42,8 @@ namespace internal {
|
|||||||
//
|
//
|
||||||
// A different range might be chosen on a different platform, to optimize digit
|
// A different range might be chosen on a different platform, to optimize digit
|
||||||
// generation, but a smaller range requires more powers of ten to be cached.
|
// generation, but a smaller range requires more powers of ten to be cached.
|
||||||
static const int minimal_target_exponent = -60;
|
static const int kMinimalTargetExponent = -60;
|
||||||
static const int maximal_target_exponent = -32;
|
static const int kMaximalTargetExponent = -32;
|
||||||
|
|
||||||
|
|
||||||
// Adjusts the last digit of the generated number, and screens out generated
|
// Adjusts the last digit of the generated number, and screens out generated
|
||||||
@ -61,7 +61,7 @@ static const int maximal_target_exponent = -32;
|
|||||||
// Output: returns true if the buffer is guaranteed to contain the closest
|
// Output: returns true if the buffer is guaranteed to contain the closest
|
||||||
// representable number to the input.
|
// representable number to the input.
|
||||||
// Modifies the generated digits in the buffer to approach (round towards) w.
|
// Modifies the generated digits in the buffer to approach (round towards) w.
|
||||||
bool RoundWeed(Vector<char> buffer,
|
static bool RoundWeed(Vector<char> buffer,
|
||||||
int length,
|
int length,
|
||||||
uint64_t distance_too_high_w,
|
uint64_t distance_too_high_w,
|
||||||
uint64_t unsafe_interval,
|
uint64_t unsafe_interval,
|
||||||
@ -75,7 +75,7 @@ bool RoundWeed(Vector<char> buffer,
|
|||||||
// Note: w_low < w < w_high
|
// Note: w_low < w < w_high
|
||||||
//
|
//
|
||||||
// The real w (* unit) must lie somewhere inside the interval
|
// The real w (* unit) must lie somewhere inside the interval
|
||||||
// ]w_low; w_low[ (often written as "(w_low; w_low)")
|
// ]w_low; w_high[ (often written as "(w_low; w_high)")
|
||||||
|
|
||||||
// Basically the buffer currently contains a number in the unsafe interval
|
// Basically the buffer currently contains a number in the unsafe interval
|
||||||
// ]too_low; too_high[ with too_low < w < too_high
|
// ]too_low; too_high[ with too_low < w < too_high
|
||||||
@ -122,10 +122,10 @@ bool RoundWeed(Vector<char> buffer,
|
|||||||
// inside the safe interval then we simply do not know and bail out (returning
|
// inside the safe interval then we simply do not know and bail out (returning
|
||||||
// false).
|
// false).
|
||||||
//
|
//
|
||||||
// Similarly we have to take into account the imprecision of 'w' when rounding
|
// Similarly we have to take into account the imprecision of 'w' when finding
|
||||||
// the buffer. If we have two potential representations we need to make sure
|
// the closest representation of 'w'. If we have two potential
|
||||||
// that the chosen one is closer to w_low and w_high since v can be anywhere
|
// representations, and one is closer to both w_low and w_high, then we know
|
||||||
// between them.
|
// it is closer to the actual value v.
|
||||||
//
|
//
|
||||||
// By generating the digits of too_high we got the largest (closest to
|
// By generating the digits of too_high we got the largest (closest to
|
||||||
// too_high) buffer that is still in the unsafe interval. In the case where
|
// too_high) buffer that is still in the unsafe interval. In the case where
|
||||||
@ -139,6 +139,9 @@ bool RoundWeed(Vector<char> buffer,
|
|||||||
// (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
|
// (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
|
||||||
// Instead of using the buffer directly we use its distance to too_high.
|
// Instead of using the buffer directly we use its distance to too_high.
|
||||||
// Conceptually rest ~= too_high - 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);
|
||||||
while (rest < small_distance && // Negated condition 1
|
while (rest < small_distance && // Negated condition 1
|
||||||
unsafe_interval - rest >= ten_kappa && // Negated condition 2
|
unsafe_interval - rest >= ten_kappa && // Negated condition 2
|
||||||
(rest + ten_kappa < small_distance || // buffer{-1} > w_high
|
(rest + ten_kappa < small_distance || // buffer{-1} > w_high
|
||||||
@ -166,6 +169,62 @@ bool RoundWeed(Vector<char> buffer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Rounds the buffer upwards if the result is closer to v by possibly adding
|
||||||
|
// 1 to the buffer. If the precision of the calculation is not sufficient to
|
||||||
|
// round correctly, return false.
|
||||||
|
// The rounding might shift the whole buffer in which case the kappa is
|
||||||
|
// adjusted. For example "99", kappa = 3 might become "10", kappa = 4.
|
||||||
|
//
|
||||||
|
// If 2*rest > ten_kappa then the buffer needs to be round up.
|
||||||
|
// rest can have an error of +/- 1 unit. This function accounts for the
|
||||||
|
// imprecision and returns false, if the rounding direction cannot be
|
||||||
|
// unambiguously determined.
|
||||||
|
//
|
||||||
|
// Precondition: rest < ten_kappa.
|
||||||
|
static bool RoundWeedCounted(Vector<char> buffer,
|
||||||
|
int length,
|
||||||
|
uint64_t rest,
|
||||||
|
uint64_t ten_kappa,
|
||||||
|
uint64_t unit,
|
||||||
|
int* kappa) {
|
||||||
|
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.
|
||||||
|
//
|
||||||
|
// If the unit is too big, then we don't know which way to round. For example
|
||||||
|
// a unit of 50 means that the real number lies within rest +/- 50. If
|
||||||
|
// 10^kappa == 40 then there is no way to tell which way to round.
|
||||||
|
if (unit >= ten_kappa) return false;
|
||||||
|
// Even if unit is just half the size of 10^kappa we are already completely
|
||||||
|
// lost. (And after the previous test we know that the expression will not
|
||||||
|
// over/underflow.)
|
||||||
|
if (ten_kappa - unit <= unit) return false;
|
||||||
|
// If 2 * (rest + unit) <= 10^kappa we can safely round down.
|
||||||
|
if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// If 2 * (rest - unit) >= 10^kappa, then we can safely round up.
|
||||||
|
if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) {
|
||||||
|
// Increment the last digit recursively until we find a non '9' digit.
|
||||||
|
buffer[length - 1]++;
|
||||||
|
for (int i = length - 1; i > 0; --i) {
|
||||||
|
if (buffer[i] != '0' + 10) break;
|
||||||
|
buffer[i] = '0';
|
||||||
|
buffer[i - 1]++;
|
||||||
|
}
|
||||||
|
// If the first digit is now '0'+ 10 we had a buffer with all '9's. With the
|
||||||
|
// exception of the first digit all digits are now '0'. Simply switch the
|
||||||
|
// first digit to '1' and adjust the kappa. Example: "99" becomes "10" and
|
||||||
|
// the power (the kappa) is increased.
|
||||||
|
if (buffer[0] == '0' + 10) {
|
||||||
|
buffer[0] = '1';
|
||||||
|
(*kappa) += 1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static const uint32_t kTen4 = 10000;
|
static const uint32_t kTen4 = 10000;
|
||||||
static const uint32_t kTen5 = 100000;
|
static const uint32_t kTen5 = 100000;
|
||||||
@ -178,7 +237,7 @@ static const uint32_t kTen9 = 1000000000;
|
|||||||
// number. We furthermore receive the maximum number of bits 'number' has.
|
// number. We furthermore receive the maximum number of bits 'number' has.
|
||||||
// If number_bits == 0 then 0^-1 is returned
|
// If number_bits == 0 then 0^-1 is returned
|
||||||
// The number of bits must be <= 32.
|
// The number of bits must be <= 32.
|
||||||
// Precondition: (1 << number_bits) <= number < (1 << (number_bits + 1)).
|
// Precondition: number < (1 << (number_bits + 1)).
|
||||||
static void BiggestPowerTen(uint32_t number,
|
static void BiggestPowerTen(uint32_t number,
|
||||||
int number_bits,
|
int number_bits,
|
||||||
uint32_t* power,
|
uint32_t* power,
|
||||||
@ -281,18 +340,18 @@ static void BiggestPowerTen(uint32_t number,
|
|||||||
|
|
||||||
// Generates the digits of input number w.
|
// Generates the digits of input number w.
|
||||||
// w is a floating-point number (DiyFp), consisting of a significand and an
|
// w is a floating-point number (DiyFp), consisting of a significand and an
|
||||||
// exponent. Its exponent is bounded by minimal_target_exponent and
|
// exponent. Its exponent is bounded by kMinimalTargetExponent and
|
||||||
// maximal_target_exponent.
|
// kMaximalTargetExponent.
|
||||||
// Hence -60 <= w.e() <= -32.
|
// Hence -60 <= w.e() <= -32.
|
||||||
//
|
//
|
||||||
// Returns false if it fails, in which case the generated digits in the buffer
|
// Returns false if it fails, in which case the generated digits in the buffer
|
||||||
// should not be used.
|
// should not be used.
|
||||||
// Preconditions:
|
// Preconditions:
|
||||||
// * low, w and high are correct up to 1 ulp (unit in the last place). That
|
// * low, w and high are correct up to 1 ulp (unit in the last place). That
|
||||||
// is, their error must be less that a unit of their last digits.
|
// is, their error must be less than a unit of their last digits.
|
||||||
// * low.e() == w.e() == high.e()
|
// * low.e() == w.e() == high.e()
|
||||||
// * low < w < high, and taking into account their error: low~ <= high~
|
// * low < w < high, and taking into account their error: low~ <= high~
|
||||||
// * minimal_target_exponent <= w.e() <= maximal_target_exponent
|
// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
|
||||||
// Postconditions: returns false if procedure fails.
|
// Postconditions: returns false if procedure fails.
|
||||||
// otherwise:
|
// otherwise:
|
||||||
// * buffer is not null-terminated, but len contains the number of digits.
|
// * buffer is not null-terminated, but len contains the number of digits.
|
||||||
@ -321,7 +380,7 @@ static void BiggestPowerTen(uint32_t number,
|
|||||||
// represent 'w' we can stop. Everything inside the interval low - high
|
// represent 'w' we can stop. Everything inside the interval low - high
|
||||||
// represents w. However we have to pay attention to low, high and w's
|
// represents w. However we have to pay attention to low, high and w's
|
||||||
// imprecision.
|
// imprecision.
|
||||||
bool DigitGen(DiyFp low,
|
static bool DigitGen(DiyFp low,
|
||||||
DiyFp w,
|
DiyFp w,
|
||||||
DiyFp high,
|
DiyFp high,
|
||||||
Vector<char> buffer,
|
Vector<char> buffer,
|
||||||
@ -329,7 +388,7 @@ bool DigitGen(DiyFp low,
|
|||||||
int* kappa) {
|
int* kappa) {
|
||||||
ASSERT(low.e() == w.e() && w.e() == high.e());
|
ASSERT(low.e() == w.e() && w.e() == high.e());
|
||||||
ASSERT(low.f() + 1 <= high.f() - 1);
|
ASSERT(low.f() + 1 <= high.f() - 1);
|
||||||
ASSERT(minimal_target_exponent <= w.e() && w.e() <= maximal_target_exponent);
|
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||||
// low, w and high are imprecise, but by less than one ulp (unit in the last
|
// low, w and high are imprecise, but by less than one ulp (unit in the last
|
||||||
// place).
|
// place).
|
||||||
// If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
|
// If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
|
||||||
@ -359,23 +418,23 @@ bool DigitGen(DiyFp low,
|
|||||||
uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
|
uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
|
||||||
// Modulo by one is an and.
|
// Modulo by one is an and.
|
||||||
uint64_t fractionals = too_high.f() & (one.f() - 1);
|
uint64_t fractionals = too_high.f() & (one.f() - 1);
|
||||||
uint32_t divider;
|
uint32_t divisor;
|
||||||
int divider_exponent;
|
int divisor_exponent;
|
||||||
BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
|
BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
|
||||||
÷r, ÷r_exponent);
|
&divisor, &divisor_exponent);
|
||||||
*kappa = divider_exponent + 1;
|
*kappa = divisor_exponent + 1;
|
||||||
*length = 0;
|
*length = 0;
|
||||||
// Loop invariant: buffer = too_high / 10^kappa (integer division)
|
// Loop invariant: buffer = too_high / 10^kappa (integer division)
|
||||||
// The invariant holds for the first iteration: kappa has been initialized
|
// The invariant holds for the first iteration: kappa has been initialized
|
||||||
// with the divider exponent + 1. And the divider is the biggest power of ten
|
// with the divisor exponent + 1. And the divisor is the biggest power of ten
|
||||||
// that is smaller than integrals.
|
// that is smaller than integrals.
|
||||||
while (*kappa > 0) {
|
while (*kappa > 0) {
|
||||||
int digit = integrals / divider;
|
int digit = integrals / divisor;
|
||||||
buffer[*length] = '0' + digit;
|
buffer[*length] = '0' + digit;
|
||||||
(*length)++;
|
(*length)++;
|
||||||
integrals %= divider;
|
integrals %= divisor;
|
||||||
(*kappa)--;
|
(*kappa)--;
|
||||||
// Note that kappa now equals the exponent of the divider and that the
|
// Note that kappa now equals the exponent of the divisor and that the
|
||||||
// invariant thus holds again.
|
// invariant thus holds again.
|
||||||
uint64_t rest =
|
uint64_t rest =
|
||||||
(static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
|
(static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
|
||||||
@ -386,32 +445,24 @@ bool DigitGen(DiyFp low,
|
|||||||
// that lies within the unsafe interval.
|
// that lies within the unsafe interval.
|
||||||
return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
|
return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
|
||||||
unsafe_interval.f(), rest,
|
unsafe_interval.f(), rest,
|
||||||
static_cast<uint64_t>(divider) << -one.e(), unit);
|
static_cast<uint64_t>(divisor) << -one.e(), unit);
|
||||||
}
|
}
|
||||||
divider /= 10;
|
divisor /= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The integrals have been generated. We are at the point of the decimal
|
// The integrals have been generated. We are at the point of the decimal
|
||||||
// separator. In the following loop we simply multiply the remaining digits by
|
// separator. In the following loop we simply multiply the remaining digits by
|
||||||
// 10 and divide by one. We just need to pay attention to multiply associated
|
// 10 and divide by one. We just need to pay attention to multiply associated
|
||||||
// data (like the interval or 'unit'), too.
|
// data (like the interval or 'unit'), too.
|
||||||
// Instead of multiplying by 10 we multiply by 5 (cheaper operation) and
|
// Note that the multiplication by 10 does not overflow, because w.e >= -60
|
||||||
// increase its (imaginary) exponent. At the same time we decrease the
|
// and thus one.e >= -60.
|
||||||
// divider's (one's) exponent and shift its significand.
|
ASSERT(one.e() >= -60);
|
||||||
// Basically, if fractionals was a DiyFp (with fractionals.e == one.e):
|
ASSERT(fractionals < one.f());
|
||||||
// fractionals.f *= 10;
|
ASSERT(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f());
|
||||||
// fractionals.f >>= 1; fractionals.e++; // value remains unchanged.
|
|
||||||
// one.f >>= 1; one.e++; // value remains unchanged.
|
|
||||||
// and we have again fractionals.e == one.e which allows us to divide
|
|
||||||
// fractionals.f() by one.f()
|
|
||||||
// We simply combine the *= 10 and the >>= 1.
|
|
||||||
while (true) {
|
while (true) {
|
||||||
fractionals *= 5;
|
fractionals *= 10;
|
||||||
unit *= 5;
|
unit *= 10;
|
||||||
unsafe_interval.set_f(unsafe_interval.f() * 5);
|
unsafe_interval.set_f(unsafe_interval.f() * 10);
|
||||||
unsafe_interval.set_e(unsafe_interval.e() + 1); // Will be optimized out.
|
|
||||||
one.set_f(one.f() >> 1);
|
|
||||||
one.set_e(one.e() + 1);
|
|
||||||
// Integer division by one.
|
// Integer division by one.
|
||||||
int digit = static_cast<int>(fractionals >> -one.e());
|
int digit = static_cast<int>(fractionals >> -one.e());
|
||||||
buffer[*length] = '0' + digit;
|
buffer[*length] = '0' + digit;
|
||||||
@ -426,6 +477,113 @@ bool DigitGen(DiyFp low,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Generates (at most) requested_digits of input number w.
|
||||||
|
// w is a floating-point number (DiyFp), consisting of a significand and an
|
||||||
|
// exponent. Its exponent is bounded by kMinimalTargetExponent and
|
||||||
|
// kMaximalTargetExponent.
|
||||||
|
// Hence -60 <= w.e() <= -32.
|
||||||
|
//
|
||||||
|
// Returns false if it fails, in which case the generated digits in the buffer
|
||||||
|
// should not be used.
|
||||||
|
// Preconditions:
|
||||||
|
// * w is correct up to 1 ulp (unit in the last place). That
|
||||||
|
// is, its error must be strictly less than a unit of its last digit.
|
||||||
|
// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent
|
||||||
|
//
|
||||||
|
// Postconditions: returns false if procedure fails.
|
||||||
|
// otherwise:
|
||||||
|
// * buffer is not null-terminated, but length contains the number of
|
||||||
|
// digits.
|
||||||
|
// * the representation in buffer is the most precise representation of
|
||||||
|
// requested_digits digits.
|
||||||
|
// * buffer contains at most requested_digits digits of w. If there are less
|
||||||
|
// than requested_digits digits then some trailing '0's have been removed.
|
||||||
|
// * kappa is such that
|
||||||
|
// w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2.
|
||||||
|
//
|
||||||
|
// Remark: This procedure takes into account the imprecision of its input
|
||||||
|
// numbers. If the precision is not enough to guarantee all the postconditions
|
||||||
|
// then false is returned. This usually happens rarely, but the failure-rate
|
||||||
|
// increases with higher requested_digits.
|
||||||
|
static bool DigitGenCounted(DiyFp w,
|
||||||
|
int requested_digits,
|
||||||
|
Vector<char> buffer,
|
||||||
|
int* length,
|
||||||
|
int* kappa) {
|
||||||
|
ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent);
|
||||||
|
ASSERT(kMinimalTargetExponent >= -60);
|
||||||
|
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;
|
||||||
|
// We cut the input number into two parts: the integral digits and the
|
||||||
|
// fractional digits. We don't emit any decimal separator, but adapt kappa
|
||||||
|
// instead. Example: instead of writing "1.2" we put "12" into the buffer and
|
||||||
|
// increase kappa by 1.
|
||||||
|
DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
|
||||||
|
// Division by one is a shift.
|
||||||
|
uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e());
|
||||||
|
// Modulo by one is an and.
|
||||||
|
uint64_t fractionals = w.f() & (one.f() - 1);
|
||||||
|
uint32_t divisor;
|
||||||
|
int divisor_exponent;
|
||||||
|
BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
|
||||||
|
&divisor, &divisor_exponent);
|
||||||
|
*kappa = divisor_exponent + 1;
|
||||||
|
*length = 0;
|
||||||
|
|
||||||
|
// Loop invariant: buffer = w / 10^kappa (integer division)
|
||||||
|
// The invariant holds for the first iteration: kappa has been initialized
|
||||||
|
// with the divisor exponent + 1. And the divisor is the biggest power of ten
|
||||||
|
// that is smaller than 'integrals'.
|
||||||
|
while (*kappa > 0) {
|
||||||
|
int digit = integrals / divisor;
|
||||||
|
buffer[*length] = '0' + digit;
|
||||||
|
(*length)++;
|
||||||
|
requested_digits--;
|
||||||
|
integrals %= divisor;
|
||||||
|
(*kappa)--;
|
||||||
|
// Note that kappa now equals the exponent of the divisor and that the
|
||||||
|
// invariant thus holds again.
|
||||||
|
if (requested_digits == 0) break;
|
||||||
|
divisor /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requested_digits == 0) {
|
||||||
|
uint64_t rest =
|
||||||
|
(static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
|
||||||
|
return RoundWeedCounted(buffer, *length, rest,
|
||||||
|
static_cast<uint64_t>(divisor) << -one.e(), w_error,
|
||||||
|
kappa);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The integrals have been generated. We are at the point of the decimal
|
||||||
|
// separator. In the following loop we simply multiply the remaining digits by
|
||||||
|
// 10 and divide by one. We just need to pay attention to multiply associated
|
||||||
|
// 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(V8_2PART_UINT64_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());
|
||||||
|
buffer[*length] = '0' + digit;
|
||||||
|
(*length)++;
|
||||||
|
requested_digits--;
|
||||||
|
fractionals &= one.f() - 1; // Modulo by one.
|
||||||
|
(*kappa)--;
|
||||||
|
}
|
||||||
|
if (requested_digits != 0) return false;
|
||||||
|
return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error,
|
||||||
|
kappa);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Provides a decimal representation of v.
|
// Provides a decimal representation of v.
|
||||||
// Returns true if it succeeds, otherwise the result cannot be trusted.
|
// Returns true if it succeeds, otherwise the result cannot be trusted.
|
||||||
// There will be *length digits inside the buffer (not null-terminated).
|
// There will be *length digits inside the buffer (not null-terminated).
|
||||||
@ -437,7 +595,10 @@ bool DigitGen(DiyFp low,
|
|||||||
// The last digit will be closest to the actual v. That is, even if several
|
// The last digit will be closest to the actual v. That is, even if several
|
||||||
// digits might correctly yield 'v' when read again, the closest will be
|
// digits might correctly yield 'v' when read again, the closest will be
|
||||||
// computed.
|
// computed.
|
||||||
bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
|
static bool Grisu3(double v,
|
||||||
|
Vector<char> buffer,
|
||||||
|
int* length,
|
||||||
|
int* decimal_exponent) {
|
||||||
DiyFp w = Double(v).AsNormalizedDiyFp();
|
DiyFp w = Double(v).AsNormalizedDiyFp();
|
||||||
// boundary_minus and boundary_plus are the boundaries between v and its
|
// boundary_minus and boundary_plus are the boundaries between v and its
|
||||||
// closest floating-point neighbors. Any number strictly between
|
// closest floating-point neighbors. Any number strictly between
|
||||||
@ -448,12 +609,12 @@ bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
|
|||||||
ASSERT(boundary_plus.e() == w.e());
|
ASSERT(boundary_plus.e() == w.e());
|
||||||
DiyFp ten_mk; // Cached power of ten: 10^-k
|
DiyFp ten_mk; // Cached power of ten: 10^-k
|
||||||
int mk; // -k
|
int mk; // -k
|
||||||
GetCachedPower(w.e() + DiyFp::kSignificandSize, minimal_target_exponent,
|
GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
|
||||||
maximal_target_exponent, &mk, &ten_mk);
|
kMaximalTargetExponent, &mk, &ten_mk);
|
||||||
ASSERT(minimal_target_exponent <= w.e() + ten_mk.e() +
|
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||||
DiyFp::kSignificandSize &&
|
DiyFp::kSignificandSize) &&
|
||||||
maximal_target_exponent >= w.e() + ten_mk.e() +
|
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||||
DiyFp::kSignificandSize);
|
DiyFp::kSignificandSize));
|
||||||
// Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
|
// Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
|
||||||
// 64 bit significand and ten_mk is thus only precise up to 64 bits.
|
// 64 bit significand and ten_mk is thus only precise up to 64 bits.
|
||||||
|
|
||||||
@ -488,17 +649,73 @@ bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool FastDtoa(double v,
|
// The "counted" version of grisu3 (see above) only generates requested_digits
|
||||||
|
// number of digits. This version does not generate the shortest representation,
|
||||||
|
// and with enough requested digits 0.1 will at some point print as 0.9999999...
|
||||||
|
// Grisu3 is too imprecise for real halfway cases (1.5 will not work) and
|
||||||
|
// therefore the rounding strategy for halfway cases is irrelevant.
|
||||||
|
static bool Grisu3Counted(double v,
|
||||||
|
int requested_digits,
|
||||||
Vector<char> buffer,
|
Vector<char> buffer,
|
||||||
int* length,
|
int* length,
|
||||||
int* point) {
|
int* decimal_exponent) {
|
||||||
|
DiyFp w = Double(v).AsNormalizedDiyFp();
|
||||||
|
DiyFp ten_mk; // Cached power of ten: 10^-k
|
||||||
|
int mk; // -k
|
||||||
|
GetCachedPower(w.e() + DiyFp::kSignificandSize, kMinimalTargetExponent,
|
||||||
|
kMaximalTargetExponent, &mk, &ten_mk);
|
||||||
|
ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() +
|
||||||
|
DiyFp::kSignificandSize) &&
|
||||||
|
(kMaximalTargetExponent >= w.e() + ten_mk.e() +
|
||||||
|
DiyFp::kSignificandSize));
|
||||||
|
// Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
|
||||||
|
// 64 bit significand and ten_mk is thus only precise up to 64 bits.
|
||||||
|
|
||||||
|
// The DiyFp::Times procedure rounds its result, and ten_mk is approximated
|
||||||
|
// too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
|
||||||
|
// off by a small amount.
|
||||||
|
// In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// We now have (double) (scaled_w * 10^-mk).
|
||||||
|
// DigitGen will generate the first requested_digits digits of scaled_w and
|
||||||
|
// return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It
|
||||||
|
// will not always be exactly the same since DigitGenCounted only produces a
|
||||||
|
// limited number of digits.)
|
||||||
|
int kappa;
|
||||||
|
bool result = DigitGenCounted(scaled_w, requested_digits,
|
||||||
|
buffer, length, &kappa);
|
||||||
|
*decimal_exponent = -mk + kappa;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool FastDtoa(double v,
|
||||||
|
FastDtoaMode mode,
|
||||||
|
int requested_digits,
|
||||||
|
Vector<char> buffer,
|
||||||
|
int* length,
|
||||||
|
int* decimal_point) {
|
||||||
ASSERT(v > 0);
|
ASSERT(v > 0);
|
||||||
ASSERT(!Double(v).IsSpecial());
|
ASSERT(!Double(v).IsSpecial());
|
||||||
|
|
||||||
|
bool result = false;
|
||||||
int decimal_exponent;
|
int decimal_exponent;
|
||||||
bool result = grisu3(v, buffer, length, &decimal_exponent);
|
switch (mode) {
|
||||||
*point = *length + decimal_exponent;
|
case FAST_DTOA_SHORTEST:
|
||||||
|
result = Grisu3(v, buffer, length, &decimal_exponent);
|
||||||
|
break;
|
||||||
|
case FAST_DTOA_PRECISION:
|
||||||
|
result = Grisu3Counted(v, requested_digits,
|
||||||
|
buffer, length, &decimal_exponent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (result) {
|
||||||
|
*decimal_point = *length + decimal_exponent;
|
||||||
buffer[*length] = '\0';
|
buffer[*length] = '\0';
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,27 +31,52 @@
|
|||||||
namespace v8 {
|
namespace v8 {
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
|
enum FastDtoaMode {
|
||||||
|
// Computes the shortest representation of the given input. The returned
|
||||||
|
// result will be the most accurate number of this length. Longer
|
||||||
|
// representations might be more accurate.
|
||||||
|
FAST_DTOA_SHORTEST,
|
||||||
|
// Computes a representation where the precision (number of digits) is
|
||||||
|
// given as input. The precision is independent of the decimal point.
|
||||||
|
FAST_DTOA_PRECISION
|
||||||
|
};
|
||||||
|
|
||||||
// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
|
// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
|
||||||
// include the terminating '\0' character.
|
// include the terminating '\0' character.
|
||||||
static const int kFastDtoaMaximalLength = 17;
|
static const int kFastDtoaMaximalLength = 17;
|
||||||
|
|
||||||
// Provides a decimal representation of v.
|
// Provides a decimal representation of v.
|
||||||
// v must be a strictly positive finite double.
|
// The result should be interpreted as buffer * 10^(point - length).
|
||||||
|
//
|
||||||
|
// Precondition:
|
||||||
|
// * v must be a strictly positive finite double.
|
||||||
|
//
|
||||||
// Returns true if it succeeds, otherwise the result can not be trusted.
|
// Returns true if it succeeds, otherwise the result can not be trusted.
|
||||||
// There will be *length digits inside the buffer followed by a null terminator.
|
// There will be *length digits inside the buffer followed by a null terminator.
|
||||||
// If the function returns true then
|
// If the function returns true and mode equals
|
||||||
|
// - FAST_DTOA_SHORTEST, then
|
||||||
|
// the parameter requested_digits is ignored.
|
||||||
|
// The result satisfies
|
||||||
// v == (double) (buffer * 10^(point - length)).
|
// v == (double) (buffer * 10^(point - length)).
|
||||||
// The digits in the buffer are the shortest representation possible: no
|
// The digits in the buffer are the shortest representation possible. E.g.
|
||||||
// 0.099999999999 instead of 0.1.
|
// if 0.099999999999 and 0.1 represent the same double then "1" is returned
|
||||||
|
// with point = 0.
|
||||||
// The last digit will be closest to the actual v. That is, even if several
|
// The last digit will be closest to the actual v. That is, even if several
|
||||||
// digits might correctly yield 'v' when read again, the buffer will contain the
|
// digits might correctly yield 'v' when read again, the buffer will contain
|
||||||
// one closest to v.
|
// the one closest to v.
|
||||||
// The variable 'sign' will be '0' if the given number is positive, and '1'
|
// - FAST_DTOA_PRECISION, then
|
||||||
// otherwise.
|
// the buffer contains requested_digits digits.
|
||||||
|
// the difference v - (buffer * 10^(point-length)) is closest to zero for
|
||||||
|
// all possible representations of requested_digits digits.
|
||||||
|
// If there are two values that are equally close, then FastDtoa returns
|
||||||
|
// false.
|
||||||
|
// For both modes the buffer must be large enough to hold the result.
|
||||||
bool FastDtoa(double d,
|
bool FastDtoa(double d,
|
||||||
|
FastDtoaMode mode,
|
||||||
|
int requested_digits,
|
||||||
Vector<char> buffer,
|
Vector<char> buffer,
|
||||||
int* length,
|
int* length,
|
||||||
int* point);
|
int* decimal_point);
|
||||||
|
|
||||||
} } // namespace v8::internal
|
} } // namespace v8::internal
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ Import('context object_files')
|
|||||||
SOURCES = {
|
SOURCES = {
|
||||||
'all': [
|
'all': [
|
||||||
'gay-fixed.cc',
|
'gay-fixed.cc',
|
||||||
|
'gay-precision.cc',
|
||||||
'gay-shortest.cc',
|
'gay-shortest.cc',
|
||||||
'test-accessors.cc',
|
'test-accessors.cc',
|
||||||
'test-alloc.cc',
|
'test-alloc.cc',
|
||||||
|
100050
test/cctest/gay-precision.cc
Normal file
100050
test/cctest/gay-precision.cc
Normal file
File diff suppressed because it is too large
Load Diff
47
test/cctest/gay-precision.h
Normal file
47
test/cctest/gay-precision.h
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright 2006-2008 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 GAY_PRECISION_H_
|
||||||
|
#define GAY_PRECISION_H_
|
||||||
|
|
||||||
|
namespace v8 {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
struct PrecomputedPrecision {
|
||||||
|
double v;
|
||||||
|
int number_digits;
|
||||||
|
const char* representation;
|
||||||
|
int decimal_point;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns precomputed values of dtoa. The strings have been generated using
|
||||||
|
// Gay's dtoa in mode "precision".
|
||||||
|
Vector<const PrecomputedPrecision> PrecomputedPrecisionRepresentations();
|
||||||
|
|
||||||
|
} } // namespace v8::internal
|
||||||
|
|
||||||
|
#endif // GAY_PRECISION_H_
|
@ -9,13 +9,26 @@
|
|||||||
#include "diy-fp.h"
|
#include "diy-fp.h"
|
||||||
#include "double.h"
|
#include "double.h"
|
||||||
#include "fast-dtoa.h"
|
#include "fast-dtoa.h"
|
||||||
|
#include "gay-precision.h"
|
||||||
#include "gay-shortest.h"
|
#include "gay-shortest.h"
|
||||||
|
|
||||||
using namespace v8::internal;
|
using namespace v8::internal;
|
||||||
|
|
||||||
static const int kBufferSize = 100;
|
static const int kBufferSize = 100;
|
||||||
|
|
||||||
TEST(FastDtoaVariousDoubles) {
|
|
||||||
|
// Removes trailing '0' digits.
|
||||||
|
static void TrimRepresentation(Vector<char> representation) {
|
||||||
|
int len = strlen(representation.start());
|
||||||
|
int i;
|
||||||
|
for (i = len - 1; i >= 0; --i) {
|
||||||
|
if (representation[i] != '0') break;
|
||||||
|
}
|
||||||
|
representation[i + 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(FastDtoaShortestVariousDoubles) {
|
||||||
char buffer_container[kBufferSize];
|
char buffer_container[kBufferSize];
|
||||||
Vector<char> buffer(buffer_container, kBufferSize);
|
Vector<char> buffer(buffer_container, kBufferSize);
|
||||||
int length;
|
int length;
|
||||||
@ -23,38 +36,45 @@ TEST(FastDtoaVariousDoubles) {
|
|||||||
int status;
|
int status;
|
||||||
|
|
||||||
double min_double = 5e-324;
|
double min_double = 5e-324;
|
||||||
status = FastDtoa(min_double, buffer, &length, &point);
|
status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("5", buffer.start());
|
CHECK_EQ("5", buffer.start());
|
||||||
CHECK_EQ(-323, point);
|
CHECK_EQ(-323, point);
|
||||||
|
|
||||||
double max_double = 1.7976931348623157e308;
|
double max_double = 1.7976931348623157e308;
|
||||||
status = FastDtoa(max_double, buffer, &length, &point);
|
status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("17976931348623157", buffer.start());
|
CHECK_EQ("17976931348623157", buffer.start());
|
||||||
CHECK_EQ(309, point);
|
CHECK_EQ(309, point);
|
||||||
|
|
||||||
status = FastDtoa(4294967272.0, buffer, &length, &point);
|
status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("4294967272", buffer.start());
|
CHECK_EQ("4294967272", buffer.start());
|
||||||
CHECK_EQ(10, point);
|
CHECK_EQ(10, point);
|
||||||
|
|
||||||
status = FastDtoa(4.1855804968213567e298, buffer, &length, &point);
|
status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("4185580496821357", buffer.start());
|
CHECK_EQ("4185580496821357", buffer.start());
|
||||||
CHECK_EQ(299, point);
|
CHECK_EQ(299, point);
|
||||||
|
|
||||||
status = FastDtoa(5.5626846462680035e-309, buffer, &length, &point);
|
status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("5562684646268003", buffer.start());
|
CHECK_EQ("5562684646268003", buffer.start());
|
||||||
CHECK_EQ(-308, point);
|
CHECK_EQ(-308, point);
|
||||||
|
|
||||||
status = FastDtoa(2147483648.0, buffer, &length, &point);
|
status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
CHECK(status);
|
CHECK(status);
|
||||||
CHECK_EQ("2147483648", buffer.start());
|
CHECK_EQ("2147483648", buffer.start());
|
||||||
CHECK_EQ(10, point);
|
CHECK_EQ(10, point);
|
||||||
|
|
||||||
status = FastDtoa(3.5844466002796428e+298, buffer, &length, &point);
|
status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0,
|
||||||
|
buffer, &length, &point);
|
||||||
if (status) { // Not all FastDtoa variants manage to compute this number.
|
if (status) { // Not all FastDtoa variants manage to compute this number.
|
||||||
CHECK_EQ("35844466002796428", buffer.start());
|
CHECK_EQ("35844466002796428", buffer.start());
|
||||||
CHECK_EQ(299, point);
|
CHECK_EQ(299, point);
|
||||||
@ -62,7 +82,7 @@ TEST(FastDtoaVariousDoubles) {
|
|||||||
|
|
||||||
uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
|
uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
|
||||||
double v = Double(smallest_normal64).value();
|
double v = Double(smallest_normal64).value();
|
||||||
status = FastDtoa(v, buffer, &length, &point);
|
status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||||
if (status) {
|
if (status) {
|
||||||
CHECK_EQ("22250738585072014", buffer.start());
|
CHECK_EQ("22250738585072014", buffer.start());
|
||||||
CHECK_EQ(-307, point);
|
CHECK_EQ(-307, point);
|
||||||
@ -70,7 +90,7 @@ TEST(FastDtoaVariousDoubles) {
|
|||||||
|
|
||||||
uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
|
uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
|
||||||
v = Double(largest_denormal64).value();
|
v = Double(largest_denormal64).value();
|
||||||
status = FastDtoa(v, buffer, &length, &point);
|
status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||||
if (status) {
|
if (status) {
|
||||||
CHECK_EQ("2225073858507201", buffer.start());
|
CHECK_EQ("2225073858507201", buffer.start());
|
||||||
CHECK_EQ(-307, point);
|
CHECK_EQ(-307, point);
|
||||||
@ -78,6 +98,107 @@ TEST(FastDtoaVariousDoubles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(FastDtoaPrecisionVariousDoubles) {
|
||||||
|
char buffer_container[kBufferSize];
|
||||||
|
Vector<char> buffer(buffer_container, kBufferSize);
|
||||||
|
int length;
|
||||||
|
int point;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
status = FastDtoa(1.0, FAST_DTOA_PRECISION, 3, buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_GE(3, length);
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ("1", buffer.start());
|
||||||
|
CHECK_EQ(1, point);
|
||||||
|
|
||||||
|
status = FastDtoa(1.5, FAST_DTOA_PRECISION, 10, buffer, &length, &point);
|
||||||
|
if (status) {
|
||||||
|
CHECK_GE(10, length);
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ("15", buffer.start());
|
||||||
|
CHECK_EQ(1, point);
|
||||||
|
}
|
||||||
|
|
||||||
|
double min_double = 5e-324;
|
||||||
|
status = FastDtoa(min_double, FAST_DTOA_PRECISION, 5,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("49407", buffer.start());
|
||||||
|
CHECK_EQ(-323, point);
|
||||||
|
|
||||||
|
double max_double = 1.7976931348623157e308;
|
||||||
|
status = FastDtoa(max_double, FAST_DTOA_PRECISION, 7,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("1797693", buffer.start());
|
||||||
|
CHECK_EQ(309, point);
|
||||||
|
|
||||||
|
status = FastDtoa(4294967272.0, FAST_DTOA_PRECISION, 14,
|
||||||
|
buffer, &length, &point);
|
||||||
|
if (status) {
|
||||||
|
CHECK_GE(14, length);
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ("4294967272", buffer.start());
|
||||||
|
CHECK_EQ(10, point);
|
||||||
|
}
|
||||||
|
|
||||||
|
status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("41855804968213567", buffer.start());
|
||||||
|
CHECK_EQ(299, point);
|
||||||
|
|
||||||
|
status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("6", buffer.start());
|
||||||
|
CHECK_EQ(-308, point);
|
||||||
|
|
||||||
|
status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("21475", buffer.start());
|
||||||
|
CHECK_EQ(10, point);
|
||||||
|
|
||||||
|
status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_PRECISION, 10,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_GE(10, length);
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ("35844466", buffer.start());
|
||||||
|
CHECK_EQ(299, point);
|
||||||
|
|
||||||
|
uint64_t smallest_normal64 = V8_2PART_UINT64_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 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
|
||||||
|
v = Double(largest_denormal64).value();
|
||||||
|
status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_GE(20, length);
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ("22250738585072009", buffer.start());
|
||||||
|
CHECK_EQ(-307, point);
|
||||||
|
|
||||||
|
v = 3.3161339052167390562200598e-237;
|
||||||
|
status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("331613390521673906", buffer.start());
|
||||||
|
CHECK_EQ(-236, point);
|
||||||
|
|
||||||
|
v = 7.9885183916008099497815232e+191;
|
||||||
|
status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point);
|
||||||
|
CHECK(status);
|
||||||
|
CHECK_EQ("7989", buffer.start());
|
||||||
|
CHECK_EQ(192, point);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(FastDtoaGayShortest) {
|
TEST(FastDtoaGayShortest) {
|
||||||
char buffer_container[kBufferSize];
|
char buffer_container[kBufferSize];
|
||||||
Vector<char> buffer(buffer_container, kBufferSize);
|
Vector<char> buffer(buffer_container, kBufferSize);
|
||||||
@ -94,7 +215,7 @@ TEST(FastDtoaGayShortest) {
|
|||||||
const PrecomputedShortest current_test = precomputed[i];
|
const PrecomputedShortest current_test = precomputed[i];
|
||||||
total++;
|
total++;
|
||||||
double v = current_test.v;
|
double v = current_test.v;
|
||||||
status = FastDtoa(v, buffer, &length, &point);
|
status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
|
||||||
CHECK_GE(kFastDtoaMaximalLength, length);
|
CHECK_GE(kFastDtoaMaximalLength, length);
|
||||||
if (!status) continue;
|
if (!status) continue;
|
||||||
if (length == kFastDtoaMaximalLength) needed_max_length = true;
|
if (length == kFastDtoaMaximalLength) needed_max_length = true;
|
||||||
@ -105,3 +226,43 @@ TEST(FastDtoaGayShortest) {
|
|||||||
CHECK_GT(succeeded*1.0/total, 0.99);
|
CHECK_GT(succeeded*1.0/total, 0.99);
|
||||||
CHECK(needed_max_length);
|
CHECK(needed_max_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(FastDtoaGayPrecision) {
|
||||||
|
char buffer_container[kBufferSize];
|
||||||
|
Vector<char> buffer(buffer_container, kBufferSize);
|
||||||
|
bool status;
|
||||||
|
int length;
|
||||||
|
int point;
|
||||||
|
int succeeded = 0;
|
||||||
|
int total = 0;
|
||||||
|
// Count separately for entries with less than 15 requested digits.
|
||||||
|
int succeeded_15 = 0;
|
||||||
|
int total_15 = 0;
|
||||||
|
|
||||||
|
Vector<const PrecomputedPrecision> precomputed =
|
||||||
|
PrecomputedPrecisionRepresentations();
|
||||||
|
for (int i = 0; i < precomputed.length(); ++i) {
|
||||||
|
const PrecomputedPrecision current_test = precomputed[i];
|
||||||
|
double v = current_test.v;
|
||||||
|
int number_digits = current_test.number_digits;
|
||||||
|
total++;
|
||||||
|
if (number_digits <= 15) total_15++;
|
||||||
|
status = FastDtoa(v, FAST_DTOA_PRECISION, number_digits,
|
||||||
|
buffer, &length, &point);
|
||||||
|
CHECK_GE(number_digits, length);
|
||||||
|
if (!status) continue;
|
||||||
|
succeeded++;
|
||||||
|
if (number_digits <= 15) succeeded_15++;
|
||||||
|
TrimRepresentation(buffer);
|
||||||
|
CHECK_EQ(current_test.decimal_point, point);
|
||||||
|
CHECK_EQ(current_test.representation, buffer.start());
|
||||||
|
}
|
||||||
|
// The precomputed numbers contain many entries with many requested
|
||||||
|
// digits. These have a high failure rate and we therefore expect a lower
|
||||||
|
// success rate than for the shortest representation.
|
||||||
|
CHECK_GT(succeeded*1.0/total, 0.85);
|
||||||
|
// However with less than 15 digits almost the algorithm should almost always
|
||||||
|
// succeed.
|
||||||
|
CHECK_GT(succeeded_15*1.0/total_15, 0.9999);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user