Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Mar 2011 08:45:45 GMT
From:      Dmitry Afanasiev <KOT@MATPOCKuH.Ru>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   misc/156082: gcc makes incorrect code when swapping array elements by xor without optimization
Message-ID:  <201103310845.p2V8jj5u048843@red.freebsd.org>
Resent-Message-ID: <201103310850.p2V8oCrL035799@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         156082
>Category:       misc
>Synopsis:       gcc makes incorrect code when swapping array elements by xor without optimization
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Mar 31 08:50:12 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Dmitry Afanasiev
>Release:        FreeBSD 9.0-CURRENT i386
>Organization:
>Environment:
FreeBSD orion 9.0-CURRENT FreeBSD 9.0-CURRENT #6: Sun Mar 27 03:24:55 MSD 2011     root@orion:/usr/obj/usr/src/sys/orion  amd64

>Description:
Base gcc 4.2.1 makes incorrect code when swapping array elements by xor without optimization.
Simple .c code attached as xor.txt.
Checked on FreeBSD 9.0 on i386/amd64/sparc64 platforms.
>How-To-Repeat:
# gcc -o xor xor.txt
# gcc -o xoro1 -O1 xor.txt
# ./xor
a0 = 127 b0 = 33
a1 = 127 b1 = 33
a2 = 127 b2[0] = 33
a3 = 127 b3[0] = 0
# ./xoro1
a0 = 127 b0 = 33
a1 = 127 b1 = 33
a2 = 127 b2[0] = 33
a3 = 127 b3[0] = 33

But in theory output must be identical.
>Fix:


Patch attached with submission follows:

#include <stdio.h>
#include <sysexits.h>

int
main(int argc, char *argv[])
{
	int a0 = 33;
	int b0 = 127;
	int a1 = 33;
	int b1 = 127;
	int a2 = 33;
	int b2[] = {127};
	int a3 = 33;
	int b3[] = {127};

	a0 ^= b0 ^= a0 ^= b0;
	b1 ^= a1 ^= b1 ^= a1;
	a2 ^= b2[0] ^= a2 ^= b2[0];
	b3[0] ^= a3 ^= b3[0] ^= a3;

	printf("a0 = %i b0 = %i\n", a0, b0);
	printf("a1 = %i b1 = %i\n", a1, b1);
	printf("a2 = %i b2[0] = %i\n", a2, b2[0]);
	printf("a3 = %i b3[0] = %i\n", a3, b3[0]);

	return EX_OK;
}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201103310845.p2V8jj5u048843>