blob: d3f3ac37f4462b1fa860cb1194283bd2a8508136 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 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 --no-use-inlining
29
30// -----------------------------------------------------------------------------
31
32function ConstructDiv(divisor) {
33 return "return ((dividend | 0) / ((" + divisor + ") | 0)) | 0";
34}
35
36var RefDivByConstI =
37 new Function("dividend", "divisor", ConstructDiv("divisor"));
38
39%NeverOptimizeFunction(RefDivByConstI);
40
41// -----------------------------------------------------------------------------
42
43function ConstructMod(divisor) {
44 return "return ((dividend | 0) % ((" + divisor + ") | 0)) | 0";
45}
46
47var RefModByConstI =
48 new Function("dividend", "divisor", ConstructMod("divisor"));
49
50%NeverOptimizeFunction(RefModByConstI);
51
52// -----------------------------------------------------------------------------
53
54function ConstructFlooringDiv(divisor) {
55 return "return Math.floor(dividend / (" + divisor + ")) | 0";
56}
57
58var RefFlooringDivByConstI =
59 new Function("dividend", "divisor", ConstructFlooringDiv("divisor"));
60
61%NeverOptimizeFunction(RefFlooringDivByConstI);
62
63// -----------------------------------------------------------------------------
64
65function PushSymmetric(values, x) {
66 values.push(x, -x);
67}
68
69function PushRangeSymmetric(values, from, to) {
70 for (var x = from; x <= to; x++) {
71 PushSymmetric(values, x);
72 }
73}
74
75function CreateTestValues() {
76 var values = [
77 // -(2^31)
78 -2147483648,
79 // Some values from "Hacker's Delight", chapter 10-7.
80 715827883, 1431655766, -1431655765, -1431655764,
81 // Some "randomly" chosen numbers.
82 123, -1234, 12345, -123456, 1234567, -12345678, 123456789
83 ];
84 // Powers of 2
85 for (var shift = 6; shift < 31; shift++) {
86 PushSymmetric(values, 1 << shift);
87 }
88 // Values near zero
89 PushRangeSymmetric(values, 1, 32);
90 // Various magnitudes
91 PushRangeSymmetric(values, 100, 109);
92 PushRangeSymmetric(values, 1000, 1009);
93 PushRangeSymmetric(values, 10000, 10009);
94 PushRangeSymmetric(values, 100000, 100009);
95 PushRangeSymmetric(values, 1000000, 1000009);
96 PushRangeSymmetric(values, 10000000, 10000009);
97 PushRangeSymmetric(values, 100000000, 100000009);
98 PushRangeSymmetric(values, 1000000000, 1000000009);
99 return values;
100}
101
102// -----------------------------------------------------------------------------
103
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105function TestDivisionLike(ref, construct, values, divisor) {
106 // Define the function to test.
107 var OptFun = new Function("dividend", construct(divisor));
108
109 // Warm up type feedback.
110 OptFun(7);
111 OptFun(11);
112 %OptimizeFunctionOnNextCall(OptFun);
113 OptFun(13);
114
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400115function dude(dividend) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 // Avoid deopt caused by overflow, we do not want to test this here.
117 if (dividend === -2147483648 && divisor === -1) return;
118 assertEquals(ref(dividend, divisor), OptFun(dividend));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400119 }
120
121 // Check results.
122 values.forEach(dude);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123}
124
125function Test(ref, construct) {
126 var values = CreateTestValues();
127 values.forEach(function(divisor) {
128 TestDivisionLike(ref, construct, values, divisor);
129 });
130}
131
132Test(RefDivByConstI, ConstructDiv);
133Test(RefModByConstI, ConstructMod);
134Test(RefFlooringDivByConstI, ConstructFlooringDiv);