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"); 8load("test/mjsunit/wasm/wasm-module-builder.js"); 9 10function runSelect2(select, which, a, b) { 11 assertEquals(which == 0 ? a : b, select(a, b)); 12} 13 14function testSelect2(type) { 15 for (var which = 0; which < 2; which++) { 16 print("type = " + type + ", which = " + which); 17 18 var builder = new WasmModuleBuilder(); 19 20 builder.addFunction("select", makeSig_r_xx(type, type)) 21 .addBody([kExprGetLocal, which]) 22 .exportFunc() 23 24 var select = builder.instantiate().exports.select; 25 26 runSelect2(select, which, 99, 97); 27 runSelect2(select, which, -99, -97); 28 29 if (type != kAstF32) { 30 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); 36 } 37 38 if (type != kAstI32 && type != kAstI64) { 39 runSelect2(select, which, -1.25, 5.25); 40 runSelect2(select, which, Infinity, -Infinity); 41 } 42 } 43} 44 45 46testSelect2(kAstI32); 47testSelect2(kAstF32); 48testSelect2(kAstF64); 49 50 51function runSelect10(select, which, a, b) { 52 var x = -1; 53 54 var result = [ 55 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) 65 ]; 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 74function testSelect10(t) { 75 var kBodySize = 2; 76 var kNameOffset = kHeaderSize + 29 + kBodySize + 1; 77 78 for (var which = 0; which < 10; which++) { 79 print("type = " + t + ", which = " + which); 80 81 var builder = new WasmModuleBuilder(); 82 builder.addFunction("select", makeSig([t,t,t,t,t,t,t,t,t,t], [t])) 83 .addBody([kExprGetLocal, which]) 84 .exportFunc(); 85 86 var select = builder.instantiate().exports.select; 87 88 assertEquals("function", typeof select); 89 runSelect10(select, which, 99, 97); 90 runSelect10(select, which, -99, -97); 91 92 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); 99 } 100 101 if (t != kAstI32 && t != kAstI64) { 102 runSelect10(select, which, -1.25, 5.25); 103 runSelect10(select, which, Infinity, -Infinity); 104 } 105 } 106} 107 108 109testSelect10(kAstI32); 110testSelect10(kAstF32); 111testSelect10(kAstF64); 112