Switch to vectors instead of bare char* arrays.

Review URL: http://codereview.chromium.org/1732019

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4546 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
floitschV8@gmail.com 2010-04-29 12:15:02 +00:00
parent 3e689b5283
commit 0aa67676f6
4 changed files with 31 additions and 19 deletions

View File

@ -769,9 +769,11 @@ const char* DoubleToCString(double v, Vector<char> buffer) {
char* decimal_rep;
bool used_gay_dtoa = false;
char fast_dtoa_buffer[kFastDtoaMaximalLength + 1];
const int kFastDtoaBufferCapacity = kFastDtoaMaximalLength + 1;
char fast_dtoa_buffer[kFastDtoaBufferCapacity];
int length;
if (FastDtoa(v, fast_dtoa_buffer, &sign, &length, &decimal_point)) {
if (FastDtoa(v, Vector<char>(fast_dtoa_buffer, kFastDtoaBufferCapacity),
&sign, &length, &decimal_point)) {
decimal_rep = fast_dtoa_buffer;
} else {
decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL);

View File

@ -61,7 +61,7 @@ static const int maximal_target_exponent = -32;
// Output: returns true if the buffer is guaranteed to contain the closest
// representable number to the input.
// Modifies the generated digits in the buffer to approach (round towards) w.
bool RoundWeed(char* buffer,
bool RoundWeed(Vector<char> buffer,
int length,
uint64_t distance_too_high_w,
uint64_t unsafe_interval,
@ -324,7 +324,7 @@ static void BiggestPowerTen(uint32_t number,
bool DigitGen(DiyFp low,
DiyFp w,
DiyFp high,
char* buffer,
Vector<char> buffer,
int* length,
int* kappa) {
ASSERT(low.e() == w.e() && w.e() == high.e());
@ -437,7 +437,7 @@ bool DigitGen(DiyFp low,
// 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
// computed.
bool grisu3(double v, char* buffer, int* length, int* decimal_exponent) {
bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
DiyFp w = Double(v).AsNormalizedDiyFp();
// boundary_minus and boundary_plus are the boundaries between v and its
// closest floating-point neighbors. Any number strictly between
@ -488,7 +488,11 @@ bool grisu3(double v, char* buffer, int* length, int* decimal_exponent) {
}
bool FastDtoa(double v, char* buffer, int* sign, int* length, int* point) {
bool FastDtoa(double v,
Vector<char> buffer,
int* sign,
int* length,
int* point) {
ASSERT(v != 0);
ASSERT(!Double(v).IsSpecial());

View File

@ -48,7 +48,11 @@ static const int kFastDtoaMaximalLength = 17;
// one closest to v.
// The variable 'sign' will be '0' if the given number is positive, and '1'
// otherwise.
bool FastDtoa(double d, char* buffer, int* sign, int* length, int* point);
bool FastDtoa(double d,
Vector<char> buffer,
int* sign,
int* length,
int* point);
} } // namespace v8::internal

View File

@ -16,7 +16,8 @@ using namespace v8::internal;
static const int kBufferSize = 100;
TEST(FastDtoaVariousDoubles) {
char buffer[kBufferSize];
char buffer_container[kBufferSize];
Vector<char> buffer(buffer_container, kBufferSize);
int sign;
int length;
int point;
@ -26,43 +27,43 @@ TEST(FastDtoaVariousDoubles) {
status = FastDtoa(min_double, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("5", buffer);
CHECK_EQ("5", buffer.start());
CHECK_EQ(-323, point);
double max_double = 1.7976931348623157e308;
status = FastDtoa(max_double, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("17976931348623157", buffer);
CHECK_EQ("17976931348623157", buffer.start());
CHECK_EQ(309, point);
status = FastDtoa(4294967272.0, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("4294967272", buffer);
CHECK_EQ("4294967272", buffer.start());
CHECK_EQ(10, point);
status = FastDtoa(4.1855804968213567e298, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("4185580496821357", buffer);
CHECK_EQ("4185580496821357", buffer.start());
CHECK_EQ(299, point);
status = FastDtoa(5.5626846462680035e-309, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("5562684646268003", buffer);
CHECK_EQ("5562684646268003", buffer.start());
CHECK_EQ(-308, point);
status = FastDtoa(2147483648.0, buffer, &sign, &length, &point);
CHECK(status);
CHECK_EQ(0, sign);
CHECK_EQ("2147483648", buffer);
CHECK_EQ("2147483648", buffer.start());
CHECK_EQ(10, point);
status = FastDtoa(3.5844466002796428e+298, buffer, &sign, &length, &point);
if (status) { // Not all FastDtoa variants manage to compute this number.
CHECK_EQ("35844466002796428", buffer);
CHECK_EQ("35844466002796428", buffer.start());
CHECK_EQ(0, sign);
CHECK_EQ(299, point);
}
@ -72,7 +73,7 @@ TEST(FastDtoaVariousDoubles) {
status = FastDtoa(v, buffer, &sign, &length, &point);
if (status) {
CHECK_EQ(0, sign);
CHECK_EQ("22250738585072014", buffer);
CHECK_EQ("22250738585072014", buffer.start());
CHECK_EQ(-307, point);
}
@ -81,14 +82,15 @@ TEST(FastDtoaVariousDoubles) {
status = FastDtoa(v, buffer, &sign, &length, &point);
if (status) {
CHECK_EQ(0, sign);
CHECK_EQ("2225073858507201", buffer);
CHECK_EQ("2225073858507201", buffer.start());
CHECK_EQ(-307, point);
}
}
TEST(FastDtoaGayShortest) {
char buffer[kBufferSize];
char buffer_container[kBufferSize];
Vector<char> buffer(buffer_container, kBufferSize);
bool status;
int sign;
int length;
@ -109,7 +111,7 @@ TEST(FastDtoaGayShortest) {
succeeded++;
CHECK_EQ(0, sign); // All precomputed numbers are positive.
CHECK_EQ(current_test.decimal_point, point);
CHECK_EQ(current_test.representation, buffer);
CHECK_EQ(current_test.representation, buffer.start());
}
CHECK_GT(succeeded*1.0/total, 0.99);
CHECK(needed_max_length);