signextend.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. /* global assert,testRunner */ // eslint rule
  6. WScript.Flag("-WasmSignExtends");
  7. WScript.Flag("-WasmI64");
  8. WScript.LoadScriptFile("../UnitTestFramework/UnitTestFramework.js");
  9. function makeCSETest(type, op1, op2, tests) {
  10. return {
  11. name: op2 ? `${type}.${op1}/${type}.${op2} no cse` : `${type}.${op1} cse`,
  12. body() {
  13. const buf = WebAssembly.wabt.convertWast2Wasm(`
  14. (module
  15. (func (export "func") (param $x ${type}) (result ${type})
  16. (${type}.sub
  17. (${type}.${op1} (get_local $x))
  18. (${type}.${op2 || op1} (get_local $x))
  19. )
  20. )
  21. )`);
  22. const mod = new WebAssembly.Module(buf);
  23. const {exports: {func}} = new WebAssembly.Instance(mod);
  24. const name = op2 ? `${op1}/${op2}` : op1;
  25. const assertion = op2 ? assert.areNotEqual : assert.areEqual;
  26. let j = 0;
  27. for (let i = 0; i < tests.length * 10; ++i) {
  28. const input = tests[j++ % tests.length];
  29. const result = func(input);
  30. if (type === "i64") {
  31. assertion(0, result.low|0, `${type}.${name}(0x${input.toString(16)}) = 0x${(result.low|0).toString(16)} low`);
  32. if (!op2) {
  33. assertion(0, result.high|0, `${type}.${name}(0x${input.toString(16)}) = 0x${(result.high|0).toString(16)} high`);
  34. }
  35. } else {
  36. assertion(0, result|0, `${type}.${name}(0x${input.toString(16)})`);
  37. }
  38. }
  39. }
  40. };
  41. }
  42. const tests = [
  43. makeCSETest("i32", "extend8_s" , null, [0xFF, 1, 0x1FF]),
  44. makeCSETest("i32", "extend16_s", null, [0xFF, 1, 0xFFFF, 0x1FFFF]),
  45. makeCSETest("i64", "extend8_s" , null, [0xFF, 1, 0x1FF]),
  46. makeCSETest("i64", "extend16_s", null, [0xFF, 1, 0xFFFF, 0x1FFFF]),
  47. makeCSETest("i64", "extend32_s", null, [0xFF, 1, 0xFFFF, 0xFFFFFFFF, {low: 0xFFFFFFFF, high: 1}]),
  48. makeCSETest("i32", "extend8_s" , "extend16_s", [0xFF, 0x1FF, 0xFF4F, 0x1FF4F]),
  49. makeCSETest("i32", "extend16_s", "extend8_s" , [0xFF, 0x1FF, 0xFF4F, 0x1FF4F]),
  50. makeCSETest("i64", "extend8_s" , "extend16_s", [0xFF, 0x1FF, 0xFF4F, 0x1FF4F]),
  51. makeCSETest("i64", "extend8_s" , "extend32_s", [0xFF, 0x1FF, 0xFF4F, 0x1FF4F]),
  52. makeCSETest("i64", "extend16_s", "extend8_s" , [0xFF, 0xFF4F, 0x1FF4F]),
  53. makeCSETest("i64", "extend16_s", "extend32_s", [0xFFFF, 0x14FFF]),
  54. makeCSETest("i64", "extend32_s", "extend8_s" , [0xFF, 0xFF4F, 0xFFF4FFFF, {low: 0x4FFFFFFF, high: 1}]),
  55. makeCSETest("i64", "extend32_s", "extend16_s", [0xFF4F, 0xFFF4FFFF, {low: 0x4FFFFFFF, high: 1}]),
  56. ];
  57. WScript.LoadScriptFile("../UnitTestFramework/yargs.js");
  58. const argv = yargsParse(WScript.Arguments, {
  59. boolean: ["verbose"],
  60. number: ["start", "end"],
  61. default: {
  62. verbose: true,
  63. start: 0,
  64. end: tests.length
  65. }
  66. }).argv;
  67. const todoTests = tests
  68. .slice(argv.start, argv.end);
  69. testRunner.run(todoTests, {verbose: argv.verbose});