Skip to content

A possible improvement of SIMD intrinsics docs #81753

Open

Description

@En3Tho

In Unity there is a very useful thing they've done with their intrinsics libraries: reference C# implementation.

For example Sse2.CompareGreaterThan (mm_cmpgt_epi16) looks like this (taken from https://github.com/needle-mirror/com.unity.burst/blob/master/Runtime/Intrinsics/x86/Sse2.cs#L1213):

public static v128 cmpgt_epi16(v128 a, v128 b)
{
    v128 dst = default(v128);
    short* aptr = &a.SShort0;
    short* bptr = &b.SShort0;
    short* dptr = &dst.SShort0;
    for (int j = 0; j <= 7; j++)
    {
        dptr[j] = (short)(aptr[j] > bptr[j] ? -1 : 0);
    }
    return dst;
}

I love intrinsics API in .Net (especially readable names!) but to understand exactly what a particular instruction does you have to consult Intel's intrinsics guide or some other resource. It would be really nice to enrich documentation with such pseudo code samples. For example thing above could look like this

public static Vector128<short> CompareGreaterThan(Vector128<short> left, Vector128<short> right)
{
    var destination = Vector128<short>.Zero;
    for (var i = 0; i <= 7; i++)
    {
        destination = destination.WithElement(i, (short) (left[i] > right[i] ? -1 : 0));
    }
    return destination;
}

A better version in terms of readability would be something like this but vector doesn't have a indexer-setter. Code above is totally valid C# but I'm not sure if a pseudo-code's goal should be validity

public static Vector128<short> CompareGreaterThan(Vector128<short> left, Vector128<short> right)
{
    var destination = Vector128<short>.Zero;
    for (var i = 0; i <= 7; i++)
    {
        destination[i] = (short) (left[i] > right[i] ? -1 : 0);
    }
    return destination;
}

What do you think? I understand that making examples like that is a ton of work but maybe those Unity samples can be used as a reference?

Activity

  1. ghost added
    untriaged
    on Feb 7, 2023
  2. tannergooding commented on Feb 7, 2023

    @tannergooding
    Member

    It would be really nice to enrich documentation with such pseudo code samples. For example thing above could look like this

    The blocker here isn't a lack of desire or due to the vastness of the work, it's a legal one. The documentation for the instructions, including the relevant psuedo-code describing how they operate, is copyrighted by the respective companies (Intel for Intel's docs, AMD for AMD's docs, etc). Due to the scale, explicit permissions are required to reproduce such documentation in our own.

    There is then the additional consideration that our docs are normally creative commons, but this subset would likely not be, which adds its own complications to the process.

  3. En3Tho commented on Feb 8, 2023

    @En3Tho
    ContributorAuthor

    Curious. I have 2 questions then:

    1. What about cross-plat vector apis? Technically they are not tied to concrete instruction sets (they even night be implemented in software or in whatever way) so I guess it's kinda more of a grey area here?

    2. Going with Unity example again: what they have is not just a reference implementation, it's an actual software fallback when Instructions aren't supported by hardware. Does it mean that they had to get a legal acceptance to introduce a software fallback like this?

  4. tannergooding commented on Mar 23, 2023

    @tannergooding
    Member

    What about cross-plat vector apis? Technically they are not tied to concrete instruction sets (they even night be implemented in software or in whatever way) so I guess it's kinda more of a grey area here?

    Yes. These are generally safe. The main "improvement" would be updating the docs to explicitly use the term element-wise to better clarify the behavior.

    Going with Unity example again: what they have is not just a reference implementation, it's an actual software fallback when Instructions aren't supported by hardware. Does it mean that they had to get a legal acceptance to introduce a software fallback like this?

    Providing a software fallback is explicitly out of the design goals of our own exposed APIs. Any questions about what Unity does should be directed towards Unity.

  5. added
    documentation
    and removed
    untriaged
    on Mar 23, 2023
  6. added this to the 10.0.0 milestone on Jul 27, 2024
  7. modified the milestones: 10.0.0, Future on Jul 14, 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.Intrinsicsdocumentation

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions