aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities
diff options
context:
space:
mode:
authorjduncanator <1518948+jduncanator@users.noreply.github.com>2019-02-11 23:00:32 +1100
committergdkchan <gab.dark.100@gmail.com>2019-02-11 09:00:32 -0300
commitd306115750df9df170cfef4d49c6b0b7af498962 (patch)
tree13e51d71ad264c8815106dc6a3190af02d193777 /Ryujinx.Common/Utilities
parenta694420d11ef74e4f0bf473be2b6f64635bc89c7 (diff)
Logger and Configuration Refactoring (#573)
* Logging: Refactor log targets into Ryujinx.Common * Logger: Implement JSON Log Target * Logger: Optimize Console/File logging targets Implement a simple ObjectPool to pool up StringBuilders to avoid causing excessive GCing of gen1/2 items when large amounts of log entries are being generated. We can also pre-determine the async overflow action at initialization time, allowing for an easy optimization in the message enqueue function, avoiding a number of comparisons. * Logger: Implement LogFormatters * Config: Refactor configuration file and loading * Config: Rename to .jsonc to avoid highlighting issues in VSC and GitHub * Resolve style nits * Config: Resolve incorrect default key binding * Config: Also update key binding default in schema * Tidy up namespace imports * Config: Update CONFIG.md to reflect new Config file
Diffstat (limited to 'Ryujinx.Common/Utilities')
-rw-r--r--Ryujinx.Common/Utilities/BitUtils.cs104
-rw-r--r--Ryujinx.Common/Utilities/HexUtils.cs90
2 files changed, 194 insertions, 0 deletions
diff --git a/Ryujinx.Common/Utilities/BitUtils.cs b/Ryujinx.Common/Utilities/BitUtils.cs
new file mode 100644
index 00000000..135b397d
--- /dev/null
+++ b/Ryujinx.Common/Utilities/BitUtils.cs
@@ -0,0 +1,104 @@
+namespace Ryujinx.Common
+{
+ public static class BitUtils
+ {
+ 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)
+ {
+ 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 ulong CountLeadingZeros(ulong value, int size)
+ {
+ 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
diff --git a/Ryujinx.Common/Utilities/HexUtils.cs b/Ryujinx.Common/Utilities/HexUtils.cs
new file mode 100644
index 00000000..63587cea
--- /dev/null
+++ b/Ryujinx.Common/Utilities/HexUtils.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Text;
+
+namespace Ryujinx.Common
+{
+ public static class HexUtils
+ {
+ private static readonly char[] HexChars = "0123456789ABCDEF".ToCharArray();
+
+ private const int HexTableColumnWidth = 8;
+ private const int HexTableColumnSpace = 3;
+
+ // Modified for Ryujinx
+ // Original by Pascal Ganaye - CPOL License
+ // https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array
+ public static string HexTable(byte[] bytes, int bytesPerLine = 16)
+ {
+ if (bytes == null)
+ {
+ return "<null>";
+ }
+
+ int bytesLength = bytes.Length;
+
+ int firstHexColumn =
+ HexTableColumnWidth
+ + HexTableColumnSpace;
+
+ int firstCharColumn = firstHexColumn
+ + bytesPerLine * HexTableColumnSpace
+ + (bytesPerLine - 1) / HexTableColumnWidth
+ + 2;
+
+ int lineLength = firstCharColumn
+ + bytesPerLine
+ + Environment.NewLine.Length;
+
+ char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
+
+ int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
+
+ StringBuilder result = new StringBuilder(expectedLines * lineLength);
+
+ for (int i = 0; i < bytesLength; i += bytesPerLine)
+ {
+ line[0] = HexChars[(i >> 28) & 0xF];
+ line[1] = HexChars[(i >> 24) & 0xF];
+ line[2] = HexChars[(i >> 20) & 0xF];
+ line[3] = HexChars[(i >> 16) & 0xF];
+ line[4] = HexChars[(i >> 12) & 0xF];
+ line[5] = HexChars[(i >> 8) & 0xF];
+ line[6] = HexChars[(i >> 4) & 0xF];
+ line[7] = HexChars[(i >> 0) & 0xF];
+
+ int hexColumn = firstHexColumn;
+ int charColumn = firstCharColumn;
+
+ for (int j = 0; j < bytesPerLine; j++)
+ {
+ if (j > 0 && (j & 7) == 0)
+ {
+ hexColumn++;
+ }
+
+ if (i + j >= bytesLength)
+ {
+ line[hexColumn] = ' ';
+ line[hexColumn + 1] = ' ';
+ line[charColumn] = ' ';
+ }
+ else
+ {
+ byte b = bytes[i + j];
+
+ line[hexColumn] = HexChars[(b >> 4) & 0xF];
+ line[hexColumn + 1] = HexChars[b & 0xF];
+ line[charColumn] = (b < 32 ? '·' : (char)b);
+ }
+
+ hexColumn += 3;
+ charColumn++;
+ }
+
+ result.Append(line);
+ }
+
+ return result.ToString();
+ }
+ }
+}