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)); | |
Vitaly Repeshko
2011/04/13 21:11:50
Don't we need (at least) two calls to test to ensu
| |
35 %OptimizeFunctionOnNextCall(test); | |
36 assertEquals(expect, test(input)); | |
William Hesse
2011/04/14 08:13:03
What about the fact that -0 and +0 compare equal?
| |
37 } | |
29 | 38 |
30 function zero() { | 39 function zero() { |
31 var x = 0.5; | 40 var x = 0.5; |
32 return (function() { return x - 0.5; })(); | 41 return (function() { return x - 0.5; })(); |
33 } | 42 } |
34 | 43 |
35 function test() { | 44 function test() { |
36 assertEquals(0, Math.floor(0)); | 45 testFloor(0, 0); |
37 assertEquals(0, Math.floor(zero())); | 46 testFloor(0, zero()); |
38 assertEquals(1/-0, 1/Math.floor(-0)); // 0 == -0, so we use reciprocals. | 47 testFloor(-0, -0); |
39 assertEquals(Infinity, Math.floor(Infinity)); | 48 testFloor(Infinity, Infinity); |
40 assertEquals(-Infinity, Math.floor(-Infinity)); | 49 testFloor(-Infinity, -Infinity); |
41 assertNaN(Math.floor(NaN)); | 50 testFloor(NaN, NaN); |
42 | 51 |
43 assertEquals(0, Math.floor(0.1)); | 52 testFloor(0, 0.1); |
44 assertEquals(0, Math.floor(0.5)); | 53 testFloor(0, 0.49999999999999994); |
45 assertEquals(0, Math.floor(0.7)); | 54 testFloor(0, 0.5); |
46 assertEquals(-1, Math.floor(-0.1)); | 55 testFloor(0, 0.7); |
47 assertEquals(-1, Math.floor(-0.5)); | 56 testFloor(-1, -0.1); |
48 assertEquals(-1, Math.floor(-0.7)); | 57 testFloor(-1, -0.49999999999999994); |
49 assertEquals(1, Math.floor(1)); | 58 testFloor(-1, -0.5); |
50 assertEquals(1, Math.floor(1.1)); | 59 testFloor(-1, -0.7); |
51 assertEquals(1, Math.floor(1.5)); | 60 testFloor(1, 1); |
52 assertEquals(1, Math.floor(1.7)); | 61 testFloor(1, 1.1); |
53 assertEquals(-1, Math.floor(-1)); | 62 testFloor(1, 1.5); |
54 assertEquals(-2, Math.floor(-1.1)); | 63 testFloor(1, 1.7); |
55 assertEquals(-2, Math.floor(-1.5)); | 64 testFloor(-1, -1); |
56 assertEquals(-2, Math.floor(-1.7)); | 65 testFloor(-2, -1.1); |
66 testFloor(-2, -1.5); | |
67 testFloor(-2, -1.7); | |
57 | 68 |
58 assertEquals(0, Math.floor(Number.MIN_VALUE)); | 69 testFloor(0, Number.MIN_VALUE); |
59 assertEquals(-1, Math.floor(-Number.MIN_VALUE)); | 70 testFloor(-1, -Number.MIN_VALUE); |
60 assertEquals(Number.MAX_VALUE, Math.floor(Number.MAX_VALUE)); | 71 testFloor(Number.MAX_VALUE, Number.MAX_VALUE); |
61 assertEquals(-Number.MAX_VALUE, Math.floor(-Number.MAX_VALUE)); | 72 testFloor(-Number.MAX_VALUE, -Number.MAX_VALUE); |
62 assertEquals(Infinity, Math.floor(Infinity)); | 73 testFloor(Infinity, Infinity); |
63 assertEquals(-Infinity, Math.floor(-Infinity)); | 74 testFloor(-Infinity, -Infinity); |
64 | 75 |
65 // 2^30 is a smi boundary. | 76 // 2^30 is a smi boundary. |
66 var two_30 = 1 << 30; | 77 var two_30 = 1 << 30; |
67 | 78 |
68 assertEquals(two_30, Math.floor(two_30)); | 79 testFloor(two_30, two_30); |
69 assertEquals(two_30, Math.floor(two_30 + 0.1)); | 80 testFloor(two_30, two_30 + 0.1); |
70 assertEquals(two_30, Math.floor(two_30 + 0.5)); | 81 testFloor(two_30, two_30 + 0.5); |
71 assertEquals(two_30, Math.floor(two_30 + 0.7)); | 82 testFloor(two_30, two_30 + 0.7); |
72 | 83 |
73 assertEquals(two_30 - 1, Math.floor(two_30 - 1)); | 84 testFloor(two_30 - 1, two_30 - 1); |
74 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.1)); | 85 testFloor(two_30 - 1, two_30 - 1 + 0.1); |
75 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.5)); | 86 testFloor(two_30 - 1, two_30 - 1 + 0.5); |
76 assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.7)); | 87 testFloor(two_30 - 1, two_30 - 1 + 0.7); |
77 | 88 |
78 assertEquals(-two_30, Math.floor(-two_30)); | 89 testFloor(-two_30, -two_30); |
79 assertEquals(-two_30, Math.floor(-two_30 + 0.1)); | 90 testFloor(-two_30, -two_30 + 0.1); |
80 assertEquals(-two_30, Math.floor(-two_30 + 0.5)); | 91 testFloor(-two_30, -two_30 + 0.5); |
81 assertEquals(-two_30, Math.floor(-two_30 + 0.7)); | 92 testFloor(-two_30, -two_30 + 0.7); |
82 | 93 |
83 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1)); | 94 testFloor(-two_30 + 1, -two_30 + 1); |
84 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.1)); | 95 testFloor(-two_30 + 1, -two_30 + 1 + 0.1); |
85 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.5)); | 96 testFloor(-two_30 + 1, -two_30 + 1 + 0.5); |
86 assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.7)); | 97 testFloor(-two_30 + 1, -two_30 + 1 + 0.7); |
87 | 98 |
88 // 2^52 is a precision boundary. | 99 // 2^52 is a precision boundary. |
89 var two_52 = (1 << 30) * (1 << 22); | 100 var two_52 = (1 << 30) * (1 << 22); |
90 | 101 |
91 assertEquals(two_52, Math.floor(two_52)); | 102 testFloor(two_52, two_52); |
92 assertEquals(two_52, Math.floor(two_52 + 0.1)); | 103 testFloor(two_52, two_52 + 0.1); |
93 assertEquals(two_52, two_52 + 0.5); | 104 assertEquals(two_52, two_52 + 0.5); |
94 assertEquals(two_52, Math.floor(two_52 + 0.5)); | 105 testFloor(two_52, two_52 + 0.5); |
95 assertEquals(two_52 + 1, two_52 + 0.7); | 106 assertEquals(two_52 + 1, two_52 + 0.7); |
96 assertEquals(two_52 + 1, Math.floor(two_52 + 0.7)); | 107 testFloor(two_52 + 1, two_52 + 0.7); |
97 | 108 |
98 assertEquals(two_52 - 1, Math.floor(two_52 - 1)); | 109 testFloor(two_52 - 1, two_52 - 1); |
99 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.1)); | 110 testFloor(two_52 - 1, two_52 - 1 + 0.1); |
100 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.5)); | 111 testFloor(two_52 - 1, two_52 - 1 + 0.5); |
101 assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.7)); | 112 testFloor(two_52 - 1, two_52 - 1 + 0.7); |
102 | 113 |
103 assertEquals(-two_52, Math.floor(-two_52)); | 114 testFloor(-two_52, -two_52); |
104 assertEquals(-two_52, Math.floor(-two_52 + 0.1)); | 115 testFloor(-two_52, -two_52 + 0.1); |
105 assertEquals(-two_52, Math.floor(-two_52 + 0.5)); | 116 testFloor(-two_52, -two_52 + 0.5); |
106 assertEquals(-two_52, Math.floor(-two_52 + 0.7)); | 117 testFloor(-two_52, -two_52 + 0.7); |
107 | 118 |
108 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1)); | 119 testFloor(-two_52 + 1, -two_52 + 1); |
109 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.1)); | 120 testFloor(-two_52 + 1, -two_52 + 1 + 0.1); |
110 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.5)); | 121 testFloor(-two_52 + 1, -two_52 + 1 + 0.5); |
111 assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.7)); | 122 testFloor(-two_52 + 1, -two_52 + 1 + 0.7); |
112 } | 123 } |
113 | 124 |
114 | 125 |
115 // Test in a loop to cover the custom IC and GC-related issues. | 126 // Test in a loop to cover the custom IC and GC-related issues. |
116 for (var i = 0; i < 500; i++) { | 127 for (var i = 0; i < 500; i++) { |
117 test(); | 128 test(); |
118 } | 129 } |
OLD | NEW |