V128.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. namespace ARMeilleure.State
  5. {
  6. /// <summary>
  7. /// Represents a 128-bit vector.
  8. /// </summary>
  9. [StructLayout(LayoutKind.Sequential, Size = 16)]
  10. public struct V128 : IEquatable<V128>
  11. {
  12. // _e0 & _e1 could be marked as readonly, however they are not readonly because we modify them through the Unsafe
  13. // APIs. This also means that one should be careful when changing the layout of this struct.
  14. private ulong _e0;
  15. private ulong _e1;
  16. /// <summary>
  17. /// Gets a new <see cref="V128"/> with all bits set to zero.
  18. /// </summary>
  19. public static V128 Zero => new V128(0, 0);
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="double"/> value
  22. /// as a scalar.
  23. /// </summary>
  24. /// <param name="value">Scalar value</param>
  25. public V128(double value) : this(value, 0) { }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="double"/> elements.
  28. /// </summary>
  29. /// <param name="e0">Element 0</param>
  30. /// <param name="e1">Element 1</param>
  31. public V128(double e0, double e1)
  32. {
  33. _e0 = (ulong)BitConverter.DoubleToInt64Bits(e0);
  34. _e1 = (ulong)BitConverter.DoubleToInt64Bits(e1);
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="float"/> value as a
  38. /// scalar.
  39. /// </summary>
  40. /// <param name="value">Scalar value</param>
  41. public V128(float value) : this(value, 0, 0, 0) { }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="float"/> elements.
  44. /// </summary>
  45. /// <param name="e0">Element 0</param>
  46. /// <param name="e1">Element 1</param>
  47. /// <param name="e2">Element 2</param>
  48. /// <param name="e3">Element 3</param>
  49. public V128(float e0, float e1, float e2, float e3)
  50. {
  51. _e0 = (ulong)(uint)BitConverter.SingleToInt32Bits(e0) << 0;
  52. _e0 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e1) << 32;
  53. _e1 = (ulong)(uint)BitConverter.SingleToInt32Bits(e2) << 0;
  54. _e1 |= (ulong)(uint)BitConverter.SingleToInt32Bits(e3) << 32;
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="ulong"/>
  58. /// elements.
  59. /// </summary>
  60. /// <param name="e0">Element 0</param>
  61. /// <param name="e1">Element 1</param>
  62. public V128(long e0, long e1) : this((ulong)e0, (ulong)e1) { }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="long"/> elements.
  65. /// </summary>
  66. /// <param name="e0">Element 0</param>
  67. /// <param name="e1">Element 1</param>
  68. public V128(ulong e0, ulong e1)
  69. {
  70. _e0 = e0;
  71. _e1 = e1;
  72. }
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="int"/> elements.
  75. /// </summary>
  76. /// <param name="e0">Element 0</param>
  77. /// <param name="e1">Element 1</param>
  78. /// <param name="e2">Element 2</param>
  79. /// <param name="e3">Element 3</param>
  80. public V128(int e0, int e1, int e2, int e3) : this((uint)e0, (uint)e1, (uint)e2, (uint)e3) { }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="V128"/> struct with the specified <see cref="uint"/> elements.
  83. /// </summary>
  84. /// <param name="e0">Element 0</param>
  85. /// <param name="e1">Element 1</param>
  86. /// <param name="e2">Element 2</param>
  87. /// <param name="e3">Element 3</param>
  88. public V128(uint e0, uint e1, uint e2, uint e3)
  89. {
  90. _e0 = (ulong)e0 << 0;
  91. _e0 |= (ulong)e1 << 32;
  92. _e1 = (ulong)e2 << 0;
  93. _e1 |= (ulong)e3 << 32;
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="V128"/> struct from the specified <see cref="byte"/> array.
  97. /// </summary>
  98. /// <param name="data"><see cref="byte"/> array to use</param>
  99. public V128(byte[] data)
  100. {
  101. _e0 = (ulong)BitConverter.ToInt64(data, 0);
  102. _e1 = (ulong)BitConverter.ToInt64(data, 8);
  103. }
  104. /// <summary>
  105. /// Returns the value of the <see cref="V128"/> as a <typeparamref name="T"/> scalar.
  106. /// </summary>
  107. /// <typeparam name="T">Type of scalar</typeparam>
  108. /// <returns>Value of the <see cref="V128"/> as a <typeparamref name="T"/> scalar</returns>
  109. /// <exception cref="ArgumentOutOfRangeException">Size of <typeparamref name="T"/> is larger than 16 bytes</exception>
  110. public T As<T>() where T : unmanaged
  111. {
  112. return Extract<T>(0);
  113. }
  114. /// <summary>
  115. /// Extracts the element at the specified index as a <typeparamref name="T"/> from the <see cref="V128"/>.
  116. /// </summary>
  117. /// <typeparam name="T">Element type</typeparam>
  118. /// <param name="index">Index of element</param>
  119. /// <returns>Element at the specified index as a <typeparamref name="T"/> from the <see cref="V128"/></returns>
  120. /// <exception cref="ArgumentOutOfRangeException">
  121. /// <paramref name="index"/> is out of bound or the size of <typeparamref name="T"/> is larger than 16 bytes
  122. /// </exception>
  123. public T Extract<T>(int index) where T : unmanaged
  124. {
  125. if ((uint)index >= GetElementCount<T>())
  126. ThrowIndexOutOfRange();
  127. // Performs:
  128. // return *((*T)this + index);
  129. return Unsafe.Add(ref Unsafe.As<V128, T>(ref this), index);
  130. }
  131. /// <summary>
  132. /// Inserts the specified value into the element at the specified index in the <see cref="V128"/>.
  133. /// </summary>
  134. /// <typeparam name="T">Element type</typeparam>
  135. /// <param name="index">Index of element</param>
  136. /// <param name="value">Value to insert</param>
  137. /// <exception cref="ArgumentOutOfRangeException">
  138. /// <paramref name="index"/> is out of bound or the size of <typeparamref name="T"/> is larger than 16 bytes
  139. /// </exception>
  140. public void Insert<T>(int index, T value) where T : unmanaged
  141. {
  142. if ((uint)index >= GetElementCount<T>())
  143. ThrowIndexOutOfRange();
  144. // Performs:
  145. // *((*T)this + index) = value;
  146. Unsafe.Add(ref Unsafe.As<V128, T>(ref this), index) = value;
  147. }
  148. /// <summary>
  149. /// Returns a new <see cref="byte"/> array which represents the <see cref="V128"/>.
  150. /// </summary>
  151. /// <returns>A new <see cref="byte"/> array which represents the <see cref="V128"/></returns>
  152. public byte[] ToArray()
  153. {
  154. byte[] data = new byte[16];
  155. Span<byte> span = data;
  156. BitConverter.TryWriteBytes(span, _e0);
  157. BitConverter.TryWriteBytes(span.Slice(8), _e1);
  158. return data;
  159. }
  160. /// <summary>
  161. /// Performs a bitwise logical left shift on the specified <see cref="V128"/> by the specified shift count.
  162. /// </summary>
  163. /// <param name="x"><see cref="V128"/> instance</param>
  164. /// <param name="shift">Number of shifts</param>
  165. /// <returns>Result of left shift</returns>
  166. /// <remarks>
  167. /// This supports shift counts up to 63; anything above may result in unexpected behaviour.
  168. /// </remarks>
  169. public static V128 operator <<(V128 x, int shift)
  170. {
  171. if (shift == 0)
  172. {
  173. return new V128(x._e0, x._e1);
  174. }
  175. ulong shiftOut = x._e0 >> (64 - shift);
  176. return new V128(x._e0 << shift, (x._e1 << shift) | shiftOut);
  177. }
  178. /// <summary>
  179. /// Performs a bitwise logical right shift on the specified <see cref="V128"/> by the specified shift count.
  180. /// </summary>
  181. /// <param name="x"><see cref="V128"/> instance</param>
  182. /// <param name="shift">Number of shifts</param>
  183. /// <returns>Result of right shift</returns>
  184. /// <remarks>
  185. /// This supports shift counts up to 63; anything above may result in unexpected behaviour.
  186. /// </remarks>
  187. public static V128 operator >>(V128 x, int shift)
  188. {
  189. if (shift == 0)
  190. {
  191. return new V128(x._e0, x._e1);
  192. }
  193. ulong shiftOut = x._e1 & ((1UL << shift) - 1);
  194. return new V128((x._e0 >> shift) | (shiftOut << (64 - shift)), x._e1 >> shift);
  195. }
  196. /// <summary>
  197. /// Performs a bitwise not on the specified <see cref="V128"/>.
  198. /// </summary>
  199. /// <param name="x">Target <see cref="V128"/></param>
  200. /// <returns>Result of not operation</returns>
  201. public static V128 operator ~(V128 x) => new V128(~x._e0, ~x._e1);
  202. /// <summary>
  203. /// Performs a bitwise and on the specified <see cref="V128"/> instances.
  204. /// </summary>
  205. /// <param name="x">First instance</param>
  206. /// <param name="y">Second instance</param>
  207. /// <returns>Result of and operation</returns>
  208. public static V128 operator &(V128 x, V128 y) => new V128(x._e0 & y._e0, x._e1 & y._e1);
  209. /// <summary>
  210. /// Performs a bitwise or on the specified <see cref="V128"/> instances.
  211. /// </summary>
  212. /// <param name="x">First instance</param>
  213. /// <param name="y">Second instance</param>
  214. /// <returns>Result of or operation</returns>
  215. public static V128 operator |(V128 x, V128 y) => new V128(x._e0 | y._e0, x._e1 | y._e1);
  216. /// <summary>
  217. /// Performs a bitwise exlusive or on the specified <see cref="V128"/> instances.
  218. /// </summary>
  219. /// <param name="x">First instance</param>
  220. /// <param name="y">Second instance</param>
  221. /// <returns>Result of exclusive or operation</returns>
  222. public static V128 operator ^(V128 x, V128 y) => new V128(x._e0 ^ y._e0, x._e1 ^ y._e1);
  223. /// <summary>
  224. /// Determines if the specified <see cref="V128"/> instances are equal.
  225. /// </summary>
  226. /// <param name="x">First instance</param>
  227. /// <param name="y">Second instance</param>
  228. /// <returns>true if equal; otherwise false</returns>
  229. public static bool operator ==(V128 x, V128 y) => x.Equals(y);
  230. /// <summary>
  231. /// Determines if the specified <see cref="V128"/> instances are not equal.
  232. /// </summary>
  233. /// <param name="x">First instance</param>
  234. /// <param name="y">Second instance</param>
  235. /// <returns>true if not equal; otherwise false</returns>
  236. public static bool operator !=(V128 x, V128 y) => !x.Equals(y);
  237. /// <summary>
  238. /// Determines if the specified <see cref="V128"/> is equal to this <see cref="V128"/> instance.
  239. /// </summary>
  240. /// <param name="other">Other <see cref="V128"/> instance</param>
  241. /// <returns>true if equal; otherwise false</returns>
  242. public bool Equals(V128 other)
  243. {
  244. return other._e0 == _e0 && other._e1 == _e1;
  245. }
  246. /// <summary>
  247. /// Determines if the specified <see cref="object"/> is equal to this <see cref="V128"/> instance.
  248. /// </summary>
  249. /// <param name="obj">Other <see cref="object"/> instance</param>
  250. /// <returns>true if equal; otherwise false</returns>
  251. public override bool Equals(object obj)
  252. {
  253. return obj is V128 vector && Equals(vector);
  254. }
  255. /// <inheritdoc/>
  256. public override int GetHashCode()
  257. {
  258. return HashCode.Combine(_e0, _e1);
  259. }
  260. /// <inheritdoc/>
  261. public override string ToString()
  262. {
  263. return $"0x{_e1:X16}{_e0:X16}";
  264. }
  265. private uint GetElementCount<T>() where T : unmanaged
  266. {
  267. return (uint)(Unsafe.SizeOf<V128>() / Unsafe.SizeOf<T>());
  268. }
  269. private static void ThrowIndexOutOfRange()
  270. {
  271. throw new ArgumentOutOfRangeException("index");
  272. }
  273. }
  274. }