aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Memory/Tracking/BitMap.cs
blob: 8f0086fc1089a001e48b543a1b4cc58f5305dbf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System.Runtime.CompilerServices;

namespace Ryujinx.Memory.Tracking
{
    /// <summary>
    /// A bitmap that can check or set large ranges of true/false values at once.
    /// </summary>
    readonly struct BitMap
    {
        public const int IntSize = 64;

        private const int IntShift = 6;
        private const int IntMask = IntSize - 1;

        /// <summary>
        /// Masks representing the bitmap. Least significant bit first, 64-bits per mask.
        /// </summary>
        public readonly long[] Masks;

        /// <summary>
        /// Create a new bitmap.
        /// </summary>
        /// <param name="count">The number of bits to reserve</param>
        public BitMap(int count)
        {
            Masks = new long[(count + IntMask) / IntSize];
        }

        /// <summary>
        /// Check if any bit in the bitmap is set.
        /// </summary>
        /// <returns>True if any bits are set, false otherwise</returns>
        public bool AnySet()
        {
            for (int i = 0; i < Masks.Length; i++)
            {
                if (Masks[i] != 0)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Check if a bit in the bitmap is set.
        /// </summary>
        /// <param name="bit">The bit index to check</param>
        /// <returns>True if the bit is set, false otherwise</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsSet(int bit)
        {
            int wordIndex = bit >> IntShift;
            int wordBit = bit & IntMask;

            long wordMask = 1L << wordBit;

            return (Masks[wordIndex] & wordMask) != 0;
        }

        /// <summary>
        /// Check if any bit in a range of bits in the bitmap are set. (inclusive)
        /// </summary>
        /// <param name="start">The first bit index to check</param>
        /// <param name="end">The last bit index to check</param>
        /// <returns>True if a bit is set, false otherwise</returns>
        public bool IsSet(int start, int end)
        {
            if (start == end)
            {
                return IsSet(start);
            }

            int startIndex = start >> IntShift;
            int startBit = start & IntMask;
            long startMask = -1L << startBit;

            int endIndex = end >> IntShift;
            int endBit = end & IntMask;
            long endMask = (long)(ulong.MaxValue >> (IntMask - endBit));

            if (startIndex == endIndex)
            {
                return (Masks[startIndex] & startMask & endMask) != 0;
            }

            if ((Masks[startIndex] & startMask) != 0)
            {
                return true;
            }

            for (int i = startIndex + 1; i < endIndex; i++)
            {
                if (Masks[i] != 0)
                {
                    return true;
                }
            }

            if ((Masks[endIndex] & endMask) != 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Set a bit at a specific index to 1.
        /// </summary>
        /// <param name="bit">The bit index to set</param>
        /// <returns>True if the bit is set, false if it was already set</returns>
        public bool Set(int bit)
        {
            int wordIndex = bit >> IntShift;
            int wordBit = bit & IntMask;

            long wordMask = 1L << wordBit;

            if ((Masks[wordIndex] & wordMask) != 0)
            {
                return false;
            }

            Masks[wordIndex] |= wordMask;

            return true;
        }

        /// <summary>
        /// Set a range of bits in the bitmap to 1.
        /// </summary>
        /// <param name="start">The first bit index to set</param>
        /// <param name="end">The last bit index to set</param>
        public void SetRange(int start, int end)
        {
            if (start == end)
            {
                Set(start);
                return;
            }

            int startIndex = start >> IntShift;
            int startBit = start & IntMask;
            long startMask = -1L << startBit;

            int endIndex = end >> IntShift;
            int endBit = end & IntMask;
            long endMask = (long)(ulong.MaxValue >> (IntMask - endBit));

            if (startIndex == endIndex)
            {
                Masks[startIndex] |= startMask & endMask;
            }
            else
            {
                Masks[startIndex] |= startMask;

                for (int i = startIndex + 1; i < endIndex; i++)
                {
                    Masks[i] |= -1;
                }

                Masks[endIndex] |= endMask;
            }
        }

        /// <summary>
        /// Clear a bit at a specific index to 0.
        /// </summary>
        /// <param name="bit">The bit index to clear</param>
        /// <returns>True if the bit was set, false if it was not</returns>
        public bool Clear(int bit)
        {
            int wordIndex = bit >> IntShift;
            int wordBit = bit & IntMask;

            long wordMask = 1L << wordBit;

            bool wasSet = (Masks[wordIndex] & wordMask) != 0;

            Masks[wordIndex] &= ~wordMask;

            return wasSet;
        }

        /// <summary>
        /// Clear the bitmap entirely, setting all bits to 0.
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < Masks.Length; i++)
            {
                Masks[i] = 0;
            }
        }
    }
}