Skip to content

Commit

Permalink
Use internal BitOperations.ResetLowestSetBit (#87798)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtqqczze authored Jun 22, 2023
1 parent 7b7d56f commit 426d18a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ ref Unsafe.Add(ref valueRef, 1), valueTailLength))
// Do a full IgnoreCase equality comparison. SpanHelpers.IndexOf skips comparing the two characters in some cases,
// but we don't actually know that the two characters are equal, since we compared with | 0x20. So we just compare
// the full string always.
int bitPos = BitOperations.TrailingZeroCount(mask);
nint charPos = (nint)((uint)bitPos / 2); // div by 2 (shr) because we work with 2-byte chars
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
if (EqualsIgnoreCase(ref Unsafe.Add(ref searchSpace, offset + charPos), ref valueRef, value.Length))
{
// Match! Return the index.
Expand All @@ -472,14 +471,7 @@ ref Unsafe.Add(ref valueRef, 1), valueTailLength))

// Clear the two lowest set bits in the mask. If there are no more set bits, we're done.
// If any remain, we loop around to do the next comparison.
if (Bmi1.IsSupported)
{
mask = Bmi1.ResetLowestSetBit(Bmi1.ResetLowestSetBit(mask));
}
else
{
mask &= ~(uint)(0b11 << bitPos);
}
mask = BitOperations.ResetLowestSetBit(BitOperations.ResetLowestSetBit(mask));
} while (mask != 0);
goto LoopFooter;

Expand Down Expand Up @@ -536,8 +528,7 @@ ref Unsafe.Add(ref valueRef, 1), valueTailLength))
// Do a full IgnoreCase equality comparison. SpanHelpers.IndexOf skips comparing the two characters in some cases,
// but we don't actually know that the two characters are equal, since we compared with | 0x20. So we just compare
// the full string always.
int bitPos = BitOperations.TrailingZeroCount(mask);
int charPos = (int)((uint)bitPos / 2); // div by 2 (shr) because we work with 2-byte chars
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
if (EqualsIgnoreCase(ref Unsafe.Add(ref searchSpace, offset + charPos), ref valueRef, value.Length))
{
// Match! Return the index.
Expand All @@ -546,14 +537,7 @@ ref Unsafe.Add(ref valueRef, 1), valueTailLength))

// Clear the two lowest set bits in the mask. If there are no more set bits, we're done.
// If any remain, we loop around to do the next comparison.
if (Bmi1.IsSupported)
{
mask = Bmi1.ResetLowestSetBit(Bmi1.ResetLowestSetBit(mask));
}
else
{
mask &= ~(uint)(0b11 << bitPos);
}
mask = BitOperations.ResetLowestSetBit(BitOperations.ResetLowestSetBit(mask));
} while (mask != 0);
goto LoopFooter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ ref Unsafe.As<char, byte>(ref Unsafe.Add(ref searchSpace, offset + 1)),
uint mask = cmpAnd.ExtractMostSignificantBits();
do
{
int bitPos = BitOperations.TrailingZeroCount(mask);
// div by 2 (shr) because we work with 2-byte chars
nint charPos = (nint)((uint)bitPos / 2);
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
if (valueLength == 2 || // we already matched two chars
SequenceEqual(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref searchSpace, offset + charPos)),
Expand All @@ -126,10 +124,7 @@ ref Unsafe.As<char, byte>(ref value), (nuint)(uint)valueLength * 2))
}

// Clear two the lowest set bits
if (Bmi1.IsSupported)
mask = Bmi1.ResetLowestSetBit(Bmi1.ResetLowestSetBit(mask));
else
mask &= ~(uint)(0b11 << bitPos);
mask = BitOperations.ResetLowestSetBit(BitOperations.ResetLowestSetBit(mask));
} while (mask != 0);
goto LOOP_FOOTER;

Expand Down Expand Up @@ -181,9 +176,7 @@ ref Unsafe.As<char, byte>(ref value), (nuint)(uint)valueLength * 2))
uint mask = cmpAnd.ExtractMostSignificantBits();
do
{
int bitPos = BitOperations.TrailingZeroCount(mask);
// div by 2 (shr) because we work with 2-byte chars
int charPos = (int)((uint)bitPos / 2);
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
if (valueLength == 2 || // we already matched two chars
SequenceEqual(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref searchSpace, offset + charPos)),
Expand All @@ -193,10 +186,7 @@ ref Unsafe.As<char, byte>(ref value), (nuint)(uint)valueLength * 2))
}

// Clear two lowest set bits
if (Bmi1.IsSupported)
mask = Bmi1.ResetLowestSetBit(Bmi1.ResetLowestSetBit(mask));
else
mask &= ~(uint)(0b11 << bitPos);
mask = BitOperations.ResetLowestSetBit(BitOperations.ResetLowestSetBit(mask));
} while (mask != 0);
goto LOOP_FOOTER;

Expand Down

0 comments on commit 426d18a

Please sign in to comment.