UInt128.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Ryujinx.HLE.Utilities;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Ryujinx.HLE.Utilities
  6. {
  7. public struct UInt128
  8. {
  9. public long High { get; private set; }
  10. public long Low { get; private set; }
  11. public UInt128(long Low, long High)
  12. {
  13. this.Low = Low;
  14. this.High = High;
  15. byte[] Bytes = new byte[16];
  16. int Index = Bytes.Length;
  17. void WriteBytes(long Value)
  18. {
  19. for (int Byte = 0; Byte < 8; Byte++)
  20. {
  21. Bytes[--Index] = (byte)(Value >> Byte * 8);
  22. }
  23. }
  24. WriteBytes(Low);
  25. WriteBytes(High);
  26. }
  27. public UInt128(string UInt128Hex)
  28. {
  29. if (UInt128Hex == null || UInt128Hex.Length != 32 || !UInt128Hex.All("0123456789abcdefABCDEF".Contains))
  30. {
  31. throw new ArgumentException("Invalid Hex value!", nameof(UInt128Hex));
  32. }
  33. Low = Convert.ToInt64(UInt128Hex.Substring(16),16);
  34. High = Convert.ToInt64(UInt128Hex.Substring(0, 16), 16);
  35. }
  36. public void Write(BinaryWriter BinaryWriter)
  37. {
  38. BinaryWriter.Write(High);
  39. BinaryWriter.Write(Low);
  40. }
  41. public override string ToString()
  42. {
  43. return High.ToString("x16") + Low.ToString("x16");
  44. }
  45. public bool IsZero()
  46. {
  47. return (Low | High) == 0;
  48. }
  49. }
  50. }