1// Copyright 2013 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 --max-opt-count=1000
29
30var imul_func = Math.imul;
31function imul_polyfill(a, b) {
32  var ah  = (a >>> 16) & 0xffff;
33  var al = a & 0xffff;
34  var bh  = (b >>> 16) & 0xffff;
35  var bl = b & 0xffff;
36  return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
37}
38
39function TestMathImul(expected, a, b) {
40  function imul_meth_closure(a, b) { return Math.imul(a, b); }
41  function imul_func_closure(a, b) { return imul_func(a, b); }
42
43  // Test reference implementation.
44  assertEquals(expected, imul_polyfill(a, b));
45
46  // Test direct method call.
47  assertEquals(expected, Math.imul(a, b));
48
49  // Test direct function call.
50  assertEquals(expected, imul_func(a, b));
51
52  // Test optimized method call inside closure.
53  assertEquals(expected, imul_meth_closure(a, b));
54  assertEquals(expected, imul_meth_closure(a, b));
55  %OptimizeFunctionOnNextCall(imul_meth_closure);
56  assertEquals(expected, imul_meth_closure(a, b));
57
58  // Test optimized function call inside closure.
59  assertEquals(expected, imul_func_closure(a, b));
60  assertEquals(expected, imul_func_closure(a, b));
61  %OptimizeFunctionOnNextCall(imul_func_closure);
62  assertEquals(expected, imul_func_closure(a, b));
63
64  // Deoptimize closures and forget type feedback.
65  %DeoptimizeFunction(imul_meth_closure);
66  %DeoptimizeFunction(imul_func_closure);
67  %ClearFunctionTypeFeedback(imul_meth_closure);
68  %ClearFunctionTypeFeedback(imul_func_closure);
69}
70
71TestMathImul(8, 2, 4);
72TestMathImul(-8, -1, 8);
73TestMathImul(4, -2, -2);
74TestMathImul(-5, 0xffffffff, 5);
75TestMathImul(-10, 0xfffffffe, 5);
76
77TestMathImul(0, false, 7);
78TestMathImul(0, 7, false);
79TestMathImul(0, false, false);
80
81TestMathImul(7, true, 7);
82TestMathImul(7, 7, true);
83TestMathImul(1, true, true);
84
85TestMathImul(0, false, true);
86TestMathImul(0, true, false);
87
88TestMathImul(0, undefined, 7);
89TestMathImul(0, 7, undefined);
90TestMathImul(0, undefined, undefined);
91
92TestMathImul(0, -0, 7);
93TestMathImul(0, 7, -0);
94
95TestMathImul(0, 0.1, 7);
96TestMathImul(0, 7, 0.1);
97TestMathImul(0, 0.9, 7);
98TestMathImul(0, 7, 0.9);
99TestMathImul(7, 1.1, 7);
100TestMathImul(7, 7, 1.1);
101TestMathImul(7, 1.9, 7);
102TestMathImul(7, 7, 1.9);
103
104TestMathImul(0, "str", 7);
105TestMathImul(0, 7, "str");
106
107TestMathImul(0, {}, 7);
108TestMathImul(0, 7, {});
109
110TestMathImul(0, [], 7);
111TestMathImul(0, 7, []);
112
113// 2^30 is a smi boundary on arm and ia32.
114var two_30 = 1 << 30;
115
116TestMathImul(-two_30, two_30, 7);
117TestMathImul(-two_30, 7, two_30);
118TestMathImul(0, two_30, two_30);
119
120TestMathImul(two_30, -two_30, 7);
121TestMathImul(two_30, 7, -two_30);
122TestMathImul(0, -two_30, -two_30);
123
124// 2^31 is a smi boundary on x64.
125var two_31 = 2 * two_30;
126
127TestMathImul(-two_31, two_31, 7);
128TestMathImul(-two_31, 7, two_31);
129TestMathImul(0, two_31, two_31);
130
131TestMathImul(-two_31, -two_31, 7);
132TestMathImul(-two_31, 7, -two_31);
133TestMathImul(0, -two_31, -two_31);
134
135// 2^31 - 1 is the largest int32 value.
136var max_val = two_31 - 1;
137
138TestMathImul(two_31 - 7, max_val, 7);
139TestMathImul(two_31 - 7, 7, max_val);
140TestMathImul(1, max_val, max_val);
141
142// 2^16 is a boundary value that overflows when squared.
143var two_16 = 1 << 16;
144
145TestMathImul(0, two_16, two_16);
146TestMathImul(-two_16, two_16 - 1, two_16);
147TestMathImul(-two_16, two_16, two_16 - 1);
148TestMathImul(-2 * two_16 + 1, two_16 - 1, two_16 - 1);
149