truncConvTests.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 { message : `Expected ${expected}, received ${actual}`};
  10. }
  11. }
  12. const INITIAL_SIZE = 1;
  13. const memObj = new WebAssembly.Memory({initial:INITIAL_SIZE});
  14. const arrays = {
  15. "i32x4" : new Int32Array (memObj.buffer),
  16. "f32x4" : new Float32Array (memObj.buffer)
  17. };
  18. const module = new WebAssembly.Module(readbuffer('truncconv.wasm'));
  19. const instance = new WebAssembly.Instance(module, { "dummy" : { "memory" : memObj } }).exports;
  20. let testTruncConvTests = function (funcname, args1, resultArr) {
  21. const len = args1.length;
  22. const inputIndex = conversionType(funcname.split("_")[0]);
  23. const outputIndex = funcname.split("_")[0];
  24. const inputArr = arrays[inputIndex];
  25. const outputArr = arrays[outputIndex];
  26. moveArgsIntoArray(args1, 0, inputArr);
  27. instance[funcname]();
  28. moveArgsIntoArray(resultArr, len, outputArr); //should come after instance[funcname]() for checkThrows to work
  29. for (let i = 0; i < len; i++) {
  30. assertEquals(outputArr[i], outputArr[i + len]);
  31. }
  32. return true; //otherwise will throw an exception
  33. }
  34. let checkInternal = function(expected, ...args) {
  35. var result;
  36. try {
  37. result = testTruncConvTests(...args);
  38. }
  39. catch (e) {
  40. result = e.message.replace("SIMD.Int32x4.FromFloat32x4: ", "");
  41. }
  42. if(result != expected)
  43. {
  44. passed = false;
  45. print(`testTruncConvTests(${[...args]}) produced ${result}, expected ${expected}`);
  46. }
  47. }
  48. let checkThrows = function(expected, ...args) {
  49. checkInternal(expected, ...args);
  50. }
  51. let check = function (...args) {
  52. checkInternal(true, ...args);
  53. }
  54. function moveArgsIntoArray(args, offset, arr) {
  55. for (let i = 0; i < args.length; i++) {
  56. arr[offset + i] = args[i];
  57. }
  58. }
  59. let conversionType = function (type) {
  60. return type == "i32x4" ? "f32x4" : "i32x4";
  61. }
  62. check("i32x4_trunc_s", [2147483520.0, -1, 1.25, -0.25], [2147483520, -1, 1, 0]);
  63. checkThrows("argument out of range", "i32x4_trunc_s", [2147483520.0, -1, Number.NaN, -0.25]);
  64. checkThrows("argument out of range", "i32x4_trunc_s", [2147483520.0, 2147483647.0, 1.25, -0.25]);
  65. checkThrows("argument out of range", "i32x4_trunc_s", [-4294967040.0, -1, 1.25, -0.25]);
  66. check("i32x4_trunc_u", [4294967040.0, 2147483520.0, 1.25, 0.25], [4294967040, 2147483520, 1, 0]);
  67. checkThrows("argument out of range", "i32x4_trunc_u", [4294967040.0, 2147483520.0, Number.NaN, 0.25]);
  68. checkThrows("argument out of range", "i32x4_trunc_u", [4294967040.0, 4294967296.0, 1.25, 0.25]);
  69. checkThrows("argument out of range", "i32x4_trunc_u", [4294967040.0, 2147483520.0, 1.25, -1]);
  70. check("f32x4_convert_s", [2147483647, -2147483647, 0, 65535], [2.14748365e+09, -2.14748365e+09, 0, 65535]);
  71. check("f32x4_convert_s", [101, 1003, -10007, -65535], [101, 1003, -10007, -65535]);
  72. check("f32x4_convert_u", [2147483647, 4294967295, 0, 65535], [2.14748365e+09, 4.29496730e+09, 0, 65535]);
  73. check("f32x4_convert_u", [32767, 9999, 100003, 1], [32767, 9999, 100003, 1]);
  74. if (passed) {
  75. print("Passed");
  76. }