暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

052-TestFixed.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityBenchShared;
  2. namespace Burst.Compiler.IL.Tests
  3. {
  4. internal class TestFixed
  5. {
  6. public unsafe struct SomeStruct
  7. {
  8. public static readonly int[] Ints = new int[4] { 1, 2, 3, 4 };
  9. public struct OtherStruct
  10. {
  11. public int x;
  12. }
  13. public static readonly OtherStruct[] Structs = new OtherStruct[2] { new OtherStruct { x = 42 }, new OtherStruct { x = 13 } };
  14. public fixed ushort array[42];
  15. public struct Provider : IArgumentProvider
  16. {
  17. public object Value
  18. {
  19. get
  20. {
  21. var s = new SomeStruct();
  22. for (ushort i = 0; i < 42; i++)
  23. {
  24. s.array[i] = i;
  25. }
  26. return s;
  27. }
  28. }
  29. }
  30. }
  31. [TestCompiler]
  32. public static unsafe int ReadInts()
  33. {
  34. fixed (int* ptr = SomeStruct.Ints)
  35. {
  36. return ptr[2];
  37. }
  38. }
  39. [TestCompiler]
  40. public static unsafe int ReadIntsElement()
  41. {
  42. fixed (int* ptr = &SomeStruct.Ints[1])
  43. {
  44. return ptr[0];
  45. }
  46. }
  47. [TestCompiler]
  48. public static unsafe int ReadStructs()
  49. {
  50. fixed (SomeStruct.OtherStruct* ptr = SomeStruct.Structs)
  51. {
  52. return ptr[1].x;
  53. }
  54. }
  55. [TestCompiler]
  56. public static unsafe int ReadStructsElement()
  57. {
  58. fixed (SomeStruct.OtherStruct* ptr = &SomeStruct.Structs[1])
  59. {
  60. return ptr[0].x;
  61. }
  62. }
  63. [TestCompiler(typeof(SomeStruct.Provider))]
  64. public static unsafe ushort ReadFromFixedArray(ref SomeStruct s)
  65. {
  66. fixed (ushort* ptr = s.array)
  67. {
  68. ushort total = 0;
  69. for (ushort i = 0; i < 42; i++)
  70. {
  71. total += ptr[i];
  72. }
  73. return total;
  74. }
  75. }
  76. // The below tests are designed to verify the indexer is treated correctly for various fixed arrays (only the smallest case)
  77. //(the bug was actually to do with pointer addition, so see 031-Pointer.cs for additional coverage)
  78. //Its not perfect as if the indexer is treated as signed, then in burst we will read off the beginning of the array
  79. //which might be into another array or off the beginning of the struct... and the value might accidently be correct.
  80. public unsafe struct IndexerStructTestSByte
  81. {
  82. public fixed sbyte sbyteArray[256];
  83. public struct Provider : IArgumentProvider
  84. {
  85. public object Value
  86. {
  87. get
  88. {
  89. var s = new IndexerStructTestSByte();
  90. for (int a=0;a<256;a++)
  91. {
  92. s.sbyteArray[a] = sbyte.MinValue;
  93. }
  94. s.sbyteArray[127] = 127;
  95. s.sbyteArray[128] = 63;
  96. s.sbyteArray[255] = 23;
  97. return s;
  98. }
  99. }
  100. }
  101. }
  102. public unsafe struct IndexerStructTestByte
  103. {
  104. public fixed byte byteArray[256];
  105. public struct Provider : IArgumentProvider
  106. {
  107. public object Value
  108. {
  109. get
  110. {
  111. var s = new IndexerStructTestByte();
  112. for (int a=0;a<256;a++)
  113. {
  114. s.byteArray[a] = byte.MinValue;
  115. }
  116. s.byteArray[127] = 129;
  117. s.byteArray[128] = 212;
  118. s.byteArray[255] = 165;
  119. return s;
  120. }
  121. }
  122. }
  123. }
  124. // SByte array with different indexer types
  125. [TestCompiler(typeof(IndexerStructTestSByte.Provider),(byte)0)]
  126. [TestCompiler(typeof(IndexerStructTestSByte.Provider),(byte)128)]
  127. [TestCompiler(typeof(IndexerStructTestSByte.Provider),(byte)255)]
  128. public static unsafe sbyte IndexerReadFromSByteArrayWithByteOffset(ref IndexerStructTestSByte s, byte offset)
  129. {
  130. return s.sbyteArray[offset];
  131. }
  132. [TestCompiler(typeof(IndexerStructTestSByte.Provider),(sbyte)0)]
  133. [TestCompiler(typeof(IndexerStructTestSByte.Provider),(sbyte)127)] // signed offset so limited
  134. public static unsafe sbyte IndexerReadFromSByteArrayWithSByteOffset(ref IndexerStructTestSByte s, sbyte offset)
  135. {
  136. return s.sbyteArray[offset];
  137. }
  138. // Byte array with different indexer types
  139. [TestCompiler(typeof(IndexerStructTestByte.Provider),(byte)0)]
  140. [TestCompiler(typeof(IndexerStructTestByte.Provider),(byte)128)]
  141. [TestCompiler(typeof(IndexerStructTestByte.Provider),(byte)255)]
  142. public static unsafe byte IndexerReadFromByteArrayWithByteOffset(ref IndexerStructTestByte s, byte offset)
  143. {
  144. return s.byteArray[offset];
  145. }
  146. [TestCompiler(typeof(IndexerStructTestByte.Provider),(sbyte)0)]
  147. [TestCompiler(typeof(IndexerStructTestByte.Provider),(sbyte)127)] // signed offset so limited
  148. public static unsafe byte IndexerReadFromByteArrayWithSByteOffset(ref IndexerStructTestByte s, sbyte offset)
  149. {
  150. return s.byteArray[offset];
  151. }
  152. }
  153. }