UInt128.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.HLE.Utilities
  6. {
  7. [StructLayout(LayoutKind.Sequential)]
  8. public struct UInt128 : IEquatable<UInt128>
  9. {
  10. public readonly long Low;
  11. public readonly long High;
  12. public bool IsNull => (Low | High) == 0;
  13. public UInt128(long low, long high)
  14. {
  15. Low = low;
  16. High = high;
  17. }
  18. public UInt128(byte[] bytes)
  19. {
  20. Low = BitConverter.ToInt64(bytes, 0);
  21. High = BitConverter.ToInt64(bytes, 8);
  22. }
  23. public UInt128(string hex)
  24. {
  25. if (hex == null || hex.Length != 32 || !hex.All("0123456789abcdefABCDEF".Contains))
  26. {
  27. throw new ArgumentException("Invalid Hex value!", nameof(hex));
  28. }
  29. Low = Convert.ToInt64(hex.Substring(16), 16);
  30. High = Convert.ToInt64(hex.Substring(0, 16), 16);
  31. }
  32. public void Write(BinaryWriter binaryWriter)
  33. {
  34. binaryWriter.Write(Low);
  35. binaryWriter.Write(High);
  36. }
  37. public override string ToString()
  38. {
  39. return High.ToString("x16") + Low.ToString("x16");
  40. }
  41. public static bool operator ==(UInt128 x, UInt128 y)
  42. {
  43. return x.Equals(y);
  44. }
  45. public static bool operator !=(UInt128 x, UInt128 y)
  46. {
  47. return !x.Equals(y);
  48. }
  49. public override bool Equals(object obj)
  50. {
  51. return obj is UInt128 uint128 && Equals(uint128);
  52. }
  53. public bool Equals(UInt128 cmpObj)
  54. {
  55. return Low == cmpObj.Low && High == cmpObj.High;
  56. }
  57. public override int GetHashCode()
  58. {
  59. return HashCode.Combine(Low, High);
  60. }
  61. }
  62. }