BitOperations.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.Intrinsics.X86;
  7. using Internal.Runtime.CompilerServices;
  8. // Some routines inspired by the Stanford Bit Twiddling Hacks by Sean Eron Anderson:
  9. // http://graphics.stanford.edu/~seander/bithacks.html
  10. namespace System.Numerics
  11. {
  12. /// <summary>
  13. /// Utility methods for intrinsic bit-twiddling operations.
  14. /// The methods use hardware intrinsics when available on the underlying platform,
  15. /// otherwise they use optimized software fallbacks.
  16. /// </summary>
  17. public static class BitOperations
  18. {
  19. // C# no-alloc optimization that directly wraps the data section of the dll (similar to string constants)
  20. // https://github.com/dotnet/roslyn/pull/24621
  21. private static ReadOnlySpan<byte> s_TrailingZeroCountDeBruijn => new byte[32]
  22. {
  23. 00, 01, 28, 02, 29, 14, 24, 03,
  24. 30, 22, 20, 15, 25, 17, 04, 08,
  25. 31, 27, 13, 23, 21, 19, 16, 07,
  26. 26, 12, 18, 06, 11, 05, 10, 09
  27. };
  28. private static ReadOnlySpan<byte> s_Log2DeBruijn => new byte[32]
  29. {
  30. 00, 09, 01, 10, 13, 21, 02, 29,
  31. 11, 14, 16, 18, 22, 25, 03, 30,
  32. 08, 12, 20, 28, 15, 17, 24, 07,
  33. 19, 27, 23, 06, 26, 05, 04, 31
  34. };
  35. /// <summary>
  36. /// Count the number of leading zero bits in a mask.
  37. /// Similar in behavior to the x86 instruction LZCNT.
  38. /// </summary>
  39. /// <param name="value">The value.</param>
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. [CLSCompliant(false)]
  42. public static int LeadingZeroCount(uint value)
  43. {
  44. if (Lzcnt.IsSupported)
  45. {
  46. // LZCNT contract is 0->32
  47. return (int)Lzcnt.LeadingZeroCount(value);
  48. }
  49. // Unguarded fallback contract is 0->31
  50. if (value == 0)
  51. {
  52. return 32;
  53. }
  54. return 31 - Log2SoftwareFallback(value);
  55. }
  56. /// <summary>
  57. /// Count the number of leading zero bits in a mask.
  58. /// Similar in behavior to the x86 instruction LZCNT.
  59. /// </summary>
  60. /// <param name="value">The value.</param>
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. [CLSCompliant(false)]
  63. public static int LeadingZeroCount(ulong value)
  64. {
  65. if (Lzcnt.X64.IsSupported)
  66. {
  67. // LZCNT contract is 0->64
  68. return (int)Lzcnt.X64.LeadingZeroCount(value);
  69. }
  70. uint hi = (uint)(value >> 32);
  71. if (hi == 0)
  72. {
  73. return 32 + LeadingZeroCount((uint)value);
  74. }
  75. return LeadingZeroCount(hi);
  76. }
  77. /// <summary>
  78. /// Returns the integer (floor) log of the specified value, base 2.
  79. /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
  80. /// </summary>
  81. /// <param name="value">The value.</param>
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. [CLSCompliant(false)]
  84. public static int Log2(uint value)
  85. {
  86. // value lzcnt actual expected
  87. // ..0000 32 0 0 (by convention, guard clause)
  88. // ..0001 31 31-31 0
  89. // ..0010 30 31-30 1
  90. // 0010.. 2 31-2 29
  91. // 0100.. 1 31-1 30
  92. // 1000.. 0 31-0 31
  93. if (Lzcnt.IsSupported)
  94. {
  95. // Enforce conventional contract 0->0 (Log(0) is undefined)
  96. if (value == 0)
  97. {
  98. return 0;
  99. }
  100. // LZCNT contract is 0->32
  101. return 31 - (int)Lzcnt.LeadingZeroCount(value);
  102. }
  103. // Fallback contract is 0->0
  104. return Log2SoftwareFallback(value);
  105. }
  106. /// <summary>
  107. /// Returns the integer (floor) log of the specified value, base 2.
  108. /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
  109. /// </summary>
  110. /// <param name="value">The value.</param>
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. [CLSCompliant(false)]
  113. public static int Log2(ulong value)
  114. {
  115. if (Lzcnt.X64.IsSupported)
  116. {
  117. // Enforce conventional contract 0->0 (Log(0) is undefined)
  118. if (value == 0)
  119. {
  120. return 0;
  121. }
  122. // LZCNT contract is 0->64
  123. return 63 - (int)Lzcnt.X64.LeadingZeroCount(value);
  124. }
  125. uint hi = (uint)(value >> 32);
  126. if (hi == 0)
  127. {
  128. return Log2((uint)value);
  129. }
  130. return 32 + Log2(hi);
  131. }
  132. /// <summary>
  133. /// Returns the integer (floor) log of the specified value, base 2.
  134. /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
  135. /// Does not directly use any hardware intrinsics, nor does it incur branching.
  136. /// </summary>
  137. /// <param name="value">The value.</param>
  138. private static int Log2SoftwareFallback(uint value)
  139. {
  140. // No AggressiveInlining due to large method size
  141. // Has conventional contract 0->0 (Log(0) is undefined)
  142. // Fill trailing zeros with ones, eg 00010010 becomes 00011111
  143. value |= value >> 01;
  144. value |= value >> 02;
  145. value |= value >> 04;
  146. value |= value >> 08;
  147. value |= value >> 16;
  148. // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check
  149. return Unsafe.AddByteOffset(
  150. // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u
  151. ref MemoryMarshal.GetReference(s_Log2DeBruijn),
  152. // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here
  153. (IntPtr)(int)((value * 0x07C4ACDDu) >> 27));
  154. }
  155. /// <summary>
  156. /// Returns the population count (number of bits set) of a mask.
  157. /// Similar in behavior to the x86 instruction POPCNT.
  158. /// </summary>
  159. /// <param name="value">The value.</param>
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. [CLSCompliant(false)]
  162. public static int PopCount(uint value)
  163. {
  164. if (Popcnt.IsSupported)
  165. {
  166. return (int)Popcnt.PopCount(value);
  167. }
  168. return SoftwareFallback(value);
  169. static int SoftwareFallback(uint value)
  170. {
  171. const uint c1 = 0x_55555555u;
  172. const uint c2 = 0x_33333333u;
  173. const uint c3 = 0x_0F0F0F0Fu;
  174. const uint c4 = 0x_01010101u;
  175. value = value - ((value >> 1) & c1);
  176. value = (value & c2) + ((value >> 2) & c2);
  177. value = (((value + (value >> 4)) & c3) * c4) >> 24;
  178. return (int)value;
  179. }
  180. }
  181. /// <summary>
  182. /// Returns the population count (number of bits set) of a mask.
  183. /// Similar in behavior to the x86 instruction POPCNT.
  184. /// </summary>
  185. /// <param name="value">The value.</param>
  186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  187. [CLSCompliant(false)]
  188. public static int PopCount(ulong value)
  189. {
  190. if (Popcnt.X64.IsSupported)
  191. {
  192. return (int)Popcnt.X64.PopCount(value);
  193. }
  194. #if BIT32
  195. return PopCount((uint)value) // lo
  196. + PopCount((uint)(value >> 32)); // hi
  197. #else
  198. return SoftwareFallback(value);
  199. static int SoftwareFallback(ulong value)
  200. {
  201. const ulong c1 = 0x_55555555_55555555ul;
  202. const ulong c2 = 0x_33333333_33333333ul;
  203. const ulong c3 = 0x_0F0F0F0F_0F0F0F0Ful;
  204. const ulong c4 = 0x_01010101_01010101ul;
  205. value = value - ((value >> 1) & c1);
  206. value = (value & c2) + ((value >> 2) & c2);
  207. value = (((value + (value >> 4)) & c3) * c4) >> 56;
  208. return (int)value;
  209. }
  210. #endif
  211. }
  212. /// <summary>
  213. /// Count the number of trailing zero bits in an integer value.
  214. /// Similar in behavior to the x86 instruction TZCNT.
  215. /// </summary>
  216. /// <param name="value">The value.</param>
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. public static int TrailingZeroCount(int value)
  219. => TrailingZeroCount((uint)value);
  220. /// <summary>
  221. /// Count the number of trailing zero bits in an integer value.
  222. /// Similar in behavior to the x86 instruction TZCNT.
  223. /// </summary>
  224. /// <param name="value">The value.</param>
  225. [CLSCompliant(false)]
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. public static int TrailingZeroCount(uint value)
  228. {
  229. if (Bmi1.IsSupported)
  230. {
  231. // TZCNT contract is 0->32
  232. return (int)Bmi1.TrailingZeroCount(value);
  233. }
  234. // Unguarded fallback contract is 0->0
  235. if (value == 0)
  236. {
  237. return 32;
  238. }
  239. // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check
  240. return Unsafe.AddByteOffset(
  241. // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_0111_1100_1011_0101_0011_0001u
  242. ref MemoryMarshal.GetReference(s_TrailingZeroCountDeBruijn),
  243. // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here
  244. (IntPtr)(int)(((value & (uint)-(int)value) * 0x077CB531u) >> 27)); // Multi-cast mitigates redundant conv.u8
  245. }
  246. /// <summary>
  247. /// Count the number of trailing zero bits in a mask.
  248. /// Similar in behavior to the x86 instruction TZCNT.
  249. /// </summary>
  250. /// <param name="value">The value.</param>
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. public static int TrailingZeroCount(long value)
  253. => TrailingZeroCount((ulong)value);
  254. /// <summary>
  255. /// Count the number of trailing zero bits in a mask.
  256. /// Similar in behavior to the x86 instruction TZCNT.
  257. /// </summary>
  258. /// <param name="value">The value.</param>
  259. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  260. [CLSCompliant(false)]
  261. public static int TrailingZeroCount(ulong value)
  262. {
  263. if (Bmi1.X64.IsSupported)
  264. {
  265. // TZCNT contract is 0->64
  266. return (int)Bmi1.X64.TrailingZeroCount(value);
  267. }
  268. uint lo = (uint)value;
  269. if (lo == 0)
  270. {
  271. return 32 + TrailingZeroCount((uint)(value >> 32));
  272. }
  273. return TrailingZeroCount(lo);
  274. }
  275. /// <summary>
  276. /// Rotates the specified value left by the specified number of bits.
  277. /// Similar in behavior to the x86 instruction ROL.
  278. /// </summary>
  279. /// <param name="value">The value to rotate.</param>
  280. /// <param name="offset">The number of bits to rotate by.
  281. /// Any value outside the range [0..31] is treated as congruent mod 32.</param>
  282. /// <returns>The rotated value.</returns>
  283. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  284. [CLSCompliant(false)]
  285. public static uint RotateLeft(uint value, int offset)
  286. => (value << offset) | (value >> (32 - offset));
  287. /// <summary>
  288. /// Rotates the specified value left by the specified number of bits.
  289. /// Similar in behavior to the x86 instruction ROL.
  290. /// </summary>
  291. /// <param name="value">The value to rotate.</param>
  292. /// <param name="offset">The number of bits to rotate by.
  293. /// Any value outside the range [0..63] is treated as congruent mod 64.</param>
  294. /// <returns>The rotated value.</returns>
  295. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  296. [CLSCompliant(false)]
  297. public static ulong RotateLeft(ulong value, int offset)
  298. => (value << offset) | (value >> (64 - offset));
  299. /// <summary>
  300. /// Rotates the specified value right by the specified number of bits.
  301. /// Similar in behavior to the x86 instruction ROR.
  302. /// </summary>
  303. /// <param name="value">The value to rotate.</param>
  304. /// <param name="offset">The number of bits to rotate by.
  305. /// Any value outside the range [0..31] is treated as congruent mod 32.</param>
  306. /// <returns>The rotated value.</returns>
  307. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  308. [CLSCompliant(false)]
  309. public static uint RotateRight(uint value, int offset)
  310. => (value >> offset) | (value << (32 - offset));
  311. /// <summary>
  312. /// Rotates the specified value right by the specified number of bits.
  313. /// Similar in behavior to the x86 instruction ROR.
  314. /// </summary>
  315. /// <param name="value">The value to rotate.</param>
  316. /// <param name="offset">The number of bits to rotate by.
  317. /// Any value outside the range [0..63] is treated as congruent mod 64.</param>
  318. /// <returns>The rotated value.</returns>
  319. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  320. [CLSCompliant(false)]
  321. public static ulong RotateRight(ulong value, int offset)
  322. => (value >> offset) | (value << (64 - offset));
  323. }
  324. }