testScale.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, buffer) {
  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 i4load = i4.load;
  29. var i4load1 = i4.load1;
  30. var i4load2 = i4.load2;
  31. var i4load3 = i4.load3;
  32. var i4store = i4.store
  33. var i4store1 = i4.store1;
  34. var i4store2 = i4.store2;
  35. var i4store3 = i4.store3;
  36. //var i4shiftLeftByScalar = i4.shiftLeftByScalar;
  37. //var i4shiftRightByScalar = i4.shiftRightByScalar;
  38. //var i4shiftRightArithmeticByScalar = i4.shiftRightArithmeticByScalar;
  39. var f4 = stdlib.SIMD.Float32x4;
  40. var f4check = f4.check;
  41. var f4splat = f4.splat;
  42. var f4fromInt32x4 = f4.fromInt32x4;
  43. var f4fromInt32x4Bits = f4.fromInt32x4Bits;
  44. var f4abs = f4.abs;
  45. var f4neg = f4.neg;
  46. var f4add = f4.add;
  47. var f4sub = f4.sub;
  48. var f4mul = f4.mul;
  49. var f4div = f4.div;
  50. var f4min = f4.min;
  51. var f4max = f4.max;
  52. var f4sqrt = f4.sqrt;
  53. var f4swizzle = f4.swizzle;
  54. var f4shuffle = f4.shuffle;
  55. var f4lessThan = f4.lessThan;
  56. var f4lessThanOrEqual = f4.lessThanOrEqual;
  57. var f4equal = f4.equal;
  58. var f4notEqual = f4.notEqual;
  59. var f4greaterThan = f4.greaterThan;
  60. var f4greaterThanOrEqual = f4.greaterThanOrEqual;
  61. var f4select = f4.select;
  62. var f4load = f4.load;
  63. var f4load1 = f4.load1;
  64. var f4load2 = f4.load2;
  65. var f4load3 = f4.load3;
  66. var f4store = f4.store;
  67. var f4store1 = f4.store1;
  68. var f4store2 = f4.store2;
  69. var f4store3 = f4.store3;
  70. var fround = stdlib.Math.fround;
  71. var globImportF4 = f4check(imports.g1); // global var import
  72. var globImportI4 = i4check(imports.g2); // global var import
  73. var g1 = f4(-5033.2,-3401.0,665.34,32234.1); // global var initialized
  74. var g2 = i4(1065353216, -1073741824, -1077936128, 1082130432); // global var initialized
  75. var gval = 1234;
  76. var gval2 = 1234.0;
  77. var OFFSET_1 = 10;
  78. var OFFSET_2 = 15;
  79. var loopCOUNT = 10;
  80. var Int8Heap = new stdlib.Int8Array (buffer);
  81. var Uint8Heap = new stdlib.Uint8Array (buffer);
  82. var Int16Heap = new stdlib.Int16Array(buffer);
  83. var Uint16Heap = new stdlib.Uint16Array(buffer);
  84. var Int32Heap = new stdlib.Int32Array(buffer);
  85. var Uint32Heap = new stdlib.Uint32Array(buffer);
  86. var Float32Heap = new stdlib.Float32Array(buffer);
  87. function nestedLoadStoreUint8(idx)
  88. {
  89. idx = idx|0;
  90. idx = idx<<0;
  91. f4store(Uint8Heap, (idx + 5)|0, f4(9.0,8.0,10.1, -22.121));
  92. //return f4load(Uint8Heap, (idx + 5)|0);
  93. f4store(Uint8Heap, (idx + 0)|0, f4load(Uint8Heap, (idx + 5)|0));
  94. return f4load(Uint8Heap, (idx + 0)|0);
  95. }
  96. function scale(fromIdx, toIdx)
  97. {
  98. fromIdx = fromIdx | 0;
  99. toIdx = toIdx | 0;
  100. var i = 0;
  101. var xx = f4(0.0, 0.0, 0.0, 0.0);
  102. //No bound check to enable negative tests.
  103. for (i = fromIdx; (i|0) < (toIdx|0); i = (i + 16) | 0)
  104. {
  105. xx = f4load(Int8Heap, i | 0);
  106. xx = f4mul(xx, f4(2.0, 2.0, 2.0, 2.0));
  107. f4store(Int8Heap, toIdx, xx);
  108. f4store(Int8Heap, (i + 0) | 0, f4load(Int8Heap, (toIdx + 0) | 0));
  109. //f4store(Int8Heap, (i) | 0, f4load(Int8Heap, (toIdx) | 0));
  110. }
  111. }
  112. function scale2(fromIdx, toIdx)
  113. {
  114. fromIdx = fromIdx | 0;
  115. toIdx = toIdx | 0;
  116. var i = 0;
  117. var xx = i4(0, 0, 0, 0);
  118. //No bound check to enable negative tests.
  119. for (i = fromIdx;(i|0) < (toIdx|0); i = (i + 16) | 0)
  120. {
  121. xx = i4load(Int8Heap, i | 0);
  122. xx = i4mul(xx, i4(2, 2, 2, 2));
  123. i4store(Int8Heap, toIdx, xx);
  124. i4store(Int8Heap, (i + 0) | 0, i4load(Int8Heap, (toIdx + 0) | 0));
  125. //f4store(Int8Heap, (i) | 0, f4load(Int8Heap, (toIdx) | 0));
  126. }
  127. }
  128. /*
  129. function scale3(fromIdx, toIdx)
  130. {
  131. fromIdx = fromIdx | 0;
  132. toIdx = toIdx | 0;
  133. var i = 0;
  134. var xx = d2(0.0, 0.0);
  135. //No bound check to enable negative tests.
  136. for (i = fromIdx; (i|0) < (toIdx|0); i = (i + 16) | 0)
  137. {
  138. xx = d2load(Int8Heap, i | 0);
  139. xx = d2mul(xx, d2(2.0, 2.0));
  140. d2store(Int8Heap, toIdx, xx);
  141. d2store(Int8Heap, (i + 0) | 0, d2load(Int8Heap, (toIdx + 0) | 0));
  142. //f4store(Int8Heap, (i) | 0, f4load(Int8Heap, (toIdx) | 0));
  143. }
  144. }
  145. */
  146. return {scale: scale
  147. ,scale2: scale2
  148. /*,scale3, scale3*/
  149. ,nestedLoadStoreUint8:nestedLoadStoreUint8};
  150. }
  151. var buffer = new ArrayBuffer(0x10000); //16mb min 2^12
  152. //Reset or flush the buffer
  153. function initF32(buffer) {
  154. var values = new Float32Array( buffer );
  155. for( var i=0; i < values.length ; ++i ) {
  156. values[i] = i * 10;
  157. }
  158. return values.length;
  159. }
  160. function initI32(buffer) {
  161. var values = new Int32Array( buffer );
  162. for( var i=0; i < values.length ; ++i ) {
  163. values[i] = i * 10;
  164. }
  165. return values.length;
  166. }
  167. function validateBuffer(buffer, count)
  168. {
  169. var f4;
  170. var data = [
  171. SIMD.Float32x4(0.0,20.0,40.0,60.0),
  172. SIMD.Float32x4(80.0,100.0,120.0,140.0),
  173. SIMD.Float32x4(160.0,180.0,200.0,220.0),
  174. SIMD.Float32x4(240.0,260.0,280.0,300.0),
  175. SIMD.Float32x4(320.0,340.0,360.0,380.0),
  176. SIMD.Float32x4(400.0,420.0,440.0,460.0),
  177. SIMD.Float32x4(480.0,500.0,520.0,540.0),
  178. SIMD.Float32x4(560.0,580.0,600.0,620.0),
  179. SIMD.Float32x4(640.0,660.0,680.0,700.0),
  180. SIMD.Float32x4(720.0,740.0,760.0,780.0)
  181. ];
  182. for (var i = 0; i < count; i += 4)
  183. {
  184. f4 = SIMD.Float32x4.load(buffer, i);
  185. equalSimd(data[i/4], f4, SIMD.Float32x4, "validateBuffer");
  186. }
  187. }
  188. function validateBuffer2(buffer, count)
  189. {
  190. var i4;
  191. var data = [
  192. SIMD.Int32x4(0,20,40,60),
  193. SIMD.Int32x4(80,100,120,140),
  194. SIMD.Int32x4(160,180,200,220),
  195. SIMD.Int32x4(240,260,280,300),
  196. SIMD.Int32x4(320,340,360,380),
  197. SIMD.Int32x4(400,420,440,460),
  198. SIMD.Int32x4(480,500,520,540),
  199. SIMD.Int32x4(560,580,600,620),
  200. SIMD.Int32x4(640,660,680,700),
  201. SIMD.Int32x4(720,740,760,780)
  202. ];
  203. for (var i = 0; i < count; i += 4)
  204. {
  205. i4 = SIMD.Int32x4.load(buffer, i);
  206. equalSimd(data[i/4], i4, SIMD.Int32x4, "validateBuffer2");
  207. }
  208. }
  209. function printResults(res)
  210. {
  211. print(typeof(res));
  212. print(res.toString());
  213. }
  214. inputLength = initF32(buffer);
  215. //Module initialization
  216. var m = asmModule(this, {g0:initF32(buffer),g1:SIMD.Float32x4(9,9,9,9), g2:SIMD.Int32x4(1, 2, 3, 4)}, buffer);
  217. var values = new Float32Array(buffer);
  218. m.scale(0, 16 * 10); //Scale
  219. validateBuffer(values, 4 * 10);
  220. initI32(buffer);
  221. m.scale2(0, 16 * 10); //Scale
  222. validateBuffer2(values, 4 * 10);
  223. print("PASS");