That is, what is the fastest way to do the test
if( a >= ( b - c > 0 ? b - c : 0 ) &&
a <= ( b + c < 255 ? b + c : 255 ) )
...
if a, b, and c are all unsigned char aka BYTE. I am trying to optimize an image scanning process to find a sub-image, and a comparison such as this is done about 3 million times per scan, so even minor optimizations could be helpful.
Not sure, but maybe some sort of bitwise operation? Maybe adding 1 to c and testing for less-than and greater-than without the or-equal-to part? I don’t know!
Well, first of all let’s see what you are trying to check without all kinds of over/underflow checks:
Now that is equal to
And in code:
Now, this code is a tad faster and doesn’t contain (or need) complicated underflow/overflow checks.
I’ve profiled mine and 6502’s code with 1000000000 repitions and there officially was no difference whatsoever. I would suggest to pick the most elegant solution (which is IMO mine, but opinions differ), since performance is not an argument.
However, there was a notable difference between my and the asker’s code. This is the profiling code I used:
With my if statement this took 120ms on average and the asker’s if statement took 135ms on average.