blob: 29262e53063ac5c6a74383dd12f5c19561b0c01a [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_CONVERSIONS_H_
6#define V8_CONVERSIONS_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include <limits>
9
10#include "src/base/logging.h"
11#include "src/handles.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/utils.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010013
Steve Blocka7e24c12009-10-30 11:49:00 +000014namespace v8 {
15namespace internal {
16
Ben Murdoch589d6972011-11-30 16:04:58 +000017class UnicodeCache;
18
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000019// Maximum number of significant digits in decimal representation.
20// The longest possible double in decimal representation is
21// (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074
22// (768 digits). If we parse a number whose first digits are equal to a
23// mean of 2 adjacent doubles (that could have up to 769 digits) the result
24// must be rounded to the bigger one unless the tail consists of zeros, so
25// we don't need to preserve all the digits.
26const int kMaxSignificantDigits = 772;
27
28
Ben Murdoch3ef787d2012-04-12 10:51:47 +010029inline bool isDigit(int x, int radix) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000030 return (x >= '0' && x <= '9' && x < '0' + radix)
31 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10)
32 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10);
33}
34
35
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036inline bool isBinaryDigit(int x) {
37 return x == '0' || x == '1';
38}
39
40
41// The fast double-to-(unsigned-)int conversion routine does not guarantee
42// rounding towards zero.
43// If x is NaN, the result is INT_MIN. Otherwise the result is the argument x,
44// clamped to [INT_MIN, INT_MAX] and then rounded to an integer.
45inline int FastD2IChecked(double x) {
46 if (!(x >= INT_MIN)) return INT_MIN; // Negation to catch NaNs.
47 if (x > INT_MAX) return INT_MAX;
48 return static_cast<int>(x);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000049}
50
Steve Block3ce2e202009-11-05 08:53:23 +000051
Steve Block6ded16b2010-05-10 14:33:55 +010052// The fast double-to-(unsigned-)int conversion routine does not guarantee
Steve Blocka7e24c12009-10-30 11:49:00 +000053// rounding towards zero.
54// The result is unspecified if x is infinite or NaN, or if the rounded
55// integer value is outside the range of type int.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056inline int FastD2I(double x) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 return static_cast<int32_t>(x);
Steve Block6ded16b2010-05-10 14:33:55 +010058}
59
Ben Murdoch3ef787d2012-04-12 10:51:47 +010060inline unsigned int FastD2UI(double x);
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063inline double FastI2D(int x) {
Steve Blocka7e24c12009-10-30 11:49:00 +000064 // There is no rounding involved in converting an integer to a
65 // double, so this code should compile to a few instructions without
66 // any FPU pipeline stalls.
67 return static_cast<double>(x);
68}
69
70
Ben Murdoch3ef787d2012-04-12 10:51:47 +010071inline double FastUI2D(unsigned x) {
Steve Blocka7e24c12009-10-30 11:49:00 +000072 // There is no rounding involved in converting an unsigned integer to a
73 // double, so this code should compile to a few instructions without
74 // any FPU pipeline stalls.
75 return static_cast<double>(x);
76}
77
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079// This function should match the exact semantics of ECMA-262 20.2.2.17.
80inline float DoubleToFloat32(double x);
81
82
Steve Blocka7e24c12009-10-30 11:49:00 +000083// This function should match the exact semantics of ECMA-262 9.4.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084inline double DoubleToInteger(double x);
Steve Blocka7e24c12009-10-30 11:49:00 +000085
86
87// This function should match the exact semantics of ECMA-262 9.5.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088inline int32_t DoubleToInt32(double x);
Steve Blocka7e24c12009-10-30 11:49:00 +000089
90
91// This function should match the exact semantics of ECMA-262 9.6.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092inline uint32_t DoubleToUint32(double x);
Steve Blocka7e24c12009-10-30 11:49:00 +000093
94
Steve Blocka7e24c12009-10-30 11:49:00 +000095// Enumeration for allowing octals and ignoring junk when converting
96// strings to numbers.
97enum ConversionFlags {
98 NO_FLAGS = 0,
99 ALLOW_HEX = 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 ALLOW_OCTAL = 2,
101 ALLOW_IMPLICIT_OCTAL = 4,
102 ALLOW_BINARY = 8,
103 ALLOW_TRAILING_JUNK = 16
Steve Blocka7e24c12009-10-30 11:49:00 +0000104};
105
106
Steve Blocka7e24c12009-10-30 11:49:00 +0000107// Converts a string into a double value according to ECMA-262 9.3.1
Ben Murdoch8b112d22011-06-08 16:22:53 +0100108double StringToDouble(UnicodeCache* unicode_cache,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 Vector<const uint8_t> str,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100110 int flags,
111 double empty_string_val = 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000112double StringToDouble(UnicodeCache* unicode_cache,
113 Vector<const uc16> str,
114 int flags,
115 double empty_string_val = 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100116// This version expects a zero-terminated character array.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100117double StringToDouble(UnicodeCache* unicode_cache,
118 const char* str,
119 int flags,
120 double empty_string_val = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000121
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122// Converts a string into an integer.
123double StringToInt(UnicodeCache* unicode_cache,
124 Vector<const uint8_t> vector,
125 int radix);
126
127
128double StringToInt(UnicodeCache* unicode_cache,
129 Vector<const uc16> vector,
130 int radix);
131
Ben Murdoch589d6972011-11-30 16:04:58 +0000132const int kDoubleToCStringMinBufferSize = 100;
133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134// Converts a double to a string value according to ECMA-262 9.8.1.
135// The buffer should be large enough for any floating point number.
136// 100 characters is enough.
137const char* DoubleToCString(double value, Vector<char> buffer);
138
139// Convert an int to a null-terminated string. The returned string is
140// located inside the buffer, but not necessarily at the start.
141const char* IntToCString(int n, Vector<char> buffer);
142
143// Additional number to string conversions for the number type.
144// The caller is responsible for calling free on the returned pointer.
145char* DoubleToFixedCString(double value, int f);
146char* DoubleToExponentialCString(double value, int f);
147char* DoubleToPrecisionCString(double value, int f);
148char* DoubleToRadixCString(double value, int radix);
149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150
151static inline bool IsMinusZero(double value) {
152 static const DoubleRepresentation minus_zero(-0.0);
153 return DoubleRepresentation(value) == minus_zero;
154}
155
156
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157inline bool IsSmiDouble(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158
159
160// Integer32 is an integer that can be represented as a signed 32-bit
161// integer. It has to be in the range [-2^31, 2^31 - 1].
162// We also have to check for negative 0 as it is not an Integer32.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163inline bool IsInt32Double(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164
165
166// UInteger32 is an integer that can be represented as an unsigned 32-bit
167// integer. It has to be in the range [0, 2^32 - 1].
168// We also have to check for negative 0 as it is not a UInteger32.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169inline bool IsUint32Double(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170
171
172// Convert from Number object to C integer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173inline int32_t NumberToInt32(Object* number);
174inline uint32_t NumberToUint32(Object* number);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100175inline int64_t NumberToInt64(Object* number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400177double StringToDouble(UnicodeCache* unicode_cache, Handle<String> string,
178 int flags, double empty_string_val = 0.0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179
180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181inline bool TryNumberToSize(Isolate* isolate, Object* number, size_t* result);
182
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183
184// Converts a number into size_t.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185inline size_t NumberToSize(Isolate* isolate, Object* number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000187
188// returns DoubleToString(StringToDouble(string)) == string
189bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string);
190
191} // namespace internal
192} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
194#endif // V8_CONVERSIONS_H_