aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Ngc/Detail/SbvRank.cs
blob: c541b1f57da8ac6037d3415bb6f645c8f7eff38c (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
using System;
using System.Numerics;

namespace Ryujinx.Horizon.Sdk.Ngc.Detail
{
    class SbvRank
    {
        private const int BitsPerWord = Set.BitsPerWord;
        private const int Rank1Entries = 8;
        private const int BitsPerRank0Entry = BitsPerWord * Rank1Entries;

        private uint[] _rank0;
        private byte[] _rank1;

        public SbvRank()
        {
        }

        public SbvRank(ReadOnlySpan<uint> bitmap, int setCapacity)
        {
            Build(bitmap, setCapacity);
        }

        public void Build(ReadOnlySpan<uint> bitmap, int setCapacity)
        {
            _rank0 = new uint[CalculateRank0Length(setCapacity)];
            _rank1 = new byte[CalculateRank1Length(setCapacity)];

            BuildRankDictionary(_rank0, _rank1, (setCapacity + BitsPerWord - 1) / BitsPerWord, bitmap);
        }

        private static void BuildRankDictionary(Span<uint> rank0, Span<byte> rank1, int length, ReadOnlySpan<uint> bitmap)
        {
            uint rank0Count;
            uint rank1Count = 0;

            for (int index = 0; index < length; index++)
            {
                if ((index % Rank1Entries) != 0)
                {
                    rank0Count = rank0[index / Rank1Entries];
                }
                else
                {
                    rank0[index / Rank1Entries] = rank1Count;
                    rank0Count = rank1Count;
                }

                rank1[index] = (byte)(rank1Count - rank0Count);

                rank1Count += (uint)BitOperations.PopCount(bitmap[index]);
            }
        }

        public bool Import(ref BinaryReader reader, int setCapacity)
        {
            if (setCapacity == 0)
            {
                return true;
            }

            int rank0Length = CalculateRank0Length(setCapacity);
            int rank1Length = CalculateRank1Length(setCapacity);

            return reader.AllocateAndReadArray(ref _rank0, rank0Length) == rank0Length &&
                reader.AllocateAndReadArray(ref _rank1, rank1Length) == rank1Length;
        }

        public int CalcRank1(int index, uint[] membershipBitmap)
        {
            int rank0Index = index / BitsPerRank0Entry;
            int rank1Index = index / BitsPerWord;

            uint membershipBits = membershipBitmap[rank1Index] & (uint.MaxValue >> (BitsPerWord - 1 - (index % BitsPerWord)));

            return (int)_rank0[rank0Index] + _rank1[rank1Index] + BitOperations.PopCount(membershipBits);
        }

        public int CalcSelect0(int index, int length, uint[] membershipBitmap)
        {
            int rank0Index;

            if (length > BitsPerRank0Entry)
            {
                int left = 0;
                int right = (length + BitsPerRank0Entry - 1) / BitsPerRank0Entry;

                while (true)
                {
                    int range = right - left;
                    if (range < 0)
                    {
                        range++;
                    }

                    int middle = left + (range / 2);

                    int foundIndex = middle * BitsPerRank0Entry - (int)_rank0[middle];

                    if ((uint)foundIndex <= (uint)index)
                    {
                        left = middle;
                    }
                    else
                    {
                        right = middle;
                    }

                    if (right <= left + 1)
                    {
                        break;
                    }
                }

                rank0Index = left;
            }
            else
            {
                rank0Index = 0;
            }

            int lengthInWords = (length + BitsPerWord - 1) / BitsPerWord;
            int rank1WordsCount = rank0Index == (length / BitsPerRank0Entry) && (lengthInWords % Rank1Entries) != 0
                ? lengthInWords % Rank1Entries
                : Rank1Entries;

            int baseIndex = (int)_rank0[rank0Index] + rank0Index * -BitsPerRank0Entry + index;
            int plainIndex;
            int count;
            int remainingBits;
            uint membershipBits;

            for (plainIndex = rank0Index * Rank1Entries - 1, count = 0; count < rank1WordsCount; plainIndex++, count++)
            {
                int currentIndex = baseIndex + count * -BitsPerWord;

                if (_rank1[plainIndex + 1] + currentIndex < 0)
                {
                    remainingBits = _rank1[plainIndex] + currentIndex + BitsPerWord;
                    membershipBits = ~membershipBitmap[plainIndex];

                    return plainIndex * BitsPerWord + SbvSelect.SelectPos(membershipBits, remainingBits);
                }
            }

            remainingBits = _rank1[plainIndex] + baseIndex + (rank1WordsCount - 1) * -BitsPerWord;
            membershipBits = ~membershipBitmap[plainIndex];

            return plainIndex * BitsPerWord + SbvSelect.SelectPos(membershipBits, remainingBits);
        }

        private static int CalculateRank0Length(int setCapacity)
        {
            return (setCapacity / (BitsPerWord * Rank1Entries)) + 1;
        }

        private static int CalculateRank1Length(int setCapacity)
        {
            return (setCapacity / BitsPerWord) + 1;
        }
    }
}