OLD | NEW |
(Empty) | |
| 1 // 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 |
| 7 function WrapInAsmModule(func) { |
| 8 function MODULE_NAME(stdlib) { |
| 9 "use asm"; |
| 10 var imul = stdlib.Math.imul; |
| 11 |
| 12 FUNC_BODY |
| 13 return {main: FUNC_NAME}; |
| 14 } |
| 15 |
| 16 var source = MODULE_NAME.toString() |
| 17 .replace(/MODULE_NAME/g, func.name + "_module") |
| 18 .replace(/FUNC_BODY/g, func.toString()) |
| 19 .replace(/FUNC_NAME/g, func.name); |
| 20 return eval("(" + source + ")"); |
| 21 } |
| 22 |
| 23 function RunThreeWayTest(asmfunc, expect) { |
| 24 var asm_source = asmfunc.toString(); |
| 25 var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| 26 var stdlib = {Math: Math}; |
| 27 |
| 28 var js_module = eval("(" + nonasm_source + ")")(stdlib); |
| 29 print("Testing " + asmfunc.name + " (js)..."); |
| 30 expect(js_module); |
| 31 |
| 32 print("Testing " + asmfunc.name + " (asm.js)..."); |
| 33 var asm_module = asmfunc(stdlib); |
| 34 expect(asm_module); |
| 35 |
| 36 print("Testing " + asmfunc.name + " (wasm)..."); |
| 37 var wasm_module = Wasm.instantiateModuleFromAsm(asm_source, stdlib); |
| 38 expect(wasm_module); |
| 39 } |
| 40 |
| 41 const imul = Math.imul; |
| 42 |
| 43 function u32_add(a, b) { |
| 44 a = a | 0; |
| 45 b = b | 0; |
| 46 return +((a >>> 0) + (b >>> 0)); |
| 47 } |
| 48 |
| 49 function u32_sub(a, b) { |
| 50 a = a | 0; |
| 51 b = b | 0; |
| 52 return +((a >>> 0) - (b >>> 0)); |
| 53 } |
| 54 |
| 55 function u32_mul(a, b) { |
| 56 a = a | 0; |
| 57 b = b | 0; |
| 58 return +imul(a >>> 0, b >>> 0); |
| 59 } |
| 60 |
| 61 function u32_div(a, b) { |
| 62 a = a | 0; |
| 63 b = b | 0; |
| 64 return ((a >>> 0) / (b >>> 0)) | 0; |
| 65 } |
| 66 |
| 67 function u32_mod(a, b) { |
| 68 a = a | 0; |
| 69 b = b | 0; |
| 70 return ((a >>> 0) % (b >>> 0)) | 0; |
| 71 } |
| 72 |
| 73 function u32_and(a, b) { |
| 74 a = a | 0; |
| 75 b = b | 0; |
| 76 return +((a >>> 0) & (b >>> 0)); |
| 77 } |
| 78 |
| 79 function u32_or(a, b) { |
| 80 a = a | 0; |
| 81 b = b | 0; |
| 82 return +((a >>> 0) | (b >>> 0)); |
| 83 } |
| 84 |
| 85 function u32_xor(a, b) { |
| 86 a = a | 0; |
| 87 b = b | 0; |
| 88 return +((a >>> 0) ^ (b >>> 0)); |
| 89 } |
| 90 |
| 91 function u32_shl(a, b) { |
| 92 a = a | 0; |
| 93 b = b | 0; |
| 94 return +((a >>> 0) << (b >>> 0)); |
| 95 } |
| 96 |
| 97 function u32_shr(a, b) { |
| 98 a = a | 0; |
| 99 b = b | 0; |
| 100 return +((a >>> 0) >> (b >>> 0)); |
| 101 } |
| 102 |
| 103 function u32_sar(a, b) { |
| 104 a = a | 0; |
| 105 b = b | 0; |
| 106 return ((a >>> 0) >>> (b >>> 0)) | 0; |
| 107 } |
| 108 |
| 109 function u32_eq(a, b) { |
| 110 a = a | 0; |
| 111 b = b | 0; |
| 112 if ((a >>> 0) == (b >>> 0)) { |
| 113 return 1; |
| 114 } |
| 115 return 0; |
| 116 } |
| 117 |
| 118 function u32_ne(a, b) { |
| 119 a = a | 0; |
| 120 b = b | 0; |
| 121 if ((a >>> 0) < (b >>> 0)) { |
| 122 return 1; |
| 123 } |
| 124 return 0; |
| 125 } |
| 126 |
| 127 function u32_lt(a, b) { |
| 128 a = a | 0; |
| 129 b = b | 0; |
| 130 if ((a >>> 0) < (b >>> 0)) { |
| 131 return 1; |
| 132 } |
| 133 return 0; |
| 134 } |
| 135 |
| 136 function u32_lteq(a, b) { |
| 137 a = a | 0; |
| 138 b = b | 0; |
| 139 if ((a >>> 0) <= (b >>> 0)) { |
| 140 return 1; |
| 141 } |
| 142 return 0; |
| 143 } |
| 144 |
| 145 function u32_gt(a, b) { |
| 146 a = a | 0; |
| 147 b = b | 0; |
| 148 if ((a >>> 0) > (b >>> 0)) { |
| 149 return 1; |
| 150 } |
| 151 return 0; |
| 152 } |
| 153 |
| 154 function u32_gteq(a, b) { |
| 155 a = a | 0; |
| 156 b = b | 0; |
| 157 if ((a >>> 0) >= (b >>> 0)) { |
| 158 return 1; |
| 159 } |
| 160 return 0; |
| 161 } |
| 162 |
| 163 |
| 164 var inputs = [ |
| 165 0, 1, 2, 3, 4, |
| 166 10, 20, 30, 31, 32, 33, 100, 2000, |
| 167 30000, 400000, 5000000, |
| 168 100000000, 2000000000, |
| 169 2147483646, |
| 170 2147483647, |
| 171 2147483648, |
| 172 2147483649, |
| 173 0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344, |
| 174 0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c, |
| 175 0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00, |
| 176 0x761c4761, 0x80000000, 0x88888888, 0xa0000000, 0xdddddddd, 0xe0000000, |
| 177 0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff, |
| 178 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff, |
| 179 0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff, |
| 180 -1, -2, -3, -4, |
| 181 -10, -20, -30, -31, -32, -33, -100, -2000, |
| 182 -30000, -400000, -5000000, |
| 183 -100000000, -2000000000, |
| 184 -2147483646, |
| 185 -2147483647, |
| 186 -2147483648, |
| 187 -2147483649, |
| 188 ]; |
| 189 |
| 190 var funcs = [ |
| 191 // TODO(bradnelson): u32_add, |
| 192 // TODO(bradnelson): u32_sub, |
| 193 // TODO(titzer): u32_mul requires Math.imul |
| 194 // TODO(titzer): u32_div by zero is incorrect |
| 195 // TODO(titzer): u32_mod by zero is incorrect |
| 196 // TODO(titzer): u32_mul crashes turbofan in asm.js mode |
| 197 u32_and, |
| 198 u32_or, |
| 199 u32_xor, |
| 200 // TODO(titzer): u32_shl on arm |
| 201 // TODO(titzer): u32_shr on arm |
| 202 // TODO(titzer): u32_sar on arm |
| 203 u32_eq, |
| 204 u32_ne, |
| 205 u32_lt, |
| 206 u32_lteq, |
| 207 u32_gt, |
| 208 u32_gteq, |
| 209 // TODO(titzer): u32_min |
| 210 // TODO(titzer): u32_max |
| 211 // TODO(titzer): u32_abs |
| 212 ]; |
| 213 |
| 214 (function () { |
| 215 for (func of funcs) { |
| 216 RunThreeWayTest(WrapInAsmModule(func), function (module) { |
| 217 for (a of inputs) { |
| 218 for (b of inputs) { |
| 219 var expected = func(a, b); |
| 220 assertEquals(expected, module.main(a, b)); |
| 221 } |
| 222 } |
| 223 }); |
| 224 } |
| 225 |
| 226 })(); |
OLD | NEW |