| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
| 6 | 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 8 | 9 |
| 9 function runSelect2(module, which, a, b) { | 10 function runSelect2(select, which, a, b) { |
| 10 assertEquals(which == 0 ? a : b, module.select(a, b)); | 11 assertEquals(which == 0 ? a : b, select(a, b)); |
| 11 } | 12 } |
| 12 | 13 |
| 13 function testSelect2(type) { | 14 function testSelect2(type) { |
| 14 var kBodySize = 2; | |
| 15 var kNameOffset = kHeaderSize + 21 + kBodySize + 1; | |
| 16 | |
| 17 for (var which = 0; which < 2; which++) { | 15 for (var which = 0; which < 2; which++) { |
| 18 print("type = " + type + ", which = " + which); | 16 print("type = " + type + ", which = " + which); |
| 19 | 17 |
| 20 var data = bytesWithHeader( | 18 var builder = new WasmModuleBuilder(); |
| 21 // -- memory | |
| 22 kDeclMemory, | |
| 23 1, 1, 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 | 19 |
| 38 var module = _WASMEXP_.instantiateModule(data); | 20 builder.addFunction("select", [type, type, type]) |
| 21 .addBody([kExprGetLocal, which]) |
| 22 .exportFunc() |
| 39 | 23 |
| 40 assertEquals("function", typeof module.select); | 24 var select = builder.instantiate().exports.select; |
| 41 runSelect2(module, which, 99, 97); | 25 |
| 42 runSelect2(module, which, -99, -97); | 26 runSelect2(select, which, 99, 97); |
| 27 runSelect2(select, which, -99, -97); |
| 43 | 28 |
| 44 if (type != kAstF32) { | 29 if (type != kAstF32) { |
| 45 runSelect2(module, which, 0x80000000 | 0, 0x7fffffff | 0); | 30 runSelect2(select, which, 0x80000000 | 0, 0x7fffffff | 0); |
| 46 runSelect2(module, which, 0x80000001 | 0, 0x7ffffffe | 0); | 31 runSelect2(select, which, 0x80000001 | 0, 0x7ffffffe | 0); |
| 47 runSelect2(module, which, 0xffffffff | 0, 0xfffffffe | 0); | 32 runSelect2(select, which, 0xffffffff | 0, 0xfffffffe | 0); |
| 48 runSelect2(module, which, -2147483647, 2147483646); | 33 runSelect2(select, which, -2147483647, 2147483646); |
| 49 runSelect2(module, which, -2147483646, 2147483645); | 34 runSelect2(select, which, -2147483646, 2147483645); |
| 50 runSelect2(module, which, -2147483648, 2147483647); | 35 runSelect2(select, which, -2147483648, 2147483647); |
| 51 } | 36 } |
| 52 | 37 |
| 53 if (type != kAstI32 && type != kAstI64) { | 38 if (type != kAstI32 && type != kAstI64) { |
| 54 runSelect2(module, which, -1.25, 5.25); | 39 runSelect2(select, which, -1.25, 5.25); |
| 55 runSelect2(module, which, Infinity, -Infinity); | 40 runSelect2(select, which, Infinity, -Infinity); |
| 56 } | 41 } |
| 57 } | 42 } |
| 58 } | 43 } |
| 59 | 44 |
| 60 | 45 |
| 61 testSelect2(kAstI32); | 46 testSelect2(kAstI32); |
| 62 testSelect2(kAstF32); | 47 testSelect2(kAstF32); |
| 63 testSelect2(kAstF64); | 48 testSelect2(kAstF64); |
| 64 | 49 |
| 65 | 50 |
| 66 function runSelect10(module, which, a, b) { | 51 function runSelect10(select, which, a, b) { |
| 67 var x = -1; | 52 var x = -1; |
| 68 | 53 |
| 69 var result = [ | 54 var result = [ |
| 70 module.select(a, b, x, x, x, x, x, x, x, x), | 55 select(a, b, x, x, x, x, x, x, x, x), |
| 71 module.select(x, a, b, x, x, x, x, x, x, x), | 56 select(x, a, b, x, x, x, x, x, x, x), |
| 72 module.select(x, x, a, b, x, x, x, x, x, x), | 57 select(x, x, a, b, x, x, x, x, x, x), |
| 73 module.select(x, x, x, a, b, x, x, x, x, x), | 58 select(x, x, x, a, b, x, x, x, x, x), |
| 74 module.select(x, x, x, x, a, b, x, x, x, x), | 59 select(x, x, x, x, a, b, x, x, x, x), |
| 75 module.select(x, x, x, x, x, a, b, x, x, x), | 60 select(x, x, x, x, x, a, b, x, x, x), |
| 76 module.select(x, x, x, x, x, x, a, b, x, x), | 61 select(x, x, x, x, x, x, a, b, x, x), |
| 77 module.select(x, x, x, x, x, x, x, a, b, x), | 62 select(x, x, x, x, x, x, x, a, b, x), |
| 78 module.select(x, x, x, x, x, x, x, x, a, b), | 63 select(x, x, x, x, x, x, x, x, a, b), |
| 79 module.select(x, x, x, x, x, x, x, x, x, a) | 64 select(x, x, x, x, x, x, x, x, x, a) |
| 80 ]; | 65 ]; |
| 81 | 66 |
| 82 for (var i = 0; i < 10; i++) { | 67 for (var i = 0; i < 10; i++) { |
| 83 if (which == i) assertEquals(a, result[i]); | 68 if (which == i) assertEquals(a, result[i]); |
| 84 else if (which == i+1) assertEquals(b, result[i]); | 69 else if (which == i+1) assertEquals(b, result[i]); |
| 85 else assertEquals(x, result[i]); | 70 else assertEquals(x, result[i]); |
| 86 } | 71 } |
| 87 } | 72 } |
| 88 | 73 |
| 89 function testSelect10(type) { | 74 function testSelect10(t) { |
| 90 var kBodySize = 2; | 75 var kBodySize = 2; |
| 91 var kNameOffset = kHeaderSize + 29 + kBodySize + 1; | 76 var kNameOffset = kHeaderSize + 29 + kBodySize + 1; |
| 92 | 77 |
| 93 for (var which = 0; which < 10; which++) { | 78 for (var which = 0; which < 10; which++) { |
| 94 print("type = " + type + ", which = " + which); | 79 print("type = " + t + ", which = " + which); |
| 95 | 80 |
| 96 var t = type; | 81 var builder = new WasmModuleBuilder(); |
| 97 var data = bytesWithHeader( | 82 builder.addFunction("select", [t,t,t,t,t,t,t,t,t,t,t]) |
| 98 kDeclMemory, | 83 .addBody([kExprGetLocal, which]) |
| 99 1, 1, 1, // memory | 84 .exportFunc(); |
| 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 | 85 |
| 114 var module = _WASMEXP_.instantiateModule(data); | 86 var select = builder.instantiate().exports.select; |
| 115 | 87 |
| 116 assertEquals("function", typeof module.select); | 88 assertEquals("function", typeof select); |
| 117 runSelect10(module, which, 99, 97); | 89 runSelect10(select, which, 99, 97); |
| 118 runSelect10(module, which, -99, -97); | 90 runSelect10(select, which, -99, -97); |
| 119 | 91 |
| 120 if (type != kAstF32) { | 92 if (t != kAstF32) { |
| 121 runSelect10(module, which, 0x80000000 | 0, 0x7fffffff | 0); | 93 runSelect10(select, which, 0x80000000 | 0, 0x7fffffff | 0); |
| 122 runSelect10(module, which, 0x80000001 | 0, 0x7ffffffe | 0); | 94 runSelect10(select, which, 0x80000001 | 0, 0x7ffffffe | 0); |
| 123 runSelect10(module, which, 0xffffffff | 0, 0xfffffffe | 0); | 95 runSelect10(select, which, 0xffffffff | 0, 0xfffffffe | 0); |
| 124 runSelect10(module, which, -2147483647, 2147483646); | 96 runSelect10(select, which, -2147483647, 2147483646); |
| 125 runSelect10(module, which, -2147483646, 2147483645); | 97 runSelect10(select, which, -2147483646, 2147483645); |
| 126 runSelect10(module, which, -2147483648, 2147483647); | 98 runSelect10(select, which, -2147483648, 2147483647); |
| 127 } | 99 } |
| 128 | 100 |
| 129 if (type != kAstI32 && type != kAstI64) { | 101 if (t != kAstI32 && t != kAstI64) { |
| 130 runSelect10(module, which, -1.25, 5.25); | 102 runSelect10(select, which, -1.25, 5.25); |
| 131 runSelect10(module, which, Infinity, -Infinity); | 103 runSelect10(select, which, Infinity, -Infinity); |
| 132 } | 104 } |
| 133 } | 105 } |
| 134 } | 106 } |
| 135 | 107 |
| 136 | 108 |
| 137 testSelect10(kAstI32); | 109 testSelect10(kAstI32); |
| 138 testSelect10(kAstF32); | 110 testSelect10(kAstF32); |
| 139 testSelect10(kAstF64); | 111 testSelect10(kAstF64); |
| OLD | NEW |