Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use intrinsic for GetHighestSetBitIndex #7224

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(b);
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved

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