1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
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_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 #include <stdarg.h>
34
35 // ----------------------------------------------------------------------------
36 // Extra POSIX/ANSI functions for Win32/MSVC.
37
38 #include "conversions.h"
39 #include "platform.h"
40
41 namespace v8 {
42 namespace internal {
43
44 // The fast double-to-unsigned-int conversion routine does not guarantee
45 // rounding towards zero, or any reasonable value if the argument is larger
46 // than what fits in an unsigned 32-bit integer.
FastD2UI(double x)47 static inline unsigned int FastD2UI(double x) {
48 // There is no unsigned version of lrint, so there is no fast path
49 // in this function as there is in FastD2I. Using lrint doesn't work
50 // for values of 2^31 and above.
51
52 // Convert "small enough" doubles to uint32_t by fixing the 32
53 // least significant non-fractional bits in the low 32 bits of the
54 // double, and reading them from there.
55 const double k2Pow52 = 4503599627370496.0;
56 bool negative = x < 0;
57 if (negative) {
58 x = -x;
59 }
60 if (x < k2Pow52) {
61 x += k2Pow52;
62 uint32_t result;
63 Address mantissa_ptr = reinterpret_cast<Address>(&x);
64 // Copy least significant 32 bits of mantissa.
65 memcpy(&result, mantissa_ptr, sizeof(result));
66 return negative ? ~result + 1 : result;
67 }
68 // Large number (outside uint32 range), Infinity or NaN.
69 return 0x80000000u; // Return integer indefinite.
70 }
71
72
DoubleToInteger(double x)73 static inline double DoubleToInteger(double x) {
74 if (isnan(x)) return 0;
75 if (!isfinite(x) || x == 0) return x;
76 return (x >= 0) ? floor(x) : ceil(x);
77 }
78
79
NumberToInt32(Object * number)80 int32_t NumberToInt32(Object* number) {
81 if (number->IsSmi()) return Smi::cast(number)->value();
82 return DoubleToInt32(number->Number());
83 }
84
85
NumberToUint32(Object * number)86 uint32_t NumberToUint32(Object* number) {
87 if (number->IsSmi()) return Smi::cast(number)->value();
88 return DoubleToUint32(number->Number());
89 }
90
91
DoubleToInt32(double x)92 int32_t DoubleToInt32(double x) {
93 int32_t i = FastD2I(x);
94 if (FastI2D(i) == x) return i;
95 static const double two32 = 4294967296.0;
96 static const double two31 = 2147483648.0;
97 if (!isfinite(x) || x == 0) return 0;
98 if (x < 0 || x >= two32) x = modulo(x, two32);
99 x = (x >= 0) ? floor(x) : ceil(x) + two32;
100 return (int32_t) ((x >= two31) ? x - two32 : x);
101 }
102
103
104 } } // namespace v8::internal
105
106 #endif // V8_CONVERSIONS_INL_H_
107