2022-12-26 09:11:05 -05:00
|
|
|
using System;
|
2022-02-17 20:35:23 -05:00
|
|
|
using System.Numerics;
|
2022-02-17 15:38:50 -05:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
namespace Ryujinx.Common
|
|
|
|
{
|
|
|
|
public static class BitUtils
|
|
|
|
{
|
2022-12-26 09:11:05 -05:00
|
|
|
public static T AlignUp<T>(T value, T size)
|
|
|
|
where T : IBinaryInteger<T>
|
2020-12-13 02:30:27 -05:00
|
|
|
{
|
2022-12-26 09:11:05 -05:00
|
|
|
return (value + (size - T.One)) & -size;
|
2020-12-13 02:30:27 -05:00
|
|
|
}
|
|
|
|
|
2022-12-26 09:11:05 -05:00
|
|
|
public static T AlignDown<T>(T value, T size)
|
|
|
|
where T : IBinaryInteger<T>
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-10 19:11:46 -05:00
|
|
|
return value & -size;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2022-12-26 09:11:05 -05:00
|
|
|
public static T DivRoundUp<T>(T value, T dividend)
|
|
|
|
where T : IBinaryInteger<T>
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2022-12-26 09:11:05 -05:00
|
|
|
return (value + (dividend - T.One)) / dividend;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:24 -05: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)
|
|
|
|
{
|
2022-02-17 20:35:23 -05:00
|
|
|
return BitOperations.IsPow2(value) ? value : Pow2RoundUp(value) >> 1;
|
2019-02-27 20:12:24 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:11:46 -05:00
|
|
|
public static long ReverseBits64(long value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-10 19:11:46 -05:00
|
|
|
return (long)ReverseBits64((ulong)value);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:11:46 -05:00
|
|
|
private static ulong ReverseBits64(ulong value)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-10 19:11:46 -05: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 17:18:09 -05:00
|
|
|
|
2019-01-10 19:11:46 -05:00
|
|
|
return (value >> 32) | (value << 32);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 04:23:52 -04:00
|
|
|
}
|