BitUtils.cs 4.0 KB

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