BitMap.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Runtime.CompilerServices;
  2. namespace Ryujinx.Memory.Tracking
  3. {
  4. /// <summary>
  5. /// A bitmap that can check or set large ranges of true/false values at once.
  6. /// </summary>
  7. readonly struct BitMap
  8. {
  9. public const int IntSize = 64;
  10. private const int IntShift = 6;
  11. private const int IntMask = IntSize - 1;
  12. /// <summary>
  13. /// Masks representing the bitmap. Least significant bit first, 64-bits per mask.
  14. /// </summary>
  15. public readonly long[] Masks;
  16. /// <summary>
  17. /// Create a new bitmap.
  18. /// </summary>
  19. /// <param name="count">The number of bits to reserve</param>
  20. public BitMap(int count)
  21. {
  22. Masks = new long[(count + IntMask) / IntSize];
  23. }
  24. /// <summary>
  25. /// Check if any bit in the bitmap is set.
  26. /// </summary>
  27. /// <returns>True if any bits are set, false otherwise</returns>
  28. public bool AnySet()
  29. {
  30. for (int i = 0; i < Masks.Length; i++)
  31. {
  32. if (Masks[i] != 0)
  33. {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. /// <summary>
  40. /// Check if a bit in the bitmap is set.
  41. /// </summary>
  42. /// <param name="bit">The bit index to check</param>
  43. /// <returns>True if the bit is set, false otherwise</returns>
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public bool IsSet(int bit)
  46. {
  47. int wordIndex = bit >> IntShift;
  48. int wordBit = bit & IntMask;
  49. long wordMask = 1L << wordBit;
  50. return (Masks[wordIndex] & wordMask) != 0;
  51. }
  52. /// <summary>
  53. /// Check if any bit in a range of bits in the bitmap are set. (inclusive)
  54. /// </summary>
  55. /// <param name="start">The first bit index to check</param>
  56. /// <param name="end">The last bit index to check</param>
  57. /// <returns>True if a bit is set, false otherwise</returns>
  58. public bool IsSet(int start, int end)
  59. {
  60. if (start == end)
  61. {
  62. return IsSet(start);
  63. }
  64. int startIndex = start >> IntShift;
  65. int startBit = start & IntMask;
  66. long startMask = -1L << startBit;
  67. int endIndex = end >> IntShift;
  68. int endBit = end & IntMask;
  69. long endMask = (long)(ulong.MaxValue >> (IntMask - endBit));
  70. if (startIndex == endIndex)
  71. {
  72. return (Masks[startIndex] & startMask & endMask) != 0;
  73. }
  74. if ((Masks[startIndex] & startMask) != 0)
  75. {
  76. return true;
  77. }
  78. for (int i = startIndex + 1; i < endIndex; i++)
  79. {
  80. if (Masks[i] != 0)
  81. {
  82. return true;
  83. }
  84. }
  85. if ((Masks[endIndex] & endMask) != 0)
  86. {
  87. return true;
  88. }
  89. return false;
  90. }
  91. /// <summary>
  92. /// Set a bit at a specific index to 1.
  93. /// </summary>
  94. /// <param name="bit">The bit index to set</param>
  95. /// <returns>True if the bit is set, false if it was already set</returns>
  96. public bool Set(int bit)
  97. {
  98. int wordIndex = bit >> IntShift;
  99. int wordBit = bit & IntMask;
  100. long wordMask = 1L << wordBit;
  101. if ((Masks[wordIndex] & wordMask) != 0)
  102. {
  103. return false;
  104. }
  105. Masks[wordIndex] |= wordMask;
  106. return true;
  107. }
  108. /// <summary>
  109. /// Set a range of bits in the bitmap to 1.
  110. /// </summary>
  111. /// <param name="start">The first bit index to set</param>
  112. /// <param name="end">The last bit index to set</param>
  113. public void SetRange(int start, int end)
  114. {
  115. if (start == end)
  116. {
  117. Set(start);
  118. return;
  119. }
  120. int startIndex = start >> IntShift;
  121. int startBit = start & IntMask;
  122. long startMask = -1L << startBit;
  123. int endIndex = end >> IntShift;
  124. int endBit = end & IntMask;
  125. long endMask = (long)(ulong.MaxValue >> (IntMask - endBit));
  126. if (startIndex == endIndex)
  127. {
  128. Masks[startIndex] |= startMask & endMask;
  129. }
  130. else
  131. {
  132. Masks[startIndex] |= startMask;
  133. for (int i = startIndex + 1; i < endIndex; i++)
  134. {
  135. Masks[i] |= -1;
  136. }
  137. Masks[endIndex] |= endMask;
  138. }
  139. }
  140. /// <summary>
  141. /// Clear a bit at a specific index to 0.
  142. /// </summary>
  143. /// <param name="bit">The bit index to clear</param>
  144. /// <returns>True if the bit was set, false if it was not</returns>
  145. public bool Clear(int bit)
  146. {
  147. int wordIndex = bit >> IntShift;
  148. int wordBit = bit & IntMask;
  149. long wordMask = 1L << wordBit;
  150. bool wasSet = (Masks[wordIndex] & wordMask) != 0;
  151. Masks[wordIndex] &= ~wordMask;
  152. return wasSet;
  153. }
  154. /// <summary>
  155. /// Clear the bitmap entirely, setting all bits to 0.
  156. /// </summary>
  157. public void Clear()
  158. {
  159. for (int i = 0; i < Masks.Length; i++)
  160. {
  161. Masks[i] = 0;
  162. }
  163. }
  164. }
  165. }