Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 31 | #include "scanner-base.h" |
| 32 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 36 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 37 | // The fast double-to-(unsigned-)int conversion routine does not guarantee |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | // rounding towards zero. |
| 39 | // The result is unspecified if x is infinite or NaN, or if the rounded |
| 40 | // integer value is outside the range of type int. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 41 | static inline int FastD2I(double x) { |
| 42 | // The static_cast convertion from double to int used to be slow, but |
| 43 | // as new benchmarks show, now it is much faster than lrint(). |
| 44 | return static_cast<int>(x); |
| 45 | } |
| 46 | |
| 47 | static inline unsigned int FastD2UI(double x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | static inline double FastI2D(int x) { |
| 51 | // There is no rounding involved in converting an integer to a |
| 52 | // double, so this code should compile to a few instructions without |
| 53 | // any FPU pipeline stalls. |
| 54 | return static_cast<double>(x); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static inline double FastUI2D(unsigned x) { |
| 59 | // There is no rounding involved in converting an unsigned integer to a |
| 60 | // double, so this code should compile to a few instructions without |
| 61 | // any FPU pipeline stalls. |
| 62 | return static_cast<double>(x); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // This function should match the exact semantics of ECMA-262 9.4. |
| 67 | static inline double DoubleToInteger(double x); |
| 68 | |
| 69 | |
| 70 | // This function should match the exact semantics of ECMA-262 9.5. |
| 71 | static inline int32_t DoubleToInt32(double x); |
| 72 | |
| 73 | |
| 74 | // This function should match the exact semantics of ECMA-262 9.6. |
| 75 | static inline uint32_t DoubleToUint32(double x) { |
| 76 | return static_cast<uint32_t>(DoubleToInt32(x)); |
| 77 | } |
| 78 | |
| 79 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | // Enumeration for allowing octals and ignoring junk when converting |
| 81 | // strings to numbers. |
| 82 | enum ConversionFlags { |
| 83 | NO_FLAGS = 0, |
| 84 | ALLOW_HEX = 1, |
| 85 | ALLOW_OCTALS = 2, |
| 86 | ALLOW_TRAILING_JUNK = 4 |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | // Convert from Number object to C integer. |
| 91 | static inline int32_t NumberToInt32(Object* number); |
| 92 | static inline uint32_t NumberToUint32(Object* number); |
| 93 | |
| 94 | |
| 95 | // 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] | 96 | double StringToDouble(UnicodeCache* unicode_cache, |
| 97 | String* str, |
| 98 | int flags, |
| 99 | double empty_string_val = 0); |
| 100 | double StringToDouble(UnicodeCache* unicode_cache, |
| 101 | Vector<const char> str, |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 102 | int flags, |
| 103 | double empty_string_val = 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 104 | double StringToDouble(UnicodeCache* unicode_cache, |
| 105 | Vector<const uc16> str, |
| 106 | int flags, |
| 107 | double empty_string_val = 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 108 | // This version expects a zero-terminated character array. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 109 | double StringToDouble(UnicodeCache* unicode_cache, |
| 110 | const char* str, |
| 111 | int flags, |
| 112 | double empty_string_val = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
| 114 | // Converts a string into an integer. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 115 | double StringToInt(UnicodeCache* unicode_cache, String* str, int radix); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | |
| 117 | // Converts a double to a string value according to ECMA-262 9.8.1. |
| 118 | // The buffer should be large enough for any floating point number. |
| 119 | // 100 characters is enough. |
| 120 | const char* DoubleToCString(double value, Vector<char> buffer); |
| 121 | |
| 122 | // Convert an int to a null-terminated string. The returned string is |
| 123 | // located inside the buffer, but not necessarily at the start. |
| 124 | const char* IntToCString(int n, Vector<char> buffer); |
| 125 | |
| 126 | // Additional number to string conversions for the number type. |
| 127 | // The caller is responsible for calling free on the returned pointer. |
| 128 | char* DoubleToFixedCString(double value, int f); |
| 129 | char* DoubleToExponentialCString(double value, int f); |
| 130 | char* DoubleToPrecisionCString(double value, int f); |
| 131 | char* DoubleToRadixCString(double value, int radix); |
| 132 | |
| 133 | } } // namespace v8::internal |
| 134 | |
| 135 | #endif // V8_CONVERSIONS_H_ |