-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Handle final elements in SpanHelpers.Contains for byte and char vectorized #67492
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
Merged
adamsitnik
merged 4 commits into
dotnet:main
from
gfoidl:spanhelpers_final_elements_opt
Apr 25, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89cc087
Handle final elements in SpanHelpers.Contains(ref byte, byte, int) ve…
gfoidl 4763c44
Handle final elements in SpanHelpers.Contains(ref char, char, int) ve…
gfoidl 567e692
Fixed error SA1028: Code should not contain trailing whitespace
gfoidl debc817
Use equality operator instead of Vector<T>.Zero.Equals due to codegen…
gfoidl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,26 +341,26 @@ public static bool Contains(ref byte searchSpace, byte value, int length) | |
|
|
||
| uint uValue = value; // Use uint for comparisons to avoid unnecessary 8->32 extensions | ||
| nuint offset = 0; // Use nuint for arithmetic to avoid unnecessary 64->32->64 truncations | ||
| nuint lengthToExamine = (nuint)(uint)length; | ||
| nuint lengthToExamine = (uint)length; | ||
|
|
||
| if (Vector.IsHardwareAccelerated && length >= Vector<byte>.Count * 2) | ||
| { | ||
| lengthToExamine = UnalignedCountVector(ref searchSpace); | ||
| } | ||
|
|
||
| SequentialScan: | ||
| while (lengthToExamine >= 8) | ||
| { | ||
| lengthToExamine -= 8; | ||
|
|
||
| if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 0) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 4) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 5) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 6) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 7)) | ||
| ref byte start = ref Unsafe.AddByteOffset(ref searchSpace, offset); | ||
|
|
||
| if (uValue == Unsafe.AddByteOffset(ref start, 0) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 1) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 2) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 3) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 4) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 5) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 6) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 7)) | ||
| { | ||
| goto Found; | ||
| } | ||
|
|
@@ -371,11 +371,12 @@ public static bool Contains(ref byte searchSpace, byte value, int length) | |
| if (lengthToExamine >= 4) | ||
| { | ||
| lengthToExamine -= 4; | ||
| ref byte start = ref Unsafe.AddByteOffset(ref searchSpace, offset); | ||
|
|
||
| if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 0) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2) || | ||
| uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3)) | ||
| if (uValue == Unsafe.AddByteOffset(ref start, 0) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 1) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 2) || | ||
| uValue == Unsafe.AddByteOffset(ref start, 3)) | ||
| { | ||
| goto Found; | ||
| } | ||
|
|
@@ -385,24 +386,25 @@ public static bool Contains(ref byte searchSpace, byte value, int length) | |
|
|
||
| while (lengthToExamine > 0) | ||
| { | ||
| lengthToExamine -= 1; | ||
| lengthToExamine--; | ||
|
|
||
| if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset)) | ||
| goto Found; | ||
|
|
||
| offset += 1; | ||
| offset++; | ||
| } | ||
|
|
||
| if (Vector.IsHardwareAccelerated && (offset < (nuint)(uint)length)) | ||
| if (Vector.IsHardwareAccelerated && (offset < (uint)length)) | ||
| { | ||
| lengthToExamine = (((nuint)(uint)length - offset) & (nuint)~(Vector<byte>.Count - 1)); | ||
| lengthToExamine = ((uint)length - offset) & (nuint)~(Vector<byte>.Count - 1); | ||
|
|
||
| Vector<byte> values = new Vector<byte>(value); | ||
| Vector<byte> values = new(value); | ||
| Vector<byte> matches; | ||
|
|
||
| while (lengthToExamine > offset) | ||
| while (offset < lengthToExamine) | ||
| { | ||
| var matches = Vector.Equals(values, LoadVector(ref searchSpace, offset)); | ||
| if (Vector<byte>.Zero.Equals(matches)) | ||
| matches = Vector.Equals(values, LoadVector(ref searchSpace, offset)); | ||
| if (matches == Vector<byte>.Zero) | ||
| { | ||
| offset += (nuint)Vector<byte>.Count; | ||
| continue; | ||
|
|
@@ -411,10 +413,17 @@ public static bool Contains(ref byte searchSpace, byte value, int length) | |
| goto Found; | ||
| } | ||
|
|
||
| if (offset < (nuint)(uint)length) | ||
| // The total length is at least Vector<byte>.Count, so instead of falling back to a | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for adding the comment (otherwise it would not be obvious to me) 👍 |
||
| // sequential scan for the remainder, we check the vector read from the end -- note: unaligned read necessary. | ||
| // We do this only if at least one element is left. | ||
| if (offset < (uint)length) | ||
| { | ||
| lengthToExamine = ((nuint)(uint)length - offset); | ||
| goto SequentialScan; | ||
| offset = (uint)(length - Vector<byte>.Count); | ||
| matches = Vector.Equals(values, LoadVector(ref searchSpace, offset)); | ||
| if (matches != Vector<byte>.Zero) | ||
| { | ||
| goto Found; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a removal of anything
goto-related is always welcomed 👍