public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/66673] New: swapping variables via chained xor fails
@ 2015-06-25 19:23 joe.carnuccio at qlogic dot com
  2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: joe.carnuccio at qlogic dot com @ 2015-06-25 19:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66673

            Bug ID: 66673
           Summary: swapping variables via chained xor fails
           Product: gcc
           Version: 4.4.6
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joe.carnuccio at qlogic dot com
  Target Milestone: ---

This is the same as 39121 which has been marked RESOLVED INVALID (to which I
strongly disagree):

this produces incorrect executable: *p ^= *q ^= *p ^= *q;
( if gcc option "-Os" is used, then that produces correct executable )

( by contrast, this always produces correct executable: a ^= b ^= a ^= b; )


root@elab305:/home/joe/test/c# cat x.c
#include <stdio.h>

int main(int  argc, char **argv)
{
        int a = 0x32, b = 0x45;
        int *p = &a, *q = &b;

        *p ^= *q ^= *p ^= *q;
        printf("%x %x\n", a, b);

        return 0;
}
root@elab305:/home/joe/test/c# make -B x
cc    -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
0 32                          <--INCORRECT
root@elab305:/home/joe/test/c# make -B x CFLAGS+='-Os'
cc -Os   -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
45 32                         <--CORRECT
root@elab305:/home/joe/test/c#


Notice that when -Os (optimize for space rather than speed) is used, the
executable produces the correct result.


Also, doing the chained xor on the integer variables a and b themselves always
produces the correct result (regardless of optimization).


root@elab305:/home/joe/test/c# gcc --version
gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3)
. . .

root@elab305:/home/joe/test/c# uname -a
Linux elab305 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64
x86_64 x86_64 GNU/Linux

root@elab305:/home/joe/test/c# cat /etc/issue
Red Hat Enterprise Linux Server release 6.2 (Santiago)
Kernel \r on an \m


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

end of thread, other threads:[~2015-06-25 21:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
2015-06-25 19:41 ` joe.carnuccio at qlogic dot com
2015-06-25 19:42 ` joe.carnuccio at qlogic dot com
2015-06-25 19:46 ` joe.carnuccio at qlogic dot com
2015-06-25 19:54 ` trippels at gcc dot gnu.org
2015-06-25 19:56 ` mpolacek at gcc dot gnu.org
2015-06-25 21:09 ` [Bug c/66673] Wsequence-point warning missing when swapping variables via chained xor trippels 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).