OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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: --max-new-space-size=256 | 28 // Flags: --max-new-space-size=256 --allow-natives-syntax |
| 29 |
| 30 function testFloor(expect, input) { |
| 31 function test(n) { |
| 32 return Math.floor(n); |
| 33 } |
| 34 assertEquals(expect, test(input)); |
| 35 assertEquals(expect, test(input)); |
| 36 assertEquals(expect, test(input)); |
| 37 %OptimizeFunctionOnNextCall(test); |
| 38 assertEquals(expect, test(input)); |
| 39 } |
29 | 40 |
30 function zero() { | 41 function zero() { |
31 var x = 0.5; | 42 var x = 0.5; |
32 return (function() { return x - 0.5; })(); | 43 return (function() { return x - 0.5; })(); |
33 } | 44 } |
34 | 45 |
35 function test() { | 46 function test() { |
36 assertEquals(0, Math.floor(0)); | 47 testFloor(0, 0); |
37 assertEquals(0, Math.floor(zero())); | 48 testFloor(0, zero()); |
38 assertEquals(1/-0, 1/Math.floor(-0)); // 0 == -0, so we use reciprocals. | 49 testFloor(-0, -0); |
39 assertEquals(Infinity, Math.floor(Infinity)); | 50 testFloor(Infinity, Infinity); |
40 assertEquals(-Infinity, Math.floor(-Infinity)); | 51 testFloor(-Infinity, -Infinity); |
41 assertNaN(Math.floor(NaN)); | 52 testFloor(NaN, NaN); |
42 | 53 |
43 assertEquals(0, Math.floor(0.1)); | 54 testFloor(0, 0.1); |
44 assertEquals(0, Math.floor(0.5)); | 55 testFloor(0, 0.49999999999999994); |
45 assertEquals(0, Math.floor(0.7)); | 56 testFloor(0, 0.5); |
46 assertEquals(-1, Math.floor(-0.1)); | 57 testFloor(0, 0.7); |
47 assertEquals(-1, Math.floor(-0.5)); | 58 testFloor(-1, -0.1); |
48 assertEquals(-1, Math.floor(-0.7)); | 59 testFloor(-1, -0.49999999999999994); |
49 assertEquals(1, Math.floor(1)); | 60 testFloor(-1, -0.5); |
50 assertEquals(1, Math.floor(1.1)); | 61 testFloor(-1, -0.7); |
51 assertEquals(1, Math.floor(1.5)); | 62 testFloor(1, 1); |
52 assertEquals(1, Math.floor(1.7)); | 63 testFloor(1, 1.1); |
53 assertEquals(-1, Math.floor(-1)); | 64 testFloor(1, 1.5); |
54 assertEquals(-2, Math.floor(-1.1)); | 65 testFloor(1, 1.7); |
55 assertEquals(-2, Math.floor(-1.5)); | 66 testFloor(-1, -1); |
56 assertEquals(-2, Math.floor(-1.7)); | 67 testFloor(-2, -1.1); |
| 68 testFloor(-2, -1.5); |
| 69 testFloor(-2, -1.7); |
57 | 70 |
58 assertEquals(0, Math.floor(Number.MIN_VALUE)); | 71 testFloor(0, Number.MIN_VALUE); |
59 assertEquals(-1, Math.floor(-Number.MIN_VALUE)); | 72 testFloor(-1, -Number.MIN_VALUE); |
60 assertEquals(Number.MAX_VALUE, Math.floor(Number.MAX_VALUE)); | 73 testFloor(Number.MAX_VALUE, Number.MAX_VALUE); |
61 assertEquals(-Number.MAX_VALUE, Math.floor(-Number.MAX_VALUE)); | 74 testFloor(-Number.MAX_VALUE, -Number.MAX_VALUE); |
62 assertEquals(Infinity, Math.floor(Infinity)); | 75 testFloor(Infinity, Infinity); |
63 assertEquals(-Infinity, Math.floor(-Infinity)); | 76 testFloor(-Infinity, -Infinity); |
64 | 77 |
65 // 2^30 is a smi boundary. | 78 // 2^30 is a smi boundary. |
66 var two_30 = 1 << 30; | 79 var two_30 = 1 << 30; |
67 | 80 |
68 assertEquals(two_30, Math.floor(two_30)); | 81 testFloor(two_30, two_30); |
69 assertEquals(two_30, Math.floor(two_30 + 0.1)); | 82 testFloor(two_30, two_30 + 0.1); |
70 assertEquals(two_30, Math.floor(two_30 + 0.5)); | 83 testFloor(two_30, two_30 + 0.5); |
71 assertEquals(two_30, Math.floor(two_30 + 0.7)); | 84 testFloor(two_30, two_30 + 0.7); |
72 | 85 |
73 assertEquals(two_30 - 1, Math.floor(two_30 - 1)); | 86 testFloor(two_30 - 1, two_30 - 1); |
74 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.1)); | 87 testFloor(two_30 - 1, two_30 - 1 + 0.1); |
75 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.5)); | 88 testFloor(two_30 - 1, two_30 - 1 + 0.5); |
76 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.7)); | 89 testFloor(two_30 - 1, two_30 - 1 + 0.7); |
77 | 90 |
78 assertEquals(-two_30, Math.floor(-two_30)); | 91 testFloor(-two_30, -two_30); |
79 assertEquals(-two_30, Math.floor(-two_30 + 0.1)); | 92 testFloor(-two_30, -two_30 + 0.1); |
80 assertEquals(-two_30, Math.floor(-two_30 + 0.5)); | 93 testFloor(-two_30, -two_30 + 0.5); |
81 assertEquals(-two_30, Math.floor(-two_30 + 0.7)); | 94 testFloor(-two_30, -two_30 + 0.7); |
82 | 95 |
83 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1)); | 96 testFloor(-two_30 + 1, -two_30 + 1); |
84 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.1)); | 97 testFloor(-two_30 + 1, -two_30 + 1 + 0.1); |
85 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.5)); | 98 testFloor(-two_30 + 1, -two_30 + 1 + 0.5); |
86 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.7)); | 99 testFloor(-two_30 + 1, -two_30 + 1 + 0.7); |
87 | 100 |
88 // 2^52 is a precision boundary. | 101 // 2^52 is a precision boundary. |
89 var two_52 = (1 << 30) * (1 << 22); | 102 var two_52 = (1 << 30) * (1 << 22); |
90 | 103 |
91 assertEquals(two_52, Math.floor(two_52)); | 104 testFloor(two_52, two_52); |
92 assertEquals(two_52, Math.floor(two_52 + 0.1)); | 105 testFloor(two_52, two_52 + 0.1); |
93 assertEquals(two_52, two_52 + 0.5); | 106 assertEquals(two_52, two_52 + 0.5); |
94 assertEquals(two_52, Math.floor(two_52 + 0.5)); | 107 testFloor(two_52, two_52 + 0.5); |
95 assertEquals(two_52 + 1, two_52 + 0.7); | 108 assertEquals(two_52 + 1, two_52 + 0.7); |
96 assertEquals(two_52 + 1, Math.floor(two_52 + 0.7)); | 109 testFloor(two_52 + 1, two_52 + 0.7); |
97 | 110 |
98 assertEquals(two_52 - 1, Math.floor(two_52 - 1)); | 111 testFloor(two_52 - 1, two_52 - 1); |
99 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.1)); | 112 testFloor(two_52 - 1, two_52 - 1 + 0.1); |
100 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.5)); | 113 testFloor(two_52 - 1, two_52 - 1 + 0.5); |
101 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.7)); | 114 testFloor(two_52 - 1, two_52 - 1 + 0.7); |
102 | 115 |
103 assertEquals(-two_52, Math.floor(-two_52)); | 116 testFloor(-two_52, -two_52); |
104 assertEquals(-two_52, Math.floor(-two_52 + 0.1)); | 117 testFloor(-two_52, -two_52 + 0.1); |
105 assertEquals(-two_52, Math.floor(-two_52 + 0.5)); | 118 testFloor(-two_52, -two_52 + 0.5); |
106 assertEquals(-two_52, Math.floor(-two_52 + 0.7)); | 119 testFloor(-two_52, -two_52 + 0.7); |
107 | 120 |
108 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1)); | 121 testFloor(-two_52 + 1, -two_52 + 1); |
109 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.1)); | 122 testFloor(-two_52 + 1, -two_52 + 1 + 0.1); |
110 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.5)); | 123 testFloor(-two_52 + 1, -two_52 + 1 + 0.5); |
111 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.7)); | 124 testFloor(-two_52 + 1, -two_52 + 1 + 0.7); |
112 } | 125 } |
113 | 126 |
114 | 127 |
115 // Test in a loop to cover the custom IC and GC-related issues. | 128 // Test in a loop to cover the custom IC and GC-related issues. |
116 for (var i = 0; i < 500; i++) { | 129 for (var i = 0; i < 500; i++) { |
117 test(); | 130 test(); |
118 } | 131 } |
OLD | NEW |