ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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() |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 33 | #include <stdarg.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | // Extra POSIX/ANSI functions for Win32/MSVC. |
| 37 | |
| 38 | #include "conversions.h" |
| 39 | #include "platform.h" |
| 40 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 41 | namespace v8 { |
| 42 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 44 | // The fast double-to-unsigned-int conversion routine does not guarantee |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 45 | // rounding towards zero, or any reasonable value if the argument is larger |
| 46 | // than what fits in an unsigned 32-bit integer. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 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; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 63 | #ifdef BIG_ENDIAN_FLOATING_POINT |
| 64 | Address mantissa_ptr = reinterpret_cast<Address>(&x) + kIntSize; |
| 65 | #else |
| 66 | Address mantissa_ptr = reinterpret_cast<Address>(&x); |
| 67 | #endif |
| 68 | // Copy least significant 32 bits of mantissa. |
| 69 | memcpy(&result, mantissa_ptr, sizeof(result)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 70 | return negative ? ~result + 1 : result; |
| 71 | } |
| 72 | // Large number (outside uint32 range), Infinity or NaN. |
| 73 | return 0x80000000u; // Return integer indefinite. |
| 74 | } |
| 75 | |
| 76 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | static inline double DoubleToInteger(double x) { |
| 78 | if (isnan(x)) return 0; |
| 79 | if (!isfinite(x) || x == 0) return x; |
| 80 | return (x >= 0) ? floor(x) : ceil(x); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | int32_t NumberToInt32(Object* number) { |
| 85 | if (number->IsSmi()) return Smi::cast(number)->value(); |
| 86 | return DoubleToInt32(number->Number()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | uint32_t NumberToUint32(Object* number) { |
| 91 | if (number->IsSmi()) return Smi::cast(number)->value(); |
| 92 | return DoubleToUint32(number->Number()); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | int32_t DoubleToInt32(double x) { |
| 97 | int32_t i = FastD2I(x); |
| 98 | if (FastI2D(i) == x) return i; |
| 99 | static const double two32 = 4294967296.0; |
| 100 | static const double two31 = 2147483648.0; |
| 101 | if (!isfinite(x) || x == 0) return 0; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 102 | if (x < 0 || x >= two32) x = modulo(x, two32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | x = (x >= 0) ? floor(x) : ceil(x) + two32; |
| 104 | return (int32_t) ((x >= two31) ? x - two32 : x); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | } } // namespace v8::internal |
| 109 | |
| 110 | #endif // V8_CONVERSIONS_INL_H_ |