1 // Copyright 2012 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
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.
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.
28 // Flags: --allow-natives-syntax
30 // Test inlining of Math.floor when assigned to a global.
34 function testFloor(expect, input) {
35 var test = new Function('n',
36 '"' + (test_id++) + '";return flo(n)');
37 assertEquals(expect, test(input));
38 assertEquals(expect, test(input));
39 assertEquals(expect, test(input));
40 %OptimizeFunctionOnNextCall(test);
41 assertEquals(expect, test(input));
46 return (function() { return x - 0.5; })();
53 testFloor(Infinity, Infinity);
54 testFloor(-Infinity, -Infinity);
57 // Ensure that a negative zero coming from Math.floor is properly handled
58 // by other operations.
60 return 1 / Math.floor(x);
62 assertEquals(-Infinity, ifloor(-0));
63 assertEquals(-Infinity, ifloor(-0));
64 assertEquals(-Infinity, ifloor(-0));
65 %OptimizeFunctionOnNextCall(ifloor);
66 assertEquals(-Infinity, ifloor(-0));
69 testFloor(0, 0.49999999999999994);
73 testFloor(-1, -0.49999999999999994);
85 testFloor(0, Number.MIN_VALUE);
86 testFloor(-1, -Number.MIN_VALUE);
87 testFloor(Number.MAX_VALUE, Number.MAX_VALUE);
88 testFloor(-Number.MAX_VALUE, -Number.MAX_VALUE);
89 testFloor(Infinity, Infinity);
90 testFloor(-Infinity, -Infinity);
92 // 2^30 is a smi boundary.
95 testFloor(two_30, two_30);
96 testFloor(two_30, two_30 + 0.1);
97 testFloor(two_30, two_30 + 0.5);
98 testFloor(two_30, two_30 + 0.7);
100 testFloor(two_30 - 1, two_30 - 1);
101 testFloor(two_30 - 1, two_30 - 1 + 0.1);
102 testFloor(two_30 - 1, two_30 - 1 + 0.5);
103 testFloor(two_30 - 1, two_30 - 1 + 0.7);
105 testFloor(-two_30, -two_30);
106 testFloor(-two_30, -two_30 + 0.1);
107 testFloor(-two_30, -two_30 + 0.5);
108 testFloor(-two_30, -two_30 + 0.7);
110 testFloor(-two_30 + 1, -two_30 + 1);
111 testFloor(-two_30 + 1, -two_30 + 1 + 0.1);
112 testFloor(-two_30 + 1, -two_30 + 1 + 0.5);
113 testFloor(-two_30 + 1, -two_30 + 1 + 0.7);
115 // 2^52 is a precision boundary.
116 var two_52 = (1 << 30) * (1 << 22);
118 testFloor(two_52, two_52);
119 testFloor(two_52, two_52 + 0.1);
120 assertEquals(two_52, two_52 + 0.5);
121 testFloor(two_52, two_52 + 0.5);
122 assertEquals(two_52 + 1, two_52 + 0.7);
123 testFloor(two_52 + 1, two_52 + 0.7);
125 testFloor(two_52 - 1, two_52 - 1);
126 testFloor(two_52 - 1, two_52 - 1 + 0.1);
127 testFloor(two_52 - 1, two_52 - 1 + 0.5);
128 testFloor(two_52 - 1, two_52 - 1 + 0.7);
130 testFloor(-two_52, -two_52);
131 testFloor(-two_52, -two_52 + 0.1);
132 testFloor(-two_52, -two_52 + 0.5);
133 testFloor(-two_52, -two_52 + 0.7);
135 testFloor(-two_52 + 1, -two_52 + 1);
136 testFloor(-two_52 + 1, -two_52 + 1 + 0.1);
137 testFloor(-two_52 + 1, -two_52 + 1 + 0.5);
138 testFloor(-two_52 + 1, -two_52 + 1 + 0.7);
142 // Test in a loop to cover the custom IC and GC-related issues.
143 for (var i = 0; i < 10; i++) {
145 new Array(i * 10000);
149 // Regression test for a bug where a negative zero coming from Math.floor
150 // was not properly handled by other operations.
151 function floorsum(i, n) {
152 var ret = Math.floor(n);
154 ret += Math.floor(n);
158 assertEquals(-0, floorsum(1, -0));
159 %OptimizeFunctionOnNextCall(floorsum);
160 // The optimized function will deopt. Run it with enough iterations to try
161 // to optimize via OSR (triggering the bug).
162 assertEquals(-0, floorsum(50000, -0));