Let's say I have a constant array of floats with defined size 22 that looks like this:
array[0]= 0;
array[1]= 0.5;
array[2]= 0.7;
array[3]= 1.8;
...
...
array[21]= 4.2;
The values of this array are monotonic, this is, they always increase with the index ( array[0]<=array[1]<=array[2]<=....<=array[21] ).
And I want a function that given a float, it finds the index of the array whose value is immediately below the input float (and thus, the value of the next index is immediately above)
For example, in the previous case, if the input of the function is the value 0.68, the output of the function should be 1, since array[1]<= 0.68
Now, this is easy to implement, but i am dealing with a very time critical part of the code in an embedded system, and I really need a very optimized algorithm, that avoids loops (to avoid the overhead).
The simplest approach i am using now is just to unfold the loop with if-elses and that's it.
Example:
if(input >= array[size-1])
return size-1;
else if(input>= array[size-2])
return size-2;
else if(input >= array[size-3])
return size-3;
...
...
But the problem here is that then I have jitter, since the path for different inputs take significantly different time.
So my question is: is there a fastest and more deterministic (less jitter) way to implement this?
Thank you. Jorge.