testSwizzle.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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 i4extractLane = i4.extractLane;
  11. var i4fromFloat64x2 = i4.fromFloat64x2;
  12. var i4fromFloat64x2Bits = i4.fromFloat64x2Bits;
  13. var i4fromFloat32x4 = i4.fromFloat32x4;
  14. var i4fromFloat32x4Bits = i4.fromFloat32x4Bits;
  15. //var i4abs = i4.abs;
  16. var i4neg = i4.neg;
  17. var i4add = i4.add;
  18. var i4sub = i4.sub;
  19. var i4mul = i4.mul;
  20. var i4swizzle = i4.swizzle;
  21. var i4shuffle = i4.shuffle;
  22. var i4lessThan = i4.lessThan;
  23. var i4equal = i4.equal;
  24. var i4greaterThan = i4.greaterThan;
  25. var i4select = i4.select;
  26. var i4and = i4.and;
  27. var i4or = i4.or;
  28. var i4xor = i4.xor;
  29. var i4not = i4.not;
  30. //var i4shiftLeftByScalar = i4.shiftLeftByScalar;
  31. //var i4shiftRightByScalar = i4.shiftRightByScalar;
  32. //var i4shiftRightArithmeticByScalar = i4.shiftRightArithmeticByScalar;
  33. var f4 = stdlib.SIMD.Float32x4;
  34. var f4check = f4.check;
  35. var f4fromFloat64x2 = f4.fromFloat64x2;
  36. var f4fromFloat64x2Bits = f4.fromFloat64x2Bits;
  37. var f4fromInt32x4 = f4.fromInt32x4;
  38. var f4fromInt32x4Bits = f4.fromInt32x4Bits;
  39. var f4abs = f4.abs;
  40. var f4neg = f4.neg;
  41. var f4add = f4.add;
  42. var f4sub = f4.sub;
  43. var f4mul = f4.mul;
  44. var f4div = f4.div;
  45. var f4clamp = f4.clamp;
  46. var f4min = f4.min;
  47. var f4max = f4.max;
  48. var f4sqrt = f4.sqrt;
  49. var f4swizzle = f4.swizzle;
  50. var f4shuffle = f4.shuffle;
  51. var f4lessThan = f4.lessThan;
  52. var f4lessThanOrEqual = f4.lessThanOrEqual;
  53. var f4equal = f4.equal;
  54. var f4notEqual = f4.notEqual;
  55. var f4greaterThan = f4.greaterThan;
  56. var f4greaterThanOrEqual = f4.greaterThanOrEqual;
  57. var f4select = f4.select;
  58. var f4and = f4.and;
  59. var f4or = f4.or;
  60. var f4xor = f4.xor;
  61. var f4not = f4.not;
  62. var fround = stdlib.Math.fround;
  63. var globImportF4 = f4check(imports.g1); // global var import
  64. var globImportI4 = i4check(imports.g2); // global var import
  65. var g1 = f4(1.0, 2.0, 3.0, -0.0); // global var initialized
  66. var g2 = f4(-5.3, -0.0, 7.332, 8.0); // global var initialized
  67. var g3 = i4(1, 2, 3, 4); // global var initialized
  68. var g4 = i4(5, 6, 7, 8); // global var initialized
  69. var gval = 1234;
  70. var gval2 = 1234.0;
  71. var f4splat = f4.splat;
  72. var sqrt = stdlib.Math.sqrt;
  73. var pow = stdlib.Math.pow;
  74. var loopCOUNT = 3;
  75. function swizzle1() {
  76. var xyxy = i4(0, 0, 0, 0);
  77. var zwzw = i4(0, 0, 0, 0);
  78. var xxxx = i4(0, 0, 0, 0);
  79. var xxyy = i4(0, 0, 0, 0);
  80. var x = 0, y = 0, z = 0, w = 0;
  81. var loopIndex = 0;
  82. while ((loopIndex | 0) < (loopCOUNT | 0)) {
  83. xyxy = i4swizzle(g3, 0, 1, 0, 1);
  84. zwzw = i4swizzle(g3, 2, 3, 2, 3);
  85. xxxx = i4swizzle(g3, 0, 0, 0, 0);
  86. xxyy = i4swizzle(g3, 0, 0, 1, 1);
  87. loopIndex = (loopIndex + 1) | 0;
  88. }
  89. x = (((i4extractLane(xyxy, 0) + i4extractLane(xyxy, 0)) | 0) + ((i4extractLane(xyxy, 1) + i4extractLane(xyxy, 1)) | 0) + ((i4extractLane(xyxy, 2) + i4extractLane(xyxy, 2)) | 0) + ((i4extractLane(xyxy, 3) + i4extractLane(xyxy, 3)) | 0)) | 0;
  90. y = (((i4extractLane(zwzw, 0) + i4extractLane(zwzw, 0)) | 0) + ((i4extractLane(zwzw, 1) + i4extractLane(zwzw, 1)) | 0) + ((i4extractLane(zwzw, 2) + i4extractLane(zwzw, 2)) | 0) + ((i4extractLane(zwzw, 3) + i4extractLane(zwzw, 3)) | 0)) | 0;
  91. z = (((i4extractLane(xxxx, 0) + i4extractLane(xxxx, 0)) | 0) + ((i4extractLane(xxxx, 1) + i4extractLane(xxxx, 1)) | 0) + ((i4extractLane(xxxx, 2) + i4extractLane(xxxx, 2)) | 0) + ((i4extractLane(xxxx, 3) + i4extractLane(xxxx, 3)) | 0)) | 0;
  92. w = (((i4extractLane(xxyy, 0) + i4extractLane(xxyy, 0)) | 0) + ((i4extractLane(xxyy, 1) + i4extractLane(xxyy, 1)) | 0) + ((i4extractLane(xxyy, 2) + i4extractLane(xxyy, 2)) | 0) + ((i4extractLane(xxyy, 3) + i4extractLane(xxyy, 3)) | 0)) | 0;
  93. return i4check(i4(x, y, z, w));
  94. }
  95. function swizzle2() {
  96. var xyxy = i4(0, 0, 0, 0);
  97. var zwzw = i4(0, 0, 0, 0);
  98. var xxxx = i4(0, 0, 0, 0);
  99. var xxyy = i4(0, 0, 0, 0);
  100. var v1 = i4(122, 0, 334, -9500);
  101. var v2 = i4(102, 3313, 1, 233);
  102. var x = 0, y = 0, z = 0, w = 0;
  103. var loopIndex = 0;
  104. for (loopIndex = 0; (loopIndex | 0) < (loopCOUNT | 0) ; loopIndex = (loopIndex + 1) | 0) {
  105. xyxy = i4swizzle(i4add(v1, v2), 0, 1, 0, 1);
  106. zwzw = i4swizzle(i4mul(v1, v2), 2, 3, 2, 3);
  107. xxxx = i4swizzle(i4sub(v1, v2), 0, 0, 0, 0);
  108. xxyy = i4swizzle(v2, 0, 0, 1, 1);
  109. }
  110. x = (((i4extractLane(xyxy, 0) + i4extractLane(xyxy, 0)) | 0) + ((i4extractLane(xyxy, 1) + i4extractLane(xyxy, 1)) | 0) + ((i4extractLane(xyxy, 2) + i4extractLane(xyxy, 2)) | 0) + ((i4extractLane(xyxy, 3) + i4extractLane(xyxy, 3)) | 0)) | 0;
  111. y = (((i4extractLane(zwzw, 0) + i4extractLane(zwzw, 0)) | 0) + ((i4extractLane(zwzw, 1) + i4extractLane(zwzw, 1)) | 0) + ((i4extractLane(zwzw, 2) + i4extractLane(zwzw, 2)) | 0) + ((i4extractLane(zwzw, 3) + i4extractLane(zwzw, 3)) | 0)) | 0;
  112. z = (((i4extractLane(xxxx, 0) + i4extractLane(xxxx, 0)) | 0) + ((i4extractLane(xxxx, 1) + i4extractLane(xxxx, 1)) | 0) + ((i4extractLane(xxxx, 2) + i4extractLane(xxxx, 2)) | 0) + ((i4extractLane(xxxx, 3) + i4extractLane(xxxx, 3)) | 0)) | 0;
  113. w = (((i4extractLane(xxyy, 0) + i4extractLane(xxyy, 0)) | 0) + ((i4extractLane(xxyy, 1) + i4extractLane(xxyy, 1)) | 0) + ((i4extractLane(xxyy, 2) + i4extractLane(xxyy, 2)) | 0) + ((i4extractLane(xxyy, 3) + i4extractLane(xxyy, 3)) | 0)) | 0;
  114. return i4check(i4(x, y, z, w));
  115. }
  116. function swizzle3() {
  117. var xyxy = i4(0, 0, 0, 0);
  118. var zwzw = i4(0, 0, 0, 0);
  119. var xxxx = i4(0, 0, 0, 0);
  120. var xxyy = i4(0, 0, 0, 0);
  121. var v1 = i4(122, 0, 334, -9500);
  122. var x = 0, y = 0, z = 0, w = 0;
  123. var loopIndex = 0;
  124. loopIndex = loopCOUNT | 0;
  125. do {
  126. xyxy = i4swizzle(i4add(v1, g3), 0, 1, 0, 1);
  127. zwzw = i4swizzle(i4mul(v1, g3), 2, 3, 2, 3);
  128. xxxx = i4swizzle(i4sub(v1, g3), 0, 0, 0, 0);
  129. xxyy = i4swizzle(g4, 0, 0, 1, 1);
  130. loopIndex = (loopIndex - 1) | 0;
  131. }
  132. while ((loopIndex | 0) > 0);
  133. x = (((i4extractLane(xyxy, 0) + i4extractLane(xyxy, 0)) | 0) + ((i4extractLane(xyxy, 1) + i4extractLane(xyxy, 1)) | 0) + ((i4extractLane(xyxy, 2) + i4extractLane(xyxy, 2)) | 0) + ((i4extractLane(xyxy, 3) + i4extractLane(xyxy, 3)) | 0)) | 0;
  134. y = (((i4extractLane(zwzw, 0) + i4extractLane(zwzw, 0)) | 0) + ((i4extractLane(zwzw, 1) + i4extractLane(zwzw, 1)) | 0) + ((i4extractLane(zwzw, 2) + i4extractLane(zwzw, 2)) | 0) + ((i4extractLane(zwzw, 3) + i4extractLane(zwzw, 3)) | 0)) | 0;
  135. z = (((i4extractLane(xxxx, 0) + i4extractLane(xxxx, 0)) | 0) + ((i4extractLane(xxxx, 1) + i4extractLane(xxxx, 1)) | 0) + ((i4extractLane(xxxx, 2) + i4extractLane(xxxx, 2)) | 0) + ((i4extractLane(xxxx, 3) + i4extractLane(xxxx, 3)) | 0)) | 0;
  136. w = (((i4extractLane(xxyy, 0) + i4extractLane(xxyy, 0)) | 0) + ((i4extractLane(xxyy, 1) + i4extractLane(xxyy, 1)) | 0) + ((i4extractLane(xxyy, 2) + i4extractLane(xxyy, 2)) | 0) + ((i4extractLane(xxyy, 3) + i4extractLane(xxyy, 3)) | 0)) | 0;
  137. return i4check(i4(x, y, z, w));
  138. }
  139. return { func1: swizzle1 , func2: swizzle2, func3: swizzle3 };
  140. }
  141. var m = asmModule(this, { g1: SIMD.Float32x4(9, 9, 9, 9), g2: SIMD.Int32x4(1, 2, 3, 4) });
  142. var ret1 = m.func1();
  143. var ret2 = m.func2();
  144. var ret3 = m.func3();
  145. equalSimd([12, 28, 8, 12], ret1, SIMD.Int32x4, "");
  146. equalSimd([14148, -8852664, 160, 13660], ret2, SIMD.Int32x4, "");
  147. equalSimd([500, -147992, 968, 44], ret3, SIMD.Int32x4, "");
  148. print("PASS");