Finding min value of an array using SIMD I have the following code to find the minimum sbyte value in an array. It is using System.Runtime.Intrinsics to perform a SIMD min on chunks of the array, and then loops over the resulting vector to find the true minimum.

public static sbyte Min( sbyte[] array )
{
  if( array.Length &lt;= 0 )
    return 0;

  var length = array.Length;
  var stepSize = Vector128&lt;sbyte&gt;.Count;

  fixed ( sbyte* pStep = &amp;array[ 0 ] )
  {
    var i = stepSize;
    var minVector = Avx.LoadVector128( pStep );
    for( ; i &lt;= length - stepSize; i += stepSize )
      minVector = Avx.Min( minVector, Avx.LoadVector128( pStep + i ) );

    var _ = stackalloc sbyte[ stepSize ];
    Avx.Store( _, minVector );

    // Find min of minVector
    var min = sbyte.MaxValue;
    for( var j = 0; j &lt; stepSize; j++ )
      if( min &gt; _[ j ] )
        min = _[ j ];

    // Evaluate remaining elements
    if( i &lt; length )
      while( i &lt; length )
      {
        if( min &gt; pStep[ i ] )
          min = pStep[ i ];
        i++;
      }

    return min;
  }
}


I feel like this is a slightly excessive amount of code, but seeing how this function is meant to handle arrays with lengths that can't be equally partitioned into a 128-bit register, I'm not sure if there's any cleaner way to do this. 

I do intend to implement this method for all supported primitive types, but it seems like most of the code will just be duplicated.
Correctness

  if( array.Length &lt;= 0 )
    return 0;


This piece of code is suspicious. Maybe you should return sbyte.MaxValue, maybe null, or maybe throw ArgumentException. (I can imagine that there were a Max method, both, together is used to find the range of values, then maybe 0 is a valid return value.) This is one of the rare times I'd appreciate a comment to explain such extrinsic information.

Useless Code

 if( i &lt; length )
      while( i &lt; length )


Lose the if.

Excessive code

When you are operating on fixed size chunks, 
 - pad the input with some appropriate value (sbyte.MaxValue)
 - handle the last fixed size chunk of the input separately.

The last suggestion should read something like this, (which means I don't even guarantee that it will compile):

if (length &lt; stepSize)
    throw new ArgumentException($"this method cannot be used for arrays shorter than {stepSize}") 

Vector128&lt;sbyte&gt; minVector;
SetAllVector128(minVector, sbyte.MaxValue);
for (var i = 0; i &lt; length - stepSize; i += stepSize)
    minVector = Avx.Min(minVector, Avx.LoadVector128(pStep + i));

minVector = Avx.Min(minVector, Avx.LoadVector128(pStep + length - stepSize));


Then you can use the last loop altogether;