1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --allow-natives-syntax --nouse-inlining --enable-sudiv
// Use this function as reference. Make sure it is not inlined.
function div(a, b) {
return a / b;
}
var limit = 0x1000000;
var exhaustive_limit = 100;
var step = 10;
var values = [0x10000001,
0x12345678,
-0x789abcdf, // 0x87654321
0x01234567,
0x76543210,
-0x80000000, // 0x80000000
0x7fffffff,
-0x0fffffff, // 0xf0000001
0x00000010,
-0x01000000 // 0xff000000
];
function test_div() {
var c = 0;
for (var k = 0; k <= limit; k++) {
if (k > exhaustive_limit) { c += step; k += c; }
assertEquals(Math.floor(div(k, 1)), Math.floor(k / 1));
assertEquals(Math.floor(div(k, -1)), Math.floor(k / -1));
assertEquals(Math.floor(div(k, 2)), Math.floor(k / 2));
assertEquals(Math.floor(div(k, -2)), Math.floor(k / -2));
assertEquals(Math.floor(div(k, 3)), Math.floor(k / 3));
assertEquals(Math.floor(div(k, -3)), Math.floor(k / -3));
assertEquals(Math.floor(div(k, 4)), Math.floor(k / 4));
assertEquals(Math.floor(div(k, -4)), Math.floor(k / -4));
assertEquals(Math.floor(div(k, 5)), Math.floor(k / 5));
assertEquals(Math.floor(div(k, -5)), Math.floor(k / -5));
assertEquals(Math.floor(div(k, 6)), Math.floor(k / 6));
assertEquals(Math.floor(div(k, -6)), Math.floor(k / -6));
assertEquals(Math.floor(div(k, 7)), Math.floor(k / 7));
assertEquals(Math.floor(div(k, -7)), Math.floor(k / -7));
assertEquals(Math.floor(div(k, 8)), Math.floor(k / 8));
assertEquals(Math.floor(div(k, -8)), Math.floor(k / -8));
assertEquals(Math.floor(div(k, 9)), Math.floor(k / 9));
assertEquals(Math.floor(div(k, -9)), Math.floor(k / -9));
assertEquals(Math.floor(div(k, 10)), Math.floor(k / 10));
assertEquals(Math.floor(div(k, -10)), Math.floor(k / -10));
assertEquals(Math.floor(div(k, 11)), Math.floor(k / 11));
assertEquals(Math.floor(div(k, -11)), Math.floor(k / -11));
assertEquals(Math.floor(div(k, 12)), Math.floor(k / 12));
assertEquals(Math.floor(div(k, -12)), Math.floor(k / -12));
assertEquals(Math.floor(div(k, 13)), Math.floor(k / 13));
assertEquals(Math.floor(div(k, -13)), Math.floor(k / -13));
assertEquals(Math.floor(div(k, 14)), Math.floor(k / 14));
assertEquals(Math.floor(div(k, -14)), Math.floor(k / -14));
assertEquals(Math.floor(div(k, 15)), Math.floor(k / 15));
assertEquals(Math.floor(div(k, -15)), Math.floor(k / -15));
assertEquals(Math.floor(div(k, 16)), Math.floor(k / 16));
assertEquals(Math.floor(div(k, -16)), Math.floor(k / -16));
assertEquals(Math.floor(div(k, 17)), Math.floor(k / 17));
assertEquals(Math.floor(div(k, -17)), Math.floor(k / -17));
assertEquals(Math.floor(div(k, 18)), Math.floor(k / 18));
assertEquals(Math.floor(div(k, -18)), Math.floor(k / -18));
assertEquals(Math.floor(div(k, 19)), Math.floor(k / 19));
assertEquals(Math.floor(div(k, -19)), Math.floor(k / -19));
assertEquals(Math.floor(div(k, 20)), Math.floor(k / 20));
assertEquals(Math.floor(div(k, -20)), Math.floor(k / -20));
assertEquals(Math.floor(div(k, 21)), Math.floor(k / 21));
assertEquals(Math.floor(div(k, -21)), Math.floor(k / -21));
assertEquals(Math.floor(div(k, 22)), Math.floor(k / 22));
assertEquals(Math.floor(div(k, -22)), Math.floor(k / -22));
assertEquals(Math.floor(div(k, 23)), Math.floor(k / 23));
assertEquals(Math.floor(div(k, -23)), Math.floor(k / -23));
assertEquals(Math.floor(div(k, 24)), Math.floor(k / 24));
assertEquals(Math.floor(div(k, -24)), Math.floor(k / -24));
assertEquals(Math.floor(div(k, 25)), Math.floor(k / 25));
assertEquals(Math.floor(div(k, -25)), Math.floor(k / -25));
assertEquals(Math.floor(div(k, 125)), Math.floor(k / 125));
assertEquals(Math.floor(div(k, -125)), Math.floor(k / -125));
assertEquals(Math.floor(div(k, 625)), Math.floor(k / 625));
assertEquals(Math.floor(div(k, -625)), Math.floor(k / -625));
}
c = 0;
for (var k = 0; k <= limit; k++) {
if (k > exhaustive_limit) { c += step; k += c; }
assertEquals(Math.floor(div(-k, 1)), Math.floor(-k / 1));
assertEquals(Math.floor(div(-k, -1)), Math.floor(-k / -1));
assertEquals(Math.floor(div(-k, 2)), Math.floor(-k / 2));
assertEquals(Math.floor(div(-k, -2)), Math.floor(-k / -2));
assertEquals(Math.floor(div(-k, 3)), Math.floor(-k / 3));
assertEquals(Math.floor(div(-k, -3)), Math.floor(-k / -3));
assertEquals(Math.floor(div(-k, 4)), Math.floor(-k / 4));
assertEquals(Math.floor(div(-k, -4)), Math.floor(-k / -4));
assertEquals(Math.floor(div(-k, 5)), Math.floor(-k / 5));
assertEquals(Math.floor(div(-k, -5)), Math.floor(-k / -5));
assertEquals(Math.floor(div(-k, 6)), Math.floor(-k / 6));
assertEquals(Math.floor(div(-k, -6)), Math.floor(-k / -6));
assertEquals(Math.floor(div(-k, 7)), Math.floor(-k / 7));
assertEquals(Math.floor(div(-k, -7)), Math.floor(-k / -7));
assertEquals(Math.floor(div(-k, 8)), Math.floor(-k / 8));
assertEquals(Math.floor(div(-k, -8)), Math.floor(-k / -8));
assertEquals(Math.floor(div(-k, 9)), Math.floor(-k / 9));
assertEquals(Math.floor(div(-k, -9)), Math.floor(-k / -9));
assertEquals(Math.floor(div(-k, 10)), Math.floor(-k / 10));
assertEquals(Math.floor(div(-k, -10)), Math.floor(-k / -10));
assertEquals(Math.floor(div(-k, 11)), Math.floor(-k / 11));
assertEquals(Math.floor(div(-k, -11)), Math.floor(-k / -11));
assertEquals(Math.floor(div(-k, 12)), Math.floor(-k / 12));
assertEquals(Math.floor(div(-k, -12)), Math.floor(-k / -12));
assertEquals(Math.floor(div(-k, 13)), Math.floor(-k / 13));
assertEquals(Math.floor(div(-k, -13)), Math.floor(-k / -13));
assertEquals(Math.floor(div(-k, 14)), Math.floor(-k / 14));
assertEquals(Math.floor(div(-k, -14)), Math.floor(-k / -14));
assertEquals(Math.floor(div(-k, 15)), Math.floor(-k / 15));
assertEquals(Math.floor(div(-k, -15)), Math.floor(-k / -15));
assertEquals(Math.floor(div(-k, 16)), Math.floor(-k / 16));
assertEquals(Math.floor(div(-k, -16)), Math.floor(-k / -16));
assertEquals(Math.floor(div(-k, 17)), Math.floor(-k / 17));
assertEquals(Math.floor(div(-k, -17)), Math.floor(-k / -17));
assertEquals(Math.floor(div(-k, 18)), Math.floor(-k / 18));
assertEquals(Math.floor(div(-k, -18)), Math.floor(-k / -18));
assertEquals(Math.floor(div(-k, 19)), Math.floor(-k / 19));
assertEquals(Math.floor(div(-k, -19)), Math.floor(-k / -19));
assertEquals(Math.floor(div(-k, 20)), Math.floor(-k / 20));
assertEquals(Math.floor(div(-k, -20)), Math.floor(-k / -20));
assertEquals(Math.floor(div(-k, 21)), Math.floor(-k / 21));
assertEquals(Math.floor(div(-k, -21)), Math.floor(-k / -21));
assertEquals(Math.floor(div(-k, 22)), Math.floor(-k / 22));
assertEquals(Math.floor(div(-k, -22)), Math.floor(-k / -22));
assertEquals(Math.floor(div(-k, 23)), Math.floor(-k / 23));
assertEquals(Math.floor(div(-k, -23)), Math.floor(-k / -23));
assertEquals(Math.floor(div(-k, 24)), Math.floor(-k / 24));
assertEquals(Math.floor(div(-k, -24)), Math.floor(-k / -24));
assertEquals(Math.floor(div(-k, 25)), Math.floor(-k / 25));
assertEquals(Math.floor(div(-k, -25)), Math.floor(-k / -25));
assertEquals(Math.floor(div(-k, 125)), Math.floor(-k / 125));
assertEquals(Math.floor(div(-k, -125)), Math.floor(-k / -125));
assertEquals(Math.floor(div(-k, 625)), Math.floor(-k / 625));
assertEquals(Math.floor(div(-k, -625)), Math.floor(-k / -625));
}
// Test for edge cases.
// Use (values[key] | 0) to force the integer type.
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values.length; j++) {
assertEquals(Math.floor(div((values[i] | 0), (values[j] | 0))),
Math.floor((values[i] | 0) / (values[j] | 0)));
assertEquals(Math.floor(div(-(values[i] | 0), (values[j] | 0))),
Math.floor(-(values[i] | 0) / (values[j] | 0)));
assertEquals(Math.floor(div((values[i] | 0), -(values[j] | 0))),
Math.floor((values[i] | 0) / -(values[j] | 0)));
assertEquals(Math.floor(div(-(values[i] | 0), -(values[j] | 0))),
Math.floor(-(values[i] | 0) / -(values[j] | 0)));
}
}
}
test_div();
%OptimizeFunctionOnNextCall(test_div);
test_div();
// Test for flooring correctness.
var values2 = [1, 3, 10, 99, 100, 101, 0x7fffffff];
function test_div2() {
for (var i = 0; i < values2.length; i++) {
for (var j = 0; j < values2.length; j++) {
assertEquals(Math.floor(div((values2[i] | 0), (values2[j] | 0))),
Math.floor((values2[i] | 0) / (values2[j] | 0)));
assertEquals(Math.floor(div(-(values2[i] | 0), (values2[j] | 0))),
Math.floor(-(values2[i] | 0) / (values2[j] | 0)));
assertEquals(Math.floor(div((values2[i] | 0), -(values2[j] | 0))),
Math.floor((values2[i] | 0) / -(values2[j] | 0)));
assertEquals(Math.floor(div(-(values2[i] | 0), -(values2[j] | 0))),
Math.floor(-(values2[i] | 0) / -(values2[j] | 0)));
}
}
}
test_div2();
%OptimizeFunctionOnNextCall(test_div2);
test_div2();
// Test for negative zero, overflow and division by 0.
// Separate the tests to prevent deoptimizations from making the other optimized
// test unreachable.
// We box the value in an array to avoid constant propagation.
var neg_one_in_array = [-1];
var zero_in_array = [0];
var min_int_in_array = [-2147483648];
// Test for dividing by constant.
function IsNegativeZero(x) {
assertTrue(x == 0); // Is 0 or -0.
var y = 1 / x;
assertFalse(isFinite(y));
return y < 0;
}
function test_div_deopt_minus_zero() {
for (var i = 0; i < 2; ++i) {
assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) / -1)));
}
}
function test_div_deopt_overflow() {
for (var i = 0; i < 2; ++i) {
// We use '| 0' to force the representation to int32.
assertEquals(-min_int_in_array[0],
Math.floor((min_int_in_array[0] | 0) / -1));
}
}
function test_div_deopt_div_by_zero() {
for (var i = 0; i < 2; ++i) {
assertEquals(div(i, 0),
Math.floor(i / 0));
}
}
test_div_deopt_minus_zero();
test_div_deopt_overflow();
test_div_deopt_div_by_zero();
%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero);
%OptimizeFunctionOnNextCall(test_div_deopt_overflow);
%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero);
test_div_deopt_minus_zero();
test_div_deopt_overflow();
test_div_deopt_div_by_zero();
// Test for dividing by variable.
function test_div_deopt_minus_zero_v() {
for (var i = 0; i < 2; ++i) {
assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) /
neg_one_in_array[0])));
}
}
function test_div_deopt_overflow_v() {
for (var i = 0; i < 2; ++i) {
// We use '| 0' to force the representation to int32.
assertEquals(-min_int_in_array[0],
Math.floor((min_int_in_array[0] | 0) / neg_one_in_array[0]));
}
}
function test_div_deopt_div_by_zero_v() {
for (var i = 0; i < 2; ++i) {
assertEquals(div(i, 0),
Math.floor(i / zero_in_array[0]));
}
}
test_div_deopt_minus_zero_v();
test_div_deopt_overflow_v();
test_div_deopt_div_by_zero_v();
%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero_v);
%OptimizeFunctionOnNextCall(test_div_deopt_overflow_v);
%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero_v);
test_div_deopt_minus_zero_v();
test_div_deopt_overflow_v();
test_div_deopt_div_by_zero_v();
// Test for flooring division with negative dividend.
function flooring_div_by_3(y) {
return Math.floor(y / 3);
}
assertEquals(-1, flooring_div_by_3(-2));
assertEquals(-1, flooring_div_by_3(-2));
%OptimizeFunctionOnNextCall(flooring_div_by_3);
assertEquals(-1, flooring_div_by_3(-2));
|