conversions-inl.h revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// Copyright 2006-2008 the V8 project authors. All rights reserved. 2417387625d9c6292f542a0925122a00fbf92b1abTom Wai-Hong Tam// Redistribution and use in source and binary forms, with or without 3417387625d9c6292f542a0925122a00fbf92b1abTom Wai-Hong Tam// modification, are permitted provided that the following conditions are 4417387625d9c6292f542a0925122a00fbf92b1abTom Wai-Hong Tam// met: 5a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// 6a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// * Redistributions of source code must retain the above copyright 7a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// notice, this list of conditions and the following disclaimer. 8a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// * Redistributions in binary form must reproduce the above 9a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// copyright notice, this list of conditions and the following 10a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// disclaimer in the documentation and/or other materials provided 11a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// with the distribution. 12a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// * Neither the name of Google Inc. nor the names of its 13a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// contributors may be used to endorse or promote products derived 14a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// from this software without specific prior written permission. 15a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// 16a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17ed4d67be4d0faa1a267d33e86905df1153d78333Tom Wai-Hong Tam// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18ed4d67be4d0faa1a267d33e86905df1153d78333Tom Wai-Hong Tam// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19ed4d67be4d0faa1a267d33e86905df1153d78333Tom Wai-Hong Tam// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21fe06ed8afdd7323a1f3ee60c746b5c0335c659c0Scott// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25cea96baa3a9ca9755088f1585dba94b266125083Danny Chan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 260695ad28ef916bb21a81d18ca0c278bfd7705f71Mohammed Habibulla// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 286f59efa78ba6765b5ebf1d847865ae5a18bbe019Vic (Chun-Ju) Yang#ifndef V8_CONVERSIONS_INL_H_ 29a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#define V8_CONVERSIONS_INL_H_ 30a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 31a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#include <math.h> 3215c2dcbdcc125d5be186ff18bd3a593c9afc2af3Vic Yang#include <float.h> // required for DBL_MAX and on Win32 for finite() 33a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#include <stdarg.h> 34a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 35a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// ---------------------------------------------------------------------------- 36a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// Extra POSIX/ANSI functions for Win32/MSVC. 37a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 38a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#include "conversions.h" 39a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#include "platform.h" 40a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 41a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinallynamespace v8 { 42a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinallynamespace internal { 43a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 44a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// The fast double-to-int conversion routine does not guarantee 45a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally// rounding towards zero. 46a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinallystatic inline int FastD2I(double x) { 47417387625d9c6292f542a0925122a00fbf92b1abTom Wai-Hong Tam#ifdef __USE_ISOC99 489ece2c6fb01d4c29d23a7238def15bdf656c885cJulius Werner // The ISO C99 standard defines the lrint() function which rounds a 499ece2c6fb01d4c29d23a7238def15bdf656c885cJulius Werner // double to an integer according to the current rounding direction. 509ece2c6fb01d4c29d23a7238def15bdf656c885cJulius Werner return lrint(x); 519ece2c6fb01d4c29d23a7238def15bdf656c885cJulius Werner#else 52f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang // This is incredibly slow on Intel x86. The reason is that rounding 53f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang // towards zero is implied by the C standard. This means that the 54a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally // status register of the FPU has to be changed with the 'fldcw' 5557108980ee6107c9257d567a8d85388af33561a2Daisuke Nojiri // instruction. This completely stalls the pipeline and takes many 5657108980ee6107c9257d567a8d85388af33561a2Daisuke Nojiri // hundreds of clock cycles. 5757108980ee6107c9257d567a8d85388af33561a2Daisuke Nojiri return static_cast<int>(x); 58b745ebf71fe976442c0dd9191a63a47778b6952dDaisuke Nojiri#endif 5957108980ee6107c9257d567a8d85388af33561a2Daisuke Nojiri} 600dc84b8cf118e16ed0b3fb44db3fb14ed2252b17Vic Yang 610dc84b8cf118e16ed0b3fb44db3fb14ed2252b17Vic Yang 62a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinallystatic inline double DoubleToInteger(double x) { 633dea40e52bc58e7b19b890e2e17b3a4816227c1fTom Wai-Hong Tam if (isnan(x)) return 0; 643dea40e52bc58e7b19b890e2e17b3a4816227c1fTom Wai-Hong Tam if (!isfinite(x) || x == 0) return x; 653dea40e52bc58e7b19b890e2e17b3a4816227c1fTom Wai-Hong Tam return (x >= 0) ? floor(x) : ceil(x); 66f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang} 67f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang 68a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 69f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yangint32_t NumberToInt32(Object* number) { 70f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang if (number->IsSmi()) return Smi::cast(number)->value(); 71a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally return DoubleToInt32(number->Number()); 72f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang} 73f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang 74a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 750430288e1ea7c2976db8612c595223dc2a220563Tom Wai-Hong Tamuint32_t NumberToUint32(Object* number) { 760430288e1ea7c2976db8612c595223dc2a220563Tom Wai-Hong Tam if (number->IsSmi()) return Smi::cast(number)->value(); 77a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally return DoubleToUint32(number->Number()); 78f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang} 79f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang 80a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 818da40de326d282484cea03a17895550beb2c757dTom Wai-Hong Tamint32_t DoubleToInt32(double x) { 828da40de326d282484cea03a17895550beb2c757dTom Wai-Hong Tam int32_t i = FastD2I(x); 83a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally if (FastI2D(i) == x) return i; 84f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang static const double two32 = 4294967296.0; 854c9684a1f1a3891ffe3f9408f26b6ab85e57d2daTom Wai-Hong Tam static const double two31 = 2147483648.0; 86a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally if (!isfinite(x) || x == 0) return 0; 87f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang if (x < 0 || x >= two32) x = fmod(x, two32); 88f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang x = (x >= 0) ? floor(x) : ceil(x) + two32; 89a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally return (int32_t) ((x >= two31) ? x - two32 : x); 90f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang} 91f1fdf71cc3a0545bf69e3c2a1246e204b6ef48eaVic Yang 92a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally 9377201c1e10b00a12d008178c3dbd93013e338bcfVic Yang} } // namespace v8::internal 9477201c1e10b00a12d008178c3dbd93013e338bcfVic Yang 95a39f97038dfa865c7bfcea2625d516d098207066Yusuf Mohsinally#endif // V8_CONVERSIONS_INL_H_ 9625d703a846f3a717dd8a65997aee0ddfd6afe440David Hendricks