SimdValue.cs 2.6 KB

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