✅ How to make the compile treat a specific struct as a vector?

As I understand it, the compile has special handling for vector types, which allows them to have better performance than regular structs. Is there a way to get the compile to recognize a struct as a vector to get the same performance benefits? Or have I misunderstood what it is doing and it doesn't have special handling?
10 Replies
Jimmacle
Jimmacle2mo ago
what do you mean when you say vector? if you're referring to vectors in terms of SIMD then that is a specific hardware intrinsic used to optimize doing the same operation on multiple numbers at the same time it can't be generally applied to any data structure to make it faster
MechWarrior99
MechWarrior99OP2mo ago
I read (now I can't remember where, but was creditable) that the System.Numerics Vector2, Vector3, etc. were treated differently in some cases to be more performant. For context, I am working on a mathematics library, containing things like double based vector2/3/4, and potentially alternatives for the System.Numerics types.
MechWarrior99
MechWarrior99OP2mo ago
Stack Overflow
What does the [Intrinsic] attribute in C# do?
A quick Google search for "instrinsic attribute c#" only returns articles about other attributes, such as [Serializable]. Apparently these are called "intrinsic attributes". However, there is als...
Jimmacle
Jimmacle2mo ago
i don't think you can use that in your own code
MechWarrior99
MechWarrior99OP2mo ago
Yeah, that is what it seems, but I think that may be what was being referred to when the person said that the System.Numerics vectors had different handling
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle2mo ago
yes, that suggests that the JIT/runtime has specific special optimizations for only those types/methods
MechWarrior99
MechWarrior99OP2mo ago
Thanks I will try there
Anton
Anton2mo ago
if your vector type has the same layout as the builtin one, you could reinterpret a span of your vectors as that of the built-in vectors if you really had to
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?