aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Graphics3d/Texture/BitArrayStream.cs
blob: 24069d722b6d3a2a79e3ad87bebc880bfc17379c (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
using System;
using System.Collections;

namespace Ryujinx.Graphics.Texture
{
    public class BitArrayStream
    {
        public BitArray BitsArray;

        public int Position { get; private set; }

        public BitArrayStream(BitArray bitArray)
        {
            BitsArray = bitArray;
            Position  = 0;
        }

        public short ReadBits(int length)
        {
            int retValue = 0;
            for (int i = Position; i < Position + length; i++)
            {
                if (BitsArray[i])
                {
                    retValue |= 1 << (i - Position);
                }
            }

            Position += length;
            return (short)retValue;
        }

        public int ReadBits(int start, int end)
        {
            int retValue = 0;
            for (int i = start; i <= end; i++)
            {
                if (BitsArray[i])
                {
                    retValue |= 1 << (i - start);
                }
            }

            return retValue;
        }

        public int ReadBit(int index)
        {
            return Convert.ToInt32(BitsArray[index]);
        }

        public void WriteBits(int value, int length)
        {
            for (int i = Position; i < Position + length; i++)
            {
                BitsArray[i] = ((value >> (i - Position)) & 1) != 0;
            }

            Position += length;
        }

        public byte[] ToByteArray()
        {
            byte[] retArray = new byte[(BitsArray.Length + 7) / 8];
            BitsArray.CopyTo(retArray, 0);
            return retArray;
        }

        public static int Replicate(int value, int numberBits, int toBit)
        {
            if (numberBits == 0) return 0;
            if (toBit == 0) return 0;

            int tempValue = value & ((1 << numberBits) - 1);
            int retValue  = tempValue;
            int resLength = numberBits;

            while (resLength < toBit)
            {
                int comp = 0;
                if (numberBits > toBit - resLength)
                {
                    int newShift = toBit - resLength;
                    comp         = numberBits - newShift;
                    numberBits   = newShift;
                }
                retValue <<= numberBits;
                retValue  |= tempValue >> comp;
                resLength += numberBits;
            }
            return retValue;
        }

        public static int PopCnt(int number)
        {
            int counter;
            for (counter = 0; number != 0; counter++)
            {
                number &= number - 1;
            }
            return counter;
        }

        public static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp = lhs;
            lhs = rhs;
            rhs = temp;
        }

        // Transfers a bit as described in C.2.14
        public static void BitTransferSigned(ref int a, ref int b)
        {
            b >>= 1;
            b |= a & 0x80;
            a >>= 1;
            a &= 0x3F;
            if ((a & 0x20) != 0) a -= 0x40;
        }
    }
}