blob: 9c0b8f3560bf4e8de36817ec172d20c29e6cc765 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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#ifndef V8_CONVERSIONS_H_
29#define V8_CONVERSIONS_H_
30
Ben Murdoch8b112d22011-06-08 16:22:53 +010031#include "scanner-base.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000032#include "utils.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010033
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037// Maximum number of significant digits in decimal representation.
38// The longest possible double in decimal representation is
39// (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
40// (768 digits). If we parse a number whose first digits are equal to a
41// mean of 2 adjacent doubles (that could have up to 769 digits) the result
42// must be rounded to the bigger one unless the tail consists of zeros, so
43// we don't need to preserve all the digits.
44const int kMaxSignificantDigits = 772;
45
46
47static bool isDigit(int x, int radix) {
48 return (x >= '0' && x <= '9' && x < '0' + radix)
49 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
50 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
51}
52
53
54static double SignedZero(bool negative) {
55 return negative ? -0.0 : 0.0;
56}
57
Steve Block3ce2e202009-11-05 08:53:23 +000058
Steve Block6ded16b2010-05-10 14:33:55 +010059// The fast double-to-(unsigned-)int conversion routine does not guarantee
Steve Blocka7e24c12009-10-30 11:49:00 +000060// rounding towards zero.
61// The result is unspecified if x is infinite or NaN, or if the rounded
62// integer value is outside the range of type int.
Steve Block6ded16b2010-05-10 14:33:55 +010063static inline int FastD2I(double x) {
64 // The static_cast convertion from double to int used to be slow, but
65 // as new benchmarks show, now it is much faster than lrint().
66 return static_cast<int>(x);
67}
68
69static inline unsigned int FastD2UI(double x);
Steve Blocka7e24c12009-10-30 11:49:00 +000070
71
72static inline double FastI2D(int x) {
73 // There is no rounding involved in converting an integer to a
74 // double, so this code should compile to a few instructions without
75 // any FPU pipeline stalls.
76 return static_cast<double>(x);
77}
78
79
80static inline double FastUI2D(unsigned x) {
81 // There is no rounding involved in converting an unsigned integer to a
82 // double, so this code should compile to a few instructions without
83 // any FPU pipeline stalls.
84 return static_cast<double>(x);
85}
86
87
88// This function should match the exact semantics of ECMA-262 9.4.
89static inline double DoubleToInteger(double x);
90
91
92// This function should match the exact semantics of ECMA-262 9.5.
93static inline int32_t DoubleToInt32(double x);
94
95
96// This function should match the exact semantics of ECMA-262 9.6.
97static inline uint32_t DoubleToUint32(double x) {
98 return static_cast<uint32_t>(DoubleToInt32(x));
99}
100
101
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// Enumeration for allowing octals and ignoring junk when converting
103// strings to numbers.
104enum ConversionFlags {
105 NO_FLAGS = 0,
106 ALLOW_HEX = 1,
107 ALLOW_OCTALS = 2,
108 ALLOW_TRAILING_JUNK = 4
109};
110
111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112// Converts a string into a double value according to ECMA-262 9.3.1
Ben Murdoch8b112d22011-06-08 16:22:53 +0100113double StringToDouble(UnicodeCache* unicode_cache,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100114 Vector<const char> str,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100115 int flags,
116 double empty_string_val = 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000117double StringToDouble(UnicodeCache* unicode_cache,
118 Vector<const uc16> str,
119 int flags,
120 double empty_string_val = 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100121// This version expects a zero-terminated character array.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100122double StringToDouble(UnicodeCache* unicode_cache,
123 const char* str,
124 int flags,
125 double empty_string_val = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
Steve Blocka7e24c12009-10-30 11:49:00 +0000127// Converts a double to a string value according to ECMA-262 9.8.1.
128// The buffer should be large enough for any floating point number.
129// 100 characters is enough.
130const char* DoubleToCString(double value, Vector<char> buffer);
131
132// Convert an int to a null-terminated string. The returned string is
133// located inside the buffer, but not necessarily at the start.
134const char* IntToCString(int n, Vector<char> buffer);
135
136// Additional number to string conversions for the number type.
137// The caller is responsible for calling free on the returned pointer.
138char* DoubleToFixedCString(double value, int f);
139char* DoubleToExponentialCString(double value, int f);
140char* DoubleToPrecisionCString(double value, int f);
141char* DoubleToRadixCString(double value, int radix);
142
143} } // namespace v8::internal
144
145#endif // V8_CONVERSIONS_H_