ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 34 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 35 | // The fast double-to-(unsigned-)int conversion routine does not guarantee |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | // rounding towards zero. |
| 37 | // The result is unspecified if x is infinite or NaN, or if the rounded |
| 38 | // integer value is outside the range of type int. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 39 | static inline int FastD2I(double x) { |
| 40 | // The static_cast convertion from double to int used to be slow, but |
| 41 | // as new benchmarks show, now it is much faster than lrint(). |
| 42 | return static_cast<int>(x); |
| 43 | } |
| 44 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 45 | static inline unsigned int FastD2UI(double x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | static inline double FastI2D(int x) { |
| 49 | // There is no rounding involved in converting an integer to a |
| 50 | // double, so this code should compile to a few instructions without |
| 51 | // any FPU pipeline stalls. |
| 52 | return static_cast<double>(x); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static inline double FastUI2D(unsigned x) { |
| 57 | // There is no rounding involved in converting an unsigned integer to a |
| 58 | // double, so this code should compile to a few instructions without |
| 59 | // any FPU pipeline stalls. |
| 60 | return static_cast<double>(x); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // This function should match the exact semantics of ECMA-262 9.4. |
| 65 | static inline double DoubleToInteger(double x); |
| 66 | |
| 67 | |
| 68 | // This function should match the exact semantics of ECMA-262 9.5. |
| 69 | static inline int32_t DoubleToInt32(double x); |
| 70 | |
| 71 | |
| 72 | // This function should match the exact semantics of ECMA-262 9.6. |
| 73 | static inline uint32_t DoubleToUint32(double x) { |
| 74 | return static_cast<uint32_t>(DoubleToInt32(x)); |
| 75 | } |
| 76 | |
| 77 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | // Enumeration for allowing octals and ignoring junk when converting |
| 79 | // strings to numbers. |
| 80 | enum ConversionFlags { |
| 81 | NO_FLAGS = 0, |
| 82 | ALLOW_HEX = 1, |
| 83 | ALLOW_OCTALS = 2, |
| 84 | ALLOW_TRAILING_JUNK = 4 |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | // Convert from Number object to C integer. |
| 89 | static inline int32_t NumberToInt32(Object* number); |
| 90 | static inline uint32_t NumberToUint32(Object* number); |
| 91 | |
| 92 | |
| 93 | // Converts a string into a double value according to ECMA-262 9.3.1 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | double StringToDouble(String* str, int flags, double empty_string_val = 0); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 95 | double StringToDouble(Vector<const char> str, |
| 96 | int flags, |
| 97 | double empty_string_val = 0); |
| 98 | // This version expects a zero-terminated character array. |
| 99 | double StringToDouble(const char* str, int flags, double empty_string_val = 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | |
| 101 | // Converts a string into an integer. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 102 | double StringToInt(String* str, int radix); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | |
| 104 | // Converts a double to a string value according to ECMA-262 9.8.1. |
| 105 | // The buffer should be large enough for any floating point number. |
| 106 | // 100 characters is enough. |
| 107 | const char* DoubleToCString(double value, Vector<char> buffer); |
| 108 | |
| 109 | // Convert an int to a null-terminated string. The returned string is |
| 110 | // located inside the buffer, but not necessarily at the start. |
| 111 | const char* IntToCString(int n, Vector<char> buffer); |
| 112 | |
| 113 | // Additional number to string conversions for the number type. |
| 114 | // The caller is responsible for calling free on the returned pointer. |
| 115 | char* DoubleToFixedCString(double value, int f); |
| 116 | char* DoubleToExponentialCString(double value, int f); |
| 117 | char* DoubleToPrecisionCString(double value, int f); |
| 118 | char* DoubleToRadixCString(double value, int radix); |
| 119 | |
| 120 | } } // namespace v8::internal |
| 121 | |
| 122 | #endif // V8_CONVERSIONS_H_ |