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 internal BitOperations.ResetLowestSetBit #87798

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
nint charPos = (nint)(uint.TrailingZeroCount(mask) / sizeof(char));

@stephentoub This would be slightly clearer, but might not be worth spinning CI again?

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