Optimize VectorX.Clamp according to proposed change (#34896)
authorAlFas <AlFasGD@users.noreply.github.com>
Thu, 23 Apr 2020 00:53:07 +0000 (03:53 +0300)
committerGitHub <noreply@github.com>
Thu, 23 Apr 2020 00:53:07 +0000 (17:53 -0700)
* Optimize Vector4.Clamp according to proposed change

* Apply clamp optimizations to all vector types

src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs

index 10bdc32..dc74961 100644 (file)
@@ -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);
         }
 
         /// <summary>
index a1e5c8c..af110ff 100644 (file)
@@ -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);
         }
 
         /// <summary>
index b240061..30dd343 100644 (file)
@@ -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);
         }
 
         /// <summary>