For the bounty: how can this behavior be disabled in each case without disabling or reducing the level of optimization?
The following conditional expression was compiled on MinGW GCC 3.4.5, where a is of type signed long and m is of type unsigned long .
if (!a && m > 0x002 && m < 0x111)
The CFLAGS used were -g -O2 . Here is the corresponding output of the GCC assembly (reset with objdump )
120: 8b 5d d0 mov ebx,DWORD PTR [ebp-0x30] 123: 85 db test ebx,ebx 125: 0f 94 c0 sete al 128: 31 d2 xor edx,edx 12a: 83 7d d4 02 cmp DWORD PTR [ebp-0x2c],0x2 12e: 0f 97 c2 seta dl 131: 85 c2 test edx,eax 133: 0f 84 1e 01 00 00 je 257 <_MyFunction+0x227> 139: 81 7d d4 10 01 00 00 cmp DWORD PTR [ebp-0x2c],0x110 140: 0f 87 11 01 00 00 ja 257 <_MyFunction+0x227>
120 - 131 you can easily follow how to evaluate !a first and then evaluate m > 0x002 . The first transition does not occur until 133 . By this time, the two expressions were evaluated, regardless of the result of the first expression !a If a was zero, the expression can (and should) be immediately terminated, which is not done here.
How does this relate to the C standard, which requires Boolean operators to close as soon as the result can be determined?
Unsigned
source share