1// 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");
8
9function runSelect2(module, which, a, b) {
10  assertEquals(which == 0 ? a : b, module.select(a, b));
11}
12
13function testSelect2(type) {
14  var kBodySize = 2;
15  var kNameOffset = 21 + kBodySize + 1;
16
17  for (var which = 0; which < 2; which++) {
18    print("type = " + type + ", which = " + which);
19
20    var data = bytes(
21      // -- memory
22      kDeclMemory,
23      12, 12, 1,                  // memory
24      // -- signatures
25      kDeclSignatures, 1,
26      2, type, type, type,        // signature: (t,t)->t
27      // -- select
28      kDeclFunctions, 1,
29      kDeclFunctionName | kDeclFunctionExport,
30      0, 0,
31      kNameOffset, 0, 0, 0,       // name offset
32      kBodySize, 0,               // body size
33      kExprGetLocal, which,       // --
34      kDeclEnd,
35      's','e','l','e','c','t',0   // name
36    );
37
38    var module = _WASMEXP_.instantiateModule(data);
39
40    assertEquals("function", typeof module.select);
41    runSelect2(module, which, 99, 97);
42    runSelect2(module, which, -99, -97);
43
44    if (type != kAstF32) {
45      runSelect2(module, which, 0x80000000 | 0, 0x7fffffff | 0);
46      runSelect2(module, which, 0x80000001 | 0, 0x7ffffffe | 0);
47      runSelect2(module, which, 0xffffffff | 0, 0xfffffffe | 0);
48      runSelect2(module, which, -2147483647, 2147483646);
49      runSelect2(module, which, -2147483646, 2147483645);
50      runSelect2(module, which, -2147483648, 2147483647);
51    }
52
53    if (type != kAstI32 && type != kAstI64) {
54      runSelect2(module, which, -1.25, 5.25);
55      runSelect2(module, which, Infinity, -Infinity);
56    }
57  }
58}
59
60
61testSelect2(kAstI32);
62testSelect2(kAstF32);
63testSelect2(kAstF64);
64
65
66function runSelect10(module, which, a, b) {
67  var x = -1;
68
69  var result = [
70    module.select(a, b, x, x, x, x, x, x, x, x),
71    module.select(x, a, b, x, x, x, x, x, x, x),
72    module.select(x, x, a, b, x, x, x, x, x, x),
73    module.select(x, x, x, a, b, x, x, x, x, x),
74    module.select(x, x, x, x, a, b, x, x, x, x),
75    module.select(x, x, x, x, x, a, b, x, x, x),
76    module.select(x, x, x, x, x, x, a, b, x, x),
77    module.select(x, x, x, x, x, x, x, a, b, x),
78    module.select(x, x, x, x, x, x, x, x, a, b),
79    module.select(x, x, x, x, x, x, x, x, x, a)
80  ];
81
82  for (var i = 0; i < 10; i++) {
83     if (which == i) assertEquals(a, result[i]);
84     else if (which == i+1) assertEquals(b, result[i]);
85     else assertEquals(x, result[i]);
86  }
87}
88
89function testSelect10(type) {
90  var kBodySize = 2;
91  var kNameOffset = 29 + kBodySize + 1;
92
93  for (var which = 0; which < 10; which++) {
94    print("type = " + type + ", which = " + which);
95
96    var t = type;
97    var data = bytes(
98      kDeclMemory,
99      12, 12, 1,                  // memory
100      // signatures
101      kDeclSignatures, 1,
102      10, t,t,t,t,t,t,t,t,t,t,t,  // (tx10)->t
103      // main function
104      kDeclFunctions, 1,
105      kDeclFunctionName | kDeclFunctionExport,
106      0, 0,
107      kNameOffset, 0, 0, 0,       // name offset
108      kBodySize, 0,               // body size
109      kExprGetLocal, which,       // --
110      kDeclEnd,
111      's','e','l','e','c','t',0   // name
112    );
113
114    var module = _WASMEXP_.instantiateModule(data);
115
116    assertEquals("function", typeof module.select);
117    runSelect10(module, which, 99, 97);
118    runSelect10(module, which, -99, -97);
119
120    if (type != kAstF32) {
121      runSelect10(module, which, 0x80000000 | 0, 0x7fffffff | 0);
122      runSelect10(module, which, 0x80000001 | 0, 0x7ffffffe | 0);
123      runSelect10(module, which, 0xffffffff | 0, 0xfffffffe | 0);
124      runSelect10(module, which, -2147483647, 2147483646);
125      runSelect10(module, which, -2147483646, 2147483645);
126      runSelect10(module, which, -2147483648, 2147483647);
127    }
128
129    if (type != kAstI32 && type != kAstI64) {
130      runSelect10(module, which, -1.25, 5.25);
131      runSelect10(module, which, Infinity, -Infinity);
132    }
133  }
134}
135
136
137testSelect10(kAstI32);
138testSelect10(kAstF32);
139testSelect10(kAstF64);
140