UInt128.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Numerics;
  3. namespace Ryujinx.Graphics.Shader.Translation
  4. {
  5. struct UInt128 : IEquatable<UInt128>
  6. {
  7. public static UInt128 Zero => new UInt128() { _v0 = 0, _v1 = 0 };
  8. private ulong _v0;
  9. private ulong _v1;
  10. public int TrailingZeroCount()
  11. {
  12. int count = BitOperations.TrailingZeroCount(_v0);
  13. if (count == 64)
  14. {
  15. count += BitOperations.TrailingZeroCount(_v1);
  16. }
  17. return count;
  18. }
  19. public static UInt128 Pow2(int x)
  20. {
  21. if (x >= 64)
  22. {
  23. return new UInt128() { _v0 = 0, _v1 = 1UL << (x - 64 ) };
  24. }
  25. return new UInt128() { _v0 = 1UL << x, _v1 = 0 };
  26. }
  27. public static UInt128 operator ~(UInt128 x)
  28. {
  29. return new UInt128() { _v0 = ~x._v0, _v1 = ~x._v1 };
  30. }
  31. public static UInt128 operator &(UInt128 x, UInt128 y)
  32. {
  33. return new UInt128() { _v0 = x._v0 & y._v0, _v1 = x._v1 & y._v1 };
  34. }
  35. public static UInt128 operator |(UInt128 x, UInt128 y)
  36. {
  37. return new UInt128() { _v0 = x._v0 | y._v0, _v1 = x._v1 | y._v1 };
  38. }
  39. public static bool operator ==(UInt128 x, UInt128 y)
  40. {
  41. return x.Equals(y);
  42. }
  43. public static bool operator !=(UInt128 x, UInt128 y)
  44. {
  45. return !x.Equals(y);
  46. }
  47. public override bool Equals(object obj)
  48. {
  49. return obj is UInt128 other && Equals(other);
  50. }
  51. public bool Equals(UInt128 other)
  52. {
  53. return _v0 == other._v0 && _v1 == other._v1;
  54. }
  55. public override int GetHashCode()
  56. {
  57. return HashCode.Combine(_v0, _v1);
  58. }
  59. }
  60. }