public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50975] New: Logical operators evaluated in wrong order if no side effects
@ 2011-11-03  7:19 gcc.hall at gmail dot com
  2011-11-03  8:26 ` [Bug c/50975] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: gcc.hall at gmail dot com @ 2011-11-03  7:19 UTC (permalink / raw)
  To: gcc-bugs

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:
--------------------------------------------


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-11-03 21:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-03  7:19 [Bug c/50975] New: Logical operators evaluated in wrong order if no side effects gcc.hall at gmail dot com
2011-11-03  8:26 ` [Bug c/50975] " rguenth at gcc dot gnu.org
2011-11-03 12:38 ` gcc.hall at gmail dot com
2011-11-03 21:09 ` ebotcazou at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).