镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

Description

@RamType0

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.

Activity

  1. Dotnet-GitSync-Bot commented on Aug 16, 2020

    @Dotnet-GitSync-Bot
    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.

  2. gfoidl commented on Aug 16, 2020

    @gfoidl
    Member

    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.

  3. added
    documentationDocumentation bug or enhancement, does not impact product or test code
    on Aug 16, 2020
  4. added this to the Future milestone on Sep 14, 2020
  5. added
    help wanted[up-for-grabs] Good issue for external contributors
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 14, 2020
  6. dotnet-policy-service commented on Mar 16, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  7. dotnet-policy-service commented on Mar 30, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  8. removed this from the Future milestone on Mar 30, 2025
  9. locked and limited conversation to collaborators on Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtime.IntrinsicsdocumentationDocumentation bug or enhancement, does not impact product or test codehelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions