1 // Copyright 2013 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 #include <stdlib.h> 29 30 #include <limits> 31 32 #include "v8.h" 33 34 #include "cctest.h" 35 #include "code-stubs.h" 36 #include "test-code-stubs.h" 37 #include "factory.h" 38 #include "macro-assembler.h" 39 #include "platform.h" 40 41 using namespace v8::internal; 42 43 44 int STDCALL ConvertDToICVersion(double d) { 45 Address double_ptr = reinterpret_cast<Address>(&d); 46 uint32_t exponent_bits = Memory::uint32_at(double_ptr + kDoubleSize / 2); 47 int32_t shifted_mask = static_cast<int32_t>(Double::kExponentMask >> 32); 48 int32_t exponent = (((exponent_bits & shifted_mask) >> 49 (Double::kPhysicalSignificandSize - 32)) - 50 HeapNumber::kExponentBias); 51 uint32_t unsigned_exponent = static_cast<uint32_t>(exponent); 52 int result = 0; 53 uint32_t max_exponent = 54 static_cast<uint32_t>(Double::kPhysicalSignificandSize); 55 if (unsigned_exponent >= max_exponent) { 56 if ((exponent - Double::kPhysicalSignificandSize) < 32) { 57 result = Memory::uint32_at(double_ptr) << 58 (exponent - Double::kPhysicalSignificandSize); 59 } 60 } else { 61 uint64_t big_result = 62 (BitCast<uint64_t>(d) & Double::kSignificandMask) | Double::kHiddenBit; 63 big_result = big_result >> (Double::kPhysicalSignificandSize - exponent); 64 result = static_cast<uint32_t>(big_result); 65 } 66 if (static_cast<int32_t>(exponent_bits) < 0) { 67 return (0 - result); 68 } else { 69 return result; 70 } 71 } 72 73 74 void RunOneTruncationTestWithTest(ConvertDToIFunc func, 75 double from, 76 double raw) { 77 uint64_t to = static_cast<int64_t>(raw); 78 int result = (*func)(from); 79 CHECK_EQ(static_cast<int>(to), result); 80 } 81 82 83 // #define NaN and Infinity so that it's possible to cut-and-paste these tests 84 // directly to a .js file and run them. 85 #define NaN (OS::nan_value()) 86 #define Infinity (std::numeric_limits<double>::infinity()) 87 #define RunOneTruncationTest(p1, p2) RunOneTruncationTestWithTest(func, p1, p2) 88 89 void RunAllTruncationTests(ConvertDToIFunc func) { 90 RunOneTruncationTest(0, 0); 91 RunOneTruncationTest(0.5, 0); 92 RunOneTruncationTest(-0.5, 0); 93 RunOneTruncationTest(1.5, 1); 94 RunOneTruncationTest(-1.5, -1); 95 RunOneTruncationTest(5.5, 5); 96 RunOneTruncationTest(-5.0, -5); 97 RunOneTruncationTest(NaN, 0); 98 RunOneTruncationTest(Infinity, 0); 99 RunOneTruncationTest(-NaN, 0); 100 RunOneTruncationTest(-Infinity, 0); 101 102 RunOneTruncationTest(4.5036e+15, 0x1635E000); 103 RunOneTruncationTest(-4.5036e+15, -372629504); 104 105 RunOneTruncationTest(4503603922337791.0, -1); 106 RunOneTruncationTest(-4503603922337791.0, 1); 107 RunOneTruncationTest(4503601774854143.0, 2147483647); 108 RunOneTruncationTest(-4503601774854143.0, -2147483647); 109 RunOneTruncationTest(9007207844675582.0, -2); 110 RunOneTruncationTest(-9007207844675582.0, 2); 111 112 RunOneTruncationTest(2.4178527921507624e+24, -536870912); 113 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); 114 RunOneTruncationTest(2.417853945072267e+24, -536870912); 115 RunOneTruncationTest(-2.417853945072267e+24, 536870912); 116 117 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); 118 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); 119 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); 120 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); 121 122 RunOneTruncationTest(9.6714111686030497e+24, -2147483648.0); 123 RunOneTruncationTest(-9.6714111686030497e+24, -2147483648.0); 124 RunOneTruncationTest(9.6714157802890681e+24, -2147483648.0); 125 RunOneTruncationTest(-9.6714157802890681e+24, -2147483648.0); 126 } 127 128 #undef NaN 129 #undef Infinity 130 #undef RunOneTruncationTest 131