Skip to content

Commit

Permalink
Use intrinsic for GetHighestSetBitIndex (#7224)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jun 27, 2024
1 parent 229bd6b commit 24f4142
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,12 @@ public static bool GetBit(this byte b, int bitNumber)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetBit(this ref byte b, int bitNumber)
{
byte mask = (byte)(1 << (7 - bitNumber));
b = b |= mask;
int mask = (1 << (7 - bitNumber));
b |= (byte)mask;
}

public static int GetHighestSetBitIndex(this byte b)
{
if ((b & 128) == 128) return 8;
if ((b & 64) == 64) return 7;
if ((b & 32) == 32) return 6;
if ((b & 16) == 16) return 5;
if ((b & 8) == 8) return 4;
if ((b & 4) == 4) return 3;
return (b & 2) == 2 ? 2 : b;
}
=> 32 - BitOperations.LeadingZeroCount((uint)b);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool AreEqual(ReadOnlySpan<byte> a1, ReadOnlySpan<byte> a2)
Expand Down

0 comments on commit 24f4142

Please sign in to comment.