logicalTests.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. let passed = true;
  6. function assertEquals(expected, actual) {
  7. if (expected != actual) {
  8. passed = false;
  9. throw `Expected ${expected}, received ${actual}`;
  10. }
  11. }
  12. const INITIAL_SIZE = 1;
  13. const memObj = new WebAssembly.Memory({initial:INITIAL_SIZE});
  14. const arrays = [new Int32Array (memObj.buffer),
  15. new Int16Array (memObj.buffer),
  16. new Int8Array (memObj.buffer),
  17. ];
  18. const module = new WebAssembly.Module(readbuffer('logical.wasm'));
  19. const instance = new WebAssembly.Instance(module, { "dummy" : { "memory" : memObj } }).exports;
  20. function moveArgsIntoArray(args, offset, arr) {
  21. for (let i = 0; i < args.length; i++) {
  22. arr[offset + i] = args[i];
  23. }
  24. }
  25. let testIntLogicalOps = function (funcname, args1, args2, resultArr) {
  26. const len = args1.length;
  27. const typeIndex = Math.log2(len) - 2;
  28. let arr = arrays [typeIndex];
  29. moveArgsIntoArray(args1, 0, arr);
  30. moveArgsIntoArray(args2, len, arr);
  31. moveArgsIntoArray(resultArr, 2 * len, arr);
  32. instance[funcname]();
  33. const resultOffset = 2 * len;
  34. for (let i = 0; i < len; i++) {
  35. assertEquals(arr[i], arr[resultOffset + i]);
  36. }
  37. }
  38. testIntLogicalOps("m128_and", [-1, 0x80007001, 0, 7], [-1, 0x80007001, 0, 7], [-1, 0x80007001, 0, 7]);
  39. testIntLogicalOps("m128_and", [-1, 0x80000001, 0x80007001, 7], [0, 0x80000001, -1 ^ 0x80007001, 7], [0, 0x80000001, 0, 7]);
  40. testIntLogicalOps("m128_or", [-1, 0xFFFF0000, 0x80000001, 0x55555555], [0, 0x0000FFFF, 0x80000001 ^ -1, 0xaaaaaaaa], [-1, -1, -1, -1]);
  41. testIntLogicalOps("m128_or", [0xFFFF0000, 0, 0x80007001, 0], [0, 0, 0, 0x55555555], [0xFFFF0000, 0, 0x80007001, 0x55555555]);
  42. testIntLogicalOps("m128_xor", [-1, 0xFFFF0000, 0x80000001, 0x55555555], [0, 0x0000FFFF, 0x80000001 ^ -1, 0xaaaaaaaa], [-1, -1, -1, -1]);
  43. testIntLogicalOps("m128_xor", [0xFFFF0000, 0x55555555, -1, 0], [0, 0xaaaaaaaa, 0xFFFF0000, 0x55555555], [0xFFFF0000, -1, 0x0000FFFF, 0x55555555]);
  44. testIntLogicalOps("m128_not", [-1, 0xFFFF0000, 0x80000001, 0x55555555], [0, 0, 0, 0] /*dummy*/, [0, 0x0000FFFF, 0x80000001 ^ -1, 0xaaaaaaaa]);
  45. let testBoolReduceOps = function (funcname, args1, expected) {
  46. const len = args1.length;
  47. let typeIndex = Math.log2(len) - 2;
  48. let arr = arrays [typeIndex];
  49. moveArgsIntoArray(args1, 0, arr);
  50. let result = instance[funcname]();
  51. assertEquals(expected, result);
  52. }
  53. testBoolReduceOps("i32x4_anytrue", [0, 0, 0, 0], false);
  54. testBoolReduceOps("i32x4_anytrue", [0, 0, 0, 0xFFFFFFFF], true);
  55. testBoolReduceOps("i32x4_anytrue", [0xFFFFFFFF, 0, 0, 0], true);
  56. testBoolReduceOps("i32x4_anytrue", [0x80000000, 0, 0, 0], true);
  57. testBoolReduceOps("i32x4_anytrue", [0, 0, 0, 0x10], true);
  58. testBoolReduceOps("i32x4_alltrue", [0, 0, 0, 0], false);
  59. testBoolReduceOps("i32x4_alltrue", [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0], false);
  60. testBoolReduceOps("i32x4_alltrue", [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF], true);
  61. testBoolReduceOps("i32x4_alltrue", [0x10, 0x4, 0x8, 0x1], true);
  62. testBoolReduceOps("i32x4_alltrue", [0x80000000, 0x40000000, 0x20000000, 0x10000000], true);
  63. testBoolReduceOps("i32x4_alltrue", [0x80000000, 0x40000000, 0x20000000, 0], false);
  64. testBoolReduceOps("i16x8_anytrue", [0, 0, 0, 0, 0, 0, 0, 0], false);
  65. testBoolReduceOps("i16x8_anytrue", [0, 0, 0, 0, 0, 0, 0xFFFF, 0], true);
  66. testBoolReduceOps("i16x8_anytrue", [0, 0, 0xFFFF, 0, 0, 0, 0, 0], true);
  67. testBoolReduceOps("i16x8_anytrue", [0, 0, 1, 0, 0, 0, 0, 0], true);
  68. testBoolReduceOps("i16x8_anytrue", [0, 0, 1, 0, 0, 0, 0x10, 0], true);
  69. testBoolReduceOps("i16x8_anytrue", [0, 0, 0, 0, 0, 0, 0, 0x800], true);
  70. testBoolReduceOps("i16x8_alltrue", [0, 0, 0, 0, 0, 0, 0, 0], false);
  71. testBoolReduceOps("i16x8_alltrue", [0, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF], false);
  72. testBoolReduceOps("i16x8_alltrue", [0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF], true);
  73. testBoolReduceOps("i16x8_alltrue", [0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF], true);
  74. testBoolReduceOps("i16x8_alltrue", [0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100], true);
  75. testBoolReduceOps("i16x8_alltrue", [0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1], true);
  76. testBoolReduceOps("i16x8_alltrue", [0x80, 0x40, 0x20, 0x10, 0x8, 0, 0x2, 0x1], false);
  77. testBoolReduceOps("i8x16_anytrue", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], false);
  78. testBoolReduceOps("i8x16_anytrue", [0, 0, 0, 0, 0, 0, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0], true);
  79. testBoolReduceOps("i8x16_anytrue", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0, 0], true);
  80. testBoolReduceOps("i8x16_anytrue", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0], true);
  81. testBoolReduceOps("i8x16_anytrue", [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], true);
  82. testBoolReduceOps("i8x16_alltrue", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], false);
  83. testBoolReduceOps("i8x16_alltrue", [0xFF, 0xFF, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], false);
  84. testBoolReduceOps("i8x16_alltrue", [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], true);
  85. testBoolReduceOps("i8x16_alltrue", [0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 3, 5, 7, 9, 11, 87, 42, 1], true);
  86. testBoolReduceOps("i8x16_alltrue", [0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0, 0x1, 3, 5, 7, 9, 11, 87, 42, 1], false);
  87. if (passed) {
  88. print("Passed");
  89. }