mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-26 10:11:04 +08:00
Added HasByteEqualTo APIs
This commit is contained in:
@@ -134,6 +134,37 @@ namespace Microsoft.Toolkit.HighPerformance.Helpers
|
||||
return ((value - 0x0101_0101_0101_0101ul) & ~value & 0x8080_8080_8080_8080ul) != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a byte in the input <see cref="uint"/> value matches a target value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input value to check.</param>
|
||||
/// <param name="target">The target byte to look for.</param>
|
||||
/// <returns>Whether <paramref name="value"/> has any bytes set to <paramref name="target"/>.</returns>
|
||||
/// <remarks>
|
||||
/// This method contains no branches.
|
||||
/// For more info, see <see href="https://graphics.stanford.edu/~seander/bithacks.html"/>.
|
||||
/// </remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool HasByteEqualTo(uint value, byte target)
|
||||
{
|
||||
return HasZeroByte(value ^ (0x0101_0101u * target));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a byte in the input <see cref="uint"/> value matches a target value.
|
||||
/// This method mirrors <see cref="HasByteEqualTo(uint,byte)"/>, but with <see cref="ulong"/> values.
|
||||
/// </summary>
|
||||
/// <param name="value">The input value to check.</param>
|
||||
/// <param name="target">The target byte to look for.</param>
|
||||
/// <returns>Whether <paramref name="value"/> has any bytes set to <paramref name="target"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool HasByteEqualTo(ulong value, byte target)
|
||||
{
|
||||
return HasZeroByte(value ^ (0x0101_0101_0101_0101u * target));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a bit to a specified value.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user