SimdJsHelpers.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function sameValue(x, y) {
  6. if (x == y)
  7. return x != 0 || y != 0 || (1/x == 1/y); // 0.0 != -0.0
  8. return x != x && y != y; // NaN == NaN
  9. }
  10. function equal(v, ev) {
  11. var eps = 0.000001;
  12. if (sameValue(ev, v))
  13. return true;
  14. else if ((ev == 0.0 || v == 0.0) && Math.abs(v - ev) <= eps) // -0.0 covered here
  15. return true;
  16. else if (Math.abs(v - ev) / ev <= eps)
  17. return true;
  18. else
  19. return false;
  20. }
  21. function equalSimd(values, simdValue, type, msg)
  22. { var ok = true;
  23. if (type == SIMD.Float32x4 || type === SIMD.Int32x4)
  24. length = 4;
  25. if (Array.isArray(values))
  26. {
  27. for ( var i = 0; i < length; i ++)
  28. {
  29. if(!equal(values[i],type.extractLane(simdValue, i)))
  30. {
  31. ok = false;
  32. }
  33. }
  34. if (ok)
  35. return;
  36. else
  37. {
  38. WScript.Echo(">> Fail!");
  39. if (msg !== undefined)
  40. {
  41. WScript.Echo(msg);
  42. }
  43. printSimd(simdValue, type);
  44. }
  45. }
  46. else
  47. {
  48. type.check(values);
  49. for ( var i = 0; i < length; i ++)
  50. {
  51. if(!equal(type.extractLane(values, i),type.extractLane(simdValue, i)))
  52. {
  53. ok = false;
  54. }
  55. }
  56. if (ok)
  57. return;
  58. else
  59. {
  60. WScript.Echo(">> Fail!");
  61. if (msg !== undefined)
  62. {
  63. WScript.Echo(msg);
  64. }
  65. printSimd(simdValue, type);
  66. }
  67. }
  68. }
  69. function printSimd(simdValue, type)
  70. {
  71. var length;
  72. var vals = "";
  73. if (type === SIMD.Float32x4 || type === SIMD.Int32x4)
  74. {
  75. length = 4;
  76. }
  77. for (var i = 0; i < length; i ++)
  78. {
  79. vals += type.extractLane(simdValue,i);
  80. if (i < 3)
  81. vals += ", "
  82. }
  83. WScript.Echo(type.toString() + "(" + vals + ")");
  84. }