diff --git a/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs b/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs index 6923785..7416e5d 100644 --- a/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs @@ -37,7 +37,7 @@ namespace Recyclable.Collections.Benchmarks.POC [GlobalSetup] public void Setup() { - _cutoff = BlockSize << 4; + _cutoff = checked((long)BlockSize << 4); _increment = Vector128.Create(-BlockSize, 1L); _lastCaseIndex = 0; } diff --git a/Recyclable.Collections.Benchmarks/POC/RoundUpToPowerOf2vsOthers.cs b/Recyclable.Collections.Benchmarks/POC/RoundUpToPowerOf2vsOthers.cs index b0f3948..96906d9 100644 --- a/Recyclable.Collections.Benchmarks/POC/RoundUpToPowerOf2vsOthers.cs +++ b/Recyclable.Collections.Benchmarks/POC/RoundUpToPowerOf2vsOthers.cs @@ -54,7 +54,7 @@ namespace Recyclable.Collections.Benchmarks.POC int result = 1; while (result < value) { - result <<= 1; + result = checked(result << 1); } return result; diff --git a/Recyclable.Collections/MathUtils.cs b/Recyclable.Collections/MathUtils.cs index d82edc1..c7e37f8 100644 --- a/Recyclable.Collections/MathUtils.cs +++ b/Recyclable.Collections/MathUtils.cs @@ -14,7 +14,7 @@ namespace Recyclable.Collections [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long DivMod(long number, int divider, out long remainder) { - if (number >= divider << 4) + if (number >= checked((long)divider << 4)) { return Math.DivRem(number, divider, out remainder); } diff --git a/Recyclable.Collections/RecyclableList.Helpers.cs b/Recyclable.Collections/RecyclableList.Helpers.cs index 8ffb960..a33a5ff 100644 --- a/Recyclable.Collections/RecyclableList.Helpers.cs +++ b/Recyclable.Collections/RecyclableList.Helpers.cs @@ -15,7 +15,7 @@ namespace Recyclable.Collections // TODO: Measure performance // T[] newMemoryBlock = RecyclableListHelpers._arrayPool.Rent(_capacity <<= 1); T[] oldMemoryBlock = list._memoryBlock; - list._memoryBlock = (list._capacity <<= 1) < RecyclableDefaults.MinPooledArrayLength + list._memoryBlock = checked(list._capacity <<= 1) < RecyclableDefaults.MinPooledArrayLength ? new T[list._capacity] : RecyclableArrayPool.RentShared(list._capacity); diff --git a/Recyclable.Collections/RecyclableLongList.Helpers.cs b/Recyclable.Collections/RecyclableLongList.Helpers.cs index e2c43cf..b7e870e 100644 --- a/Recyclable.Collections/RecyclableLongList.Helpers.cs +++ b/Recyclable.Collections/RecyclableLongList.Helpers.cs @@ -192,7 +192,7 @@ namespace Recyclable.Collections } } - return requiredBlockCount << minBlockSizePow2Shift; + return checked(requiredBlockCount << minBlockSizePow2Shift); } #pragma warning disable CA2208 diff --git a/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs b/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs index 8bc01d3..c3f0756 100644 --- a/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs +++ b/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs @@ -28,7 +28,7 @@ namespace Recyclable.Collections long index = Array.IndexOf(list._memoryBlocks[blockIndex], itemToFind, 0, list._blockSize); if (index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + index); return; } else if (context._isItemFound) @@ -41,7 +41,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 1], itemToFind, 0, list._blockSize); if (index >= 0) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 1, ((long)(blockIndex + 1) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 1, checked((long)(blockIndex + 1) << list._blockSizePow2BitShift) + index); return; } else if (context._isItemFound) @@ -56,7 +56,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 2], itemToFind, 0, list._blockSize); if (index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 2, ((long)(blockIndex + 2) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 2, checked((long)(blockIndex + 2) << list._blockSizePow2BitShift) + index); return; } else if (context._isItemFound) @@ -71,7 +71,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 3], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 3, ((long)(blockIndex + 3) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 3, checked((long)(blockIndex + 3) << list._blockSizePow2BitShift) + index); } #endif @@ -81,7 +81,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 4], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 4, ((long)(blockIndex + 4) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 4, checked((long)(blockIndex + 4) << list._blockSizePow2BitShift) + index); } #endif @@ -91,7 +91,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 5], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 5, ((long)(blockIndex + 5) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 5, checked((long)(blockIndex + 5) << list._blockSizePow2BitShift) + index); } #endif @@ -101,7 +101,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 6], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 6, ((long)(blockIndex + 6) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 6, checked((long)(blockIndex + 6) << list._blockSizePow2BitShift) + index); } #endif @@ -111,7 +111,7 @@ namespace Recyclable.Collections index = Array.IndexOf(list._memoryBlocks[blockIndex + 7], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0 && (!context._isItemFound || index < context.FoundItemIndex)) { - ExchangeFoundItemIndexIfLower(context, blockIndex + 7, ((long)(blockIndex + 7) << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex + 7, checked((long)(blockIndex + 7) << list._blockSizePow2BitShift) + index); } #endif @@ -157,7 +157,7 @@ namespace Recyclable.Collections long index = Array.IndexOf(list._memoryBlocks[blockIndex], itemToFind, 0, list._blockSize); if (!context._isItemFound && index >= 0) { - ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + index); + ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + index); } } catch (Exception e) @@ -214,8 +214,8 @@ namespace Recyclable.Collections // & WAS SLOWER // int blockSize = _blockSize; // & WAS SLOWER - // while ((blockIndex << list._blockSizePow2BitShift) + itemIndex > step) - //long blockSize = list._blockSize << 1; + // while (checked((long)blockIndex << list._blockSizePow2BitShift) + itemIndex > step) + //long blockSize = checked((long)list._blockSize << 1); int lastBlockWithData = list._lastBlockWithData - BlocksForEachTask; while (blockIndex < lastBlockWithData) { @@ -265,7 +265,7 @@ namespace Recyclable.Collections int itemIndex = Array.IndexOf(list._memoryBlocks[list._lastBlockWithData], item, 0, list._nextItemIndex > 0 ? list._nextItemIndex : list._blockSize); if (!context._isItemFound && itemIndex >= 0) { - ExchangeFoundItemIndexIfLower(context, blockIndex, ((long)blockIndex << list._blockSizePow2BitShift) + itemIndex); + ExchangeFoundItemIndexIfLower(context, blockIndex, checked((long)blockIndex << list._blockSizePow2BitShift) + itemIndex); } } @@ -307,7 +307,7 @@ namespace Recyclable.Collections itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex], item, 0, list._blockSize); if (itemIndex >= 0) { - return itemIndex + ((long)blockIndex << list._blockSizePow2BitShift); + return itemIndex + checked((long)blockIndex << list._blockSizePow2BitShift); } if (blockIndex + 1 < list._lastBlockWithData) @@ -315,7 +315,7 @@ namespace Recyclable.Collections itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 1], item, 0, list._blockSize); if (itemIndex >= 0) { - return itemIndex + ((long)(blockIndex + 1)<< list._blockSizePow2BitShift); + return itemIndex + checked((long)(blockIndex + 1)<< list._blockSizePow2BitShift); } if (blockIndex + 2 < list._lastBlockWithData) @@ -323,7 +323,7 @@ namespace Recyclable.Collections itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 2], item, 0, list._blockSize); if (itemIndex >= 0) { - return itemIndex + ((long)(blockIndex + 2)<< list._blockSizePow2BitShift); + return itemIndex + checked((long)(blockIndex + 2)<< list._blockSizePow2BitShift); } if (blockIndex + 3 < list._lastBlockWithData) @@ -331,7 +331,7 @@ namespace Recyclable.Collections itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex + 3], item, 0, list._blockSize); if (itemIndex >= 0) { - return itemIndex + ((long)(blockIndex + 3)<< list._blockSizePow2BitShift); + return itemIndex + checked((long)(blockIndex + 3)<< list._blockSizePow2BitShift); } } } @@ -342,7 +342,7 @@ namespace Recyclable.Collections ? Array.IndexOf(memoryBlocksSpan[list._lastBlockWithData], item, 0, list._nextItemIndex) : Array.IndexOf(memoryBlocksSpan[list._lastBlockWithData], item, 0, list._blockSize); - return itemIndex >= 0 ? itemIndex + ((long)list._lastBlockWithData << list._blockSizePow2BitShift) : RecyclableDefaults.ItemNotFoundIndexLong; + return itemIndex >= 0 ? itemIndex + checked((long)list._lastBlockWithData << list._blockSizePow2BitShift) : RecyclableDefaults.ItemNotFoundIndexLong; } } } diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index 1997e88..ebfa680 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -183,7 +183,7 @@ namespace Recyclable.CollectionsTests { // Prepare using var list = new RecyclableList(); - itemsCount <<= 1; + itemsCount = checked(itemsCount << 1); // Act if (testCase.Contains("Array[", StringComparison.OrdinalIgnoreCase)) @@ -275,7 +275,7 @@ namespace Recyclable.CollectionsTests // Prepare using var list = new RecyclableList(); testData = testData.Concat(testData.Reverse()); - itemsCount <<= 1; + itemsCount = checked(itemsCount << 1); // Act foreach (var index in testData) @@ -340,7 +340,7 @@ namespace Recyclable.CollectionsTests { // Prepare using var list = new RecyclableList(); - itemsCount <<= 1; + itemsCount = checked(itemsCount << 1); // Act list.AddRange(testData); @@ -416,7 +416,7 @@ namespace Recyclable.CollectionsTests { // Prepare testData = testData.Concat(testData.Reverse()); - itemsCount <<= 1; + itemsCount = checked(itemsCount << 1); // Act using var list = new RecyclableList(testData, itemsCount); @@ -1633,7 +1633,7 @@ namespace Recyclable.CollectionsTests { // Prepare using var list = new RecyclableList(testData, itemsCount); - var expectedCapacity = (int)BitOperations.RoundUpToPowerOf2((uint)itemsCount << 1); + var expectedCapacity = (int)BitOperations.RoundUpToPowerOf2(checked((uint)itemsCount << 1)); expectedCapacity = expectedCapacity >= RecyclableDefaults.InitialCapacity ? expectedCapacity : RecyclableDefaults.InitialCapacity; // Act