Ei kuvausta
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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. index &= 0xff;
  30. if (index >= (sizeof(uint) * 8))
  31. {
  32. return a;
  33. }
  34. return a & ((1u << (int)index) - 1u);
  35. }
  36. /// <summary>
  37. /// Copy all bits from unsigned 64-bit integer a to dst, and reset (set to 0) the high bits in dst starting at index.
  38. /// </summary>
  39. /// <remarks>
  40. /// **** bzhi r64, r64, r64
  41. /// </remarks>
  42. /// <param name="a">64-bit integer</param>
  43. /// <param name="index">Starting point</param>
  44. /// <returns>64-bit integer</returns>
  45. [DebuggerStepThrough]
  46. public static ulong bzhi_u64(ulong a, ulong index)
  47. {
  48. index &= 0xff;
  49. if (index >= (sizeof(ulong) * 8))
  50. {
  51. return a;
  52. }
  53. return a & ((1ul << (int)index) - 1ul);
  54. }
  55. /// <summary>
  56. /// 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.
  57. /// </summary>
  58. /// <remarks>
  59. /// **** mulx r32, r32, m32
  60. /// </remarks>
  61. /// <param name="a">32-bit integer</param>
  62. /// <param name="b">32-bit integer</param>
  63. /// <param name="hi">Stores the high 32-bits</param>
  64. /// <returns>32-bit integer</returns>
  65. [DebuggerStepThrough]
  66. public static uint mulx_u32(uint a, uint b, out uint hi)
  67. {
  68. ulong aBig = a;
  69. ulong bBig = b;
  70. ulong result = aBig * bBig;
  71. hi = (uint)(result >> 32);
  72. return (uint)(result & 0xffffffff);
  73. }
  74. /// <summary>
  75. /// 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.
  76. /// </summary>
  77. /// <remarks>
  78. /// **** mulx r64, r64, m64
  79. /// </remarks>
  80. /// <param name="a">64-bit integer</param>
  81. /// <param name="b">64-bit integer</param>
  82. /// <param name="hi">Stores the high 64-bits</param>
  83. /// <returns>64-bit integer</returns>
  84. [DebuggerStepThrough]
  85. public static ulong mulx_u64(ulong a, ulong b, out ulong hi)
  86. {
  87. return Common.umul128(a, b, out hi);
  88. }
  89. /// <summary>
  90. /// 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.
  91. /// </summary>
  92. /// <remarks>
  93. /// **** pdep r32, r32, r32
  94. /// </remarks>
  95. /// <param name="a">32-bit integer</param>
  96. /// <param name="mask">Mask</param>
  97. /// <returns>32-bit integer</returns>
  98. [DebuggerStepThrough]
  99. public static uint pdep_u32(uint a, uint mask)
  100. {
  101. uint result = 0;
  102. int k = 0;
  103. for (int i = 0; i < 32; i++)
  104. {
  105. if ((mask & (1u << i)) != 0)
  106. {
  107. result |= ((a >> k) & 1u) << i;
  108. k++;
  109. }
  110. }
  111. return result;
  112. }
  113. /// <summary>
  114. /// 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.
  115. /// </summary>
  116. /// <remarks>
  117. /// **** pdep r64, r64, r64
  118. /// </remarks>
  119. /// <param name="a">64-bit integer</param>
  120. /// <param name="mask">Mask</param>
  121. /// <returns>64-bit integer</returns>
  122. [DebuggerStepThrough]
  123. public static ulong pdep_u64(ulong a, ulong mask)
  124. {
  125. ulong result = 0;
  126. int k = 0;
  127. for (int i = 0; i < 64; i++)
  128. {
  129. if ((mask & (1ul << i)) != 0)
  130. {
  131. result |= ((a >> k) & 1ul) << i;
  132. k++;
  133. }
  134. }
  135. return result;
  136. }
  137. /// <summary>
  138. /// 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.
  139. /// </summary>
  140. /// <remarks>
  141. /// **** pext r32, r32, r32
  142. /// </remarks>
  143. /// <param name="a">32-bit integer</param>
  144. /// <param name="mask">Mask</param>
  145. /// <returns>32-bit integer</returns>
  146. [DebuggerStepThrough]
  147. public static uint pext_u32(uint a, uint mask)
  148. {
  149. uint result = 0;
  150. int k = 0;
  151. for (int i = 0; i < 32; i++)
  152. {
  153. if ((mask & (1u << i)) != 0)
  154. {
  155. result |= ((a >> i) & 1u) << k;
  156. k++;
  157. }
  158. }
  159. return result;
  160. }
  161. /// <summary>
  162. /// 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.
  163. /// </summary>
  164. /// <remarks>
  165. /// **** pext r64, r64, r64
  166. /// </remarks>
  167. /// <param name="a">64-bit integer</param>
  168. /// <param name="mask">Mask</param>
  169. /// <returns>64-bit integer</returns>
  170. [DebuggerStepThrough]
  171. public static ulong pext_u64(ulong a, ulong mask)
  172. {
  173. ulong result = 0;
  174. int k = 0;
  175. for (int i = 0; i < 64; i++)
  176. {
  177. if ((mask & (1ul << i)) != 0)
  178. {
  179. result |= ((a >> i) & 1ul) << k;
  180. k++;
  181. }
  182. }
  183. return result;
  184. }
  185. }
  186. }
  187. }