V128.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. namespace ARMeilleure.State
  3. {
  4. public struct V128 : IEquatable<V128>
  5. {
  6. private ulong _e0;
  7. private ulong _e1;
  8. private static V128 _zero = new V128(0, 0);
  9. public static V128 Zero => _zero;
  10. public V128(float value) : this(value, 0, 0, 0) { }
  11. public V128(double value) : this(value, 0) { }
  12. public V128(float e0, float e1, float e2, float e3)
  13. {
  14. _e0 = (ulong)(uint)BitConverter.SingleToInt32Bits(e0) << 0;
  15. _e0 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e1) << 32;
  16. _e1 = (ulong)(uint)BitConverter.SingleToInt32Bits(e2) << 0;
  17. _e1 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e3) << 32;
  18. }
  19. public V128(double e0, double e1)
  20. {
  21. _e0 = (ulong)BitConverter.DoubleToInt64Bits(e0);
  22. _e1 = (ulong)BitConverter.DoubleToInt64Bits(e1);
  23. }
  24. public V128(int e0, int e1, int e2, int e3)
  25. {
  26. _e0 = (ulong)(uint)e0 << 0;
  27. _e0 |= (ulong)(uint)e1 << 32;
  28. _e1 = (ulong)(uint)e2 << 0;
  29. _e1 |= (ulong)(uint)e3 << 32;
  30. }
  31. public V128(uint e0, uint e1, uint e2, uint e3)
  32. {
  33. _e0 = (ulong)e0 << 0;
  34. _e0 |= (ulong)e1 << 32;
  35. _e1 = (ulong)e2 << 0;
  36. _e1 |= (ulong)e3 << 32;
  37. }
  38. public V128(long e0, long e1)
  39. {
  40. _e0 = (ulong)e0;
  41. _e1 = (ulong)e1;
  42. }
  43. public V128(ulong e0, ulong e1)
  44. {
  45. _e0 = e0;
  46. _e1 = e1;
  47. }
  48. public V128(byte[] data)
  49. {
  50. _e0 = (ulong)BitConverter.ToInt64(data, 0);
  51. _e1 = (ulong)BitConverter.ToInt64(data, 8);
  52. }
  53. public void Insert(int index, uint value)
  54. {
  55. switch (index)
  56. {
  57. case 0: _e0 = (_e0 & 0xffffffff00000000) | ((ulong)value << 0); break;
  58. case 1: _e0 = (_e0 & 0x00000000ffffffff) | ((ulong)value << 32); break;
  59. case 2: _e1 = (_e1 & 0xffffffff00000000) | ((ulong)value << 0); break;
  60. case 3: _e1 = (_e1 & 0x00000000ffffffff) | ((ulong)value << 32); break;
  61. default: throw new ArgumentOutOfRangeException(nameof(index));
  62. }
  63. }
  64. public void Insert(int index, ulong value)
  65. {
  66. switch (index)
  67. {
  68. case 0: _e0 = value; break;
  69. case 1: _e1 = value; break;
  70. default: throw new ArgumentOutOfRangeException(nameof(index));
  71. }
  72. }
  73. public float AsFloat()
  74. {
  75. return GetFloat(0);
  76. }
  77. public double AsDouble()
  78. {
  79. return GetDouble(0);
  80. }
  81. public float GetFloat(int index)
  82. {
  83. return BitConverter.Int32BitsToSingle(GetInt32(index));
  84. }
  85. public double GetDouble(int index)
  86. {
  87. return BitConverter.Int64BitsToDouble(GetInt64(index));
  88. }
  89. public int GetInt32(int index) => (int)GetUInt32(index);
  90. public long GetInt64(int index) => (long)GetUInt64(index);
  91. public uint GetUInt32(int index)
  92. {
  93. switch (index)
  94. {
  95. case 0: return (uint)(_e0 >> 0);
  96. case 1: return (uint)(_e0 >> 32);
  97. case 2: return (uint)(_e1 >> 0);
  98. case 3: return (uint)(_e1 >> 32);
  99. }
  100. throw new ArgumentOutOfRangeException(nameof(index));
  101. }
  102. public ulong GetUInt64(int index)
  103. {
  104. switch (index)
  105. {
  106. case 0: return _e0;
  107. case 1: return _e1;
  108. }
  109. throw new ArgumentOutOfRangeException(nameof(index));
  110. }
  111. public byte[] ToArray()
  112. {
  113. byte[] e0Data = BitConverter.GetBytes(_e0);
  114. byte[] e1Data = BitConverter.GetBytes(_e1);
  115. byte[] data = new byte[16];
  116. Buffer.BlockCopy(e0Data, 0, data, 0, 8);
  117. Buffer.BlockCopy(e1Data, 0, data, 8, 8);
  118. return data;
  119. }
  120. public override int GetHashCode()
  121. {
  122. return HashCode.Combine(_e0, _e1);
  123. }
  124. public static V128 operator ~(V128 x)
  125. {
  126. return new V128(~x._e0, ~x._e1);
  127. }
  128. public static V128 operator &(V128 x, V128 y)
  129. {
  130. return new V128(x._e0 & y._e0, x._e1 & y._e1);
  131. }
  132. public static V128 operator |(V128 x, V128 y)
  133. {
  134. return new V128(x._e0 | y._e0, x._e1 | y._e1);
  135. }
  136. public static V128 operator ^(V128 x, V128 y)
  137. {
  138. return new V128(x._e0 ^ y._e0, x._e1 ^ y._e1);
  139. }
  140. public static V128 operator <<(V128 x, int shift)
  141. {
  142. ulong shiftOut = x._e0 >> (64 - shift);
  143. return new V128(x._e0 << shift, (x._e1 << shift) | shiftOut);
  144. }
  145. public static V128 operator >>(V128 x, int shift)
  146. {
  147. ulong shiftOut = x._e1 & ((1UL << shift) - 1);
  148. return new V128((x._e0 >> shift) | (shiftOut << (64 - shift)), x._e1 >> shift);
  149. }
  150. public static bool operator ==(V128 x, V128 y)
  151. {
  152. return x.Equals(y);
  153. }
  154. public static bool operator !=(V128 x, V128 y)
  155. {
  156. return !x.Equals(y);
  157. }
  158. public override bool Equals(object obj)
  159. {
  160. return obj is V128 vector && Equals(vector);
  161. }
  162. public bool Equals(V128 other)
  163. {
  164. return other._e0 == _e0 && other._e1 == _e1;
  165. }
  166. public override string ToString()
  167. {
  168. return $"0x{_e1:X16}{_e0:X16}";
  169. }
  170. }
  171. }