C# System.Numerics.Vector Clamp The software that I develop uses large floating-point arrays up to the maximum size that can be allocated in C#. I have a large number of algorithms such as convolutions and filters that get executed over those large arrays. I am currently updating as many algorithms as possible to fully threaded and vectorized.

By utilizing the System.Numerics.Vector&lt;T&gt; methods, I am seeing typically a 300%+ performance improvement in many of the algorithms on computers equipped with AVX (where Vector&lt;float&gt;.Count returns 4) and a 600%+ performance improvement in many of the algorithms on computers equipped with AVX2 (where Vector&lt;float&gt;.Count returns 8).

NET Standard 2.1 System.Numerics.Vector&lt;T&gt; here:

https://docs.microsoft.com/en-us/dotnet/api/system.numerics.vector-1?view=netstandard-2.1

One of the functions that I require on a number of the algorithms is to Clamp the array element value to a bounds Minimum or Maximum value after performing some mathematical operation on it. That is of course really easy to do with the single-threaded and multi-threaded algorithms that use standard arithmetic operations.

The issue that I am having is that System.Numerics.Vector&lt;T&gt; doesn't include any kind of Clamp method (Vector2, 3, 4 do). So, for example, if I loop over a large array, modifying the array in Vector&lt;float&gt;.Count chunks, I need to clamp each vector result to a min and/or max bounds prior to writing that vector-sized chunk back to the array.

I tried doing the Clamp in a loop on the array chunk data after the Vector operations, but the performance is abysmal. It is as slow or slower than simply doing the algorithm without vectorization.

Is there any way that I can conceivably improve the performance of this Clamp method?

This is some typical code of how I tried clamping. I fill the vector with a chunk of the array, perform some vector math, write the chunk back to the array, this is all nice and speedy, but then Clamping the array chunk in a loop after just kills the vectorization performance advantage.

int length = array.Length;

int floatcount = System.Numerics.Vector&lt;float&gt;.Count;

for (int i = 0; i &lt; length; i += floatcount)
{
    System.Numerics.Vector&lt;float&gt; arrayvector = new System.Numerics.Vector&lt;float&gt;(array, i);

    arrayvector = System.Numerics.Vector.Multiply&lt;float&gt;(arrayvector, 2.0f);
    // There may be different or multiple vector operations in here.

    arrayvector.CopyTo(array, i);

    // This is how I tried clamping the array data after the vector operation:    
    for (int j = 0; j &lt; floatcount; j++)
    {
        if (array[i + j] &gt; maximimum) { array[i + j] = maximimum; }
    }

}


I'm probably being myopic and missing something really simple. That's what months of 16-hour programming days gets you. ;) Thanks for any insight.
Have you tried something like this:

Create the following vector:

  System.Numerics.Vector&lt;float&gt; maxima = new System.Numerics.Vector&lt;float&gt;(maximimum);


Then after the multiplication call:

arrayvector = System.Numerics.Vector.Min(arrayvector, maxima);


Here you may have to create an new vector instead of reassigning to arrayvector?



So all in all it ends up like:

  int length = array.Length;

  int floatcount = System.Numerics.Vector&lt;float&gt;.Count;
  System.Numerics.Vector&lt;float&gt; maxima = new System.Numerics.Vector&lt;float&gt;(maximimum);

  for (int i = 0; i &lt; length; i += floatcount)
  {
    System.Numerics.Vector&lt;float&gt; arrayvector = new System.Numerics.Vector&lt;float&gt;(array, i);

    arrayvector = System.Numerics.Vector.Multiply(arrayvector, 2.0f);
    arrayvector = System.Numerics.Vector.Min(arrayvector, maxima);
    // There may be different or multiple vector operations in here.

    arrayvector.CopyTo(array, i);

  }


Disclaimer: I haven't tested the above, so don't hang me if it's not an improvement :-)