blob: e3db4e95b3509c361306f506426df02a070f82c4 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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_INL_H_
29#define V8_CONVERSIONS_INL_H_
30
31#include <math.h>
32#include <float.h> // required for DBL_MAX and on Win32 for finite()
33
34// ----------------------------------------------------------------------------
35// Extra POSIX/ANSI functions for Win32/MSVC.
36
37#include "conversions.h"
38#include "platform.h"
39
40namespace v8 { namespace internal {
41
42// The fast double-to-int conversion routine does not guarantee
43// rounding towards zero.
44static inline int FastD2I(double x) {
45#ifdef __USE_ISOC99
46 // The ISO C99 standard defines the lrint() function which rounds a
47 // double to an integer according to the current rounding direction.
48 return lrint(x);
49#else
50 // This is incredibly slow on Intel x86. The reason is that rounding
51 // towards zero is implied by the C standard. This means that the
52 // status register of the FPU has to be changed with the 'fldcw'
53 // instruction. This completely stalls the pipeline and takes many
54 // hundreds of clock cycles.
55 return static_cast<int>(x);
56#endif
57}
58
59
60static inline double DoubleToInteger(double x) {
61 if (isnan(x)) return 0;
62 if (!isfinite(x) || x == 0) return x;
63 return (x >= 0) ? floor(x) : ceil(x);
64}
65
66
67int32_t NumberToInt32(Object* number) {
68 if (number->IsSmi()) return Smi::cast(number)->value();
69 return DoubleToInt32(number->Number());
70}
71
72
73uint32_t NumberToUint32(Object* number) {
74 if (number->IsSmi()) return Smi::cast(number)->value();
75 return DoubleToUint32(number->Number());
76}
77
78
79int32_t DoubleToInt32(double x) {
80 int32_t i = FastD2I(x);
81 if (FastI2D(i) == x) return i;
82 static const double two32 = 4294967296.0;
83 static const double two31 = 2147483648.0;
84 if (!isfinite(x) || x == 0) return 0;
85 if (x < 0 || x >= two32) x = fmod(x, two32);
86 x = (x >= 0) ? floor(x) : ceil(x) + two32;
87 return (int32_t) ((x >= two31) ? x - two32 : x);
88}
89
90
91} } // namespace v8::internal
92
93#endif // V8_CONVERSIONS_INL_H_