Reputation: 81
I'm trying to make vector with 4 doubles with System.Numerics library because of SIMD. So I made this struct:
public struct Vector4D
{
System.Numerics.Vector<double> vecXY, vecZW;
...
}
In this phase I code it for 128bit SIMD register. It works fine, but when I want something like this:
Vector4D* pntr = stackalloc Vector4D[8];
I get this:
Cannot take the address of, get the size of, or declare a pointer to a managed type ('Vector4D')
Any idea how to use stackalloc with System.Numerics.Vector? With System.Numerics.Vector4 (which is float-precision) there is no problem with pointers, but I need double-precision.
Upvotes: 5
Views: 892
Reputation: 81
I solved it:
public struct Vector4D
{
public double X, Y, Z, W;
private unsafe Vector<double> vectorXY
{
get
{
fixed (Vector4D* ptr = &this)
{
return SharpDX.Utilities.Read<Vector<double>>((IntPtr)ptr);
}
}
set
{
fixed (Vector4D* ptr = &this)
{
SharpDX.Utilities.Write<Vector<double>>((IntPtr)ptr, ref value);
}
}
}
private unsafe Vector<double> vectorZW
{
get
{
fixed (Vector4D* ptr = &this)
{
return SharpDX.Utilities.Read<Vector<double>>((IntPtr)((double*)ptr) + 2);
}
}
set
{
fixed (Vector4D* ptr = &this)
{
SharpDX.Utilities.Write<Vector<double>>((IntPtr)((double*)ptr) + 2, ref value);
}
}
}
...
}
This gives you Vector for SIMD operations and also you can use pointer to struct. Unfortunately it's around 50% slower than using static array without SIMD.
Upvotes: 1