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

Vectorize TensorPrimitives.PopCount #98281

Merged
merged 4 commits into from
Feb 16, 2024

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Feb 11, 2024

Contributes to #97193

@ghost
Copy link

ghost commented Feb 11, 2024

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

Use popcount hardware intrinsics to vectorize TensorPrimitives.PopCount for sizeof(T) == 1 on platforms that have intrinsics.

I tried doing this more generally on other platforms with:

Vector128<byte> lookup = Vector128.Create((byte)0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4);
Vector128<byte> cnt1 = Vector128.Shuffle(lookup, x.AsByte() & Vector128.Create((byte)0xF));
Vector128<byte> cnt2 = Vector128.Shuffle(lookup, (x.AsByte() >> 4) & Vector128.Create((byte)0xF));
return (cnt1 + cnt2).As<byte, T>();

and the equivalent for Vector256/512, but it ended up being ~5x slower than just using the scalar byte.PopCount for each element. I also tried using this with SumAbsoluteDifferences in support of sizeof(T) == 8, and it ended up being an order of magnitude worse.

Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Numerics

Milestone: -

@stephentoub stephentoub changed the title Use intrinsics in TensorPrimitives.PopCount for sizeof(T) == 1 Vectorize TensorPrimitives.PopCount Feb 15, 2024
@stephentoub stephentoub merged commit bfc1a74 into dotnet:main Feb 16, 2024
109 of 111 checks passed
@stephentoub stephentoub deleted the vectorizepopcount branch February 16, 2024 00:22
@github-actions github-actions bot locked and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants