memset.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Compares the value set by interpreter with the jitted code
  6. // need to run with -mic:1 -off:simplejit -off:JITLoopBody -off:inline
  7. // Run locally with -trace:memop -trace:bailout to help find bugs
  8. let testCases = [
  9. function() {
  10. return {
  11. start: 0,
  12. end: 100,
  13. test: function testBasic(a) {
  14. for(let i = 0; i < 100; i++) {
  15. a[i] = 0;
  16. }
  17. }
  18. };
  19. },
  20. function() {
  21. return {
  22. start: 5,
  23. end: 101,
  24. test: function testReverse(a) {
  25. for(let i = 100; i >= 5; i--) {
  26. a[i] = 0;
  27. }
  28. }
  29. };
  30. },
  31. function() {
  32. let results = [];
  33. return {
  34. runner: function testMultipleMemset(arrayGen) {
  35. let a = arrayGen(), b = arrayGen(), c = arrayGen();
  36. for(let i = 0; i < 10; i++) {
  37. a[i] = b[i] = c[i] = 0;
  38. }
  39. results.push([a, b, c]);
  40. },
  41. check: function() {
  42. let base = results[0];
  43. for(let i = 1; i < results.length; ++i) {
  44. for(let j = 0; j < 3; ++j) {
  45. compareResult(base[j], results[i][j], 0, 10);
  46. }
  47. }
  48. }
  49. };
  50. },
  51. function() {
  52. return {
  53. start: 4,
  54. end: 30,
  55. test: function testUnroll(a) {
  56. for(let i = 4; i < 30;) {
  57. a[i] = 0;
  58. i++;
  59. a[i] = 0;
  60. i++;
  61. }
  62. }
  63. };
  64. },
  65. function() {
  66. return {
  67. start: 8,
  68. end: 10,
  69. test: function testMissingValues(a) {
  70. for(let i = 8; i < 10; i++) {
  71. a[i] = 0;
  72. }
  73. }
  74. };
  75. },
  76. function() {
  77. return {
  78. start: 0,
  79. end: 6,
  80. test: function testOverwrite(a) {
  81. a[5] = 3;
  82. for(let i = 0; i < 6; i++) {
  83. a[i] = 0;
  84. }
  85. }
  86. };
  87. },
  88. function() {
  89. return {
  90. start: 10,
  91. end: 50,
  92. test: function testNegativeConstant(a) {
  93. for(let i = 10; i < 50; i++) {
  94. a[i] = -1;
  95. }
  96. }
  97. };
  98. },
  99. function() {
  100. return {
  101. start: -50,
  102. end: 10,
  103. test: function testNegativeStartIndex(a) {
  104. for(let i = -50; i < 10; i++) {
  105. a[i] = -3;
  106. }
  107. }
  108. };
  109. }
  110. ];
  111. let arrayGenerators = [
  112. // the one for the interpreter
  113. function() {
  114. return new Array(10);
  115. },
  116. function() {
  117. return new Array(10);
  118. },
  119. function() {
  120. return [];
  121. }
  122. // causes bailouts right now: BailOut: function: testMultipleMemset ( (#1.2), #3) offset: #0036 Opcode: BailOnNotArray Kind: BailOutOnNotNativeArray
  123. // function() {return [1, 2, 3, 4, 5, 6, 7]; }
  124. ];
  125. for(let testCase of testCases) {
  126. let results = [];
  127. let testInfo = testCase();
  128. for(let gen of arrayGenerators) {
  129. if(testInfo.runner) {
  130. let result = testInfo.runner(gen);
  131. results.push(result);
  132. } else {
  133. let newArray = gen();
  134. testInfo.test(newArray);
  135. results.push(newArray);
  136. }
  137. }
  138. if(testInfo.check) {
  139. testInfo.check(results);
  140. } else {
  141. let base = results[0]; // result from the interpreter
  142. for(let i = 1; i < results.length; ++i) {
  143. compareResult(base, results[i], testInfo.start, testInfo.end);
  144. }
  145. }
  146. }
  147. let passed = true;
  148. function compareResult(a, b, start, end) {
  149. for(let i = start; i < end; i++) {
  150. if(a[i] !== b[i]) {
  151. print(`${i}: ${a[i]} !== ${b[i]}`);
  152. passed = false;
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. if(passed) {
  159. print("PASSED");
  160. } else {
  161. print("FAILED");
  162. }