From ff4de7476b544b821fc761ead58952e27fb1f0a9 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Tue, 13 Dec 2022 19:43:24 -0700 Subject: [PATCH] Use generic math alignment in assert code --- src/LibHac/Diag/Assert.cs | 69 +++---------------- src/LibHac/Diag/Impl/AssertImpl.cs | 7 +- src/LibHac/FsSystem/AesXtsStorage.cs | 6 +- src/LibHac/FsSystem/BufferedStorage.cs | 2 +- .../FsSystem/Buffers/FileSystemBuddyHeap.cs | 16 ++--- src/LibHac/Util/BitUtil.cs | 1 - 6 files changed, 24 insertions(+), 77 deletions(-) diff --git a/src/LibHac/Diag/Assert.cs b/src/LibHac/Diag/Assert.cs index a2e94f8d..ed0e81ff 100644 --- a/src/LibHac/Diag/Assert.cs +++ b/src/LibHac/Diag/Assert.cs @@ -664,7 +664,7 @@ public static class Assert } // --------------------------------------------------------------------- - // In range T + // In range // --------------------------------------------------------------------- private static void InRangeImpl(AssertionType assertionType, T value, T lower, T upper, string valueText, @@ -721,7 +721,7 @@ public static class Assert } // --------------------------------------------------------------------- - // Within min-max T + // Within min-max // --------------------------------------------------------------------- private static void WithinMinMaxImpl(AssertionType assertionType, T value, T min, T max, string valueText, @@ -1174,11 +1174,11 @@ public static class Assert } // --------------------------------------------------------------------- - // Aligned long + // Aligned // --------------------------------------------------------------------- - private static void AlignedImpl(AssertionType assertionType, long value, int alignment, string valueText, - string alignmentText, string functionName, string fileName, int lineNumber) + private static void AlignedImpl(AssertionType assertionType, T value, int alignment, string valueText, + string alignmentText, string functionName, string fileName, int lineNumber) where T : IBinaryNumber { if (AssertImpl.IsAligned(value, alignment)) return; @@ -1188,86 +1188,39 @@ public static class Assert } [Conditional(AssertCondition)] - public static void Aligned(long value, int alignment, + public static void Aligned(T value, int alignment, [CallerArgumentExpression(nameof(value))] string valueText = "", [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", [CallerMemberName] string functionName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0) + where T : IBinaryNumber { AlignedImpl(AssertionType.UserAssert, value, alignment, valueText, alignmentText, functionName, fileName, lineNumber); } [Conditional(AssertCondition)] - internal static void SdkAligned(long value, int alignment, + internal static void SdkAligned(T value, int alignment, [CallerArgumentExpression(nameof(value))] string valueText = "", [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", [CallerMemberName] string functionName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0) + where T : IBinaryNumber { AlignedImpl(AssertionType.SdkAssert, value, alignment, valueText, alignmentText, functionName, fileName, lineNumber); } [Conditional(AssertCondition)] - internal static void SdkRequiresAligned(long value, int alignment, - [CallerArgumentExpression(nameof(value))] string valueText = "", - [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", - [CallerMemberName] string functionName = "", - [CallerFilePath] string fileName = "", - [CallerLineNumber] int lineNumber = 0) - { - AlignedImpl(AssertionType.SdkRequires, value, alignment, valueText, alignmentText, functionName, fileName, - lineNumber); - } - - // --------------------------------------------------------------------- - // Aligned ulong - // --------------------------------------------------------------------- - - private static void AlignedImpl(AssertionType assertionType, ulong value, int alignment, string valueText, - string alignmentText, string functionName, string fileName, int lineNumber) - { - if (AssertImpl.IsAligned(value, alignment)) - return; - - AssertImpl.InvokeAssertionAligned(assertionType, value, alignment, valueText, alignmentText, functionName, fileName, - lineNumber); - } - - [Conditional(AssertCondition)] - public static void Aligned(ulong value, int alignment, - [CallerArgumentExpression(nameof(value))] string valueText = "", - [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", - [CallerMemberName] string functionName = "", - [CallerFilePath] string fileName = "", - [CallerLineNumber] int lineNumber = 0) - { - AlignedImpl(AssertionType.UserAssert, value, alignment, valueText, alignmentText, functionName, fileName, - lineNumber); - } - - [Conditional(AssertCondition)] - internal static void SdkAligned(ulong value, int alignment, - [CallerArgumentExpression(nameof(value))] string valueText = "", - [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", - [CallerMemberName] string functionName = "", - [CallerFilePath] string fileName = "", - [CallerLineNumber] int lineNumber = 0) - { - AlignedImpl(AssertionType.SdkAssert, value, alignment, valueText, alignmentText, functionName, fileName, - lineNumber); - } - - [Conditional(AssertCondition)] - internal static void SdkRequiresAligned(ulong value, int alignment, + internal static void SdkRequiresAligned(T value, int alignment, [CallerArgumentExpression(nameof(value))] string valueText = "", [CallerArgumentExpression(nameof(alignment))] string alignmentText = "", [CallerMemberName] string functionName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0) + where T : IBinaryNumber { AlignedImpl(AssertionType.SdkRequires, value, alignment, valueText, alignmentText, functionName, fileName, lineNumber); diff --git a/src/LibHac/Diag/Impl/AssertImpl.cs b/src/LibHac/Diag/Impl/AssertImpl.cs index 4bc0317f..2faf80a0 100644 --- a/src/LibHac/Diag/Impl/AssertImpl.cs +++ b/src/LibHac/Diag/Impl/AssertImpl.cs @@ -240,12 +240,7 @@ internal static class AssertImpl return lhs.CompareTo(rhs) >= 0; } - public static bool IsAligned(long value, int alignment) - { - return Alignment.IsAligned(value, (uint)alignment); - } - - public static bool IsAligned(ulong value, int alignment) + public static bool IsAligned(T value, int alignment) where T : IBinaryNumber { return Alignment.IsAligned(value, (uint)alignment); } diff --git a/src/LibHac/FsSystem/AesXtsStorage.cs b/src/LibHac/FsSystem/AesXtsStorage.cs index e5259801..ebcedb7f 100644 --- a/src/LibHac/FsSystem/AesXtsStorage.cs +++ b/src/LibHac/FsSystem/AesXtsStorage.cs @@ -35,7 +35,7 @@ public class AesXtsStorage : IStorage { Assert.Equal(outIv.Length, IvSize); Assert.SdkRequiresGreaterEqual(offset, 0); - Assert.SdkRequiresAligned((ulong)blockSize, AesBlockSize); + Assert.SdkRequiresAligned(blockSize, AesBlockSize); BinaryPrimitives.WriteInt64BigEndian(outIv.Slice(sizeof(long)), offset / blockSize); } @@ -50,7 +50,7 @@ public class AesXtsStorage : IStorage Assert.SdkRequiresEqual(KeySize, key1.Length); Assert.SdkRequiresEqual(KeySize, key2.Length); Assert.SdkRequiresEqual(IvSize, iv.Length); - Assert.SdkRequiresAligned((ulong)blockSize, AesBlockSize); + Assert.SdkRequiresAligned(blockSize, AesBlockSize); key1.CopyTo(_key1.Items); key2.CopyTo(_key2.Items); @@ -68,7 +68,7 @@ public class AesXtsStorage : IStorage Assert.SdkRequiresEqual(KeySize, key1.Length); Assert.SdkRequiresEqual(KeySize, key2.Length); Assert.SdkRequiresEqual(IvSize, iv.Length); - Assert.SdkRequiresAligned((ulong)blockSize, AesBlockSize); + Assert.SdkRequiresAligned(blockSize, AesBlockSize); key1.CopyTo(_key1.Items); key2.CopyTo(_key2.Items); diff --git a/src/LibHac/FsSystem/BufferedStorage.cs b/src/LibHac/FsSystem/BufferedStorage.cs index e1d20a47..f4e4f969 100644 --- a/src/LibHac/FsSystem/BufferedStorage.cs +++ b/src/LibHac/FsSystem/BufferedStorage.cs @@ -431,7 +431,7 @@ public class BufferedStorage : IStorage Assert.SdkRequiresEqual(_prevIndex, InvalidIndex); Assert.SdkRequires(!IsValid()); Assert.SdkRequires(!_isDirty); - Assert.SdkRequiresAligned((ulong)offset, (int)_bufferedStorage._blockSize); + Assert.SdkRequiresAligned(offset, (int)_bufferedStorage._blockSize); // Make sure this Cache has an allocated buffer if (_memoryRange.IsNull) diff --git a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs index 03b8bc7a..64d620d1 100644 --- a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs +++ b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs @@ -221,8 +221,8 @@ public unsafe class FileSystemBuddyHeap : IDisposable public Result Initialize(UIntPtr address, nuint size, nuint blockSize, int orderMax) { Assert.SdkRequires(FreeLists == null); - Assert.SdkRequiresNotEqual(address, UIntPtr.Zero); - Assert.SdkRequiresAligned(address.ToUInt64(), (int)BufferAlignment); + Assert.SdkRequiresNotEqual(address, nuint.Zero); + Assert.SdkRequiresAligned(address, (int)BufferAlignment); Assert.SdkRequiresGreaterEqual(blockSize, BlockSizeMin); Assert.SdkRequires(BitUtil.IsPowerOfTwo(blockSize)); Assert.SdkRequiresGreaterEqual(size, blockSize); @@ -240,7 +240,7 @@ public unsafe class FileSystemBuddyHeap : IDisposable // Determine page sizes nuint maxPageSize = BlockSize << OrderMax; nuint maxPageCount = (nuint)Alignment.AlignUp(HeapSize, (uint)maxPageSize) / maxPageSize; - Assert.SdkGreater((int)maxPageCount, 0); + Assert.SdkGreater(maxPageCount, nuint.Zero); // Setup the free lists if (ExternalFreeLists != null) @@ -539,8 +539,8 @@ public unsafe class FileSystemBuddyHeap : IDisposable var address = new UIntPtr(pageEntry); Assert.SdkRequiresGreaterEqual(address, HeapStart); - Assert.SdkRequiresLess((nuint)address, HeapStart + HeapSize); - Assert.SdkRequiresAligned((nuint)address - HeapStart, (int)GetBlockSize()); + Assert.SdkRequiresLess(address, HeapStart + HeapSize); + Assert.SdkRequiresAligned(address - HeapStart, (int)GetBlockSize()); return address; } @@ -548,10 +548,10 @@ public unsafe class FileSystemBuddyHeap : IDisposable private PageEntry* GetPageEntryFromAddress(UIntPtr address) { Assert.SdkRequiresGreaterEqual(address, HeapStart); - Assert.SdkRequiresLess((nuint)address, HeapStart + HeapSize); + Assert.SdkRequiresLess(address, HeapStart + HeapSize); ulong blockStart = (ulong)HeapStart + - Alignment.AlignDown((nuint)address - HeapStart, (uint)GetBlockSize()); + Alignment.AlignDown(address - HeapStart, (uint)GetBlockSize()); return (PageEntry*)blockStart; } @@ -559,7 +559,7 @@ public unsafe class FileSystemBuddyHeap : IDisposable { var address = (nuint)pageEntry; - Assert.SdkRequiresGreaterEqual(address, (nuint)HeapStart); + Assert.SdkRequiresGreaterEqual(address, HeapStart); Assert.SdkRequiresLess(address, HeapStart + HeapSize); Assert.SdkRequiresAligned(address - HeapStart, (int)GetBlockSize()); diff --git a/src/LibHac/Util/BitUtil.cs b/src/LibHac/Util/BitUtil.cs index 2d0cdc82..ff76877b 100644 --- a/src/LibHac/Util/BitUtil.cs +++ b/src/LibHac/Util/BitUtil.cs @@ -5,7 +5,6 @@ namespace LibHac.Util; public static class BitUtil { - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsPowerOfTwo(T value) where T : IBitwiseOperators, INumberBase {