MathUtils.IsPow2 & MathUtils.GetPow2Shift performance improvements.

This commit is contained in:
mlemanczyk committed 2023-03-27 01:12:39 +02:00
1 parent b33904a17f
commit 1f86ef2c59
2 files changed
+62 -8

No files matched your search

+4 -3
View File
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections
{
@@ -13,10 +14,10 @@ namespace Recyclable.Collections
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPow2(int value) => Array.BinarySearch(Pow2, value) >= 0;
public static bool IsPow2(long value) => (value != 0) && ((value & (value - 1)) == 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPow2Shift(int value) => Array.BinarySearch(Pow2, value);
public static int GetPow2Shift(long value) => BitOperations.IsPow2(value) ? 63 - BitOperations.LeadingZeroCount((ulong)value) : -1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long DivMod(long number, int divider, out long remainder)