SimdJsHelpers.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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. return false;
  35. }
  36. }
  37. function equalSimd(values, simdValue, type, msg)
  38. { var ok = true;
  39. length = getLength(type);
  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. length = getLength(type);
  89. for (var i = 0; i < length; i ++)
  90. {
  91. vals += type.extractLane(simdValue,i);
  92. if (i < length - 1)
  93. vals += ", "
  94. }
  95. WScript.Echo(type.toString() + "(" + vals + ")");
  96. }
  97. function printSimdBaseline(simdValue, typeName, varName, msg)
  98. {
  99. var length;
  100. var vals = "";
  101. if (typeName === "SIMD.Float32x4")
  102. {
  103. type = SIMD.Float32x4;
  104. }
  105. else if (typeName === "SIMD.Int32x4")
  106. {
  107. type = SIMD.Int32x4;
  108. }
  109. else if (typeName === "SIMD.Int16x8")
  110. {
  111. type = SIMD.Int16x8;
  112. }
  113. else if (typeName === "SIMD.Int8x16")
  114. {
  115. type = SIMD.Int8x16;
  116. }
  117. else if (typeName === "SIMD.Uint32x4")
  118. {
  119. type = SIMD.Uint32x4;
  120. }
  121. else if (typeName === "SIMD.Uint16x8")
  122. {
  123. type = SIMD.Uint16x8;
  124. }
  125. else if (typeName === "SIMD.Uint8x16")
  126. {
  127. type = SIMD.Uint8x16;
  128. }
  129. else if (typeName === "SIMD.Bool32x4")
  130. {
  131. type = SIMD.Bool32x4;
  132. }
  133. else if (typeName === "SIMD.Bool16x8")
  134. {
  135. type = SIMD.Bool16x8;
  136. }
  137. else if (typeName === "SIMD.Bool8x16")
  138. {
  139. type = SIMD.Bool8x16;
  140. }
  141. else
  142. {
  143. throw "Unsupported type";
  144. }
  145. length = getLength(type);
  146. for (var i = 0; i < length; i ++)
  147. {
  148. vals += type.extractLane(simdValue,i);
  149. if (i < length - 1)
  150. vals += ", "
  151. }
  152. print("equalSimd([" + vals + "], " + varName + ", " + typeName + ", \"" + msg + "\")");
  153. }
  154. function getLength(type)
  155. {
  156. var length;
  157. switch (type)
  158. {
  159. case SIMD.Float32x4:
  160. case SIMD.Int32x4:
  161. case SIMD.Uint32x4:
  162. case SIMD.Bool32x4:
  163. length = 4;
  164. break;
  165. case SIMD.Int16x8:
  166. case SIMD.Uint16x8:
  167. case SIMD.Bool16x8:
  168. length = 8;
  169. break;
  170. case SIMD.Int8x16:
  171. case SIMD.Uint8x16:
  172. case SIMD.Bool8x16:
  173. length = 16;
  174. break;
  175. default:
  176. throw "Undefined type";
  177. }
  178. return length;
  179. }