Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_CONVERSIONS_H_ |
| 6 | #define V8_CONVERSIONS_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include <limits> |
| 9 | |
| 10 | #include "src/base/logging.h" |
| 11 | #include "src/handles.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/utils.h" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 17 | class UnicodeCache; |
| 18 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 19 | // 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. |
| 26 | const int kMaxSignificantDigits = 772; |
| 27 | |
| 28 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 29 | inline bool isDigit(int x, int radix) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 30 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | inline 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. |
| 45 | inline 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 51 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 52 | // The fast double-to-(unsigned-)int conversion routine does not guarantee |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | // 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 56 | inline int FastD2I(double x) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | return static_cast<int32_t>(x); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 60 | inline unsigned int FastD2UI(double x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
| 62 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | inline double FastI2D(int x) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | // 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 71 | inline double FastUI2D(unsigned x) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | // This function should match the exact semantics of ECMA-262 20.2.2.17. |
| 80 | inline float DoubleToFloat32(double x); |
| 81 | |
| 82 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | // This function should match the exact semantics of ECMA-262 9.4. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 84 | inline double DoubleToInteger(double x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | // This function should match the exact semantics of ECMA-262 9.5. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 88 | inline int32_t DoubleToInt32(double x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | // This function should match the exact semantics of ECMA-262 9.6. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | inline uint32_t DoubleToUint32(double x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
| 94 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | // Enumeration for allowing octals and ignoring junk when converting |
| 96 | // strings to numbers. |
| 97 | enum ConversionFlags { |
| 98 | NO_FLAGS = 0, |
| 99 | ALLOW_HEX = 1, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | ALLOW_OCTAL = 2, |
| 101 | ALLOW_IMPLICIT_OCTAL = 4, |
| 102 | ALLOW_BINARY = 8, |
| 103 | ALLOW_TRAILING_JUNK = 16 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | // Converts a string into a double value according to ECMA-262 9.3.1 |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 108 | double StringToDouble(UnicodeCache* unicode_cache, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | Vector<const uint8_t> str, |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 110 | int flags, |
| 111 | double empty_string_val = 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 112 | double StringToDouble(UnicodeCache* unicode_cache, |
| 113 | Vector<const uc16> str, |
| 114 | int flags, |
| 115 | double empty_string_val = 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 116 | // This version expects a zero-terminated character array. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 117 | double StringToDouble(UnicodeCache* unicode_cache, |
| 118 | const char* str, |
| 119 | int flags, |
| 120 | double empty_string_val = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 122 | // Converts a string into an integer. |
| 123 | double StringToInt(UnicodeCache* unicode_cache, |
| 124 | Vector<const uint8_t> vector, |
| 125 | int radix); |
| 126 | |
| 127 | |
| 128 | double StringToInt(UnicodeCache* unicode_cache, |
| 129 | Vector<const uc16> vector, |
| 130 | int radix); |
| 131 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 132 | const int kDoubleToCStringMinBufferSize = 100; |
| 133 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | // 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. |
| 137 | const 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. |
| 141 | const 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. |
| 145 | char* DoubleToFixedCString(double value, int f); |
| 146 | char* DoubleToExponentialCString(double value, int f); |
| 147 | char* DoubleToPrecisionCString(double value, int f); |
| 148 | char* DoubleToRadixCString(double value, int radix); |
| 149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | |
| 151 | static inline bool IsMinusZero(double value) { |
| 152 | static const DoubleRepresentation minus_zero(-0.0); |
| 153 | return DoubleRepresentation(value) == minus_zero; |
| 154 | } |
| 155 | |
| 156 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | inline bool IsSmiDouble(double value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 163 | inline bool IsInt32Double(double value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | inline bool IsUint32Double(double value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | // Convert from Number object to C integer. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | inline int32_t NumberToInt32(Object* number); |
| 174 | inline uint32_t NumberToUint32(Object* number); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | |
| 176 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 177 | double StringToDouble(UnicodeCache* unicode_cache, Handle<String> string, |
| 178 | int flags, double empty_string_val = 0.0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | |
| 180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | inline bool TryNumberToSize(Isolate* isolate, Object* number, size_t* result); |
| 182 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 183 | |
| 184 | // Converts a number into size_t. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 185 | inline size_t NumberToSize(Isolate* isolate, Object* number); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 186 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 187 | |
| 188 | // returns DoubleToString(StringToDouble(string)) == string |
| 189 | bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string); |
| 190 | |
| 191 | } // namespace internal |
| 192 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | |
| 194 | #endif // V8_CONVERSIONS_H_ |