From 6ceab9613fbd4c83565970836d8dfd7d539fcac3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 24 Jun 2020 02:02:15 +0200 Subject: [PATCH] Added HasByteEqualTo APIs --- .../Helpers/BitHelper.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Microsoft.Toolkit.HighPerformance/Helpers/BitHelper.cs b/Microsoft.Toolkit.HighPerformance/Helpers/BitHelper.cs index 2e72020f..eabce3cb 100644 --- a/Microsoft.Toolkit.HighPerformance/Helpers/BitHelper.cs +++ b/Microsoft.Toolkit.HighPerformance/Helpers/BitHelper.cs @@ -134,6 +134,37 @@ namespace Microsoft.Toolkit.HighPerformance.Helpers return ((value - 0x0101_0101_0101_0101ul) & ~value & 0x8080_8080_8080_8080ul) != 0; } + /// + /// Checks whether a byte in the input value matches a target value. + /// + /// The input value to check. + /// The target byte to look for. + /// Whether has any bytes set to . + /// + /// This method contains no branches. + /// For more info, see . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool HasByteEqualTo(uint value, byte target) + { + return HasZeroByte(value ^ (0x0101_0101u * target)); + } + + /// + /// Checks whether a byte in the input value matches a target value. + /// This method mirrors , but with values. + /// + /// The input value to check. + /// The target byte to look for. + /// Whether has any bytes set to . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool HasByteEqualTo(ulong value, byte target) + { + return HasZeroByte(value ^ (0x0101_0101_0101_0101u * target)); + } + /// /// Sets a bit to a specified value. ///