blob: 5ebe90b705fc2e69b2b512aedf5716b3cecc5f68 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// 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
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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028// Flags: --max-semi-space-size=1 --allow-natives-syntax
Ben Murdoch3ef787d2012-04-12 10:51:47 +010029
30// Test inlining of Math.floor when assigned to a local.
31var test_id = 0;
32
33function testFloor(expect, input) {
34 var test = new Function('n',
35 '"' + (test_id++) +
36 '";var f = Math.floor; return f(n)');
37 assertEquals(expect, test(input));
38 assertEquals(expect, test(input));
39 assertEquals(expect, test(input));
40 %OptimizeFunctionOnNextCall(test);
41 assertEquals(expect, test(input));
42}
43
44function zero() {
45 var x = 0.5;
46 return (function() { return x - 0.5; })();
47}
48
49function test() {
50 testFloor(0, 0);
51 testFloor(0, zero());
52 testFloor(-0, -0);
53 testFloor(Infinity, Infinity);
54 testFloor(-Infinity, -Infinity);
55 testFloor(NaN, NaN);
56
57 // Ensure that a negative zero coming from Math.floor is properly handled
58 // by other operations.
59 function ifloor(x) {
60 return 1 / Math.floor(x);
61 }
62 assertEquals(-Infinity, ifloor(-0));
63 assertEquals(-Infinity, ifloor(-0));
64 assertEquals(-Infinity, ifloor(-0));
65 %OptimizeFunctionOnNextCall(ifloor);
66 assertEquals(-Infinity, ifloor(-0));
67
68 testFloor(0, 0.1);
69 testFloor(0, 0.49999999999999994);
70 testFloor(0, 0.5);
71 testFloor(0, 0.7);
72 testFloor(-1, -0.1);
73 testFloor(-1, -0.49999999999999994);
74 testFloor(-1, -0.5);
75 testFloor(-1, -0.7);
76 testFloor(1, 1);
77 testFloor(1, 1.1);
78 testFloor(1, 1.5);
79 testFloor(1, 1.7);
80 testFloor(-1, -1);
81 testFloor(-2, -1.1);
82 testFloor(-2, -1.5);
83 testFloor(-2, -1.7);
84
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);
91
92 // 2^30 is a smi boundary.
93 var two_30 = 1 << 30;
94
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);
99
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);
104
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);
109
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);
114
115 // 2^52 is a precision boundary.
116 var two_52 = (1 << 30) * (1 << 22);
117
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);
124
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);
129
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);
134
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);
139}
140
141
142// Test in a loop to cover the custom IC and GC-related issues.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143for (var i = 0; i < 10; i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144 test();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 new Array(i * 10000);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146}
147
148
149// Regression test for a bug where a negative zero coming from Math.floor
150// was not properly handled by other operations.
151function floorsum(i, n) {
152 var ret = Math.floor(n);
153 while (--i > 0) {
154 ret += Math.floor(n);
155 }
156 return ret;
157}
158assertEquals(-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).
162assertEquals(-0, floorsum(100000, -0));