BitUtils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. namespace Ryujinx.Common
  2. {
  3. public static class BitUtils
  4. {
  5. private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  6. public static int AlignUp(int value, int size)
  7. {
  8. return (value + (size - 1)) & -size;
  9. }
  10. public static ulong AlignUp(ulong value, int size)
  11. {
  12. return (ulong)AlignUp((long)value, size);
  13. }
  14. public static long AlignUp(long value, int size)
  15. {
  16. return (value + (size - 1)) & -(long)size;
  17. }
  18. public static int AlignDown(int value, int size)
  19. {
  20. return value & -size;
  21. }
  22. public static ulong AlignDown(ulong value, int size)
  23. {
  24. return (ulong)AlignDown((long)value, size);
  25. }
  26. public static long AlignDown(long value, int size)
  27. {
  28. return value & -(long)size;
  29. }
  30. public static int DivRoundUp(int value, int dividend)
  31. {
  32. return (value + dividend - 1) / dividend;
  33. }
  34. public static ulong DivRoundUp(ulong value, uint dividend)
  35. {
  36. return (value + dividend - 1) / dividend;
  37. }
  38. public static long DivRoundUp(long value, int dividend)
  39. {
  40. return (value + dividend - 1) / dividend;
  41. }
  42. public static int Pow2RoundUp(int value)
  43. {
  44. value--;
  45. value |= (value >> 1);
  46. value |= (value >> 2);
  47. value |= (value >> 4);
  48. value |= (value >> 8);
  49. value |= (value >> 16);
  50. return ++value;
  51. }
  52. public static int Pow2RoundDown(int value)
  53. {
  54. return IsPowerOfTwo32(value) ? value : Pow2RoundUp(value) >> 1;
  55. }
  56. public static bool IsPowerOfTwo32(int value)
  57. {
  58. return value != 0 && (value & (value - 1)) == 0;
  59. }
  60. public static bool IsPowerOfTwo64(long value)
  61. {
  62. return value != 0 && (value & (value - 1)) == 0;
  63. }
  64. public static int CountLeadingZeros32(int value)
  65. {
  66. return (int)CountLeadingZeros((ulong)value, 32);
  67. }
  68. public static int CountLeadingZeros64(long value)
  69. {
  70. return (int)CountLeadingZeros((ulong)value, 64);
  71. }
  72. private static ulong CountLeadingZeros(ulong value, int size)
  73. {
  74. if (value == 0ul)
  75. {
  76. return (ulong)size;
  77. }
  78. int nibbleIdx = size;
  79. int preCount, count = 0;
  80. do
  81. {
  82. nibbleIdx -= 4;
  83. preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
  84. count += preCount;
  85. }
  86. while (preCount == 4);
  87. return (ulong)count;
  88. }
  89. public static int CountTrailingZeros32(int value)
  90. {
  91. int count = 0;
  92. while (((value >> count) & 1) == 0)
  93. {
  94. count++;
  95. }
  96. return count;
  97. }
  98. public static long ReverseBits64(long value)
  99. {
  100. return (long)ReverseBits64((ulong)value);
  101. }
  102. private static ulong ReverseBits64(ulong value)
  103. {
  104. value = ((value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((value & 0x5555555555555555) << 1 );
  105. value = ((value & 0xcccccccccccccccc) >> 2 ) | ((value & 0x3333333333333333) << 2 );
  106. value = ((value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((value & 0x0f0f0f0f0f0f0f0f) << 4 );
  107. value = ((value & 0xff00ff00ff00ff00) >> 8 ) | ((value & 0x00ff00ff00ff00ff) << 8 );
  108. value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16);
  109. return (value >> 32) | (value << 32);
  110. }
  111. }
  112. }