aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Texture/Astc/Bits.cs
blob: bedc14f78f28a74c40f133609ef65a71a9869850 (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
namespace Ryujinx.Graphics.Texture.Astc
{
    internal static class Bits
    {
        public static readonly ushort[] Replicate8_16Table;
        public static readonly byte[] Replicate1_7Table;

        static Bits()
        {
            Replicate8_16Table = new ushort[0x200];
            Replicate1_7Table = new byte[0x200];

            for (int i = 0; i < 0x200; i++)
            {
                Replicate8_16Table[i] = (ushort)Replicate(i, 8, 16);
                Replicate1_7Table[i] = (byte)Replicate(i, 1, 7);
            }
        }

        public static int Replicate8_16(int value)
        {
            return Replicate8_16Table[value];
        }

        public static int Replicate1_7(int value)
        {
            return Replicate1_7Table[value];
        }

        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;
        }

        // 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;
            }
        }
    }
}