blob: fe1b7d4cfef9e9268aa5700485020694753ae52a [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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
7load("test/mjsunit/wasm/wasm-constants.js");
Ben Murdochda12d292016-06-02 14:46:10 +01008load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009
Ben Murdochda12d292016-06-02 14:46:10 +010010function runSelect2(select, which, a, b) {
11 assertEquals(which == 0 ? a : b, select(a, b));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012}
13
14function testSelect2(type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015 for (var which = 0; which < 2; which++) {
16 print("type = " + type + ", which = " + which);
17
Ben Murdochda12d292016-06-02 14:46:10 +010018 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019
Ben Murdochc5610432016-08-08 18:44:38 +010020 builder.addFunction("select", makeSig_r_xx(type, type))
Ben Murdochda12d292016-06-02 14:46:10 +010021 .addBody([kExprGetLocal, which])
22 .exportFunc()
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023
Ben Murdochda12d292016-06-02 14:46:10 +010024 var select = builder.instantiate().exports.select;
25
26 runSelect2(select, which, 99, 97);
27 runSelect2(select, which, -99, -97);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028
29 if (type != kAstF32) {
Ben Murdochda12d292016-06-02 14:46:10 +010030 runSelect2(select, which, 0x80000000 | 0, 0x7fffffff | 0);
31 runSelect2(select, which, 0x80000001 | 0, 0x7ffffffe | 0);
32 runSelect2(select, which, 0xffffffff | 0, 0xfffffffe | 0);
33 runSelect2(select, which, -2147483647, 2147483646);
34 runSelect2(select, which, -2147483646, 2147483645);
35 runSelect2(select, which, -2147483648, 2147483647);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 }
37
38 if (type != kAstI32 && type != kAstI64) {
Ben Murdochda12d292016-06-02 14:46:10 +010039 runSelect2(select, which, -1.25, 5.25);
40 runSelect2(select, which, Infinity, -Infinity);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 }
42 }
43}
44
45
46testSelect2(kAstI32);
47testSelect2(kAstF32);
48testSelect2(kAstF64);
49
50
Ben Murdochda12d292016-06-02 14:46:10 +010051function runSelect10(select, which, a, b) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 var x = -1;
53
54 var result = [
Ben Murdochda12d292016-06-02 14:46:10 +010055 select(a, b, x, x, x, x, x, x, x, x),
56 select(x, a, b, x, x, x, x, x, x, x),
57 select(x, x, a, b, x, x, x, x, x, x),
58 select(x, x, x, a, b, x, x, x, x, x),
59 select(x, x, x, x, a, b, x, x, x, x),
60 select(x, x, x, x, x, a, b, x, x, x),
61 select(x, x, x, x, x, x, a, b, x, x),
62 select(x, x, x, x, x, x, x, a, b, x),
63 select(x, x, x, x, x, x, x, x, a, b),
64 select(x, x, x, x, x, x, x, x, x, a)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 ];
66
67 for (var i = 0; i < 10; i++) {
68 if (which == i) assertEquals(a, result[i]);
69 else if (which == i+1) assertEquals(b, result[i]);
70 else assertEquals(x, result[i]);
71 }
72}
73
Ben Murdochda12d292016-06-02 14:46:10 +010074function testSelect10(t) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 var kBodySize = 2;
Ben Murdochda12d292016-06-02 14:46:10 +010076 var kNameOffset = kHeaderSize + 29 + kBodySize + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077
78 for (var which = 0; which < 10; which++) {
Ben Murdochda12d292016-06-02 14:46:10 +010079 print("type = " + t + ", which = " + which);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080
Ben Murdochda12d292016-06-02 14:46:10 +010081 var builder = new WasmModuleBuilder();
Ben Murdoch61f157c2016-09-16 13:49:30 +010082 builder.addFunction("select", makeSig([t,t,t,t,t,t,t,t,t,t], [t]))
Ben Murdochda12d292016-06-02 14:46:10 +010083 .addBody([kExprGetLocal, which])
84 .exportFunc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085
Ben Murdochda12d292016-06-02 14:46:10 +010086 var select = builder.instantiate().exports.select;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
Ben Murdochda12d292016-06-02 14:46:10 +010088 assertEquals("function", typeof select);
89 runSelect10(select, which, 99, 97);
90 runSelect10(select, which, -99, -97);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091
Ben Murdochda12d292016-06-02 14:46:10 +010092 if (t != kAstF32) {
93 runSelect10(select, which, 0x80000000 | 0, 0x7fffffff | 0);
94 runSelect10(select, which, 0x80000001 | 0, 0x7ffffffe | 0);
95 runSelect10(select, which, 0xffffffff | 0, 0xfffffffe | 0);
96 runSelect10(select, which, -2147483647, 2147483646);
97 runSelect10(select, which, -2147483646, 2147483645);
98 runSelect10(select, which, -2147483648, 2147483647);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 }
100
Ben Murdochda12d292016-06-02 14:46:10 +0100101 if (t != kAstI32 && t != kAstI64) {
102 runSelect10(select, which, -1.25, 5.25);
103 runSelect10(select, which, Infinity, -Infinity);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 }
105 }
106}
107
108
109testSelect10(kAstI32);
110testSelect10(kAstF32);
111testSelect10(kAstF64);