aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Tests.Unicorn/SimdValue.cs
blob: 2b528430587e665a8a89565913bef9ec425ab359 (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;

namespace Ryujinx.Tests.Unicorn
{
    public struct SimdValue : IEquatable<SimdValue>
    {
        private ulong _e0;
        private ulong _e1;

        public SimdValue(ulong e0, ulong e1)
        {
            _e0 = e0;
            _e1 = e1;
        }

        public SimdValue(byte[] data)
        {
            _e0 = (ulong)BitConverter.ToInt64(data, 0);
            _e1 = (ulong)BitConverter.ToInt64(data, 8);
        }

        public float AsFloat()
        {
            return GetFloat(0);
        }

        public double AsDouble()
        {
            return GetDouble(0);
        }

        public float GetFloat(int index)
        {
            return BitConverter.Int32BitsToSingle(GetInt32(index));
        }

        public double GetDouble(int index)
        {
            return BitConverter.Int64BitsToDouble(GetInt64(index));
        }

        public int  GetInt32(int index) => (int)GetUInt32(index);
        public long GetInt64(int index) => (long)GetUInt64(index);

        public uint GetUInt32(int index)
        {
            switch (index)
            {
                case 0: return (uint)(_e0 >> 0);
                case 1: return (uint)(_e0 >> 32);
                case 2: return (uint)(_e1 >> 0);
                case 3: return (uint)(_e1 >> 32);
            }

            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public ulong GetUInt64(int index)
        {
            switch (index)
            {
                case 0: return _e0;
                case 1: return _e1;
            }

            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public byte[] ToArray()
        {
            byte[] e0Data = BitConverter.GetBytes(_e0);
            byte[] e1Data = BitConverter.GetBytes(_e1);

            byte[] data = new byte[16];

            Buffer.BlockCopy(e0Data, 0, data, 0, 8);
            Buffer.BlockCopy(e1Data, 0, data, 8, 8);

            return data;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(_e0, _e1);
        }

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

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

        public override bool Equals(object obj)
        {
            return obj is SimdValue vector && Equals(vector);
        }

        public bool Equals(SimdValue other)
        {
            return other._e0 == _e0 && other._e1 == _e1;
        }

        public override string ToString()
        {
            return $"0x{_e1:X16}{_e0:X16}";
        }
    }
}