/ src / Ryujinx.Tests.Unicorn / SimdValue.cs
SimdValue.cs
  1  using System;
  2  
  3  namespace Ryujinx.Tests.Unicorn
  4  {
  5      public readonly struct SimdValue : IEquatable<SimdValue>
  6      {
  7          private readonly ulong _e0;
  8          private readonly ulong _e1;
  9  
 10          public SimdValue(ulong e0, ulong e1)
 11          {
 12              _e0 = e0;
 13              _e1 = e1;
 14          }
 15  
 16          public SimdValue(byte[] data)
 17          {
 18              _e0 = (ulong)BitConverter.ToInt64(data, 0);
 19              _e1 = (ulong)BitConverter.ToInt64(data, 8);
 20          }
 21  
 22          public float AsFloat()
 23          {
 24              return GetFloat(0);
 25          }
 26  
 27          public double AsDouble()
 28          {
 29              return GetDouble(0);
 30          }
 31  
 32          public float GetFloat(int index)
 33          {
 34              return BitConverter.Int32BitsToSingle(GetInt32(index));
 35          }
 36  
 37          public double GetDouble(int index)
 38          {
 39              return BitConverter.Int64BitsToDouble(GetInt64(index));
 40          }
 41  
 42          public int GetInt32(int index) => (int)GetUInt32(index);
 43          public long GetInt64(int index) => (long)GetUInt64(index);
 44  
 45          public uint GetUInt32(int index)
 46          {
 47              return index switch
 48              {
 49                  0 => (uint)(_e0 >> 0),
 50                  1 => (uint)(_e0 >> 32),
 51                  2 => (uint)(_e1 >> 0),
 52                  3 => (uint)(_e1 >> 32),
 53                  _ => throw new ArgumentOutOfRangeException(nameof(index)),
 54              };
 55          }
 56  
 57          public ulong GetUInt64(int index)
 58          {
 59              return index switch
 60              {
 61                  0 => _e0,
 62                  1 => _e1,
 63                  _ => throw new ArgumentOutOfRangeException(nameof(index)),
 64              };
 65          }
 66  
 67          public byte[] ToArray()
 68          {
 69              byte[] e0Data = BitConverter.GetBytes(_e0);
 70              byte[] e1Data = BitConverter.GetBytes(_e1);
 71  
 72              byte[] data = new byte[16];
 73  
 74              Buffer.BlockCopy(e0Data, 0, data, 0, 8);
 75              Buffer.BlockCopy(e1Data, 0, data, 8, 8);
 76  
 77              return data;
 78          }
 79  
 80          public override int GetHashCode()
 81          {
 82              return HashCode.Combine(_e0, _e1);
 83          }
 84  
 85          public static bool operator ==(SimdValue x, SimdValue y)
 86          {
 87              return x.Equals(y);
 88          }
 89  
 90          public static bool operator !=(SimdValue x, SimdValue y)
 91          {
 92              return !x.Equals(y);
 93          }
 94  
 95          public override bool Equals(object obj)
 96          {
 97              return obj is SimdValue vector && Equals(vector);
 98          }
 99  
100          public bool Equals(SimdValue other)
101          {
102              return other._e0 == _e0 && other._e1 == _e1;
103          }
104  
105          public override string ToString()
106          {
107              return $"0x{_e1:X16}{_e0:X16}";
108          }
109      }
110  }