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

Question : What is the most performant way to represent constant System.Runtime.Intrinsics.VectorXXX<T>? #40900

Open
RamType0 opened this issue Aug 16, 2020 · 3 comments
Labels
area-System.Runtime.Intrinsics documentation Documentation bug or enhancement, does not impact product or test code help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@RamType0
Copy link

When we want some constant vector,what is the most performant way?
(e.g. static Vector256<int> Stair => Vector256.Create(1,2,3,4,5,6,7,8);,
static readonly Vector256<int> Stair = Vector256.Create(1,2,3,4,5,6,7,8);)
I hope it documented on MSDN.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Aug 16, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@gfoidl
Copy link
Member

gfoidl commented Aug 16, 2020

Beginning with .NET 5 just use VectorXYZ.Create with constant values. E.g.

private void MethodThatUsesVectors()
{
    Vector128<int> constVec = Vector128.Create(1, 2, 3, 4);
}

Then the JIT will emit a "constant" which is then loaded from memory.
#35857 introduced that.

Before .NET 5 there are two patterns:

private static readonly Vector128<int> s_constVec1 = Vector128.Create(1, 2, 3, 4);

Or if the constant vector is of a type with size of one byte (so byte, sbyte, ...):

private void MethodThatUsesVectors()
{
    ReadOnlySpan<byte> constantData = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    Vector128<byte> constantVec = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(constantData));
}

This version uses C#'s ability to read ROS from static data segment of the assembly. See https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static/ for further infos.

Always measure your goals, and don't just blindly rely on advices, as there may be some corner-cases, etc.

I hope it documented on MSDN.

Good idea to have this infos consolidated at one place.
Side note: it's now called Microsoft Docs.

@ghost
Copy link

ghost commented Aug 16, 2020

Tagging subscribers to this area: @tannergooding
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas added the documentation Documentation bug or enhancement, does not impact product or test code label Aug 16, 2020
@tannergooding tannergooding added this to the Future milestone Sep 14, 2020
@tannergooding tannergooding added help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime.Intrinsics documentation Documentation bug or enhancement, does not impact product or test code help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

5 participants