On 15/03/2015 12:39 a.m., Niels Möller wrote:
Amos Jeffries writes:
Is the compiler optimized code for that for loop faster or slower than a loop suming the differentials?
Not sure. But I don't think performance is very important here, the function is going to be used on pretty small inputs.
volatile unsigned char d; for (d = 0; ap >= a; ap--, bp--) d += (*ap - *bp);
I don't think that is correct, since d may wrap around to zero. One would need to accumulate into a larger variable, something like
unsigned d; for (d = 0; ap >= a; ap--, bp--) d += (uint8_t)(*ap - *bp);
which, if unsigned int is 32 bits, would be correct for n up to 2^24. (I think the cast necessary, to avoid values being promoted to *signed* int). Using | is simpler and more robust.
Aha! Thank you.
AYJ