2018-11-28 23:18:09 +01:00
|
|
|
namespace Ryujinx.Common
|
|
|
|
{
|
|
|
|
public static class BitUtils
|
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
public static int AlignUp(int value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (value + (size - 1)) & -size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static ulong AlignUp(ulong value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (ulong)AlignUp((long)value, size);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static long AlignUp(long value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (value + (size - 1)) & -(long)size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static int AlignDown(int value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return value & -size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static ulong AlignDown(ulong value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (ulong)AlignDown((long)value, size);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static long AlignDown(long value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return value & -(long)size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:12:24 +01:00
|
|
|
public static int DivRoundUp(int value, int dividend)
|
|
|
|
{
|
|
|
|
return (value + dividend - 1) / dividend;
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static ulong DivRoundUp(ulong value, uint dividend)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (value + dividend - 1) / dividend;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static long DivRoundUp(long value, int dividend)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (value + dividend - 1) / dividend;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:12:24 +01:00
|
|
|
public static int Pow2RoundUp(int value)
|
|
|
|
{
|
|
|
|
value--;
|
|
|
|
|
|
|
|
value |= (value >> 1);
|
|
|
|
value |= (value >> 2);
|
|
|
|
value |= (value >> 4);
|
|
|
|
value |= (value >> 8);
|
|
|
|
value |= (value >> 16);
|
|
|
|
|
|
|
|
return ++value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int Pow2RoundDown(int value)
|
|
|
|
{
|
|
|
|
return IsPowerOfTwo32(value) ? value : Pow2RoundUp(value) >> 1;
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static bool IsPowerOfTwo32(int value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return value != 0 && (value & (value - 1)) == 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static bool IsPowerOfTwo64(long value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return value != 0 && (value & (value - 1)) == 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static int CountLeadingZeros32(int value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (int)CountLeadingZeros((ulong)value, 32);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static int CountLeadingZeros64(long value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (int)CountLeadingZeros((ulong)value, 64);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
private static ulong CountLeadingZeros(ulong value, int size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
if (value == 0ul)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (ulong)size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
int nibbleIdx = size;
|
|
|
|
int preCount, count = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
nibbleIdx -= 4;
|
2019-03-13 09:23:52 +01:00
|
|
|
preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
|
2019-01-11 01:11:46 +01:00
|
|
|
count += preCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2019-01-11 01:11:46 +01:00
|
|
|
while (preCount == 4);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
return (ulong)count;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:12:24 +01:00
|
|
|
public static int CountTrailingZeros32(int value)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (((value >> count) & 1) == 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static long ReverseBits64(long value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
return (long)ReverseBits64((ulong)value);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
private static ulong ReverseBits64(ulong value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
value = ((value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((value & 0x5555555555555555) << 1 );
|
|
|
|
value = ((value & 0xcccccccccccccccc) >> 2 ) | ((value & 0x3333333333333333) << 2 );
|
|
|
|
value = ((value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((value & 0x0f0f0f0f0f0f0f0f) << 4 );
|
|
|
|
value = ((value & 0xff00ff00ff00ff00) >> 8 ) | ((value & 0x00ff00ff00ff00ff) << 8 );
|
|
|
|
value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
return (value >> 32) | (value << 32);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 09:23:52 +01:00
|
|
|
}
|