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

Bmi1.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System.Diagnostics;
  2. namespace Unity.Burst.Intrinsics
  3. {
  4. public unsafe static partial class X86
  5. {
  6. /// <summary>
  7. /// bmi1 intrinsics
  8. /// </summary>
  9. public static class Bmi1
  10. {
  11. /// <summary>
  12. /// Evaluates to true at compile time if bmi1 intrinsics are supported.
  13. ///
  14. /// Burst ties bmi1 support to AVX2 support to simplify feature sets to support.
  15. /// </summary>
  16. public static bool IsBmi1Supported { get { return Avx2.IsAvx2Supported; } }
  17. /// <summary>
  18. /// Compute the bitwise NOT of 32-bit integer a and then AND with b, and store the results in dst.
  19. /// </summary>
  20. /// <remarks>
  21. /// **** andn r32, r32, r32
  22. /// </remarks>
  23. /// <param name="a">32-bit integer</param>
  24. /// <param name="b">32-bit integer</param>
  25. /// <returns>32-bit integer</returns>
  26. [DebuggerStepThrough]
  27. public static uint andn_u32(uint a, uint b)
  28. {
  29. return ~a & b;
  30. }
  31. /// <summary>
  32. /// Compute the bitwise NOT of 64-bit integer a and then AND with b, and store the results in dst.
  33. /// </summary>
  34. /// <remarks>
  35. /// **** andn r64, r64, r64
  36. /// </remarks>
  37. /// <param name="a">64-bit integer</param>
  38. /// <param name="b">64-bit integer</param>
  39. /// <returns>64-bit integer</returns>
  40. [DebuggerStepThrough]
  41. public static ulong andn_u64(ulong a, ulong b)
  42. {
  43. return ~a & b;
  44. }
  45. /// <summary>
  46. /// Extract contiguous bits from unsigned 32-bit integer a, and store the result in dst. Extract the number of bits specified by len, starting at the bit specified by start.
  47. /// </summary>
  48. /// <remarks>
  49. /// **** bextr r32, r32, r32
  50. /// </remarks>
  51. /// <param name="a">32-bit integer</param>
  52. /// <param name="start">Starting bit</param>
  53. /// <param name="len">Number of bits</param>
  54. /// <returns>32-bit integer</returns>
  55. [DebuggerStepThrough]
  56. public static uint bextr_u32(uint a, uint start, uint len)
  57. {
  58. if (start >= (sizeof(uint) * 8))
  59. {
  60. return 0;
  61. }
  62. uint aShifted = a >> (int)start;
  63. if (len >= (sizeof(uint) * 8))
  64. {
  65. return aShifted;
  66. }
  67. return aShifted & ((1u << (int)len) - 1u);
  68. }
  69. /// <summary>
  70. /// Extract contiguous bits from unsigned 64-bit integer a, and store the result in dst. Extract the number of bits specified by len, starting at the bit specified by start.
  71. /// </summary>
  72. /// <remarks>
  73. /// **** bextr r64, r64, r64
  74. /// </remarks>
  75. /// <param name="a">64-bit integer</param>
  76. /// <param name="start">Starting bit</param>
  77. /// <param name="len">Number of bits</param>
  78. /// <returns>64-bit integer</returns>
  79. [DebuggerStepThrough]
  80. public static ulong bextr_u64(ulong a, uint start, uint len)
  81. {
  82. if (start >= (sizeof(ulong) * 8))
  83. {
  84. return 0;
  85. }
  86. ulong aShifted = a >> (int)start;
  87. if (len >= (sizeof(ulong) * 8))
  88. {
  89. return aShifted;
  90. }
  91. return aShifted & ((((ulong)1) << (int)len) - 1u);
  92. }
  93. /// <summary>
  94. /// Extract contiguous bits from unsigned 32-bit integer a, and store the result in dst. Extract the number of bits specified by bits 15:8 of control, starting at the bit specified by bits 0:7 of control..
  95. /// </summary>
  96. /// <remarks>
  97. /// **** bextr r32, r32, r32
  98. /// </remarks>
  99. /// <param name="a">32-bit integer</param>
  100. /// <param name="control">Control</param>
  101. /// <returns>32-bit integer</returns>
  102. [DebuggerStepThrough]
  103. public static uint bextr2_u32(uint a, uint control)
  104. {
  105. uint start = control & byte.MaxValue;
  106. uint len = (control >> 8) & byte.MaxValue;
  107. return bextr_u32(a, start, len);
  108. }
  109. /// <summary>
  110. /// Extract contiguous bits from unsigned 64-bit integer a, and store the result in dst. Extract the number of bits specified by bits 15:8 of control, starting at the bit specified by bits 0:7 of control..
  111. /// </summary>
  112. /// <remarks>
  113. /// **** bextr r64, r64, r64
  114. /// </remarks>
  115. /// <param name="a">32-bit integer</param>
  116. /// <param name="control">Control</param>
  117. /// <returns>64-bit integer</returns>
  118. [DebuggerStepThrough]
  119. public static ulong bextr2_u64(ulong a, ulong control)
  120. {
  121. uint start = (uint)(control & byte.MaxValue);
  122. uint len = (uint)((control >> 8) & byte.MaxValue);
  123. return bextr_u64(a, start, len);
  124. }
  125. /// <summary>
  126. /// Extract the lowest set bit from unsigned 32-bit integer a and set the corresponding bit in dst. All other bits in dst are zeroed, and all bits are zeroed if no bits are set in a.
  127. /// </summary>
  128. /// <remarks>
  129. /// **** blsi r32, r32
  130. /// </remarks>
  131. /// <param name="a">32-bit integer</param>
  132. /// <returns>32-bit integer</returns>
  133. [DebuggerStepThrough]
  134. public static uint blsi_u32(uint a)
  135. {
  136. return (uint)(-(int)a) & a;
  137. }
  138. /// <summary>
  139. /// Extract the lowest set bit from unsigned 64-bit integer a and set the corresponding bit in dst. All other bits in dst are zeroed, and all bits are zeroed if no bits are set in a.
  140. /// </summary>
  141. /// <remarks>
  142. /// **** blsi r64, r64
  143. /// </remarks>
  144. /// <param name="a">64-bit integer</param>
  145. /// <returns>64-bit integer</returns>
  146. [DebuggerStepThrough]
  147. public static ulong blsi_u64(ulong a)
  148. {
  149. return (ulong)(-(long)a) & a;
  150. }
  151. /// <summary>
  152. /// Set all the lower bits of dst up to and including the lowest set bit in unsigned 32-bit integer a.
  153. /// </summary>
  154. /// <remarks>
  155. /// **** blsmsk r32, r32
  156. /// </remarks>
  157. /// <param name="a">32-bit integer</param>
  158. /// <returns>32-bit integer</returns>
  159. [DebuggerStepThrough]
  160. public static uint blsmsk_u32(uint a)
  161. {
  162. return (a - 1) ^ a;
  163. }
  164. /// <summary>
  165. /// Set all the lower bits of dst up to and including the lowest set bit in unsigned 64-bit integer a.
  166. /// </summary>
  167. /// <remarks>
  168. /// **** blsmsk r64, r64
  169. /// </remarks>
  170. /// <param name="a">64-bit integer</param>
  171. /// <returns>64-bit integer</returns>
  172. [DebuggerStepThrough]
  173. public static ulong blsmsk_u64(ulong a)
  174. {
  175. return (a - 1) ^ a;
  176. }
  177. /// <summary>
  178. /// Copy all bits from unsigned 32-bit integer a to dst, and reset (set to 0) the bit in dst that corresponds to the lowest set bit in a.
  179. /// </summary>
  180. /// <remarks>
  181. /// **** blsr r32, r32
  182. /// </remarks>
  183. /// <param name="a">32-bit integer</param>
  184. /// <returns>32-bit integer</returns>
  185. [DebuggerStepThrough]
  186. public static uint blsr_u32(uint a)
  187. {
  188. return (a - 1) & a;
  189. }
  190. /// <summary>
  191. /// Copy all bits from unsigned 64-bit integer a to dst, and reset (set to 0) the bit in dst that corresponds to the lowest set bit in a.
  192. /// </summary>
  193. /// <remarks>
  194. /// **** blsr r64, r64
  195. /// </remarks>
  196. /// <param name="a">64-bit integer</param>
  197. /// <returns>64-bit integer</returns>
  198. [DebuggerStepThrough]
  199. public static ulong blsr_u64(ulong a)
  200. {
  201. return (a - 1) & a;
  202. }
  203. /// <summary>
  204. /// Count the number of trailing zero bits in unsigned 32-bit integer a, and return that count in dst.
  205. /// </summary>
  206. /// <remarks>
  207. /// **** tzcnt r32, r32
  208. /// </remarks>
  209. /// <param name="a">32-bit integer</param>
  210. /// <returns>32-bit integer</returns>
  211. [DebuggerStepThrough]
  212. public static uint tzcnt_u32(uint a)
  213. {
  214. uint c = 32;
  215. a &= (uint)-(int)(a);
  216. if (a != 0) c--;
  217. if ((a & 0x0000FFFF) != 0) c -= 16;
  218. if ((a & 0x00FF00FF) != 0) c -= 8;
  219. if ((a & 0x0F0F0F0F) != 0) c -= 4;
  220. if ((a & 0x33333333) != 0) c -= 2;
  221. if ((a & 0x55555555) != 0) c -= 1;
  222. return c;
  223. }
  224. /// <summary>
  225. /// Count the number of trailing zero bits in unsigned 64-bit integer a, and return that count in dst.
  226. /// </summary>
  227. /// <remarks>
  228. /// **** tzcnt r64, r64
  229. /// </remarks>
  230. /// <param name="a">64-bit integer</param>
  231. /// <returns>64-bit integer</returns>
  232. [DebuggerStepThrough]
  233. public static ulong tzcnt_u64(ulong a)
  234. {
  235. ulong c = 64;
  236. a &= (ulong)-(long)(a);
  237. if (a != 0) c--;
  238. if ((a & 0x00000000FFFFFFFF) != 0) c -= 32;
  239. if ((a & 0x0000FFFF0000FFFF) != 0) c -= 16;
  240. if ((a & 0x00FF00FF00FF00FF) != 0) c -= 8;
  241. if ((a & 0x0F0F0F0F0F0F0F0F) != 0) c -= 4;
  242. if ((a & 0x3333333333333333) != 0) c -= 2;
  243. if ((a & 0x5555555555555555) != 0) c -= 1;
  244. return c;
  245. }
  246. }
  247. }
  248. }