First: This is pure speculation. I have not written this code and cannot confirm my guess.
There is one very important semantic difference between these two versions of code:
// Version A if (*p++ == (unsigned char)c) return ((void *)(p - 1)); // Version B if (*p == (unsigned char)c) return ((void *) p); ++p;
In version A, the increment is sequenced before the code block of this if
, while in version B it is sequenced after this block.
Thus, in version A, the increment code will be placed before the jump command, which is most likely generated from if
. At least we can assume that such a relatively direct translation from C code to assembly from compilers of the time the code was written (1988?).
Does the conditional increment nest have some optimizations for some compiler / platform?
Having incremented before the branch allows for relatively simple optimization on architectures that have a delay slot in the branch instructions: you can move the increment to this delay slot instead of having a NOP.
Thus, version A requires one less instruction for each iteration of the loop than version B, due to one decrement when the function returns. This is (micro) optimization.
source share