blob: b857a5dc5997197465363efa547be19214722eb3 [file] [log] [blame]
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001// 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
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000033#include "bignum-dtoa.h"
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000034#include "double.h"
35#include "fast-dtoa.h"
36#include "fixed-dtoa.h"
37
38namespace v8 {
39namespace internal {
40
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000041static 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,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000054 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;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000070 return;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000071 }
72
73 if (mode == DTOA_PRECISION && requested_digits == 0) {
74 buffer[0] = '\0';
75 *length = 0;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000076 return;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000077 }
78
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000079 bool fast_worked;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000080 switch (mode) {
81 case DTOA_SHORTEST:
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000082 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
83 break;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000084 case DTOA_FIXED:
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000085 fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
86 break;
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000087 case DTOA_PRECISION:
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000088 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits,
89 buffer, length, point);
90 break;
91 default:
92 UNREACHABLE();
93 fast_worked = false;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000094 }
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000095 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';
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000101}
102
103} } // namespace v8::internal