-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Vectorize IndexOfAnyExcept for four values #73696
Merged
adamsitnik
merged 4 commits into
dotnet:main
from
adamsitnik:vectorizeIndexOfAnyExcept4
Aug 15, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1c66d7
Vectorize IndexOfAnyExcept for four values
adamsitnik bd2a60a
Merge remote-tracking branch 'upstream/main' into vectorizeIndexOfAny…
adamsitnik 8b10702
remove code duplication
adamsitnik e2f3593
move the code under 1 value overload
adamsitnik 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 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
This file contains 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 |
---|---|---|
|
@@ -1220,19 +1220,72 @@ public static int IndexOfAnyExceptValueType<T>(ref T searchSpace, T value0, int | |
return ComputeIndex(ref searchSpace, ref oneVectorAwayFromEnd, notEquals); | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
internal static int IndexOfAnyExceptValueType<T>(ref T searchSpace, T value0, T value1, T value2, T value3, int length) where T : struct, IEquatable<T> | ||
{ | ||
Debug.Assert(length >= 0, "Expected non-negative length"); | ||
Debug.Assert(value0 is byte or short or int or long, "Expected caller to normalize to one of these types"); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
static int ComputeIndex(ref T searchSpace, ref T current, Vector128<T> notEquals) | ||
if (!Vector128.IsHardwareAccelerated || length < Vector128<T>.Count) | ||
{ | ||
for (int i = 0; i < length; i++) | ||
{ | ||
uint notEqualsElements = notEquals.ExtractMostSignificantBits(); | ||
int index = BitOperations.TrailingZeroCount(notEqualsElements); | ||
return index + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / Unsafe.SizeOf<T>()); | ||
T current = Unsafe.Add(ref searchSpace, i); | ||
if (!current.Equals(value0) && !current.Equals(value1) && !current.Equals(value2) && !current.Equals(value3)) | ||
{ | ||
return i; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
Vector128<T> notEquals, current, values0 = Vector128.Create(value0), values1 = Vector128.Create(value1), values2 = Vector128.Create(value2), values3 = Vector128.Create(value3); | ||
ref T currentSearchSpace = ref searchSpace; | ||
ref T oneVectorAwayFromEnd = ref Unsafe.Add(ref searchSpace, length - Vector128<T>.Count); | ||
|
||
// Loop until either we've finished all elements or there's less than a vector's-worth remaining. | ||
do | ||
{ | ||
current = Vector128.LoadUnsafe(ref currentSearchSpace); | ||
notEquals = ~(Vector128.Equals(values0, current) | Vector128.Equals(values1, current) | ||
| Vector128.Equals(values2, current) | Vector128.Equals(values3, current)); | ||
if (notEquals != Vector128<T>.Zero) | ||
{ | ||
return ComputeIndex(ref searchSpace, ref currentSearchSpace, notEquals); | ||
} | ||
|
||
currentSearchSpace = ref Unsafe.Add(ref currentSearchSpace, Vector128<T>.Count); | ||
} | ||
while (!Unsafe.IsAddressGreaterThan(ref currentSearchSpace, ref oneVectorAwayFromEnd)); | ||
|
||
// If any elements remain, process the last vector in the search space. | ||
if ((uint)length % Vector128<T>.Count != 0) | ||
{ | ||
current = Vector128.LoadUnsafe(ref oneVectorAwayFromEnd); | ||
notEquals = ~(Vector128.Equals(values0, current) | Vector128.Equals(values1, current) | ||
| Vector128.Equals(values2, current) | Vector128.Equals(values3, current)); | ||
if (notEquals != Vector128<T>.Zero) | ||
{ | ||
return ComputeIndex(ref searchSpace, ref oneVectorAwayFromEnd, notEquals); | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static int ComputeIndex<T>(ref T searchSpace, ref T current, Vector128<T> notEquals) where T : struct, IEquatable<T> | ||
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. There are many other methods in this type, whereas this "ComputeIndex" is only relevant to LastIndexOfExcept. Consider renaming it accordingly. |
||
{ | ||
uint notEqualsElements = notEquals.ExtractMostSignificantBits(); | ||
int index = BitOperations.TrailingZeroCount(notEqualsElements); | ||
return index + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / Unsafe.SizeOf<T>()); | ||
} | ||
|
||
public static bool SequenceEqual<T>(ref T first, ref T second, int length) where T : IEquatable<T>? | ||
{ | ||
Debug.Assert(length >= 0); | ||
|
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.
Nit: (don't restart CI just for this)