From ff742780ae5eca1cb5a62b6028da4b71505f316a Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Thu, 25 May 2023 18:21:22 -0700 Subject: [PATCH 1/2] Attempt to optimize the Vector4 element constructor in mono interp --- .../src/System/Numerics/Vector4.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs index 831b4f50d2556f..995692c8c49f8d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs @@ -15,6 +15,7 @@ namespace System.Numerics /// [!INCLUDE[vectors-are-rows-paragraph](~/includes/system-numerics-vectors-are-rows.md)] /// ]]> [Intrinsic] + [StructLayout(LayoutKind.Sequential)] public partial struct Vector4 : IEquatable, IFormattable { /// The X component of the vector. @@ -63,10 +64,18 @@ public Vector4(Vector3 value, float w) : this(value.X, value.Y, value.Z, w) [Intrinsic] public Vector4(float x, float y, float z, float w) { - X = x; - Y = y; - Z = z; - W = w; + if (Vector128.IsHardwareAccelerated) + { + Unsafe.SkipInit(out this); + Unsafe.As>(ref this.X) = Vector128.Create(x, y, z, w); + } + else + { + X = x; + Y = y; + Z = z; + W = w; + } } /// Constructs a vector from the given . The span must contain at least 4 elements. From 999477cdf4d8359ffcea87fb2378d673f9e1a062 Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Thu, 25 May 2023 18:48:22 -0700 Subject: [PATCH 2/2] TIL: 'ref this' is legal in structs now --- .../System.Private.CoreLib/src/System/Numerics/Vector4.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs index 995692c8c49f8d..fde8bb3182de6e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs @@ -15,7 +15,6 @@ namespace System.Numerics /// [!INCLUDE[vectors-are-rows-paragraph](~/includes/system-numerics-vectors-are-rows.md)] /// ]]> [Intrinsic] - [StructLayout(LayoutKind.Sequential)] public partial struct Vector4 : IEquatable, IFormattable { /// The X component of the vector. @@ -67,7 +66,7 @@ public Vector4(float x, float y, float z, float w) if (Vector128.IsHardwareAccelerated) { Unsafe.SkipInit(out this); - Unsafe.As>(ref this.X) = Vector128.Create(x, y, z, w); + Unsafe.As>(ref this) = Vector128.Create(x, y, z, w); } else {