UInt128.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace Ryujinx.HLE.Utilities
  5. {
  6. public struct UInt128
  7. {
  8. public long High { get; private set; }
  9. public long Low { get; private set; }
  10. public bool IsNull => (Low | High) == 0;
  11. public UInt128(long low, long high)
  12. {
  13. Low = low;
  14. High = high;
  15. }
  16. public UInt128(byte[] bytes)
  17. {
  18. Low = BitConverter.ToInt64(bytes, 0);
  19. High = BitConverter.ToInt64(bytes, 8);
  20. }
  21. public UInt128(string hex)
  22. {
  23. if (hex == null || hex.Length != 32 || !hex.All("0123456789abcdefABCDEF".Contains))
  24. {
  25. throw new ArgumentException("Invalid Hex value!", nameof(hex));
  26. }
  27. Low = Convert.ToInt64(hex.Substring(16), 16);
  28. High = Convert.ToInt64(hex.Substring(0, 16), 16);
  29. }
  30. public void Write(BinaryWriter binaryWriter)
  31. {
  32. binaryWriter.Write(Low);
  33. binaryWriter.Write(High);
  34. }
  35. public override string ToString()
  36. {
  37. return High.ToString("x16") + Low.ToString("x16");
  38. }
  39. public bool IsZero()
  40. {
  41. return (Low | High) == 0;
  42. }
  43. }
  44. }