暫無描述
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.

Bmi2.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Diagnostics;
  2. namespace Unity.Burst.Intrinsics
  3. {
  4. public unsafe static partial class X86
  5. {
  6. /// <summary>
  7. /// bmi2 intrinsics
  8. /// </summary>
  9. public static class Bmi2
  10. {
  11. /// <summary>
  12. /// Evaluates to true at compile time if bmi2 intrinsics are supported.
  13. ///
  14. /// Burst ties bmi2 support to AVX2 support to simplify feature sets to support.
  15. /// </summary>
  16. public static bool IsBmi2Supported { get { return Avx2.IsAvx2Supported; } }
  17. /// <summary>
  18. /// Copy all bits from unsigned 32-bit integer a to dst, and reset (set to 0) the high bits in dst starting at index.
  19. /// </summary>
  20. /// <remarks>
  21. /// **** bzhi r32, r32, r32
  22. /// </remarks>
  23. /// <param name="a">32-bit integer</param>
  24. /// <param name="index">Starting point</param>
  25. /// <returns>32-bit integer</returns>
  26. [DebuggerStepThrough]
  27. public static uint bzhi_u32(uint a, uint index)
  28. {
  29. if (index >= (sizeof(uint) * 8))
  30. {
  31. return a;
  32. }
  33. return a & ((1u << (int)index) - 1u);
  34. }
  35. /// <summary>
  36. /// Copy all bits from unsigned 64-bit integer a to dst, and reset (set to 0) the high bits in dst starting at index.
  37. /// </summary>
  38. /// <remarks>
  39. /// **** bzhi r64, r64, r64
  40. /// </remarks>
  41. /// <param name="a">64-bit integer</param>
  42. /// <param name="index">Starting point</param>
  43. /// <returns>64-bit integer</returns>
  44. [DebuggerStepThrough]
  45. public static ulong bzhi_u64(ulong a, ulong index)
  46. {
  47. if (index >= (sizeof(ulong) * 8))
  48. {
  49. return a;
  50. }
  51. return a & ((1u << (int)index) - 1u);
  52. }
  53. /// <summary>
  54. /// Multiply unsigned 32-bit integers a and b, store the low 32-bits of the result in dst, and store the high 32-bits in hi. This does not read or write arithmetic flags.
  55. /// </summary>
  56. /// <remarks>
  57. /// **** mulx r32, r32, m32
  58. /// </remarks>
  59. /// <param name="a">32-bit integer</param>
  60. /// <param name="b">32-bit integer</param>
  61. /// <param name="hi">Stores the high 32-bits</param>
  62. /// <returns>32-bit integer</returns>
  63. [DebuggerStepThrough]
  64. public static uint mulx_u32(uint a, uint b, out uint hi)
  65. {
  66. ulong aBig = a;
  67. ulong bBig = b;
  68. ulong result = aBig * bBig;
  69. hi = (uint)(result >> 32);
  70. return (uint)(result & 0xffffffff);
  71. }
  72. /// <summary>
  73. /// Multiply unsigned 64-bit integers a and b, store the low 64-bits of the result in dst, and store the high 64-bits in hi. This does not read or write arithmetic flags.
  74. /// </summary>
  75. /// <remarks>
  76. /// **** mulx r64, r64, m64
  77. /// </remarks>
  78. /// <param name="a">64-bit integer</param>
  79. /// <param name="b">64-bit integer</param>
  80. /// <param name="hi">Stores the high 64-bits</param>
  81. /// <returns>64-bit integer</returns>
  82. [DebuggerStepThrough]
  83. public static ulong mulx_u64(ulong a, ulong b, out ulong hi)
  84. {
  85. return Common.umul128(a, b, out hi);
  86. }
  87. /// <summary>
  88. /// Deposit contiguous low bits from unsigned 32-bit integer a to dst at the corresponding bit locations specified by mask; all other bits in dst are set to zero.
  89. /// </summary>
  90. /// <remarks>
  91. /// **** pdep r32, r32, r32
  92. /// </remarks>
  93. /// <param name="a">32-bit integer</param>
  94. /// <param name="mask">Mask</param>
  95. /// <returns>32-bit integer</returns>
  96. [DebuggerStepThrough]
  97. public static uint pdep_u32(uint a, uint mask)
  98. {
  99. uint result = 0;
  100. int k = 0;
  101. for (int i = 0; i < 32; i++)
  102. {
  103. if ((mask & (1u << i)) != 0)
  104. {
  105. result |= ((a >> k) & 1u) << i;
  106. k++;
  107. }
  108. }
  109. return result;
  110. }
  111. /// <summary>
  112. /// Deposit contiguous low bits from unsigned 64-bit integer a to dst at the corresponding bit locations specified by mask; all other bits in dst are set to zero.
  113. /// </summary>
  114. /// <remarks>
  115. /// **** pdep r64, r64, r64
  116. /// </remarks>
  117. /// <param name="a">64-bit integer</param>
  118. /// <param name="mask">Mask</param>
  119. /// <returns>64-bit integer</returns>
  120. [DebuggerStepThrough]
  121. public static ulong pdep_u64(ulong a, ulong mask)
  122. {
  123. ulong result = 0;
  124. int k = 0;
  125. for (int i = 0; i < 64; i++)
  126. {
  127. if ((mask & (1ul << i)) != 0)
  128. {
  129. result |= ((a >> k) & 1ul) << i;
  130. k++;
  131. }
  132. }
  133. return result;
  134. }
  135. /// <summary>
  136. /// Extract bits from unsigned 32-bit integer a at the corresponding bit locations specified by mask to contiguous low bits in dst; the remaining upper bits in dst are set to zero.
  137. /// </summary>
  138. /// <remarks>
  139. /// **** pext r32, r32, r32
  140. /// </remarks>
  141. /// <param name="a">32-bit integer</param>
  142. /// <param name="mask">Mask</param>
  143. /// <returns>32-bit integer</returns>
  144. [DebuggerStepThrough]
  145. public static uint pext_u32(uint a, uint mask)
  146. {
  147. uint result = 0;
  148. int k = 0;
  149. for (int i = 0; i < 32; i++)
  150. {
  151. if ((mask & (1u << i)) != 0)
  152. {
  153. result |= ((a >> i) & 1u) << k;
  154. k++;
  155. }
  156. }
  157. return result;
  158. }
  159. /// <summary>
  160. /// Extract bits from unsigned 64-bit integer a at the corresponding bit locations specified by mask to contiguous low bits in dst; the remaining upper bits in dst are set to zero.
  161. /// </summary>
  162. /// <remarks>
  163. /// **** pext r64, r64, r64
  164. /// </remarks>
  165. /// <param name="a">64-bit integer</param>
  166. /// <param name="mask">Mask</param>
  167. /// <returns>64-bit integer</returns>
  168. [DebuggerStepThrough]
  169. public static ulong pext_u64(ulong a, ulong mask)
  170. {
  171. ulong result = 0;
  172. int k = 0;
  173. for (int i = 0; i < 64; i++)
  174. {
  175. if ((mask & (1ul << i)) != 0)
  176. {
  177. result |= ((a >> i) & 1ul) << k;
  178. k++;
  179. }
  180. }
  181. return result;
  182. }
  183. }
  184. }
  185. }