1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include <math.h>
29
30 #include "v8.h"
31 #include "dtoa.h"
32
33 #include "bignum-dtoa.h"
34 #include "double.h"
35 #include "fast-dtoa.h"
36 #include "fixed-dtoa.h"
37
38 namespace v8 {
39 namespace internal {
40
DtoaToBignumDtoaMode(DtoaMode dtoa_mode)41 static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode) {
42 switch (dtoa_mode) {
43 case DTOA_SHORTEST: return BIGNUM_DTOA_SHORTEST;
44 case DTOA_FIXED: return BIGNUM_DTOA_FIXED;
45 case DTOA_PRECISION: return BIGNUM_DTOA_PRECISION;
46 default:
47 UNREACHABLE();
48 return BIGNUM_DTOA_SHORTEST; // To silence compiler.
49 }
50 }
51
52
DoubleToAscii(double v,DtoaMode mode,int requested_digits,Vector<char> buffer,int * sign,int * length,int * point)53 void DoubleToAscii(double v, DtoaMode mode, int requested_digits,
54 Vector<char> buffer, int* sign, int* length, int* point) {
55 ASSERT(!Double(v).IsSpecial());
56 ASSERT(mode == DTOA_SHORTEST || requested_digits >= 0);
57
58 if (Double(v).Sign() < 0) {
59 *sign = 1;
60 v = -v;
61 } else {
62 *sign = 0;
63 }
64
65 if (v == 0) {
66 buffer[0] = '0';
67 buffer[1] = '\0';
68 *length = 1;
69 *point = 1;
70 return;
71 }
72
73 if (mode == DTOA_PRECISION && requested_digits == 0) {
74 buffer[0] = '\0';
75 *length = 0;
76 return;
77 }
78
79 bool fast_worked;
80 switch (mode) {
81 case DTOA_SHORTEST:
82 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
83 break;
84 case DTOA_FIXED:
85 fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
86 break;
87 case DTOA_PRECISION:
88 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
89 buffer, length, point);
90 break;
91 default:
92 UNREACHABLE();
93 fast_worked = false;
94 }
95 if (fast_worked) return;
96
97 // If the fast dtoa didn't succeed use the slower bignum version.
98 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
99 BignumDtoa(v, bignum_mode, requested_digits, buffer, length, point);
100 buffer[*length] = '\0';
101 }
102
103 } } // namespace v8::internal
104