00001 // Copyright 2006-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_CONVERSIONS_INL_H_ 00029 #define V8_CONVERSIONS_INL_H_ 00030 00031 #include <math.h> 00032 #include <float.h> // required for DBL_MAX and on Win32 for finite() 00033 00034 // ---------------------------------------------------------------------------- 00035 // Extra POSIX/ANSI functions for Win32/MSVC. 00036 00037 #include "conversions.h" 00038 #include "platform.h" 00039 00040 namespace v8 { namespace internal { 00041 00042 // The fast double-to-int conversion routine does not guarantee 00043 // rounding towards zero. 00044 static inline int FastD2I(double x) { 00045 #ifdef __USE_ISOC99 00046 // The ISO C99 standard defines the lrint() function which rounds a 00047 // double to an integer according to the current rounding direction. 00048 return lrint(x); 00049 #else 00050 // This is incredibly slow on Intel x86. The reason is that rounding 00051 // towards zero is implied by the C standard. This means that the 00052 // status register of the FPU has to be changed with the 'fldcw' 00053 // instruction. This completely stalls the pipeline and takes many 00054 // hundreds of clock cycles. 00055 return static_cast<int>(x); 00056 #endif 00057 } 00058 00059 00060 static inline double DoubleToInteger(double x) { 00061 if (isnan(x)) return 0; 00062 if (!isfinite(x) || x == 0) return x; 00063 return (x >= 0) ? floor(x) : ceil(x); 00064 } 00065 00066 00067 int32_t NumberToInt32(Object* number) { 00068 if (number->IsSmi()) return Smi::cast(number)->value(); 00069 return DoubleToInt32(number->Number()); 00070 } 00071 00072 00073 uint32_t NumberToUint32(Object* number) { 00074 if (number->IsSmi()) return Smi::cast(number)->value(); 00075 return DoubleToUint32(number->Number()); 00076 } 00077 00078 00079 int32_t DoubleToInt32(double x) { 00080 int32_t i = FastD2I(x); 00081 if (FastI2D(i) == x) return i; 00082 static const double two32 = 4294967296.0; 00083 static const double two31 = 2147483648.0; 00084 if (!isfinite(x) || x == 0) return 0; 00085 if (x < 0 || x >= two32) x = fmod(x, two32); 00086 x = (x >= 0) ? floor(x) : ceil(x) + two32; 00087 return (int32_t) ((x >= two31) ? x - two32 : x); 00088 } 00089 00090 00091 } } // namespace v8::internal 00092 00093 #endif // V8_CONVERSIONS_INL_H_