Skip to content

Provide a zero-cost mechanism for going between System.Numerics.Vector and System.Runtime.Intrinsics.Vector types #952

@tannergooding

Description

@tannergooding

At the 2019 MVP Summit, I got some level of feedback that we do not currently have a zero-cost mechanism for converting between the various System.Numerics.Vector types and the System.Runtime.Intrinsic vector types.

We should come up with an API proposal to make this conversion process non-expensive.

  • For types like Vector4 <-> Vector128<float> this should be true zero-cost.
  • For types like Vector2 or Vector3 this should be zero-cost if the type is either already in register or if it was spilled to the stack as a TYP_SIMD16.
  • Vector<T> is a bit more troublesome given that it is dynamically sized.

Proposed API

// Vector2, Vector3, and Vector4 will be moved down to S.P.Corelib

namespace System.Runtime.Intrinsics
{
    public static partial class Vector128
    {
        // Converting to intrinsic types
        public static Vector128<float> AsVector128(this Vector2 value);
        public static Vector128<float> AsVector128(this Vector3 value);
        public static Vector128<float> AsVector128(this Vector4 value);
        public static Vector128<T> AsVector128<T>(this Vector<T> value) where T : struct;

        // Converting from intrinsic types
        public static Vector2 AsVector2(this Vector128<float> value);
        public static Vector3 AsVector3(this Vector128<float> value);
        public static Vector4 AsVector4(this Vector128<float> value);
        public static Vector<T> AsVector<T>(this Vector128<T> value) where T : struct;
    }

    public static partial class Vector256
    {
        // Converting to intrinsic types
        public static Vector256<T> AsVector256<T>(this Vector<T> value) where T : struct;

        // Converting from intrinsic types
        public static Vector<T> AsVector<T>(this Vector256<T> value) where T : struct;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions