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 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 uint32_t unsigned_exponent = static_cast<uint32_t>(exponent); 53 int result = 0; 54 uint32_t max_exponent = 55 static_cast<uint32_t>(Double::kPhysicalSignificandSize); 56 if (unsigned_exponent >= max_exponent) { 57 if ((exponent - Double::kPhysicalSignificandSize) < 32) { 58 result = dbl.u[0] << (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(ConvertDToICallWrapper callWrapper, 75 ConvertDToIFunc func, 76 double from, 77 double raw) { 78 uint64_t to = static_cast<int64_t>(raw); 79 int result = (*callWrapper)(func, from); 80 CHECK_EQ(static_cast<int>(to), result); 81 } 82 83 84 int32_t DefaultCallWrapper(ConvertDToIFunc func, 85 double from) { 86 return (*func)(from); 87 } 88 89 90 // #define NaN and Infinity so that it's possible to cut-and-paste these tests 91 // directly to a .js file and run them. 92 #define NaN (OS::nan_value()) 93 #define Infinity (std::numeric_limits<double>::infinity()) 94 #define RunOneTruncationTest(p1, p2) \ 95 RunOneTruncationTestWithTest(callWrapper, func, p1, p2) 96 97 98 void RunAllTruncationTests(ConvertDToIFunc func) { 99 RunAllTruncationTests(DefaultCallWrapper, func); 100 } 101 102 103 void RunAllTruncationTests(ConvertDToICallWrapper callWrapper, 104 ConvertDToIFunc func) { 105 RunOneTruncationTest(0, 0); 106 RunOneTruncationTest(0.5, 0); 107 RunOneTruncationTest(-0.5, 0); 108 RunOneTruncationTest(1.5, 1); 109 RunOneTruncationTest(-1.5, -1); 110 RunOneTruncationTest(5.5, 5); 111 RunOneTruncationTest(-5.0, -5); 112 RunOneTruncationTest(NaN, 0); 113 RunOneTruncationTest(Infinity, 0); 114 RunOneTruncationTest(-NaN, 0); 115 RunOneTruncationTest(-Infinity, 0); 116 117 RunOneTruncationTest(4.5036e+15, 0x1635E000); 118 RunOneTruncationTest(-4.5036e+15, -372629504); 119 120 RunOneTruncationTest(4503603922337791.0, -1); 121 RunOneTruncationTest(-4503603922337791.0, 1); 122 RunOneTruncationTest(4503601774854143.0, 2147483647); 123 RunOneTruncationTest(-4503601774854143.0, -2147483647); 124 RunOneTruncationTest(9007207844675582.0, -2); 125 RunOneTruncationTest(-9007207844675582.0, 2); 126 127 RunOneTruncationTest(2.4178527921507624e+24, -536870912); 128 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); 129 RunOneTruncationTest(2.417853945072267e+24, -536870912); 130 RunOneTruncationTest(-2.417853945072267e+24, 536870912); 131 132 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); 133 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); 134 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); 135 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); 136 137 RunOneTruncationTest(9.6714111686030497e+24, -2147483648.0); 138 RunOneTruncationTest(-9.6714111686030497e+24, -2147483648.0); 139 RunOneTruncationTest(9.6714157802890681e+24, -2147483648.0); 140 RunOneTruncationTest(-9.6714157802890681e+24, -2147483648.0); 141 } 142 143 #undef NaN 144 #undef Infinity 145 #undef RunOneTruncationTest 146