blob: 11f9da38f98bac1d42b00111c37e804396f36934 [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 Math_ceil = stdlib.Math.ceil;
11 var Math_floor = stdlib.Math.floor;
12 var Math_sqrt = stdlib.Math.sqrt;
13 var Math_abs = stdlib.Math.abs;
14 var Math_min = stdlib.Math.min;
15 var Math_max = stdlib.Math.max;
16 var Math_acos = stdlib.Math.acos;
17 var Math_asin = stdlib.Math.asin;
18 var Math_atan = stdlib.Math.atan;
19 var Math_cos = stdlib.Math.cos;
20 var Math_sin = stdlib.Math.sin;
21 var Math_tan = stdlib.Math.tan;
22 var Math_exp = stdlib.Math.exp;
23 var Math_log = stdlib.Math.log;
24 var Math_atan2 = stdlib.Math.atan2;
25
26 FUNC_BODY
27 return {main: FUNC_NAME};
28 }
29
30 var source = MODULE_NAME.toString()
31 .replace(/MODULE_NAME/g, func.name + "_module")
32 .replace(/FUNC_BODY/g, func.toString())
33 .replace(/FUNC_NAME/g, func.name);
34 return eval("(" + source + ")");
35}
36
37function RunThreeWayTest(asmfunc, expect) {
38 var asm_source = asmfunc.toString();
39 var nonasm_source = asm_source.replace(new RegExp("use asm"), "");
40 var stdlib = {Math: Math};
41
42 var js_module = eval("(" + nonasm_source + ")")(stdlib);
43 print("Testing " + asmfunc.name + " (js)...");
44 expect(js_module);
45
46 print("Testing " + asmfunc.name + " (asm.js)...");
47 var asm_module = asmfunc(stdlib);
48 expect(asm_module);
49
50 print("Testing " + asmfunc.name + " (wasm)...");
51 var wasm_module = Wasm.instantiateModuleFromAsm(asm_source, stdlib);
52 expect(wasm_module);
53}
54
55const Math_ceil = Math.ceil;
56const Math_floor = Math.floor;
57const Math_sqrt = Math.sqrt;
58const Math_abs = Math.abs;
59const Math_min = Math.min;
60const Math_max = Math.max;
61const Math_acos = Math.acos;
62const Math_asin = Math.asin;
63const Math_atan = Math.atan;
64const Math_cos = Math.cos;
65const Math_sin = Math.sin;
66const Math_tan = Math.tan;
67const Math_exp = Math.exp;
68const Math_log = Math.log;
69const Math_atan2 = Math.atan2;
70
71function f64_add(a, b) {
72 a = +a;
73 b = +b;
74 return +(+a + +b);
75}
76
77function f64_sub(a, b) {
78 a = +a;
79 b = +b;
80 return +(+a - +b);
81}
82
83function f64_mul(a, b) {
84 a = +a;
85 b = +b;
86 return +(+a * +b);
87}
88
89function f64_div(a, b) {
90 a = +a;
91 b = +b;
92 return +(+a / +b);
93}
94
95function f64_eq(a, b) {
96 a = +a;
97 b = +b;
98 if (+a == +b) {
99 return 1;
100 }
101 return 0;
102}
103
104function f64_ne(a, b) {
105 a = +a;
106 b = +b;
107 if (+a != +b) {
108 return 1;
109 }
110 return 0;
111}
112
113function f64_lt(a, b) {
114 a = +a;
115 b = +b;
116 if (+a < +b) {
117 return 1;
118 }
119 return 0;
120}
121
122function f64_lteq(a, b) {
123 a = +a;
124 b = +b;
125 if (+a <= +b) {
126 return 1;
127 }
128 return 0;
129}
130
131function f64_gt(a, b) {
132 a = +a;
133 b = +b;
134 if (+a > +b) {
135 return 1;
136 }
137 return 0;
138}
139
140function f64_gteq(a, b) {
141 a = +a;
142 b = +b;
143 if (+a >= +b) {
144 return 1;
145 }
146 return 0;
147}
148
149function f64_ceil(a) {
150 a = +a;
151 return +(Math_ceil(+a));
152}
153
154function f64_floor(a) {
155 a = +a;
156 return +(Math_floor(+a));
157}
158
159function f64_sqrt(a) {
160 a = +a;
161 return +(Math_sqrt(+a));
162}
163
164function f64_abs(a) {
165 a = +a;
166 return +(Math_abs(+a));
167}
168
169function f64_min(a, b) {
170 a = +a;
171 b = +b;
172 return +(Math_min(+a, +b));
173}
174
175function f64_max(a, b) {
176 a = +a;
177 b = +b;
178 return +(Math_max(+a, +b));
179}
180
181function f64_acos(a) {
182 a = +a;
183 return +Math_acos(+a);
184}
185
186function f64_asin(a) {
187 a = +a;
188 return +Math_asin(+a);
189}
190
191function f64_atan(a) {
192 a = +a;
193 return +Math_atan(+a);
194}
195
196function f64_cos(a) {
197 a = +a;
198 return +Math_cos(+a);
199}
200
201function f64_sin(a) {
202 a = +a;
203 return +Math_sin(+a);
204}
205
206function f64_tan(a) {
207 a = +a;
208 return +Math_tan(+a);
209}
210
211function f64_exp(a, b) {
212 a = +a;
213 b = +b;
214 return +Math_exp(+a, +b);
215}
216
217function f64_log(a, b) {
218 a = +a;
219 b = +b;
220 return +Math_log(+a, +b);
221}
222
223function f64_atan2(a) {
224 a = +a;
225 return +Math_atan2(+a);
226}
227
228
229var inputs = [
230 0, 1, 2, 3, 4,
231 NaN,
232 Infinity,
233 -Infinity,
234 10, 20, 30, 31, 32, 33, 100, 2000,
235 30000, 400000, 5000000,
236 100000000, 2000000000,
237 2147483646,
238 2147483647,
239 2147483648,
240 2147483649,
241 0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344,
242 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
243 0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00,
244 0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff,
245 -0,
246 -1, -2, -3, -4,
247 -10, -20, -30, -31, -32, -33, -100, -2000,
248 -30000, -400000, -5000000,
249 -100000000, -2000000000,
250 -2147483646,
251 -2147483647,
252 -2147483648,
253 -2147483649,
254 0.1,
255 1.1e-2,
256 1.2e-4,
257 1.3e-8,
258 1.4e-11,
259 1.5e-12,
260 1.6e-13
261];
262
263var funcs = [
264 f64_add,
265 f64_sub,
266 f64_mul,
267 f64_div,
268 f64_eq,
269 f64_ne,
270 f64_lt,
271 f64_lteq,
272 f64_gt,
273 f64_gteq,
274 f64_ceil,
275 f64_floor,
276// TODO(bradnelson) f64_sqrt,
277 f64_abs,
278// TODO(bradnelson) f64_min is wrong for -0
279// TODO(bradnelson) f64_max is wrong for -0
280// TODO(bradnelson) f64_acos,
281// TODO(bradnelson) f64_asin,
282// TODO(bradnelson) f64_atan,
283// TODO(bradnelson) f64_cos,
284// TODO(bradnelson) f64_sin,
285// TODO(bradnelson) f64_tan,
286// TODO(bradnelson) f64_exp,
287// TODO(bradnelson) f64_log,
288// TODO(bradnelson) f64_atan2,
289];
290
291(function () {
292 for (func of funcs) {
293 RunThreeWayTest(WrapInAsmModule(func), function (module) {
294 if (func.length == 1) {
295 for (a of inputs) {
296 assertEquals(func(a), module.main(a));
297 assertEquals(func(a / 10), module.main(a / 10));
298 assertEquals(func(a / 440.9), module.main(a / 440.9));
299 assertEquals(func(a / -33.1), module.main(a / -33.1));
300 }
301 } else {
302 for (a of inputs) {
303 for (b of inputs) {
304 assertEquals(func(a, b), module.main(a, b));
305 assertEquals(func(a / 10, b), module.main(a / 10, b));
306 assertEquals(func(a, b / 440.9), module.main(a, b / 440.9));
307 assertEquals(func(a / -33.1, b), module.main(a / -33.1, b));
308 }
309 }
310 }
311 });
312 }
313})();