The problem is the evaluation order:
The C ++ standard does not specify how to evaluate subexpressions. This is done so that the compiler is as aggressive as possible during optimization.
Let's break it:
a1 a2 v = ( ( p[ i++ ] & 0xFF ) << 4 | ( p[ i ] & 0xF0000000 ) >> 28; ----- (1) a1 = p[i] (2) i = i + 1 (i++) after (1) (3) a2 = p[i] (4) t3 = a1 & 0xFF after (1) (5) t4 = a2 & 0xF0000000 after (3) (6) t5 = t3 << 4 after (4) (7) t6 = t4 >> 28 after (5) (8) t7 = t5 | t6 after (6) and (7) (9) v = t7 after (8)
Now the compiler is free to reorder, thus, auxiliary expressions if the above “after” positions are not violated. Thus, one quick optimization is moving to 3 slots and then deleting the common expressions (1) and (3) (now next to each other) are the same, and therefore we can eliminate (3)
But the compiler does not need to do optimization (and, probably, is better than me in this and has other tricks up its sleeve). But you can see how the value of (a1) will always be what you expect, but the value of (a2) will depend on what order the compiler decides to do other subexpressions.
The only guarantees that you have are that the compiler cannot move subexpressions through a point in the sequence. Your most common point in the sequence is ';' (end of statement). There are others, but I would avoid using this knowledge, since most people do not know the compiler very well. If you write code that uses tricks with sequence points, then someone can rearrange the code to look more readable, and now your trick has just turned into undefined be-behavior.
short v = ( p[ i++ ] & 0xFF) << 4; v |= ( p[ i ] & 0xF0000000 ) >> 28; ----- (1) a1 = p[i] (2) i = i + 1 (i++) after (1) (4) t3 = a1 & 0xFF after (1) (6) t5 = t3 << 4 after (4) (A) v = t5 after (6) ------ Sequence Point (3) a2 = p[i] (5) t4 = a2 & 0xF0000000 after (3) (7) t6 = t4 >> 28 after (5) (8) t7 = v | t6 after (7) (9) v = t7 after (8)
Everything is clearly defined here, since the entry in the self is performed on the spot and is not reread in the same expression.
A simple rule. do not use ++ or - operators inside a larger expression. Your code looks just as easy to read:
++i;
See this article for a detailed explanation of the evaluation procedure:
What are all the common undefined behaviors that a C ++ programmer should know about?
Martin york
source share