SimdJsHelpers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var DEBUG = false;
  6. function sameValue(x, y) {
  7. if (x == y)
  8. {
  9. return x != 0 || y != 0 || (1/x == 1/y); // 0.0 != -0.0
  10. }
  11. return (x != x) && (y != y); // NaN == NaN
  12. }
  13. function equal(ev, v) {
  14. var eps = 0.000001;
  15. if (sameValue(ev, v))
  16. {
  17. if(DEBUG) WScript.Echo("same value");
  18. return true;
  19. }
  20. else if ((ev == 0.0 || v == 0.0) && Math.abs(v - ev) <= eps) // -0.0 covered here
  21. {
  22. if(DEBUG) WScript.Echo("Float 0.0 ");
  23. return true;
  24. }
  25. else if (Math.abs(v - ev) / Math.abs(ev) <= eps)
  26. {
  27. if(DEBUG) WScript.Echo("Float values within eps");
  28. return true;
  29. }
  30. else
  31. {
  32. WScript.Echo(">> Fail!");
  33. WScript.Echo("Expected "+ev+" Found "+v);
  34. }
  35. }
  36. function equalSimd(values, simdValue, type, msg)
  37. { var ok = true;
  38. if (type == SIMD.Float32x4 || type === SIMD.Int32x4)
  39. length = 4;
  40. if (Array.isArray(values))
  41. {
  42. for ( var i = 0; i < length; i ++)
  43. {
  44. if(!equal(values[i],type.extractLane(simdValue, i)))
  45. {
  46. ok = false;
  47. }
  48. }
  49. if (ok)
  50. return;
  51. else
  52. {
  53. WScript.Echo(">> Fail!");
  54. if (msg !== undefined)
  55. {
  56. WScript.Echo(msg);
  57. }
  58. printSimd(simdValue, type);
  59. }
  60. }
  61. else
  62. {
  63. type.check(values);
  64. for ( var i = 0; i < length; i ++)
  65. {
  66. if(!equal(type.extractLane(values, i),type.extractLane(simdValue, i)))
  67. {
  68. ok = false;
  69. }
  70. }
  71. if (ok)
  72. return;
  73. else
  74. {
  75. WScript.Echo(">> Fail!");
  76. if (msg !== undefined)
  77. {
  78. WScript.Echo(msg);
  79. }
  80. printSimd(simdValue, type);
  81. }
  82. }
  83. }
  84. function printSimd(simdValue, type)
  85. {
  86. var length;
  87. var vals = "";
  88. if (type === SIMD.Float32x4 || type === SIMD.Int32x4)
  89. {
  90. length = 4;
  91. }
  92. for (var i = 0; i < length; i ++)
  93. {
  94. vals += type.extractLane(simdValue,i);
  95. if (i < 3)
  96. vals += ", "
  97. }
  98. WScript.Echo(type.toString() + "(" + vals + ")");
  99. }