aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/Translation/UInt128.cs
blob: ffbce77a1e924c5abe5f3c1eca552b4de176a635 (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
using System;
using System.Numerics;

namespace Ryujinx.Graphics.Shader.Translation
{
    struct UInt128 : IEquatable<UInt128>
    {
        public static UInt128 Zero => new UInt128() { _v0 = 0, _v1 = 0 };

        private ulong _v0;
        private ulong _v1;

        public UInt128(ulong low, ulong high)
        {
            _v0 = low;
            _v1 = high;
        }

        public int TrailingZeroCount()
        {
            int count = BitOperations.TrailingZeroCount(_v0);
            if (count == 64)
            {
                count += BitOperations.TrailingZeroCount(_v1);
            }

            return count;
        }

        public static UInt128 Pow2(int x)
        {
            if (x >= 64)
            {
                return new UInt128(0, 1UL << (x - 64));
            }

            return new UInt128(1UL << x, 0);
        }

        public static UInt128 operator ~(UInt128 x)
        {
            return new UInt128(~x._v0, ~x._v1);
        }

        public static UInt128 operator &(UInt128 x, UInt128 y)
        {
            return new UInt128(x._v0 & y._v0, x._v1 & y._v1);
        }

        public static UInt128 operator |(UInt128 x, UInt128 y)
        {
            return new UInt128(x._v0 | y._v0, x._v1 | y._v1);
        }

        public static UInt128 operator <<(UInt128 x, int shift)
        {
            if (shift == 0)
            {
                return new UInt128(x._v0, x._v1);
            }
            else if (shift >= 64)
            {
                return new UInt128(0, x._v0 << (shift - 64));
            }

            ulong shiftOut = x._v0 >> (64 - shift);

            return new UInt128(x._v0 << shift, (x._v1 << shift) | shiftOut);
        }

        public static UInt128 operator >>(UInt128 x, int shift)
        {
            if (shift == 0)
            {
                return new UInt128(x._v0, x._v1);
            }
            else if (shift >= 64)
            {
                return new UInt128(x._v1 >> (shift - 64), 0);
            }

            ulong shiftOut = x._v1 & ((1UL << shift) - 1);

            return new UInt128((x._v0 >> shift) | (shiftOut << (64 - shift)), x._v1 >> shift);
        }

        public static bool operator ==(UInt128 x, UInt128 y)
        {
            return x.Equals(y);
        }

        public static bool operator !=(UInt128 x, UInt128 y)
        {
            return !x.Equals(y);
        }

        public override bool Equals(object obj)
        {
            return obj is UInt128 other && Equals(other);
        }

        public bool Equals(UInt128 other)
        {
            return _v0 == other._v0 && _v1 == other._v1;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(_v0, _v1);
        }
    }
}