暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

084-IntrinsicsCommon.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Burst.Compiler.IL.Tests.Helpers;
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using UnityBenchShared;
  7. using static Unity.Burst.Intrinsics.Common;
  8. namespace Burst.Compiler.IL.Tests
  9. {
  10. internal partial class IntrinsicsCommon
  11. {
  12. [TestCompiler]
  13. public static int CheckBurstCompiler()
  14. {
  15. return BurstCompiler.IsEnabled ? 1 : 0;
  16. }
  17. [TestCompiler]
  18. public static void CheckPause()
  19. {
  20. Pause();
  21. }
  22. public unsafe struct Buffer : IDisposable
  23. {
  24. public int* Data;
  25. public int Length;
  26. public void Dispose()
  27. {
  28. UnsafeUtility.Free(Data, Allocator.Persistent);
  29. }
  30. public class Provider : IArgumentProvider
  31. {
  32. public object Value
  33. {
  34. get
  35. {
  36. var length = 1024;
  37. var data = (int*)UnsafeUtility.Malloc(sizeof(int) * length, UnsafeUtility.AlignOf<int>(), Allocator.Persistent);
  38. for (var i = 0; i < length; i++)
  39. {
  40. data[i] = i;
  41. }
  42. return new Buffer { Data = data, Length = length };
  43. }
  44. }
  45. }
  46. }
  47. #if BURST_INTERNAL || UNITY_BURST_EXPERIMENTAL_PREFETCH_INTRINSIC
  48. [TestCompiler(typeof(Buffer.Provider))]
  49. public static unsafe int CheckPrefetchReadNoTemporalLocality(ref Buffer buffer)
  50. {
  51. int total = 0;
  52. for (int i = 0; i < buffer.Length; i++)
  53. {
  54. total += buffer.Data[i];
  55. Prefetch(buffer.Data + i + 1, ReadWrite.Read, Locality.NoTemporalLocality);
  56. }
  57. return total;
  58. }
  59. [TestCompiler(typeof(Buffer.Provider))]
  60. public static unsafe int CheckPrefetchReadLowTemporalLocality(ref Buffer buffer)
  61. {
  62. int total = 0;
  63. for (int i = 0; i < buffer.Length; i++)
  64. {
  65. total += buffer.Data[i];
  66. Prefetch(buffer.Data + i + 1, ReadWrite.Read, Locality.LowTemporalLocality);
  67. }
  68. return total;
  69. }
  70. [TestCompiler(typeof(Buffer.Provider))]
  71. public static unsafe int CheckPrefetchReadModerateTemporalLocality(ref Buffer buffer)
  72. {
  73. int total = 0;
  74. for (int i = 0; i < buffer.Length; i++)
  75. {
  76. total += buffer.Data[i];
  77. Prefetch(buffer.Data + i + 1, ReadWrite.Read, Locality.ModerateTemporalLocality);
  78. }
  79. return total;
  80. }
  81. [TestCompiler(typeof(Buffer.Provider))]
  82. public static unsafe int CheckPrefetchReadHighTemporalLocality(ref Buffer buffer)
  83. {
  84. int total = 0;
  85. for (int i = 0; i < buffer.Length; i++)
  86. {
  87. total += buffer.Data[i];
  88. Prefetch(buffer.Data + i + 1, ReadWrite.Read, Locality.HighTemporalLocality);
  89. }
  90. return total;
  91. }
  92. [TestCompiler(typeof(Buffer.Provider))]
  93. public static unsafe void CheckPrefetchWriteNoTemporalLocality(ref Buffer buffer)
  94. {
  95. for (int i = 0; i < buffer.Length; i++)
  96. {
  97. buffer.Data[i] = i;
  98. Prefetch(buffer.Data + i + 1, ReadWrite.Write, Locality.NoTemporalLocality);
  99. }
  100. }
  101. [TestCompiler(typeof(Buffer.Provider))]
  102. public static unsafe void CheckPrefetchWriteLowTemporalLocality(ref Buffer buffer)
  103. {
  104. for (int i = 0; i < buffer.Length; i++)
  105. {
  106. buffer.Data[i] = i;
  107. Prefetch(buffer.Data + i + 1, ReadWrite.Write, Locality.LowTemporalLocality);
  108. }
  109. }
  110. [TestCompiler(typeof(Buffer.Provider))]
  111. public static unsafe void CheckPrefetchWriteModerateTemporalLocality(ref Buffer buffer)
  112. {
  113. for (int i = 0; i < buffer.Length; i++)
  114. {
  115. buffer.Data[i] = i;
  116. Prefetch(buffer.Data + i + 1, ReadWrite.Write, Locality.ModerateTemporalLocality);
  117. }
  118. }
  119. [TestCompiler(typeof(Buffer.Provider))]
  120. public static unsafe void CheckPrefetchWriteHighTemporalLocality(ref Buffer buffer)
  121. {
  122. for (int i = 0; i < buffer.Length; i++)
  123. {
  124. buffer.Data[i] = i;
  125. Prefetch(buffer.Data + i + 1, ReadWrite.Write, Locality.HighTemporalLocality);
  126. }
  127. }
  128. #endif
  129. [TestCompiler(typeof(ReturnBox), ulong.MaxValue, ulong.MaxValue)]
  130. [TestCompiler(typeof(ReturnBox), ulong.MinValue, ulong.MaxValue)]
  131. [TestCompiler(typeof(ReturnBox), ulong.MaxValue, ulong.MinValue)]
  132. [TestCompiler(typeof(ReturnBox), ulong.MinValue, ulong.MinValue)]
  133. [TestCompiler(typeof(ReturnBox), DataRange.Standard, DataRange.Standard)]
  134. public static unsafe ulong Checkumul128(ulong* high, ulong x, ulong y)
  135. {
  136. var result = umul128(x, y, out var myHigh);
  137. *high = myHigh;
  138. return result;
  139. }
  140. #if BURST_INTERNAL || UNITY_BURST_EXPERIMENTAL_ATOMIC_INTRINSICS
  141. [TestCompiler(42, 13)]
  142. public static int CheckInterlockedAndInt(int x, int y)
  143. {
  144. InterlockedAnd(ref x, y);
  145. return x;
  146. }
  147. [TestCompiler(42U, 13U)]
  148. public static uint CheckInterlockedAndUint(uint x, uint y)
  149. {
  150. InterlockedAnd(ref x, y);
  151. return x;
  152. }
  153. [TestCompiler(42L, 13L)]
  154. public static long CheckInterlockedAndLong(long x, long y)
  155. {
  156. InterlockedAnd(ref x, y);
  157. return x;
  158. }
  159. [TestCompiler(42UL, 13UL)]
  160. public static ulong CheckInterlockedAndUlong(ulong x, ulong y)
  161. {
  162. InterlockedAnd(ref x, y);
  163. return x;
  164. }
  165. [TestCompiler(42, 13)]
  166. public static int CheckInterlockedOrInt(int x, int y)
  167. {
  168. InterlockedOr(ref x, y);
  169. return x;
  170. }
  171. [TestCompiler(42U, 13U)]
  172. public static uint CheckInterlockedOrUint(uint x, uint y)
  173. {
  174. InterlockedOr(ref x, y);
  175. return x;
  176. }
  177. [TestCompiler(42L, 13L)]
  178. public static long CheckInterlockedOrLong(long x, long y)
  179. {
  180. InterlockedOr(ref x, y);
  181. return x;
  182. }
  183. [TestCompiler(42UL, 13UL)]
  184. public static ulong CheckInterlockedOrUlong(ulong x, ulong y)
  185. {
  186. InterlockedOr(ref x, y);
  187. return x;
  188. }
  189. #endif
  190. }
  191. }