aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities/BitUtils.cs
diff options
context:
space:
mode:
authorBerkan Diler <b.diler@gmx.de>2022-02-18 02:35:23 +0100
committerGitHub <noreply@github.com>2022-02-18 02:35:23 +0100
commit98c838b24c464fd627ad09078250e4801db8967e (patch)
treeff1fcd0371d55255bcd39876cc2d685a7fe7485b /Ryujinx.Common/Utilities/BitUtils.cs
parent63c9c64196465bd97abacceb1d83fa53de358d23 (diff)
Use BitOperations methods and delete now unused BitUtils methods (#3134)1.1.42
Replaces BitUtils.CountTrailingZeros/CountLeadingZeros/IsPowerOfTwo with BitOperations methods
Diffstat (limited to 'Ryujinx.Common/Utilities/BitUtils.cs')
-rw-r--r--Ryujinx.Common/Utilities/BitUtils.cs58
1 files changed, 2 insertions, 56 deletions
diff --git a/Ryujinx.Common/Utilities/BitUtils.cs b/Ryujinx.Common/Utilities/BitUtils.cs
index 180f1063..b231886f 100644
--- a/Ryujinx.Common/Utilities/BitUtils.cs
+++ b/Ryujinx.Common/Utilities/BitUtils.cs
@@ -1,11 +1,10 @@
using System;
+using System.Numerics;
namespace Ryujinx.Common
{
public static class BitUtils
{
- private static ReadOnlySpan<byte> ClzNibbleTbl => new byte[] { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
-
public static uint AlignUp(uint value, int size)
{
return (uint)AlignUp((int)value, size);
@@ -76,60 +75,7 @@ namespace Ryujinx.Common
public static int Pow2RoundDown(int value)
{
- return IsPowerOfTwo32(value) ? value : Pow2RoundUp(value) >> 1;
- }
-
- public static bool IsPowerOfTwo32(int value)
- {
- return value != 0 && (value & (value - 1)) == 0;
- }
-
- public static bool IsPowerOfTwo64(long value)
- {
- return value != 0 && (value & (value - 1)) == 0;
- }
-
- public static int CountLeadingZeros32(int value)
- {
- return (int)CountLeadingZeros((ulong)value, 32);
- }
-
- public static int CountLeadingZeros64(long value)
- {
- return (int)CountLeadingZeros((ulong)value, 64);
- }
-
- private static ulong CountLeadingZeros(ulong value, int size)
- {
- if (value == 0ul)
- {
- return (ulong)size;
- }
-
- int nibbleIdx = size;
- int preCount, count = 0;
-
- do
- {
- nibbleIdx -= 4;
- preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
- count += preCount;
- }
- while (preCount == 4);
-
- return (ulong)count;
- }
-
- public static int CountTrailingZeros32(int value)
- {
- int count = 0;
-
- while (((value >> count) & 1) == 0)
- {
- count++;
- }
-
- return count;
+ return BitOperations.IsPow2(value) ? value : Pow2RoundUp(value) >> 1;
}
public static long ReverseBits64(long value)