No Description
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.

Common.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. namespace Unity.Burst.Intrinsics
  3. {
  4. /// <summary>
  5. /// Common intrinsics that are exposed across all Burst targets.
  6. /// </summary>
  7. public static class Common
  8. {
  9. /// <summary>
  10. /// Hint that the current thread should pause.
  11. ///
  12. /// In Burst compiled code this will map to platform specific
  13. /// ways to hint that the current thread should be paused as
  14. /// it is performing a calculation that would benefit from
  15. /// not contending with other threads. Atomic operations in
  16. /// tight loops (like spin-locks) can benefit from use of this
  17. /// intrinsic.
  18. ///
  19. /// - On x86 systems this maps to the `pause` instruction.
  20. /// - On ARM systems this maps to the `yield` instruction.
  21. ///
  22. /// Note that this is not an operating system level thread yield,
  23. /// it only provides a hint to the CPU that the current thread can
  24. /// afford to pause its execution temporarily.
  25. /// </summary>
  26. public static void Pause() { }
  27. #if UNITY_BURST_EXPERIMENTAL_PREFETCH_INTRINSIC
  28. public enum ReadWrite : int
  29. {
  30. Read = 0,
  31. Write = 1,
  32. }
  33. public enum Locality : int
  34. {
  35. NoTemporalLocality = 0,
  36. LowTemporalLocality = 1,
  37. ModerateTemporalLocality = 2,
  38. HighTemporalLocality = 3,
  39. }
  40. /// <summary>
  41. /// Prefetch a pointer.
  42. /// </summary>
  43. /// <param name="v">The pointer to prefetch.</param>
  44. /// <param name="rw">Whether the pointer will be used for reading or writing.</param>
  45. /// <param name="locality">The cache locality of the pointer.</param>
  46. public static unsafe void Prefetch(void* v, ReadWrite rw, Locality locality = Locality.HighTemporalLocality) { }
  47. #endif
  48. /// <summary>
  49. /// Return the low half of the multiplication of two numbers, and the high part as an out parameter.
  50. /// </summary>
  51. /// <param name="x">A value to multiply.</param>
  52. /// <param name="y">A value to multiply.</param>
  53. /// <param name="high">The high-half of the multiplication result.</param>
  54. /// <returns>The low-half of the multiplication result.</returns>
  55. public static ulong umul128(ulong x, ulong y, out ulong high)
  56. {
  57. // Provide a software fallback for the cases Burst isn't being used.
  58. // Split the inputs into high/low sections.
  59. ulong xLo = (uint)x;
  60. ulong xHi = x >> 32;
  61. ulong yLo = (uint)y;
  62. ulong yHi = y >> 32;
  63. // We have to use 4 multiples to compute the full range of the result.
  64. ulong hi = xHi * yHi;
  65. ulong m1 = xHi * yLo;
  66. ulong m2 = yHi * xLo;
  67. ulong lo = xLo * yLo;
  68. ulong m1Lo = (uint)m1;
  69. ulong loHi = lo >> 32;
  70. ulong m1Hi = m1 >> 32;
  71. high = hi + m1Hi + ((loHi + m1Lo + m2) >> 32);
  72. return x * y;
  73. }
  74. #if UNITY_BURST_EXPERIMENTAL_ATOMIC_INTRINSICS
  75. /// <summary>
  76. /// Bitwise and as an atomic operation.
  77. /// </summary>
  78. /// <param name="location">Where to atomically and the result into.</param>
  79. /// <param name="value">The value to be combined.</param>
  80. /// <returns>The original value in <paramref name="location" />.</returns>
  81. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  82. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.and"/>
  83. public static int InterlockedAnd(ref int location, int value)
  84. {
  85. // Provide a software fallback for the cases Burst isn't being used.
  86. var currentValue = System.Threading.Interlocked.Add(ref location, 0);
  87. while (true)
  88. {
  89. var updatedValue = currentValue & value;
  90. // If nothing would change by and'ing in our thing, bail out early.
  91. if (updatedValue == currentValue)
  92. {
  93. return currentValue;
  94. }
  95. var newValue = System.Threading.Interlocked.CompareExchange(ref location, updatedValue, currentValue);
  96. // If the original value was the same as the what we just got back from the compare exchange, it means our update succeeded.
  97. if (newValue == currentValue)
  98. {
  99. return currentValue;
  100. }
  101. // Lastly update the last known good value of location and try again!
  102. currentValue = newValue;
  103. }
  104. }
  105. /// <summary>
  106. /// Bitwise and as an atomic operation.
  107. /// </summary>
  108. /// <param name="location">Where to atomically and the result into.</param>
  109. /// <param name="value">The value to be combined.</param>
  110. /// <returns>The original value in <paramref name="location" />.</returns>
  111. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  112. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.and"/>
  113. public static uint InterlockedAnd(ref uint location, uint value)
  114. {
  115. unsafe
  116. {
  117. ref int locationAsInt = ref Unsafe.AsRef<int>(Unsafe.AsPointer(ref location));
  118. int valueAsInt = (int)value;
  119. return (uint)InterlockedAnd(ref locationAsInt, valueAsInt);
  120. }
  121. }
  122. /// <summary>
  123. /// Bitwise and as an atomic operation.
  124. /// </summary>
  125. /// <param name="location">Where to atomically and the result into.</param>
  126. /// <param name="value">The value to be combined.</param>
  127. /// <returns>The original value in <paramref name="location" />.</returns>
  128. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  129. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.and"/>
  130. public static long InterlockedAnd(ref long location, long value)
  131. {
  132. // Provide a software fallback for the cases Burst isn't being used.
  133. var currentValue = System.Threading.Interlocked.Read(ref location);
  134. while (true)
  135. {
  136. var updatedValue = currentValue & value;
  137. // If nothing would change by and'ing in our thing, bail out early.
  138. if (updatedValue == currentValue)
  139. {
  140. return currentValue;
  141. }
  142. var newValue = System.Threading.Interlocked.CompareExchange(ref location, updatedValue, currentValue);
  143. // If the original value was the same as the what we just got back from the compare exchange, it means our update succeeded.
  144. if (newValue == currentValue)
  145. {
  146. return currentValue;
  147. }
  148. // Lastly update the last known good value of location and try again!
  149. currentValue = newValue;
  150. }
  151. }
  152. /// <summary>
  153. /// Bitwise and as an atomic operation.
  154. /// </summary>
  155. /// <param name="location">Where to atomically and the result into.</param>
  156. /// <param name="value">The value to be combined.</param>
  157. /// <returns>The original value in <paramref name="location" />.</returns>
  158. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  159. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.and"/>
  160. public static ulong InterlockedAnd(ref ulong location, ulong value)
  161. {
  162. unsafe
  163. {
  164. ref long locationAsInt = ref Unsafe.AsRef<long>(Unsafe.AsPointer(ref location));
  165. long valueAsInt = (long)value;
  166. return (ulong)InterlockedAnd(ref locationAsInt, valueAsInt);
  167. }
  168. }
  169. /// <summary>
  170. /// Bitwise or as an atomic operation.
  171. /// </summary>
  172. /// <param name="location">Where to atomically or the result into.</param>
  173. /// <param name="value">The value to be combined.</param>
  174. /// <returns>The original value in <paramref name="location" />.</returns>
  175. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  176. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.or"/>
  177. public static int InterlockedOr(ref int location, int value)
  178. {
  179. // Provide a software fallback for the cases Burst isn't being used.
  180. var currentValue = System.Threading.Interlocked.Add(ref location, 0);
  181. while (true)
  182. {
  183. var updatedValue = currentValue | value;
  184. // If nothing would change by or'ing in our thing, bail out early.
  185. if (updatedValue == currentValue)
  186. {
  187. return currentValue;
  188. }
  189. var newValue = System.Threading.Interlocked.CompareExchange(ref location, updatedValue, currentValue);
  190. // If the original value was the same as the what we just got back from the compare exchange, it means our update succeeded.
  191. if (newValue == currentValue)
  192. {
  193. return currentValue;
  194. }
  195. // Lastly update the last known good value of location and try again!
  196. currentValue = newValue;
  197. }
  198. }
  199. /// <summary>
  200. /// Bitwise or as an atomic operation.
  201. /// </summary>
  202. /// <param name="location">Where to atomically or the result into.</param>
  203. /// <param name="value">The value to be combined.</param>
  204. /// <returns>The original value in <paramref name="location" />.</returns>
  205. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  206. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.or"/>
  207. public static uint InterlockedOr(ref uint location, uint value)
  208. {
  209. unsafe
  210. {
  211. ref int locationAsInt = ref Unsafe.AsRef<int>(Unsafe.AsPointer(ref location));
  212. int valueAsInt = (int)value;
  213. return (uint)InterlockedOr(ref locationAsInt, valueAsInt);
  214. }
  215. }
  216. /// <summary>
  217. /// Bitwise or as an atomic operation.
  218. /// </summary>
  219. /// <param name="location">Where to atomically or the result into.</param>
  220. /// <param name="value">The value to be combined.</param>
  221. /// <returns>The original value in <paramref name="location" />.</returns>
  222. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  223. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.or"/>
  224. public static long InterlockedOr(ref long location, long value)
  225. {
  226. // Provide a software fallback for the cases Burst isn't being used.
  227. var currentValue = System.Threading.Interlocked.Read(ref location);
  228. while (true)
  229. {
  230. var updatedValue = currentValue | value;
  231. // If nothing would change by or'ing in our thing, bail out early.
  232. if (updatedValue == currentValue)
  233. {
  234. return currentValue;
  235. }
  236. var newValue = System.Threading.Interlocked.CompareExchange(ref location, updatedValue, currentValue);
  237. // If the original value was the same as the what we just got back from the compare exchange, it means our update succeeded.
  238. if (newValue == currentValue)
  239. {
  240. return currentValue;
  241. }
  242. // Lastly update the last known good value of location and try again!
  243. currentValue = newValue;
  244. }
  245. }
  246. /// <summary>
  247. /// Bitwise or as an atomic operation.
  248. /// </summary>
  249. /// <param name="location">Where to atomically or the result into.</param>
  250. /// <param name="value">The value to be combined.</param>
  251. /// <returns>The original value in <paramref name="location" />.</returns>
  252. /// <remarks>Using the return value of this intrinsic may result in worse code-generation on some platforms (a compare-exchange loop), rather than a single atomic instruction being generated.</remarks>
  253. /// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.or"/>
  254. public static ulong InterlockedOr(ref ulong location, ulong value)
  255. {
  256. unsafe
  257. {
  258. ref long locationAsInt = ref Unsafe.AsRef<long>(Unsafe.AsPointer(ref location));
  259. long valueAsInt = (long)value;
  260. return (ulong)InterlockedOr(ref locationAsInt, valueAsInt);
  261. }
  262. }
  263. #endif
  264. }
  265. [AttributeUsage(AttributeTargets.Method, Inherited = false)]
  266. [BurstRuntime.Preserve]
  267. // expose the type to btests via Unity.Burst.dll
  268. #if BURST_INTERNAL
  269. public
  270. #else
  271. internal
  272. #endif
  273. sealed class BurstTargetCpuAttribute : Attribute
  274. {
  275. public BurstTargetCpuAttribute(BurstTargetCpu TargetCpu)
  276. {
  277. this.TargetCpu = TargetCpu;
  278. }
  279. public readonly BurstTargetCpu TargetCpu;
  280. }
  281. }