testShuffle.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. this.WScript.LoadScriptFile("..\\UnitTestFramework\\SimdJsHelpers.js");
  6. function asmModule(stdlib, imports) {
  7. "use asm";
  8. var i4 = stdlib.SIMD.Int32x4;
  9. var i4check = i4.check;
  10. var i4splat = i4.splat;
  11. var i4fromFloat32x4 = i4.fromFloat32x4;
  12. var i4fromFloat32x4Bits = i4.fromFloat32x4Bits;
  13. //var i4abs = i4.abs;
  14. var i4neg = i4.neg;
  15. var i4add = i4.add;
  16. var i4sub = i4.sub;
  17. var i4mul = i4.mul;
  18. var i4swizzle = i4.swizzle;
  19. var i4shuffle = i4.shuffle;
  20. var i4lessThan = i4.lessThan;
  21. var i4equal = i4.equal;
  22. var i4greaterThan = i4.greaterThan;
  23. var i4select = i4.select;
  24. var i4and = i4.and;
  25. var i4or = i4.or;
  26. var i4xor = i4.xor;
  27. var i4not = i4.not;
  28. //var i4shiftLeftByScalar = i4.shiftLeftByScalar;
  29. //var i4shiftRightByScalar = i4.shiftRightByScalar;
  30. //var i4shiftRightArithmeticByScalar = i4.shiftRightArithmeticByScalar;
  31. var f4 = stdlib.SIMD.Float32x4;
  32. var f4check = f4.check;
  33. var f4splat = f4.splat;
  34. var f4fromInt32x4 = f4.fromInt32x4;
  35. var f4fromInt32x4Bits = f4.fromInt32x4Bits;
  36. var f4abs = f4.abs;
  37. var f4neg = f4.neg;
  38. var f4add = f4.add;
  39. var f4sub = f4.sub;
  40. var f4mul = f4.mul;
  41. var f4div = f4.div;
  42. var f4min = f4.min;
  43. var f4max = f4.max;
  44. var f4sqrt = f4.sqrt;
  45. var f4swizzle = f4.swizzle;
  46. var f4shuffle = f4.shuffle;
  47. var f4lessThan = f4.lessThan;
  48. var f4lessThanOrEqual = f4.lessThanOrEqual;
  49. var f4equal = f4.equal;
  50. var f4notEqual = f4.notEqual;
  51. var f4greaterThan = f4.greaterThan;
  52. var f4greaterThanOrEqual = f4.greaterThanOrEqual;
  53. var f4select = f4.select;
  54. var f4extractLane = f4.extractLane;
  55. var f4replaceLane = f4.replaceLane;
  56. var fround = stdlib.Math.fround;
  57. var globImportF4 = f4check(imports.g1); // global var import
  58. var globImportI4 = i4check(imports.g2); // global var import
  59. var g1 = f4(1.0, 2.0, 3.0, -0.0); // global var initialized
  60. var g2 = f4(-5.3, -0.0, 7.332, 8.0); // global var initialized
  61. var g3 = i4(1, 2, 3, 4); // global var initialized
  62. var g4 = i4(5, 6, 7, 8); // global var initialized
  63. var gval = 1234;
  64. var gval2 = 1234.0;
  65. var sqrt = stdlib.Math.sqrt;
  66. var pow = stdlib.Math.pow;
  67. var loopCOUNT = 3;
  68. function shuffle1() {
  69. var xyxy = f4(0.0, 0.0, 0.0, 0.0);
  70. var zwzw = f4(0.0, 0.0, 0.0, 0.0);
  71. var xxxx = f4(0.0, 0.0, 0.0, 0.0);
  72. var xxyy = f4(0.0, 0.0, 0.0, 0.0);
  73. var x = 0.0, y = 0.0, z = 0.0, w = 0.0;
  74. var loopIndex = 0;
  75. while ((loopIndex | 0) < (loopCOUNT | 0)) {
  76. xyxy = f4shuffle(g1, g2, 0, 1, 4, 5);
  77. zwzw = f4shuffle(g1, g2, 2, 3, 6, 7);
  78. xxxx = f4shuffle(g1, g2, 0, 0, 4, 4);
  79. xxyy = f4shuffle(g1, g2, 0, 0, 5, 5);
  80. loopIndex = (loopIndex + 1) | 0;
  81. }
  82. x = +sqrt((+fround(f4extractLane(xyxy, 0)) * +fround(f4extractLane(xyxy, 0)))
  83. + (+fround(f4extractLane(xyxy, 1)) * +fround(f4extractLane(xyxy, 1)))
  84. + (+fround(f4extractLane(xyxy, 2)) * +fround(f4extractLane(xyxy, 2)))
  85. + (+fround(f4extractLane(xyxy, 3)) * +fround(f4extractLane(xyxy, 3))));
  86. y = +sqrt((+fround(f4extractLane(zwzw, 0)) * +fround(f4extractLane(zwzw, 0)))
  87. + (+fround(f4extractLane(zwzw, 1)) * +fround(f4extractLane(zwzw, 1)))
  88. + (+fround(f4extractLane(zwzw, 2)) * +fround(f4extractLane(zwzw, 2)))
  89. + (+fround(f4extractLane(zwzw, 3)) * +fround(f4extractLane(zwzw, 3))));
  90. z = +sqrt((+fround(f4extractLane(xxxx, 0)) * +fround(f4extractLane(xxxx, 0)))
  91. + (+fround(f4extractLane(xxxx, 1)) * +fround(f4extractLane(xxxx, 1)))
  92. + (+fround(f4extractLane(xxxx, 2)) * +fround(f4extractLane(xxxx, 2)))
  93. + (+fround(f4extractLane(xxxx, 3)) * +fround(f4extractLane(xxxx, 3))));
  94. w = +sqrt((+fround(f4extractLane(xxyy, 0)) * +fround(f4extractLane(xxyy, 0)))
  95. + (+fround(f4extractLane(xxyy, 1)) * +fround(f4extractLane(xxyy, 1)))
  96. + (+fround(f4extractLane(xxyy, 2)) * +fround(f4extractLane(xxyy, 2)))
  97. + (+fround(f4extractLane(xxyy, 3)) * +fround(f4extractLane(xxyy, 3))));
  98. return f4check(f4(fround(x), fround(y), fround(z), fround(w)));
  99. }
  100. function shuffle2() {
  101. var xyxy = f4(0.0, 0.0, 0.0, 0.0);
  102. var zwzw = f4(0.0, 0.0, 0.0, 0.0);
  103. var xxxx = f4(0.0, 0.0, 0.0, 0.0);
  104. var xxyy = f4(0.0, 0.0, 0.0, 0.0);
  105. var v1 = f4(122.0, -0.0, 334.0, -9500.231);
  106. var v2 = f4(102.0, -33.13, -1.0, 233.000001);
  107. var x = 0.0, y = 0.0, z = 0.0, w = 0.0;
  108. var loopIndex = 0;
  109. for (loopIndex = 0; (loopIndex | 0) < (loopCOUNT | 0) ; loopIndex = (loopIndex + 1) | 0) {
  110. xyxy = f4shuffle(v1, v2, 0, 1, 4, 5);
  111. zwzw = f4shuffle(v1, v2, 2, 3, 6, 7);
  112. xxxx = f4shuffle(v1, v2, 0, 0, 4, 4);
  113. xxyy = f4shuffle(v1, v2, 0, 0, 5, 5);
  114. }
  115. x = +sqrt((+fround(f4extractLane(xyxy, 0)) * +fround(f4extractLane(xyxy, 0)))
  116. + (+fround(f4extractLane(xyxy, 1)) * +fround(f4extractLane(xyxy, 1)))
  117. + (+fround(f4extractLane(xyxy, 2)) * +fround(f4extractLane(xyxy, 2)))
  118. + (+fround(f4extractLane(xyxy, 3)) * +fround(f4extractLane(xyxy, 3))));
  119. y = +sqrt((+fround(f4extractLane(zwzw, 0)) * +fround(f4extractLane(zwzw, 0)))
  120. + (+fround(f4extractLane(zwzw, 1)) * +fround(f4extractLane(zwzw, 1)))
  121. + (+fround(f4extractLane(zwzw, 2)) * +fround(f4extractLane(zwzw, 2)))
  122. + (+fround(f4extractLane(zwzw, 3)) * +fround(f4extractLane(zwzw, 3))));
  123. z = +sqrt((+fround(f4extractLane(xxxx, 0)) * +fround(f4extractLane(xxxx, 0)))
  124. + (+fround(f4extractLane(xxxx, 1)) * +fround(f4extractLane(xxxx, 1)))
  125. + (+fround(f4extractLane(xxxx, 2)) * +fround(f4extractLane(xxxx, 2)))
  126. + (+fround(f4extractLane(xxxx, 3)) * +fround(f4extractLane(xxxx, 3))));
  127. w = +sqrt((+fround(f4extractLane(xxyy, 0)) * +fround(f4extractLane(xxyy, 0)))
  128. + (+fround(f4extractLane(xxyy, 1)) * +fround(f4extractLane(xxyy, 1)))
  129. + (+fround(f4extractLane(xxyy, 2)) * +fround(f4extractLane(xxyy, 2)))
  130. + (+fround(f4extractLane(xxyy, 3)) * +fround(f4extractLane(xxyy, 3))));
  131. return f4check(f4(fround(x), fround(y), fround(z), fround(w)));
  132. }
  133. function shuffle3() {
  134. var xyxy = f4(0.0, 0.0, 0.0, 0.0);
  135. var zwzw = f4(0.0, 0.0, 0.0, 0.0);
  136. var xxxx = f4(0.0, 0.0, 0.0, 0.0);
  137. var xxyy = f4(0.0, 0.0, 0.0, 0.0);
  138. var x = 0.0, y = 0.0, z = 0.0, w = 0.0;
  139. var v1 = f4(122.0, -0.0, 334.0, -9500.231);
  140. var loopIndex = 0;
  141. loopIndex = loopCOUNT | 0;
  142. do {
  143. xyxy = f4shuffle(f4add(v1, g2), v1, 0, 1, 4, 5);
  144. zwzw = f4shuffle(f4mul(v1, g2), g1, 2, 3, 6, 7);
  145. xxxx = f4shuffle(f4sub(v1, g2), v1, 0, 0, 4, 4);
  146. xxyy = f4shuffle(g2, v1, 0, 0, 5, 5);
  147. loopIndex = (loopIndex - 1) | 0;
  148. }
  149. while ((loopIndex | 0) > 0);
  150. x = +sqrt((+fround(f4extractLane(xyxy, 0)) * +fround(f4extractLane(xyxy, 0)))
  151. + (+fround(f4extractLane(xyxy, 1)) * +fround(f4extractLane(xyxy, 1)))
  152. + (+fround(f4extractLane(xyxy, 2)) * +fround(f4extractLane(xyxy, 2)))
  153. + (+fround(f4extractLane(xyxy, 3)) * +fround(f4extractLane(xyxy, 3))));
  154. y = +sqrt((+fround(f4extractLane(zwzw, 0)) * +fround(f4extractLane(zwzw, 0)))
  155. + (+fround(f4extractLane(zwzw, 1)) * +fround(f4extractLane(zwzw, 1)))
  156. + (+fround(f4extractLane(zwzw, 2)) * +fround(f4extractLane(zwzw, 2)))
  157. + (+fround(f4extractLane(zwzw, 3)) * +fround(f4extractLane(zwzw, 3))));
  158. z = +sqrt((+fround(f4extractLane(xxxx, 0)) * +fround(f4extractLane(xxxx, 0)))
  159. + (+fround(f4extractLane(xxxx, 1)) * +fround(f4extractLane(xxxx, 1)))
  160. + (+fround(f4extractLane(xxxx, 2)) * +fround(f4extractLane(xxxx, 2)))
  161. + (+fround(f4extractLane(xxxx, 3)) * +fround(f4extractLane(xxxx, 3))));
  162. w = +sqrt((+fround(f4extractLane(xxyy, 0)) * +fround(f4extractLane(xxyy, 0)))
  163. + (+fround(f4extractLane(xxyy, 1)) * +fround(f4extractLane(xxyy, 1)))
  164. + (+fround(f4extractLane(xxyy, 2)) * +fround(f4extractLane(xxyy, 2)))
  165. + (+fround(f4extractLane(xxyy, 3)) * +fround(f4extractLane(xxyy, 3))));
  166. return f4check(f4(fround(x), fround(y), fround(z), fround(w)));
  167. }
  168. return { func1: shuffle1, func2: shuffle2, func3: shuffle3 };
  169. }
  170. var m = asmModule(this, { g1: SIMD.Float32x4(9, 9, 9, 9), g2: SIMD.Int32x4(1, 2, 3, 4) });
  171. var ret1 = m.func1();
  172. var ret2 = m.func2();
  173. var ret3 = m.func3();
  174. equalSimd([5.7523908615112305, 11.258695602416992, 7.627581596374512, 1.4142135381698608], ret1, SIMD.Float32x4, "Shuffle");
  175. equalSimd([162.4364471435547, 9508.9560546875, 224.89108276367187, 178.78253173828125], ret2, SIMD.Float32x4, "Shuffle");
  176. equalSimd([168.82798767089844, 76041.296875, 249.35633850097656, 7.49533224105835], ret3, SIMD.Float32x4, "Shuffle");
  177. print("PASS");