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 <cmath> |
6 | |
7 | #include "src/base/logging.h" |
8 | #include "src/utils.h" |
9 | |
10 | #include "src/dtoa.h" |
11 | |
12 | #include "src/bignum-dtoa.h" |
13 | #include "src/double.h" |
14 | #include "src/fast-dtoa.h" |
15 | #include "src/fixed-dtoa.h" |
16 | |
17 | namespace v8 { |
18 | namespace internal { |
19 | |
20 | static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode) { |
21 | switch (dtoa_mode) { |
22 | case DTOA_SHORTEST: return BIGNUM_DTOA_SHORTEST; |
23 | case DTOA_FIXED: return BIGNUM_DTOA_FIXED; |
24 | case DTOA_PRECISION: return BIGNUM_DTOA_PRECISION; |
25 | default: |
26 | UNREACHABLE(); |
27 | } |
28 | } |
29 | |
30 | |
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, |
67 | buffer, 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 internal |
81 | } // namespace v8 |
82 | |