OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --allow-natives-syntax | 28 #include <stdlib.h> |
29 | 29 |
30 function RunOneTruncationTest(a, b) { | 30 #include <limits> |
31 var temp = a | 0; | 31 |
32 assertEquals(b, temp); | 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 } |
33 } | 71 } |
34 | 72 |
35 function RunAllTruncationTests() { | 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) { |
36 RunOneTruncationTest(0, 0); | 90 RunOneTruncationTest(0, 0); |
37 RunOneTruncationTest(0.5, 0); | 91 RunOneTruncationTest(0.5, 0); |
38 RunOneTruncationTest(-0.5, 0); | 92 RunOneTruncationTest(-0.5, 0); |
39 RunOneTruncationTest(1.5, 1); | 93 RunOneTruncationTest(1.5, 1); |
40 RunOneTruncationTest(-1.5, -1); | 94 RunOneTruncationTest(-1.5, -1); |
41 RunOneTruncationTest(5.5, 5); | 95 RunOneTruncationTest(5.5, 5); |
42 RunOneTruncationTest(-5.0, -5); | 96 RunOneTruncationTest(-5.0, -5); |
43 RunOneTruncationTest(NaN, 0); | 97 RunOneTruncationTest(NaN, 0); |
44 RunOneTruncationTest(Infinity, 0); | 98 RunOneTruncationTest(Infinity, 0); |
45 RunOneTruncationTest(-NaN, 0); | 99 RunOneTruncationTest(-NaN, 0); |
(...skipping 12 matching lines...) Expand all Loading... |
58 RunOneTruncationTest(2.4178527921507624e+24, -536870912); | 112 RunOneTruncationTest(2.4178527921507624e+24, -536870912); |
59 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); | 113 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); |
60 RunOneTruncationTest(2.417853945072267e+24, -536870912); | 114 RunOneTruncationTest(2.417853945072267e+24, -536870912); |
61 RunOneTruncationTest(-2.417853945072267e+24, 536870912); | 115 RunOneTruncationTest(-2.417853945072267e+24, 536870912); |
62 | 116 |
63 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); | 117 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); |
64 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); | 118 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); |
65 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); | 119 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); |
66 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); | 120 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); |
67 | 121 |
68 RunOneTruncationTest(9.6714111686030497e+24, -2147483648); | 122 RunOneTruncationTest(9.6714111686030497e+24, -2147483648.0); |
69 RunOneTruncationTest(-9.6714111686030497e+24, -2147483648); | 123 RunOneTruncationTest(-9.6714111686030497e+24, -2147483648.0); |
70 RunOneTruncationTest(9.6714157802890681e+24, -2147483648); | 124 RunOneTruncationTest(9.6714157802890681e+24, -2147483648.0); |
71 RunOneTruncationTest(-9.6714157802890681e+24, -2147483648); | 125 RunOneTruncationTest(-9.6714157802890681e+24, -2147483648.0); |
72 } | 126 } |
73 | 127 |
74 RunAllTruncationTests(); | 128 #undef NaN |
75 RunAllTruncationTests(); | 129 #undef Infinity |
76 %OptimizeFunctionOnNextCall(RunOneTruncationTest); | 130 #undef RunOneTruncationTest |
77 RunAllTruncationTests(); | |
78 RunAllTruncationTests(); | |
OLD | NEW |