[Bug c/50975] New: Logical operators evaluated in wrong order if no side effects
gcc.hall at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Nov 3 07:19:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50975
Bug #: 50975
Summary: Logical operators evaluated in wrong order if no side
effects
Classification: Unclassified
Product: gcc
Version: 4.6.2
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: gcc.hall@gmail.com
gcc 4.6.2 -Os
If the expressions either side of a logical operator (|| or &&) have no
side effects, gcc sometimes evaluates them in the wrong order. See
the example below. Of course the code works, but the standard is clear,
logical operators GUARANTEE left to right evaluation. In this case
people use the feature as a micro optimization, putting the most likely to
succeed case first with || or vice-versa with &&.
-----------------------------------
#include <stdio.h>
int
main( int argc, char *argv[] )
{
char *last = argv[1];
if( *last == 10 || *last == 13 )
--last;
printf("last = %p\n", last );
return 0;
}
---------------------------------------------
movb (%eax), %dl # *last_3, D.2517
cmpb $13, %dl #, D.2517
je .L4 #,
cmpb $10, %dl #, D.2517
jne .L2 #,
.L4:
decl %eax # last
.L2:
--------------------------------------------
More information about the Gcc-bugs
mailing list