blob: b6589cb5ca96815240cf5e9649a4d8e6adbed94b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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#ifndef V8_CONVERSIONS_H_
29#define V8_CONVERSIONS_H_
30
31namespace v8 {
32namespace internal {
33
34// The fast double-to-int conversion routine does not guarantee
35// rounding towards zero.
36// The result is unspecified if x is infinite or NaN, or if the rounded
37// integer value is outside the range of type int.
38static inline int FastD2I(double x);
39
40
41static inline double FastI2D(int x) {
42 // There is no rounding involved in converting an integer to a
43 // double, so this code should compile to a few instructions without
44 // any FPU pipeline stalls.
45 return static_cast<double>(x);
46}
47
48
49static inline double FastUI2D(unsigned x) {
50 // There is no rounding involved in converting an unsigned integer to a
51 // double, so this code should compile to a few instructions without
52 // any FPU pipeline stalls.
53 return static_cast<double>(x);
54}
55
56
57// This function should match the exact semantics of ECMA-262 9.4.
58static inline double DoubleToInteger(double x);
59
60
61// This function should match the exact semantics of ECMA-262 9.5.
62static inline int32_t DoubleToInt32(double x);
63
64
65// This function should match the exact semantics of ECMA-262 9.6.
66static inline uint32_t DoubleToUint32(double x) {
67 return static_cast<uint32_t>(DoubleToInt32(x));
68}
69
70
71// Returns the value (0 .. 15) of a hexadecimal character c.
72// If c is not a legal hexadecimal character, returns a value < 0.
73int HexValue(uc32 c);
74
75
76// Enumeration for allowing octals and ignoring junk when converting
77// strings to numbers.
78enum ConversionFlags {
79 NO_FLAGS = 0,
80 ALLOW_HEX = 1,
81 ALLOW_OCTALS = 2,
82 ALLOW_TRAILING_JUNK = 4
83};
84
85
86// Convert from Number object to C integer.
87static inline int32_t NumberToInt32(Object* number);
88static inline uint32_t NumberToUint32(Object* number);
89
90
91// Converts a string into a double value according to ECMA-262 9.3.1
92double StringToDouble(const char* str, int flags, double empty_string_val = 0);
93double StringToDouble(String* str, int flags, double empty_string_val = 0);
94
95// Converts a string into an integer.
96int StringToInt(String* str, int index, int radix, double* value);
97int StringToInt(const char* str, int index, int radix, double* value);
98
99// Converts a double to a string value according to ECMA-262 9.8.1.
100// The buffer should be large enough for any floating point number.
101// 100 characters is enough.
102const char* DoubleToCString(double value, Vector<char> buffer);
103
104// Convert an int to a null-terminated string. The returned string is
105// located inside the buffer, but not necessarily at the start.
106const char* IntToCString(int n, Vector<char> buffer);
107
108// Additional number to string conversions for the number type.
109// The caller is responsible for calling free on the returned pointer.
110char* DoubleToFixedCString(double value, int f);
111char* DoubleToExponentialCString(double value, int f);
112char* DoubleToPrecisionCString(double value, int f);
113char* DoubleToRadixCString(double value, int radix);
114
115} } // namespace v8::internal
116
117#endif // V8_CONVERSIONS_H_