I need to find the highest and lowest value in an array of floats. Optionally, I want to be able to skip members of the array and evaluate only every 2nd, 4th, 8th, etc. element:
float maxValue = 0.0;
float minValue = 0.0;
int index = 0;
int stepwith = 8;
for( int i = 0; i < pixelCount; i++ )
{
float value = data[index];
if( value > maxValue )
maxValue = value;
if( value < minValue )
minValue = value;
index += stepwidth;
if( index >= dataLength )
break;
}
That seems to be the fastest way without using other magic.
So I tried other magic, namely the vIsmax() and vIsmin() functions from vecLib (included in OSX' Accelerate framework) which apparently uses processor-level acceleration of vector operations:
int maxIndex = vIsmax( pixelCount, data );
int minIndex = vIsmin( pixelCount, data );
float maxValue = data[maxIndex];
float minValue = data[minIndex];
It is very fast but doesn't allow skipping values (the functions don't offer a 'stride' argument). This makes it actually slower than my first code because I can easily skip every 8th value.
I even found a third way with BLAS which implements a similar function:
cblas_isamax( const int __N, const float *__X, const int __incX )
with __incX = stride to skip values, but it isn't fast at all and only finds absolute maxima which doesn't work for me.
So my question: can anyone think of another way to accelerate this?