aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/BitUtils.cs
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-11-28 20:18:09 -0200
committerGitHub <noreply@github.com>2018-11-28 20:18:09 -0200
commit00579927e43bf55ee06ae02933c1e755fb4120eb (patch)
tree0fd06db7b28e0accf87b465ec6f4dc74691febab /Ryujinx.Common/BitUtils.cs
parente7fe7d724778535f8eff390abef54274a343c0b7 (diff)
Better process implementation (#491)
* Initial implementation of KProcess * Some improvements to the memory manager, implement back guest stack trace printing * Better GetInfo implementation, improve checking in some places with information from process capabilities * Allow the cpu to read/write from the correct memory locations for accesses crossing a page boundary * Change long -> ulong for address/size on memory related methods to avoid unnecessary casts * Attempt at implementing ldr:ro with new KProcess * Allow BSS with size 0 on ldr:ro * Add checking for memory block slab heap usage, return errors if full, exit gracefully * Use KMemoryBlockSize const from KMemoryManager * Allow all methods to read from non-contiguous locations * Fix for TransactParcelAuto * Address PR feedback, additionally fix some small issues related to the KIP loader and implement SVCs GetProcessId, GetProcessList, GetSystemInfo, CreatePort and ManageNamedPort * Fix wrong check for source pages count from page list on MapPhysicalMemory * Fix some issues with UnloadNro on ldr:ro
Diffstat (limited to 'Ryujinx.Common/BitUtils.cs')
-rw-r--r--Ryujinx.Common/BitUtils.cs104
1 files changed, 104 insertions, 0 deletions
diff --git a/Ryujinx.Common/BitUtils.cs b/Ryujinx.Common/BitUtils.cs
new file mode 100644
index 00000000..5c858029
--- /dev/null
+++ b/Ryujinx.Common/BitUtils.cs
@@ -0,0 +1,104 @@
+namespace Ryujinx.Common
+{
+ public static class BitUtils
+ {
+ public static int AlignUp(int Value, int Size)
+ {
+ return (Value + (Size - 1)) & -Size;
+ }
+
+ public static ulong AlignUp(ulong Value, int Size)
+ {
+ return (ulong)AlignUp((long)Value, Size);
+ }
+
+ public static long AlignUp(long Value, int Size)
+ {
+ return (Value + (Size - 1)) & -(long)Size;
+ }
+
+ public static int AlignDown(int Value, int Size)
+ {
+ return Value & -Size;
+ }
+
+ public static ulong AlignDown(ulong Value, int Size)
+ {
+ return (ulong)AlignDown((long)Value, Size);
+ }
+
+ public static long AlignDown(long Value, int Size)
+ {
+ return Value & -(long)Size;
+ }
+
+ public static ulong DivRoundUp(ulong Value, uint Dividend)
+ {
+ return (Value + Dividend - 1) / Dividend;
+ }
+
+ public static long DivRoundUp(long Value, int Dividend)
+ {
+ return (Value + Dividend - 1) / Dividend;
+ }
+
+ 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 readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ private static ulong CountLeadingZeros(ulong Value, int Size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
+ {
+ if (Value == 0ul)
+ {
+ return (ulong)Size;
+ }
+
+ int NibbleIdx = Size;
+ int PreCount, Count = 0;
+
+ do
+ {
+ NibbleIdx -= 4;
+ PreCount = ClzNibbleTbl[(Value >> NibbleIdx) & 0b1111];
+ Count += PreCount;
+ }
+ while (PreCount == 4);
+
+ return (ulong)Count;
+ }
+
+ public static long ReverseBits64(long Value)
+ {
+ return (long)ReverseBits64((ulong)Value);
+ }
+
+ private static ulong ReverseBits64(ulong Value)
+ {
+ 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);
+
+ return (Value >> 32) | (Value << 32);
+ }
+ }
+} \ No newline at end of file