blob: b857a5dc5997197465363efa547be19214722eb3 [file] [log] [blame]
Kristian Monsen25f61362010-05-21 11:50:48 +01001// 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
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080033#include "bignum-dtoa.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010034#include "double.h"
35#include "fast-dtoa.h"
36#include "fixed-dtoa.h"
37
38namespace v8 {
39namespace internal {
40
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080041static 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
53void DoubleToAscii(double v, DtoaMode mode, int requested_digits,
Kristian Monsen25f61362010-05-21 11:50:48 +010054 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;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080070 return;
Kristian Monsen25f61362010-05-21 11:50:48 +010071 }
72
73 if (mode == DTOA_PRECISION && requested_digits == 0) {
74 buffer[0] = '\0';
75 *length = 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080076 return;
Kristian Monsen25f61362010-05-21 11:50:48 +010077 }
78
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080079 bool fast_worked;
Kristian Monsen25f61362010-05-21 11:50:48 +010080 switch (mode) {
81 case DTOA_SHORTEST:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080082 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
83 break;
Kristian Monsen25f61362010-05-21 11:50:48 +010084 case DTOA_FIXED:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080085 fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
86 break;
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 case DTOA_PRECISION:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080088 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
89 buffer, length, point);
90 break;
91 default:
92 UNREACHABLE();
93 fast_worked = false;
Kristian Monsen25f61362010-05-21 11:50:48 +010094 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080095 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';
Kristian Monsen25f61362010-05-21 11:50:48 +0100101}
102
103} } // namespace v8::internal