2011-09-07 12:39:53 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-05-05 13:51:27 +00:00
|
|
|
|
2013-04-19 13:26:47 +00:00
|
|
|
#include <cmath>
|
2010-05-05 13:51:27 +00:00
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/utils.h"
|
2011-09-07 12:39:53 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/dtoa.h"
|
2010-05-05 13:51:27 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bignum-dtoa.h"
|
|
|
|
#include "src/double.h"
|
|
|
|
#include "src/fast-dtoa.h"
|
|
|
|
#include "src/fixed-dtoa.h"
|
2010-05-05 13:51:27 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2010-11-17 13:20:44 +00:00
|
|
|
static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode) {
|
|
|
|
switch (dtoa_mode) {
|
|
|
|
case DTOA_SHORTEST: return BIGNUM_DTOA_SHORTEST;
|
|
|
|
case DTOA_FIXED: return BIGNUM_DTOA_FIXED;
|
|
|
|
case DTOA_PRECISION: return BIGNUM_DTOA_PRECISION;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DoubleToAscii(double v, DtoaMode mode, int requested_digits,
|
2010-05-05 13:51:27 +00:00
|
|
|
Vector<char> buffer, int* sign, int* length, int* point) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!Double(v).IsSpecial());
|
|
|
|
DCHECK(mode == DTOA_SHORTEST || requested_digits >= 0);
|
2010-05-05 13:51:27 +00:00
|
|
|
|
|
|
|
if (Double(v).Sign() < 0) {
|
|
|
|
*sign = 1;
|
|
|
|
v = -v;
|
|
|
|
} else {
|
|
|
|
*sign = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v == 0) {
|
|
|
|
buffer[0] = '0';
|
|
|
|
buffer[1] = '\0';
|
|
|
|
*length = 1;
|
|
|
|
*point = 1;
|
2010-11-17 13:20:44 +00:00
|
|
|
return;
|
2010-05-05 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == DTOA_PRECISION && requested_digits == 0) {
|
|
|
|
buffer[0] = '\0';
|
|
|
|
*length = 0;
|
2010-11-17 13:20:44 +00:00
|
|
|
return;
|
2010-05-05 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 13:20:44 +00:00
|
|
|
bool fast_worked;
|
2010-05-05 13:51:27 +00:00
|
|
|
switch (mode) {
|
|
|
|
case DTOA_SHORTEST:
|
2010-11-17 13:20:44 +00:00
|
|
|
fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
|
|
|
|
break;
|
2010-05-05 13:51:27 +00:00
|
|
|
case DTOA_FIXED:
|
2010-11-17 13:20:44 +00:00
|
|
|
fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
|
|
|
|
break;
|
2010-09-20 09:18:00 +00:00
|
|
|
case DTOA_PRECISION:
|
2010-11-17 13:20:44 +00:00
|
|
|
fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
|
|
|
|
buffer, length, point);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2010-05-05 13:51:27 +00:00
|
|
|
}
|
2010-11-17 13:20:44 +00:00
|
|
|
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, buffer, length, point);
|
|
|
|
buffer[*length] = '\0';
|
2010-05-05 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|