18b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch// Copyright 2011 the V8 project authors. All rights reserved. 2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without 3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are 4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met: 5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions of source code must retain the above copyright 7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// notice, this list of conditions and the following disclaimer. 8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions in binary form must reproduce the above 9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// copyright notice, this list of conditions and the following 10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// disclaimer in the documentation and/or other materials provided 11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// with the distribution. 12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Neither the name of Google Inc. nor the names of its 13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contributors may be used to endorse or promote products derived 14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// from this software without specific prior written permission. 15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifndef V8_CONVERSIONS_H_ 29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define V8_CONVERSIONS_H_ 30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 318b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch#include "scanner-base.h" 328b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch 33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 { 34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal { 35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 363ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve Block 376ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// The fast double-to-(unsigned-)int conversion routine does not guarantee 38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// rounding towards zero. 39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The result is unspecified if x is infinite or NaN, or if the rounded 40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// integer value is outside the range of type int. 416ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockstatic inline int FastD2I(double x) { 426ded16be15dd865a9b21ea304d5273c8be299c87Steve Block // The static_cast convertion from double to int used to be slow, but 436ded16be15dd865a9b21ea304d5273c8be299c87Steve Block // as new benchmarks show, now it is much faster than lrint(). 446ded16be15dd865a9b21ea304d5273c8be299c87Steve Block return static_cast<int>(x); 456ded16be15dd865a9b21ea304d5273c8be299c87Steve Block} 466ded16be15dd865a9b21ea304d5273c8be299c87Steve Block 476ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockstatic inline unsigned int FastD2UI(double x); 48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline double FastI2D(int x) { 51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // There is no rounding involved in converting an integer to a 52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // double, so this code should compile to a few instructions without 53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // any FPU pipeline stalls. 54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return static_cast<double>(x); 55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline double FastUI2D(unsigned x) { 59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // There is no rounding involved in converting an unsigned integer to a 60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // double, so this code should compile to a few instructions without 61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // any FPU pipeline stalls. 62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return static_cast<double>(x); 63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This function should match the exact semantics of ECMA-262 9.4. 67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline double DoubleToInteger(double x); 68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This function should match the exact semantics of ECMA-262 9.5. 71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline int32_t DoubleToInt32(double x); 72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This function should match the exact semantics of ECMA-262 9.6. 75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline uint32_t DoubleToUint32(double x) { 76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return static_cast<uint32_t>(DoubleToInt32(x)); 77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Enumeration for allowing octals and ignoring junk when converting 81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// strings to numbers. 82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockenum ConversionFlags { 83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block NO_FLAGS = 0, 84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ALLOW_HEX = 1, 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ALLOW_OCTALS = 2, 86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ALLOW_TRAILING_JUNK = 4 87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}; 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Convert from Number object to C integer. 91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline int32_t NumberToInt32(Object* number); 92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockstatic inline uint32_t NumberToUint32(Object* number); 93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Converts a string into a double value according to ECMA-262 9.3.1 968b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochdouble StringToDouble(UnicodeCache* unicode_cache, 978b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch String* str, 988b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch int flags, 998b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch double empty_string_val = 0); 1008b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochdouble StringToDouble(UnicodeCache* unicode_cache, 1018b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch Vector<const char> str, 10280d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen int flags, 10380d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen double empty_string_val = 0); 10480d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen// This version expects a zero-terminated character array. 1058b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochdouble StringToDouble(UnicodeCache* unicode_cache, 1068b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch const char* str, 1078b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch int flags, 1088b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch double empty_string_val = 0); 109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Converts a string into an integer. 1118b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochdouble StringToInt(UnicodeCache* unicode_cache, String* str, int radix); 112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Converts a double to a string value according to ECMA-262 9.8.1. 114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The buffer should be large enough for any floating point number. 115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 100 characters is enough. 116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockconst char* DoubleToCString(double value, Vector<char> buffer); 117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Convert an int to a null-terminated string. The returned string is 119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// located inside the buffer, but not necessarily at the start. 120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockconst char* IntToCString(int n, Vector<char> buffer); 121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Additional number to string conversions for the number type. 123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The caller is responsible for calling free on the returned pointer. 124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockchar* DoubleToFixedCString(double value, int f); 125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockchar* DoubleToExponentialCString(double value, int f); 126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockchar* DoubleToPrecisionCString(double value, int f); 127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockchar* DoubleToRadixCString(double value, int radix); 128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} } // namespace v8::internal 130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif // V8_CONVERSIONS_H_ 132