From d09c058e4b7a23f00f033f35ae60bc06f71f2bcf Mon Sep 17 00:00:00 2001 From: AlFas Date: Thu, 23 Apr 2020 03:53:07 +0300 Subject: [PATCH] Optimize VectorX.Clamp according to proposed change (#34896) * Optimize Vector4.Clamp according to proposed change * Apply clamp optimizations to all vector types --- .../src/System/Numerics/Vector2.cs | 11 +---------- .../src/System/Numerics/Vector3.cs | 15 +-------------- .../src/System/Numerics/Vector4.cs | 19 +------------------ 3 files changed, 3 insertions(+), 42 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs index 10bdc32..dc74961 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs @@ -193,17 +193,8 @@ namespace System.Numerics [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max) { - // This compare order is very important!!! // We must follow HLSL behavior in the case user specified min value is bigger than max value. - float x = value1.X; - x = (min.X > x) ? min.X : x; // max(x, minx) - x = (max.X < x) ? max.X : x; // min(x, maxx) - - float y = value1.Y; - y = (min.Y > y) ? min.Y : y; // max(y, miny) - y = (max.Y < y) ? max.Y : y; // min(y, maxy) - - return new Vector2(x, y); + return Vector2.Min(Vector2.Max(value1, min), max); } /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs index a1e5c8c..af110ff 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs @@ -218,21 +218,8 @@ namespace System.Numerics [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max) { - // This compare order is very important!!! // We must follow HLSL behavior in the case user specified min value is bigger than max value. - float x = value1.X; - x = (min.X > x) ? min.X : x; // max(x, minx) - x = (max.X < x) ? max.X : x; // min(x, maxx) - - float y = value1.Y; - y = (min.Y > y) ? min.Y : y; // max(y, miny) - y = (max.Y < y) ? max.Y : y; // min(y, maxy) - - float z = value1.Z; - z = (min.Z > z) ? min.Z : z; // max(z, minz) - z = (max.Z < z) ? max.Z : z; // min(z, maxz) - - return new Vector3(x, y, z); + return Vector3.Min(Vector3.Max(value1, min), max); } /// 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 b240061..30dd343 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs @@ -195,25 +195,8 @@ namespace System.Numerics [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Clamp(Vector4 value1, Vector4 min, Vector4 max) { - // This compare order is very important!!! // We must follow HLSL behavior in the case user specified min value is bigger than max value. - float x = value1.X; - x = (min.X > x) ? min.X : x; // max(x, minx) - x = (max.X < x) ? max.X : x; // min(x, maxx) - - float y = value1.Y; - y = (min.Y > y) ? min.Y : y; // max(y, miny) - y = (max.Y < y) ? max.Y : y; // min(y, maxy) - - float z = value1.Z; - z = (min.Z > z) ? min.Z : z; // max(z, minz) - z = (max.Z < z) ? max.Z : z; // min(z, maxz) - - float w = value1.W; - w = (min.W > w) ? min.W : w; // max(w, minw) - w = (max.W < w) ? max.W : w; // min(w, minw) - - return new Vector4(x, y, z, w); + return Vector4.Min(Vector4.Max(value1, min), max); } /// -- 2.7.4