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 "src/v8.h" 33 34 #include "src/base/platform/platform.h" 35 #include "src/code-stubs.h" 36 #include "src/factory.h" 37 #include "src/macro-assembler.h" 38 #include "test/cctest/cctest.h" 39 #include "test/cctest/test-code-stubs.h" 40 41 using namespace v8::internal; 42 43 44 int STDCALL ConvertDToICVersion(double d) { 45 union { double d; uint32_t u[2]; } dbl; 46 dbl.d = d; 47 uint32_t exponent_bits = dbl.u[1]; 48 int32_t shifted_mask = static_cast<int32_t>(Double::kExponentMask >> 32); 49 int32_t exponent = (((exponent_bits & shifted_mask) >> 50 (Double::kPhysicalSignificandSize - 32)) - 51 HeapNumber::kExponentBias); 52 if (exponent < 0) { 53 return 0; 54 } 55 uint32_t unsigned_exponent = static_cast<uint32_t>(exponent); 56 int result = 0; 57 uint32_t max_exponent = 58 static_cast<uint32_t>(Double::kPhysicalSignificandSize); 59 if (unsigned_exponent >= max_exponent) { 60 if ((exponent - Double::kPhysicalSignificandSize) < 32) { 61 result = dbl.u[0] << (exponent - Double::kPhysicalSignificandSize); 62 } 63 } else { 64 uint64_t big_result = 65 (bit_cast<uint64_t>(d) & Double::kSignificandMask) | Double::kHiddenBit; 66 big_result = big_result >> (Double::kPhysicalSignificandSize - exponent); 67 result = static_cast<uint32_t>(big_result); 68 } 69 if (static_cast<int32_t>(exponent_bits) < 0) { 70 return (0 - result); 71 } else { 72 return result; 73 } 74 } 75 76 77 void RunOneTruncationTestWithTest(ConvertDToICallWrapper callWrapper, 78 ConvertDToIFunc func, 79 double from, 80 int32_t to) { 81 int32_t result = (*callWrapper)(func, from); 82 CHECK_EQ(to, result); 83 } 84 85 86 int32_t DefaultCallWrapper(ConvertDToIFunc func, 87 double from) { 88 return (*func)(from); 89 } 90 91 92 // #define NaN and Infinity so that it's possible to cut-and-paste these tests 93 // directly to a .js file and run them. 94 #define NaN (std::numeric_limits<double>::quiet_NaN()) 95 #define Infinity (std::numeric_limits<double>::infinity()) 96 #define RunOneTruncationTest(p1, p2) \ 97 RunOneTruncationTestWithTest(callWrapper, func, p1, p2) 98 99 100 void RunAllTruncationTests(ConvertDToIFunc func) { 101 RunAllTruncationTests(DefaultCallWrapper, func); 102 } 103 104 105 void RunAllTruncationTests(ConvertDToICallWrapper callWrapper, 106 ConvertDToIFunc func) { 107 RunOneTruncationTest(0, 0); 108 RunOneTruncationTest(0.5, 0); 109 RunOneTruncationTest(-0.5, 0); 110 RunOneTruncationTest(1.5, 1); 111 RunOneTruncationTest(-1.5, -1); 112 RunOneTruncationTest(5.5, 5); 113 RunOneTruncationTest(-5.0, -5); 114 RunOneTruncationTest(NaN, 0); 115 RunOneTruncationTest(Infinity, 0); 116 RunOneTruncationTest(-NaN, 0); 117 RunOneTruncationTest(-Infinity, 0); 118 RunOneTruncationTest(4.94065645841e-324, 0); 119 RunOneTruncationTest(-4.94065645841e-324, 0); 120 121 RunOneTruncationTest(0.9999999999999999, 0); 122 RunOneTruncationTest(-0.9999999999999999, 0); 123 RunOneTruncationTest(4294967296.0, 0); 124 RunOneTruncationTest(-4294967296.0, 0); 125 RunOneTruncationTest(9223372036854775000.0, -1024); 126 RunOneTruncationTest(-9223372036854775000.0, 1024); 127 RunOneTruncationTest(4.5036e+15, 372629504); 128 RunOneTruncationTest(-4.5036e+15, -372629504); 129 130 RunOneTruncationTest(287524199.5377777, 0x11234567); 131 RunOneTruncationTest(-287524199.5377777, -0x11234567); 132 RunOneTruncationTest(2300193596.302222, -1994773700); 133 RunOneTruncationTest(-2300193596.302222, 1994773700); 134 RunOneTruncationTest(4600387192.604444, 305419896); 135 RunOneTruncationTest(-4600387192.604444, -305419896); 136 RunOneTruncationTest(4823855600872397.0, 1737075661); 137 RunOneTruncationTest(-4823855600872397.0, -1737075661); 138 139 RunOneTruncationTest(4503603922337791.0, -1); 140 RunOneTruncationTest(-4503603922337791.0, 1); 141 RunOneTruncationTest(4503601774854143.0, 2147483647); 142 RunOneTruncationTest(-4503601774854143.0, -2147483647); 143 RunOneTruncationTest(9007207844675582.0, -2); 144 RunOneTruncationTest(-9007207844675582.0, 2); 145 146 RunOneTruncationTest(2.4178527921507624e+24, -536870912); 147 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); 148 RunOneTruncationTest(2.417853945072267e+24, -536870912); 149 RunOneTruncationTest(-2.417853945072267e+24, 536870912); 150 151 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); 152 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); 153 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); 154 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); 155 156 RunOneTruncationTest(2147483647.0, 2147483647); 157 RunOneTruncationTest(-2147483648.0, -2147483647-1); 158 RunOneTruncationTest(9.6714111686030497e+24, -2147483647-1); 159 RunOneTruncationTest(-9.6714111686030497e+24, -2147483647-1); 160 RunOneTruncationTest(9.6714157802890681e+24, -2147483647-1); 161 RunOneTruncationTest(-9.6714157802890681e+24, -2147483647-1); 162 RunOneTruncationTest(1.9342813113834065e+25, -2147483647-1); 163 RunOneTruncationTest(-1.9342813113834065e+25, -2147483647-1); 164 165 RunOneTruncationTest(3.868562622766813e+25, 0); 166 RunOneTruncationTest(-3.868562622766813e+25, 0); 167 RunOneTruncationTest(1.7976931348623157e+308, 0); 168 RunOneTruncationTest(-1.7976931348623157e+308, 0); 169 } 170 171 #undef NaN 172 #undef Infinity 173 #undef RunOneTruncationTest 174 175 176 TEST(CodeStubMajorKeys) { 177 CcTest::InitializeVM(); 178 LocalContext context; 179 Isolate* isolate = CcTest::i_isolate(); 180 181 #define CHECK_STUB(NAME) \ 182 { \ 183 HandleScope scope(isolate); \ 184 NAME##Stub stub_impl(0xabcd, isolate); \ 185 CodeStub* stub = &stub_impl; \ 186 CHECK_EQ(stub->MajorKey(), CodeStub::NAME); \ 187 } 188 CODE_STUB_LIST(CHECK_STUB); 189 } 190