1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/base/numbers/dtoa.h"
6
7 #include <cmath>
8
9 #include "src/base/logging.h"
10 #include "src/base/numbers/bignum-dtoa.h"
11 #include "src/base/numbers/double.h"
12 #include "src/base/numbers/fast-dtoa.h"
13 #include "src/base/numbers/fixed-dtoa.h"
14
15 namespace v8 {
16 namespace base {
17
DtoaToBignumDtoaMode(DtoaMode dtoa_mode)18 static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode) {
19 switch (dtoa_mode) {
20 case DTOA_SHORTEST:
21 return BIGNUM_DTOA_SHORTEST;
22 case DTOA_FIXED:
23 return BIGNUM_DTOA_FIXED;
24 case DTOA_PRECISION:
25 return BIGNUM_DTOA_PRECISION;
26 default:
27 UNREACHABLE();
28 }
29 }
30
DoubleToAscii(double v,DtoaMode mode,int requested_digits,Vector<char> buffer,int * sign,int * length,int * point)31 void DoubleToAscii(double v, DtoaMode mode, int requested_digits,
32 Vector<char> buffer, int* sign, int* length, int* point) {
33 DCHECK(!Double(v).IsSpecial());
34 DCHECK(mode == DTOA_SHORTEST || requested_digits >= 0);
35
36 if (Double(v).Sign() < 0) {
37 *sign = 1;
38 v = -v;
39 } else {
40 *sign = 0;
41 }
42
43 if (v == 0) {
44 buffer[0] = '0';
45 buffer[1] = '\0';
46 *length = 1;
47 *point = 1;
48 return;
49 }
50
51 if (mode == DTOA_PRECISION && requested_digits == 0) {
52 buffer[0] = '\0';
53 *length = 0;
54 return;
55 }
56
57 bool fast_worked;
58 switch (mode) {
59 case DTOA_SHORTEST:
60 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
61 break;
62 case DTOA_FIXED:
63 fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
64 break;
65 case DTOA_PRECISION:
66 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, buffer,
67 length, point);
68 break;
69 default:
70 UNREACHABLE();
71 }
72 if (fast_worked) return;
73
74 // If the fast dtoa didn't succeed use the slower bignum version.
75 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
76 BignumDtoa(v, bignum_mode, requested_digits, buffer, length, point);
77 buffer[*length] = '\0';
78 }
79
80 } // namespace base
81 } // namespace v8
82