Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Improve Span.Count#103728
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Improve Span.Count #103728
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
79 changes: 23 additions & 56 deletions
79 src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
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 |
|---|---|---|
| @@ -3786,92 +3786,59 @@ public static int CountValueType<T>(ref T current, T value, int length) where T | ||
| { | ||
| Vector512<T> targetVector = Vector512.Create(value); | ||
| ref T oneVectorAwayFromEnd = ref Unsafe.Subtract(ref end, Vector512<T>.Count); | ||
| do | ||
| while (Unsafe.IsAddressLessThan(ref current, ref oneVectorAwayFromEnd)) | ||
| { | ||
| count += BitOperations.PopCount(Vector512.Equals(Vector512.LoadUnsafe(ref current), targetVector).ExtractMostSignificantBits()); | ||
| current = ref Unsafe.Add(ref current, Vector512<T>.Count); | ||
| } | ||
| while (!Unsafe.IsAddressGreaterThan(ref current, ref oneVectorAwayFromEnd)); | ||
| // If there are just a few elements remaining, then processing these elements by the scalar loop | ||
| // is cheaper than doing bitmask + popcount on the full last vector. To avoid complicated type | ||
| // based checks, other remainder-count based logic to determine the correct cut-off, for simplicity | ||
| // a half-vector size is chosen (based on benchmarks). | ||
| uint remaining = (uint)Unsafe.ByteOffset(ref current, ref end) / (uint)Unsafe.SizeOf<T>(); | ||
| if (remaining > Vector512<T>.Count / 2) | ||
| { | ||
| ulong mask = Vector512.Equals(Vector512.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| // The mask contains some elements that may be double-checked, so shift them away in order to get the correct pop-count. | ||
| uint overlaps = (uint)Vector512<T>.Count - remaining; | ||
| mask >>= (int)overlaps; | ||
| count += BitOperations.PopCount(mask); | ||
| return count; | ||
| } | ||
| // Count the last vector and mask off the elements that were already counted (number of elements between oneVectorAwayFromEnd and current). | ||
| ulong mask = Vector512.Equals(Vector512.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| mask >>= (int)((nuint)Unsafe.ByteOffset(ref oneVectorAwayFromEnd, ref current) / (uint)Unsafe.SizeOf<T>()); | ||
| count += BitOperations.PopCount(mask); | ||
| } | ||
| else if (Vector256.IsHardwareAccelerated && length >= Vector256<T>.Count) | ||
| { | ||
| Vector256<T> targetVector = Vector256.Create(value); | ||
| ref T oneVectorAwayFromEnd = ref Unsafe.Subtract(ref end, Vector256<T>.Count); | ||
| do | ||
| while (Unsafe.IsAddressLessThan(ref current, ref oneVectorAwayFromEnd)) | ||
| { | ||
| count += BitOperations.PopCount(Vector256.Equals(Vector256.LoadUnsafe(ref current), targetVector).ExtractMostSignificantBits()); | ||
| current = ref Unsafe.Add(ref current, Vector256<T>.Count); | ||
| } | ||
| while (!Unsafe.IsAddressGreaterThan(ref current, ref oneVectorAwayFromEnd)); | ||
| // If there are just a few elements remaining, then processing these elements by the scalar loop | ||
| // is cheaper than doing bitmask + popcount on the full last vector. To avoid complicated type | ||
| // based checks, other remainder-count based logic to determine the correct cut-off, for simplicity | ||
| // a half-vector size is chosen (based on benchmarks). | ||
| uint remaining = (uint)Unsafe.ByteOffset(ref current, ref end) / (uint)Unsafe.SizeOf<T>(); | ||
| if (remaining > Vector256<T>.Count / 2) | ||
stephentoub marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| uint mask = Vector256.Equals(Vector256.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| // The mask contains some elements that may be double-checked, so shift them away in order to get the correct pop-count. | ||
| uint overlaps = (uint)Vector256<T>.Count - remaining; | ||
| mask >>= (int)overlaps; | ||
| count += BitOperations.PopCount(mask); | ||
| return count; | ||
| } | ||
| // Count the last vector and mask off the elements that were already counted (number of elements between oneVectorAwayFromEnd and current). | ||
| uint mask = Vector256.Equals(Vector256.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| mask >>= (int)((nuint)Unsafe.ByteOffset(ref oneVectorAwayFromEnd, ref current) / (uint)Unsafe.SizeOf<T>()); | ||
| count += BitOperations.PopCount(mask); | ||
| } | ||
| else | ||
| { | ||
| Vector128<T> targetVector = Vector128.Create(value); | ||
| ref T oneVectorAwayFromEnd = ref Unsafe.Subtract(ref end, Vector128<T>.Count); | ||
| do | ||
| while (Unsafe.IsAddressLessThan(ref current, ref oneVectorAwayFromEnd)) | ||
| { | ||
| count += BitOperations.PopCount(Vector128.Equals(Vector128.LoadUnsafe(ref current), targetVector).ExtractMostSignificantBits()); | ||
| current = ref Unsafe.Add(ref current, Vector128<T>.Count); | ||
| } | ||
| while (!Unsafe.IsAddressGreaterThan(ref current, ref oneVectorAwayFromEnd)); | ||
| uint remaining = (uint)Unsafe.ByteOffset(ref current, ref end) / (uint)Unsafe.SizeOf<T>(); | ||
| if (remaining > Vector128<T>.Count / 2) | ||
| { | ||
| uint mask = Vector128.Equals(Vector128.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| // The mask contains some elements that may be double-checked, so shift them away in order to get the correct pop-count. | ||
| uint overlaps = (uint)Vector128<T>.Count - remaining; | ||
| mask >>= (int)overlaps; | ||
| count += BitOperations.PopCount(mask); | ||
| return count; | ||
| } | ||
| // Count the last vector and mask off the elements that were already counted (number of elements between oneVectorAwayFromEnd and current). | ||
| uint mask = Vector128.Equals(Vector128.LoadUnsafe(ref oneVectorAwayFromEnd), targetVector).ExtractMostSignificantBits(); | ||
| mask >>= (int)((nuint)Unsafe.ByteOffset(ref oneVectorAwayFromEnd, ref current) / (uint)Unsafe.SizeOf<T>()); | ||
| count += BitOperations.PopCount(mask); | ||
| } | ||
| } | ||
| while (Unsafe.IsAddressLessThan(ref current, ref end)) | ||
| else | ||
| { | ||
| if (current.Equals(value)) | ||
| while (Unsafe.IsAddressLessThan(ref current, ref end)) | ||
| { | ||
| count++; | ||
| } | ||
| if (current.Equals(value)) | ||
| { | ||
| count++; | ||
| } | ||
| current = ref Unsafe.Add(ref current, 1); | ||
| current = ref Unsafe.Add(ref current, 1); | ||
| } | ||
| } | ||
| return count; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.