aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities/BitUtils.cs
blob: 4ce7da6b533cf9dc5500df6a6f19d6fd9763c563 (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
namespace Ryujinx.Common
{
    public static class BitUtils
    {
        private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };

        public static uint AlignUp(uint value, int size)
        {
            return (uint)AlignUp((int)value, size);
        }

        public static int AlignUp(int value, int size)
        {
            return (value + (size - 1)) & -size;
        }

        public static ulong AlignUp(ulong value, int size)
        {
            return (ulong)AlignUp((long)value, size);
        }

        public static long AlignUp(long value, int size)
        {
            return (value + (size - 1)) & -(long)size;
        }

        public static uint AlignDown(uint value, int size)
        {
            return (uint)AlignDown((int)value, size);
        }

        public static int AlignDown(int value, int size)
        {
            return value & -size;
        }

        public static ulong AlignDown(ulong value, int size)
        {
            return (ulong)AlignDown((long)value, size);
        }

        public static long AlignDown(long value, int size)
        {
            return value & -(long)size;
        }

        public static int DivRoundUp(int value, int dividend)
        {
            return (value + dividend - 1) / dividend;
        }

        public static ulong DivRoundUp(ulong value, uint dividend)
        {
            return (value + dividend - 1) / dividend;
        }

        public static long DivRoundUp(long value, int dividend)
        {
            return (value + dividend - 1) / dividend;
        }

        public static int Pow2RoundUp(int value)
        {
            value--;

            value |= (value >>  1);
            value |= (value >>  2);
            value |= (value >>  4);
            value |= (value >>  8);
            value |= (value >> 16);

            return ++value;
        }

        public static int Pow2RoundDown(int value)
        {
            return IsPowerOfTwo32(value) ? value : Pow2RoundUp(value) >> 1;
        }

        public static bool IsPowerOfTwo32(int value)
        {
            return value != 0 && (value & (value - 1)) == 0;
        }

        public static bool IsPowerOfTwo64(long value)
        {
            return value != 0 && (value & (value - 1)) == 0;
        }

        public static int CountLeadingZeros32(int value)
        {
            return (int)CountLeadingZeros((ulong)value, 32);
        }

        public static int CountLeadingZeros64(long value)
        {
            return (int)CountLeadingZeros((ulong)value, 64);
        }

        private static ulong CountLeadingZeros(ulong value, int size)
        {
            if (value == 0ul)
            {
                return (ulong)size;
            }

            int nibbleIdx = size;
            int preCount, count = 0;

            do
            {
                nibbleIdx -= 4;
                preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
                count += preCount;
            }
            while (preCount == 4);

            return (ulong)count;
        }

        public static int CountTrailingZeros32(int value)
        {
            int count = 0;

            while (((value >> count) & 1) == 0)
            {
                count++;
            }

            return count;
        }

        public static long ReverseBits64(long value)
        {
            return (long)ReverseBits64((ulong)value);
        }

        private static ulong ReverseBits64(ulong value)
        {
            value = ((value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((value & 0x5555555555555555) << 1 );
            value = ((value & 0xcccccccccccccccc) >> 2 ) | ((value & 0x3333333333333333) << 2 );
            value = ((value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((value & 0x0f0f0f0f0f0f0f0f) << 4 );
            value = ((value & 0xff00ff00ff00ff00) >> 8 ) | ((value & 0x00ff00ff00ff00ff) << 8 );
            value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16);

            return (value >> 32) | (value << 32);
        }
    }
}