Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 13:55:17 +0100
From:      Poul-Henning Kamp <phk@critter.freebsd.dk>
To:        TOMITA Yoshinori <yoshint@flab.fujitsu.co.jp>
Cc:        FreeBSD-current@FreeBSD.ORG
Subject:   Re: gcc 3.2.1 optimization bug ? 
Message-ID:  <53679.1037019317@critter.freebsd.dk>
In-Reply-To: Your message of "Mon, 11 Nov 2002 21:41:42 %2B0900." <ywlvg34jn21.fsf@cerberus.proc.flab.fujitsu.co.jp> 

next in thread | previous in thread | raw e-mail | index | archive | help
In message <ywlvg34jn21.fsf@cerberus.proc.flab.fujitsu.co.jp>, TOMITA Yoshinori
 writes:
>For the source code below, compiling gcc -O2/-O3 seem to produce
>incorrect code.
>
>-----------------------------------
>#include <stdio.h>
>int main(int argc, char* argv[])
>{
>    unsigned int x = 0x12345678;
>    unsigned short tmp;
>    printf("%x\n", x);
>    tmp = ((unsigned short *)&x)[0];
>    ((unsigned short *)&x)[0] = ((unsigned short *)&x)[1];
>    ((unsigned short *)&x)[1] = tmp;
>    printf("%x\n", x);
>    return 0;
>}
>-----------------------------------

>% gcc -O2 a.c
>% ./a.out
>12345678
>12341234

% sed 's/unsigned int/unsigned volatile int/' a.c > b.c
% gcc -O2 b.c
% ./a.out
12345678
56781234

Your code forgot to tell the compiler that you would be messing
with the variables storage directly.

-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk@FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




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