aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Utilities/BitUtils.cs
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/BitUtils.cs
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/BitUtils.cs')
-rw-r--r--Ryujinx.Common/Utilities/BitUtils.cs104
1 files changed, 104 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