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
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
28// Flags: --allow-natives-syntax --nouse-inlining --noenable-sudiv
29// Flags: --noenable-armv8
30
31// Use this function as reference. Make sure it is not inlined.
32function div(a, b) {
33  return a / b;
34}
35
36var limit = 0x1000000;
37var exhaustive_limit = 100;
38var step = 10;
39var values = [0x10000001,
40              0x12345678,
41              -0x789abcdf,  // 0x87654321
42              0x01234567,
43              0x76543210,
44              -0x80000000,  // 0x80000000
45              0x7fffffff,
46              -0x0fffffff,  // 0xf0000001
47              0x00000010,
48              -0x01000000   // 0xff000000
49              ];
50
51function test_div() {
52  var c = 0;
53  for (var k = 0; k <= limit; k++) {
54    if (k > exhaustive_limit) { c += step; k += c; }
55    assertEquals(Math.floor(div(k,   1)), Math.floor(k /   1));
56    assertEquals(Math.floor(div(k,   -1)), Math.floor(k /   -1));
57    assertEquals(Math.floor(div(k,   2)), Math.floor(k /   2));
58    assertEquals(Math.floor(div(k,   -2)), Math.floor(k /   -2));
59    assertEquals(Math.floor(div(k,   3)), Math.floor(k /   3));
60    assertEquals(Math.floor(div(k,   -3)), Math.floor(k /   -3));
61    assertEquals(Math.floor(div(k,   4)), Math.floor(k /   4));
62    assertEquals(Math.floor(div(k,   -4)), Math.floor(k /   -4));
63    assertEquals(Math.floor(div(k,   5)), Math.floor(k /   5));
64    assertEquals(Math.floor(div(k,   -5)), Math.floor(k /   -5));
65    assertEquals(Math.floor(div(k,   6)), Math.floor(k /   6));
66    assertEquals(Math.floor(div(k,   -6)), Math.floor(k /   -6));
67    assertEquals(Math.floor(div(k,   7)), Math.floor(k /   7));
68    assertEquals(Math.floor(div(k,   -7)), Math.floor(k /   -7));
69    assertEquals(Math.floor(div(k,   8)), Math.floor(k /   8));
70    assertEquals(Math.floor(div(k,   -8)), Math.floor(k /   -8));
71    assertEquals(Math.floor(div(k,   9)), Math.floor(k /   9));
72    assertEquals(Math.floor(div(k,   -9)), Math.floor(k /   -9));
73    assertEquals(Math.floor(div(k,  10)), Math.floor(k /  10));
74    assertEquals(Math.floor(div(k,  -10)), Math.floor(k /  -10));
75    assertEquals(Math.floor(div(k,  11)), Math.floor(k /  11));
76    assertEquals(Math.floor(div(k,  -11)), Math.floor(k /  -11));
77    assertEquals(Math.floor(div(k,  12)), Math.floor(k /  12));
78    assertEquals(Math.floor(div(k,  -12)), Math.floor(k /  -12));
79    assertEquals(Math.floor(div(k,  13)), Math.floor(k /  13));
80    assertEquals(Math.floor(div(k,  -13)), Math.floor(k /  -13));
81    assertEquals(Math.floor(div(k,  14)), Math.floor(k /  14));
82    assertEquals(Math.floor(div(k,  -14)), Math.floor(k /  -14));
83    assertEquals(Math.floor(div(k,  15)), Math.floor(k /  15));
84    assertEquals(Math.floor(div(k,  -15)), Math.floor(k /  -15));
85    assertEquals(Math.floor(div(k,  16)), Math.floor(k /  16));
86    assertEquals(Math.floor(div(k,  -16)), Math.floor(k /  -16));
87    assertEquals(Math.floor(div(k,  17)), Math.floor(k /  17));
88    assertEquals(Math.floor(div(k,  -17)), Math.floor(k /  -17));
89    assertEquals(Math.floor(div(k,  18)), Math.floor(k /  18));
90    assertEquals(Math.floor(div(k,  -18)), Math.floor(k /  -18));
91    assertEquals(Math.floor(div(k,  19)), Math.floor(k /  19));
92    assertEquals(Math.floor(div(k,  -19)), Math.floor(k /  -19));
93    assertEquals(Math.floor(div(k,  20)), Math.floor(k /  20));
94    assertEquals(Math.floor(div(k,  -20)), Math.floor(k /  -20));
95    assertEquals(Math.floor(div(k,  21)), Math.floor(k /  21));
96    assertEquals(Math.floor(div(k,  -21)), Math.floor(k /  -21));
97    assertEquals(Math.floor(div(k,  22)), Math.floor(k /  22));
98    assertEquals(Math.floor(div(k,  -22)), Math.floor(k /  -22));
99    assertEquals(Math.floor(div(k,  23)), Math.floor(k /  23));
100    assertEquals(Math.floor(div(k,  -23)), Math.floor(k /  -23));
101    assertEquals(Math.floor(div(k,  24)), Math.floor(k /  24));
102    assertEquals(Math.floor(div(k,  -24)), Math.floor(k /  -24));
103    assertEquals(Math.floor(div(k,  25)), Math.floor(k /  25));
104    assertEquals(Math.floor(div(k,  -25)), Math.floor(k /  -25));
105    assertEquals(Math.floor(div(k, 125)), Math.floor(k / 125));
106    assertEquals(Math.floor(div(k, -125)), Math.floor(k / -125));
107    assertEquals(Math.floor(div(k, 625)), Math.floor(k / 625));
108    assertEquals(Math.floor(div(k, -625)), Math.floor(k / -625));
109  }
110  c = 0;
111  for (var k = 0; k <= limit; k++) {
112    if (k > exhaustive_limit) { c += step; k += c; }
113    assertEquals(Math.floor(div(-k,   1)), Math.floor(-k /   1));
114    assertEquals(Math.floor(div(-k,   -1)), Math.floor(-k /   -1));
115    assertEquals(Math.floor(div(-k,   2)), Math.floor(-k /   2));
116    assertEquals(Math.floor(div(-k,   -2)), Math.floor(-k /   -2));
117    assertEquals(Math.floor(div(-k,   3)), Math.floor(-k /   3));
118    assertEquals(Math.floor(div(-k,   -3)), Math.floor(-k /   -3));
119    assertEquals(Math.floor(div(-k,   4)), Math.floor(-k /   4));
120    assertEquals(Math.floor(div(-k,   -4)), Math.floor(-k /   -4));
121    assertEquals(Math.floor(div(-k,   5)), Math.floor(-k /   5));
122    assertEquals(Math.floor(div(-k,   -5)), Math.floor(-k /   -5));
123    assertEquals(Math.floor(div(-k,   6)), Math.floor(-k /   6));
124    assertEquals(Math.floor(div(-k,   -6)), Math.floor(-k /   -6));
125    assertEquals(Math.floor(div(-k,   7)), Math.floor(-k /   7));
126    assertEquals(Math.floor(div(-k,   -7)), Math.floor(-k /   -7));
127    assertEquals(Math.floor(div(-k,   8)), Math.floor(-k /   8));
128    assertEquals(Math.floor(div(-k,   -8)), Math.floor(-k /   -8));
129    assertEquals(Math.floor(div(-k,   9)), Math.floor(-k /   9));
130    assertEquals(Math.floor(div(-k,   -9)), Math.floor(-k /   -9));
131    assertEquals(Math.floor(div(-k,  10)), Math.floor(-k /  10));
132    assertEquals(Math.floor(div(-k,  -10)), Math.floor(-k /  -10));
133    assertEquals(Math.floor(div(-k,  11)), Math.floor(-k /  11));
134    assertEquals(Math.floor(div(-k,  -11)), Math.floor(-k /  -11));
135    assertEquals(Math.floor(div(-k,  12)), Math.floor(-k /  12));
136    assertEquals(Math.floor(div(-k,  -12)), Math.floor(-k /  -12));
137    assertEquals(Math.floor(div(-k,  13)), Math.floor(-k /  13));
138    assertEquals(Math.floor(div(-k,  -13)), Math.floor(-k /  -13));
139    assertEquals(Math.floor(div(-k,  14)), Math.floor(-k /  14));
140    assertEquals(Math.floor(div(-k,  -14)), Math.floor(-k /  -14));
141    assertEquals(Math.floor(div(-k,  15)), Math.floor(-k /  15));
142    assertEquals(Math.floor(div(-k,  -15)), Math.floor(-k /  -15));
143    assertEquals(Math.floor(div(-k,  16)), Math.floor(-k /  16));
144    assertEquals(Math.floor(div(-k,  -16)), Math.floor(-k /  -16));
145    assertEquals(Math.floor(div(-k,  17)), Math.floor(-k /  17));
146    assertEquals(Math.floor(div(-k,  -17)), Math.floor(-k /  -17));
147    assertEquals(Math.floor(div(-k,  18)), Math.floor(-k /  18));
148    assertEquals(Math.floor(div(-k,  -18)), Math.floor(-k /  -18));
149    assertEquals(Math.floor(div(-k,  19)), Math.floor(-k /  19));
150    assertEquals(Math.floor(div(-k,  -19)), Math.floor(-k /  -19));
151    assertEquals(Math.floor(div(-k,  20)), Math.floor(-k /  20));
152    assertEquals(Math.floor(div(-k,  -20)), Math.floor(-k /  -20));
153    assertEquals(Math.floor(div(-k,  21)), Math.floor(-k /  21));
154    assertEquals(Math.floor(div(-k,  -21)), Math.floor(-k /  -21));
155    assertEquals(Math.floor(div(-k,  22)), Math.floor(-k /  22));
156    assertEquals(Math.floor(div(-k,  -22)), Math.floor(-k /  -22));
157    assertEquals(Math.floor(div(-k,  23)), Math.floor(-k /  23));
158    assertEquals(Math.floor(div(-k,  -23)), Math.floor(-k /  -23));
159    assertEquals(Math.floor(div(-k,  24)), Math.floor(-k /  24));
160    assertEquals(Math.floor(div(-k,  -24)), Math.floor(-k /  -24));
161    assertEquals(Math.floor(div(-k,  25)), Math.floor(-k /  25));
162    assertEquals(Math.floor(div(-k,  -25)), Math.floor(-k /  -25));
163    assertEquals(Math.floor(div(-k, 125)), Math.floor(-k / 125));
164    assertEquals(Math.floor(div(-k, -125)), Math.floor(-k / -125));
165    assertEquals(Math.floor(div(-k, 625)), Math.floor(-k / 625));
166    assertEquals(Math.floor(div(-k, -625)), Math.floor(-k / -625));
167  }
168  // Test for edge cases.
169  // Use (values[key] | 0) to force the integer type.
170  for (var i = 0; i < values.length; i++) {
171    for (var j = 0; j < values.length; j++) {
172      assertEquals(Math.floor(div((values[i] | 0), (values[j] | 0))),
173                   Math.floor((values[i] | 0) / (values[j] | 0)));
174      assertEquals(Math.floor(div(-(values[i] | 0), (values[j] | 0))),
175                   Math.floor(-(values[i] | 0) / (values[j] | 0)));
176      assertEquals(Math.floor(div((values[i] | 0), -(values[j] | 0))),
177                   Math.floor((values[i] | 0) / -(values[j] | 0)));
178      assertEquals(Math.floor(div(-(values[i] | 0), -(values[j] | 0))),
179                   Math.floor(-(values[i] | 0) / -(values[j] | 0)));
180    }
181  }
182}
183
184test_div();
185%OptimizeFunctionOnNextCall(test_div);
186test_div();
187
188// Test for flooring correctness.
189var values2 = [1, 3, 10, 99, 100, 101, 0x7fffffff];
190function test_div2() {
191  for (var i = 0; i < values2.length; i++) {
192    for (var j = 0; j < values2.length; j++) {
193      assertEquals(Math.floor(div((values2[i] | 0), (values2[j] | 0))),
194                   Math.floor((values2[i] | 0) / (values2[j] | 0)));
195      assertEquals(Math.floor(div(-(values2[i] | 0), (values2[j] | 0))),
196                   Math.floor(-(values2[i] | 0) / (values2[j] | 0)));
197      assertEquals(Math.floor(div((values2[i] | 0), -(values2[j] | 0))),
198                   Math.floor((values2[i] | 0) / -(values2[j] | 0)));
199      assertEquals(Math.floor(div(-(values2[i] | 0), -(values2[j] | 0))),
200                   Math.floor(-(values2[i] | 0) / -(values2[j] | 0)));
201    }
202  }
203}
204
205test_div2();
206%OptimizeFunctionOnNextCall(test_div2);
207test_div2();
208
209
210// Test for negative zero, overflow and division by 0.
211// Separate the tests to prevent deoptimizations from making the other optimized
212// test unreachable.
213
214// We box the value in an array to avoid constant propagation.
215var neg_one_in_array = [-1];
216var zero_in_array = [0];
217var min_int_in_array = [-2147483648];
218
219// Test for dividing by constant.
220function IsNegativeZero(x) {
221  assertTrue(x == 0);  // Is 0 or -0.
222  var y = 1 / x;
223  assertFalse(isFinite(y));
224  return y < 0;
225}
226
227function test_div_deopt_minus_zero() {
228  for (var i = 0; i < 2; ++i) {
229    assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) / -1)));
230  }
231}
232
233function test_div_deopt_overflow() {
234  for (var i = 0; i < 2; ++i) {
235    // We use '| 0' to force the representation to int32.
236    assertEquals(-min_int_in_array[0],
237                 Math.floor((min_int_in_array[0] | 0) / -1));
238  }
239}
240
241function test_div_deopt_div_by_zero() {
242  for (var i = 0; i < 2; ++i) {
243    assertEquals(div(i, 0),
244                 Math.floor(i / 0));
245  }
246}
247
248test_div_deopt_minus_zero();
249test_div_deopt_overflow();
250test_div_deopt_div_by_zero();
251%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero);
252%OptimizeFunctionOnNextCall(test_div_deopt_overflow);
253%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero);
254test_div_deopt_minus_zero();
255test_div_deopt_overflow();
256test_div_deopt_div_by_zero();
257
258// Test for dividing by variable.
259function test_div_deopt_minus_zero_v() {
260  for (var i = 0; i < 2; ++i) {
261    assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) /
262               neg_one_in_array[0])));
263  }
264}
265
266function test_div_deopt_overflow_v() {
267  for (var i = 0; i < 2; ++i) {
268    // We use '| 0' to force the representation to int32.
269    assertEquals(-min_int_in_array[0],
270                 Math.floor((min_int_in_array[0] | 0) / neg_one_in_array[0]));
271  }
272}
273
274function test_div_deopt_div_by_zero_v() {
275  for (var i = 0; i < 2; ++i) {
276    assertEquals(div(i, 0),
277                 Math.floor(i / zero_in_array[0]));
278  }
279}
280
281test_div_deopt_minus_zero_v();
282test_div_deopt_overflow_v();
283test_div_deopt_div_by_zero_v();
284%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero_v);
285%OptimizeFunctionOnNextCall(test_div_deopt_overflow_v);
286%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero_v);
287test_div_deopt_minus_zero_v();
288test_div_deopt_overflow_v();
289test_div_deopt_div_by_zero_v();
290