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 Math_ceil = stdlib.Math.ceil; |
| 11 var Math_floor = stdlib.Math.floor; |
| 12 var Math_sqrt = stdlib.Math.sqrt; |
| 13 var Math_abs = stdlib.Math.abs; |
| 14 var Math_min = stdlib.Math.min; |
| 15 var Math_max = stdlib.Math.max; |
| 16 var Math_acos = stdlib.Math.acos; |
| 17 var Math_asin = stdlib.Math.asin; |
| 18 var Math_atan = stdlib.Math.atan; |
| 19 var Math_cos = stdlib.Math.cos; |
| 20 var Math_sin = stdlib.Math.sin; |
| 21 var Math_tan = stdlib.Math.tan; |
| 22 var Math_exp = stdlib.Math.exp; |
| 23 var Math_log = stdlib.Math.log; |
| 24 var Math_atan2 = stdlib.Math.atan2; |
| 25 |
| 26 FUNC_BODY |
| 27 return {main: FUNC_NAME}; |
| 28 } |
| 29 |
| 30 var source = MODULE_NAME.toString() |
| 31 .replace(/MODULE_NAME/g, func.name + "_module") |
| 32 .replace(/FUNC_BODY/g, func.toString()) |
| 33 .replace(/FUNC_NAME/g, func.name); |
| 34 return eval("(" + source + ")"); |
| 35 } |
| 36 |
| 37 function RunThreeWayTest(asmfunc, expect) { |
| 38 var asm_source = asmfunc.toString(); |
| 39 var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| 40 var stdlib = {Math: Math}; |
| 41 |
| 42 var js_module = eval("(" + nonasm_source + ")")(stdlib); |
| 43 print("Testing " + asmfunc.name + " (js)..."); |
| 44 expect(js_module); |
| 45 |
| 46 print("Testing " + asmfunc.name + " (asm.js)..."); |
| 47 var asm_module = asmfunc(stdlib); |
| 48 expect(asm_module); |
| 49 |
| 50 print("Testing " + asmfunc.name + " (wasm)..."); |
| 51 var wasm_module = Wasm.instantiateModuleFromAsm(asm_source, stdlib); |
| 52 expect(wasm_module); |
| 53 } |
| 54 |
| 55 const Math_ceil = Math.ceil; |
| 56 const Math_floor = Math.floor; |
| 57 const Math_sqrt = Math.sqrt; |
| 58 const Math_abs = Math.abs; |
| 59 const Math_min = Math.min; |
| 60 const Math_max = Math.max; |
| 61 const Math_acos = Math.acos; |
| 62 const Math_asin = Math.asin; |
| 63 const Math_atan = Math.atan; |
| 64 const Math_cos = Math.cos; |
| 65 const Math_sin = Math.sin; |
| 66 const Math_tan = Math.tan; |
| 67 const Math_exp = Math.exp; |
| 68 const Math_log = Math.log; |
| 69 const Math_atan2 = Math.atan2; |
| 70 |
| 71 function f64_add(a, b) { |
| 72 a = +a; |
| 73 b = +b; |
| 74 return +(+a + +b); |
| 75 } |
| 76 |
| 77 function f64_sub(a, b) { |
| 78 a = +a; |
| 79 b = +b; |
| 80 return +(+a - +b); |
| 81 } |
| 82 |
| 83 function f64_mul(a, b) { |
| 84 a = +a; |
| 85 b = +b; |
| 86 return +(+a * +b); |
| 87 } |
| 88 |
| 89 function f64_div(a, b) { |
| 90 a = +a; |
| 91 b = +b; |
| 92 return +(+a / +b); |
| 93 } |
| 94 |
| 95 function f64_eq(a, b) { |
| 96 a = +a; |
| 97 b = +b; |
| 98 if (+a == +b) { |
| 99 return 1; |
| 100 } |
| 101 return 0; |
| 102 } |
| 103 |
| 104 function f64_ne(a, b) { |
| 105 a = +a; |
| 106 b = +b; |
| 107 if (+a != +b) { |
| 108 return 1; |
| 109 } |
| 110 return 0; |
| 111 } |
| 112 |
| 113 function f64_lt(a, b) { |
| 114 a = +a; |
| 115 b = +b; |
| 116 if (+a < +b) { |
| 117 return 1; |
| 118 } |
| 119 return 0; |
| 120 } |
| 121 |
| 122 function f64_lteq(a, b) { |
| 123 a = +a; |
| 124 b = +b; |
| 125 if (+a <= +b) { |
| 126 return 1; |
| 127 } |
| 128 return 0; |
| 129 } |
| 130 |
| 131 function f64_gt(a, b) { |
| 132 a = +a; |
| 133 b = +b; |
| 134 if (+a > +b) { |
| 135 return 1; |
| 136 } |
| 137 return 0; |
| 138 } |
| 139 |
| 140 function f64_gteq(a, b) { |
| 141 a = +a; |
| 142 b = +b; |
| 143 if (+a >= +b) { |
| 144 return 1; |
| 145 } |
| 146 return 0; |
| 147 } |
| 148 |
| 149 function f64_ceil(a) { |
| 150 a = +a; |
| 151 return +(Math_ceil(+a)); |
| 152 } |
| 153 |
| 154 function f64_floor(a) { |
| 155 a = +a; |
| 156 return +(Math_floor(+a)); |
| 157 } |
| 158 |
| 159 function f64_sqrt(a) { |
| 160 a = +a; |
| 161 return +(Math_sqrt(+a)); |
| 162 } |
| 163 |
| 164 function f64_abs(a) { |
| 165 a = +a; |
| 166 return +(Math_abs(+a)); |
| 167 } |
| 168 |
| 169 function f64_min(a, b) { |
| 170 a = +a; |
| 171 b = +b; |
| 172 return +(Math_min(+a, +b)); |
| 173 } |
| 174 |
| 175 function f64_max(a, b) { |
| 176 a = +a; |
| 177 b = +b; |
| 178 return +(Math_max(+a, +b)); |
| 179 } |
| 180 |
| 181 function f64_acos(a) { |
| 182 a = +a; |
| 183 return +Math_acos(+a); |
| 184 } |
| 185 |
| 186 function f64_asin(a) { |
| 187 a = +a; |
| 188 return +Math_asin(+a); |
| 189 } |
| 190 |
| 191 function f64_atan(a) { |
| 192 a = +a; |
| 193 return +Math_atan(+a); |
| 194 } |
| 195 |
| 196 function f64_cos(a) { |
| 197 a = +a; |
| 198 return +Math_cos(+a); |
| 199 } |
| 200 |
| 201 function f64_sin(a) { |
| 202 a = +a; |
| 203 return +Math_sin(+a); |
| 204 } |
| 205 |
| 206 function f64_tan(a) { |
| 207 a = +a; |
| 208 return +Math_tan(+a); |
| 209 } |
| 210 |
| 211 function f64_exp(a, b) { |
| 212 a = +a; |
| 213 b = +b; |
| 214 return +Math_exp(+a, +b); |
| 215 } |
| 216 |
| 217 function f64_log(a, b) { |
| 218 a = +a; |
| 219 b = +b; |
| 220 return +Math_log(+a, +b); |
| 221 } |
| 222 |
| 223 function f64_atan2(a) { |
| 224 a = +a; |
| 225 return +Math_atan2(+a); |
| 226 } |
| 227 |
| 228 |
| 229 var inputs = [ |
| 230 0, 1, 2, 3, 4, |
| 231 10, 20, 30, 31, 32, 33, 100, 2000, |
| 232 30000, 400000, 5000000, |
| 233 100000000, 2000000000, |
| 234 2147483646, |
| 235 2147483647, |
| 236 2147483648, |
| 237 2147483649, |
| 238 0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344, |
| 239 0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c, |
| 240 0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00, |
| 241 0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff, |
| 242 0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff, |
| 243 -0, |
| 244 -1, -2, -3, -4, |
| 245 -10, -20, -30, -31, -32, -33, -100, -2000, |
| 246 -30000, -400000, -5000000, |
| 247 -100000000, -2000000000, |
| 248 -2147483646, |
| 249 -2147483647, |
| 250 -2147483648, |
| 251 -2147483649, |
| 252 0.1, |
| 253 1.1e-2, |
| 254 1.2e-4, |
| 255 1.3e-8, |
| 256 1.4e-11, |
| 257 1.5e-12, |
| 258 1.6e-13 |
| 259 ]; |
| 260 |
| 261 var funcs = [ |
| 262 f64_add, |
| 263 f64_sub, |
| 264 f64_mul, |
| 265 f64_div, |
| 266 f64_eq, |
| 267 f64_ne, |
| 268 f64_lt, |
| 269 f64_lteq, |
| 270 f64_gt, |
| 271 f64_gteq, |
| 272 // TODO(bradnelson) f64_ceil, |
| 273 // TODO(bradnelson) f64_floor, |
| 274 // TODO(bradnelson) f64_sqrt, |
| 275 // TODO(bradnelson) f64_abs, |
| 276 // TODO(bradnelson) f64_min is wrong for -0 |
| 277 // TODO(bradnelson) f64_max is wrong for -0 |
| 278 // TODO(bradnelson) f64_acos, |
| 279 // TODO(bradnelson) f64_asin, |
| 280 // TODO(bradnelson) f64_atan, |
| 281 // TODO(bradnelson) f64_cos, |
| 282 // TODO(bradnelson) f64_sin, |
| 283 // TODO(bradnelson) f64_tan, |
| 284 // TODO(bradnelson) f64_exp, |
| 285 // TODO(bradnelson) f64_log, |
| 286 // TODO(bradnelson) f64_atan2, |
| 287 ]; |
| 288 |
| 289 (function () { |
| 290 for (func of funcs) { |
| 291 RunThreeWayTest(WrapInAsmModule(func), function (module) { |
| 292 if (func.length == 1) { |
| 293 for (a of inputs) { |
| 294 assertEquals(func(a), module.main(a)); |
| 295 assertEquals(func(a / 10), module.main(a / 10)); |
| 296 assertEquals(func(a / 440.9), module.main(a / 440.9)); |
| 297 assertEquals(func(a / -33.1), module.main(a / -33.1)); |
| 298 } |
| 299 } else { |
| 300 for (a of inputs) { |
| 301 for (b of inputs) { |
| 302 assertEquals(func(a, b), module.main(a, b)); |
| 303 assertEquals(func(a / 10, b), module.main(a / 10, b)); |
| 304 assertEquals(func(a, b / 440.9), module.main(a, b / 440.9)); |
| 305 assertEquals(func(a / -33.1, b), module.main(a / -33.1, b)); |
| 306 } |
| 307 } |
| 308 } |
| 309 }); |
| 310 } |
| 311 })(); |
OLD | NEW |