2018-10-07 06:13:46 -07:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-07-04 08:14:17 -07:00
|
|
|
using System.Runtime.InteropServices;
|
2018-10-07 06:13:46 -07:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Utilities
|
|
|
|
{
|
2019-07-04 08:14:17 -07:00
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct UInt128 : IEquatable<UInt128>
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2019-07-04 08:14:17 -07:00
|
|
|
public readonly long Low;
|
|
|
|
public readonly long High;
|
2018-10-07 06:13:46 -07:00
|
|
|
|
2019-06-15 15:35:38 -07:00
|
|
|
public bool IsNull => (Low | High) == 0;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public UInt128(long low, long high)
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
Low = low;
|
|
|
|
High = high;
|
2018-10-07 06:13:46 -07:00
|
|
|
}
|
|
|
|
|
2019-06-15 15:35:38 -07:00
|
|
|
public UInt128(byte[] bytes)
|
|
|
|
{
|
|
|
|
Low = BitConverter.ToInt64(bytes, 0);
|
|
|
|
High = BitConverter.ToInt64(bytes, 8);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public UInt128(string hex)
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (hex == null || hex.Length != 32 || !hex.All("0123456789abcdefABCDEF".Contains))
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
throw new ArgumentException("Invalid Hex value!", nameof(hex));
|
2018-10-07 06:13:46 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
Low = Convert.ToInt64(hex.Substring(16), 16);
|
|
|
|
High = Convert.ToInt64(hex.Substring(0, 16), 16);
|
2018-10-07 06:13:46 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public void Write(BinaryWriter binaryWriter)
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
binaryWriter.Write(Low);
|
|
|
|
binaryWriter.Write(High);
|
2018-10-07 06:13:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return High.ToString("x16") + Low.ToString("x16");
|
|
|
|
}
|
|
|
|
|
2019-07-04 08:14:17 -07:00
|
|
|
public static bool operator ==(UInt128 x, UInt128 y)
|
2018-10-07 06:13:46 -07:00
|
|
|
{
|
2019-07-04 08:14:17 -07:00
|
|
|
return x.Equals(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(UInt128 x, UInt128 y)
|
|
|
|
{
|
|
|
|
return !x.Equals(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
return obj is UInt128 uint128 && Equals(uint128);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(UInt128 cmpObj)
|
|
|
|
{
|
|
|
|
return Low == cmpObj.Low && High == cmpObj.High;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return HashCode.Combine(Low, High);
|
2018-10-07 06:13:46 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 15:35:38 -07:00
|
|
|
}
|