Repository navigation
Description
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
tannergooding commentedon Feb 7, 2023
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.
En3Tho commentedon Feb 8, 2023
Curious. I have 2 questions then:
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?
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?
tannergooding commentedon Mar 23, 2023
Yes. These are generally safe. The main "improvement" would be updating the docs to explicitly use the term
element-wiseto better clarify the behavior.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.