blob: 6224e8fa1f49f2f9ad32394284208e6e2c6045d1 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-wasm
6
7function WrapInAsmModule(func) {
8 function MODULE_NAME(stdlib) {
9 "use asm";
10 var imul = stdlib.Math.imul;
11 var Math_max = stdlib.Math.max;
12 var Math_min = stdlib.Math.min;
13 var Math_abs = stdlib.Math.abs;
14
15 FUNC_BODY
16 return {main: FUNC_NAME};
17 }
18
19 var source = MODULE_NAME.toString()
20 .replace(/MODULE_NAME/g, func.name + "_module")
21 .replace(/FUNC_BODY/g, func.toString())
22 .replace(/FUNC_NAME/g, func.name);
23 return eval("(" + source + ")");
24}
25
26function RunThreeWayTest(asmfunc, expect) {
27 var asm_source = asmfunc.toString();
28 var nonasm_source = asm_source.replace(new RegExp("use asm"), "");
29 var stdlib = {Math: Math};
30
31 var js_module = eval("(" + nonasm_source + ")")(stdlib);
32 print("Testing " + asmfunc.name + " (js)...");
33 expect(js_module);
34
35 print("Testing " + asmfunc.name + " (asm.js)...");
36 var asm_module = asmfunc(stdlib);
37 expect(asm_module);
38
39 print("Testing " + asmfunc.name + " (wasm)...");
40 var wasm_module = Wasm.instantiateModuleFromAsm(asm_source, stdlib);
41 expect(wasm_module);
42}
43
44const imul = Math.imul;
45const Math_max = Math.max;
46const Math_min = Math.min;
47const Math_abs = Math.abs;
48
49function i32_add(a, b) {
50 a = a | 0;
51 b = b | 0;
52 return (a + b) | 0;
53}
54
55function i32_sub(a, b) {
56 a = a | 0;
57 b = b | 0;
58 return (a - b) | 0;
59}
60
61function i32_mul(a, b) {
62 a = a | 0;
63 b = b | 0;
64 return imul(a, b) | 0;
65}
66
67function i32_div(a, b) {
68 a = a | 0;
69 b = b | 0;
70 return (a / b) | 0;
71}
72
73function i32_mod(a, b) {
74 a = a | 0;
75 b = b | 0;
76 return (a % b) | 0;
77}
78
79function i32_and(a, b) {
80 a = a | 0;
81 b = b | 0;
82 return (a & b) | 0;
83}
84
85function i32_or(a, b) {
86 a = a | 0;
87 b = b | 0;
88 return (a | b) | 0;
89}
90
91function i32_xor(a, b) {
92 a = a | 0;
93 b = b | 0;
94 return (a ^ b) | 0;
95}
96
97function i32_shl(a, b) {
98 a = a | 0;
99 b = b | 0;
100 return (a << b) | 0;
101}
102
103function i32_shr(a, b) {
104 a = a | 0;
105 b = b | 0;
106 return (a >> b) | 0;
107}
108
109function i32_sar(a, b) {
110 a = a | 0;
111 b = b | 0;
112 return (a >>> b) | 0;
113}
114
115function i32_eq(a, b) {
116 a = a | 0;
117 b = b | 0;
118 if ((a | 0) == (b | 0)) {
119 return 1;
120 }
121 return 0;
122}
123
124function i32_ne(a, b) {
125 a = a | 0;
126 b = b | 0;
127 if ((a | 0) < (b | 0)) {
128 return 1;
129 }
130 return 0;
131}
132
133function i32_lt(a, b) {
134 a = a | 0;
135 b = b | 0;
136 if ((a | 0) < (b | 0)) {
137 return 1;
138 }
139 return 0;
140}
141
142function i32_lteq(a, b) {
143 a = a | 0;
144 b = b | 0;
145 if ((a | 0) <= (b | 0)) {
146 return 1;
147 }
148 return 0;
149}
150
151function i32_gt(a, b) {
152 a = a | 0;
153 b = b | 0;
154 if ((a | 0) > (b | 0)) {
155 return 1;
156 }
157 return 0;
158}
159
160function i32_gteq(a, b) {
161 a = a | 0;
162 b = b | 0;
163 if ((a | 0) >= (b | 0)) {
164 return 1;
165 }
166 return 0;
167}
168
169function i32_min(a, b) {
170 a = a | 0;
171 b = b | 0;
172 return Math_min(a | 0, b | 0) | 0;
173}
174
175function i32_max(a, b) {
176 a = a | 0;
177 b = b | 0;
178 return Math_max(a | 0, b | 0) | 0;
179}
180
181function i32_abs(a) {
182 a = a | 0;
183 return Math_abs(a | 0) | 0;
184}
185
186var inputs = [
187 0, 1, 2, 3, 4,
188 10, 20, 30, 31, 32, 33, 100, 2000,
189 30000, 400000, 5000000,
190 100000000, 2000000000,
191 2147483646,
192 2147483647,
193 2147483648,
194 2147483649,
195 0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344,
196 0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
197 0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00,
198 0x761c4761, 0x80000000, 0x88888888, 0xa0000000, 0xdddddddd, 0xe0000000,
199 0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff,
200 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff,
201 0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
202 -1, -2, -3, -4,
203 -10, -20, -30, -31, -32, -33, -100, -2000,
204 -30000, -400000, -5000000,
205 -100000000, -2000000000,
206 -2147483646,
207 -2147483647,
208 -2147483648,
209 -2147483649,
210];
211
212var funcs = [
213 i32_add,
214 i32_sub,
215 i32_mul,
216 i32_div,
217 i32_mod,
218 i32_and,
219 i32_or,
220 i32_xor,
221 i32_shl,
222 i32_shr,
223 i32_sar,
224 i32_eq,
225 i32_ne,
226 i32_lt,
227 i32_lteq,
228 i32_gt,
229 i32_gteq,
230 i32_min,
231 i32_max,
232 i32_abs
233];
234
235(function () {
236 for (func of funcs) {
237 RunThreeWayTest(WrapInAsmModule(func), function (module) {
238 if (func.length == 1) {
239 for (a of inputs) {
240 assertEquals(func(a), module.main(a));
241 }
242 } else {
243 for (a of inputs) {
244 for (b of inputs) {
245 assertEquals(func(a, b), module.main(a, b));
246 }
247 }
248 }
249 });
250 }
251
252})();