From owner-freebsd-current Mon Nov 11 4:55:44 2002 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5F48637B401 for ; Mon, 11 Nov 2002 04:55:43 -0800 (PST) Received: from critter.freebsd.dk (critter.freebsd.dk [212.242.86.163]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2117A43E7B for ; Mon, 11 Nov 2002 04:55:42 -0800 (PST) (envelope-from phk@critter.freebsd.dk) Received: from critter.freebsd.dk (localhost [127.0.0.1]) by critter.freebsd.dk (8.12.6/8.12.6) with ESMTP id gABCtHOr053680; Mon, 11 Nov 2002 13:55:22 +0100 (CET) (envelope-from phk@critter.freebsd.dk) To: TOMITA Yoshinori Cc: FreeBSD-current@FreeBSD.ORG Subject: Re: gcc 3.2.1 optimization bug ? In-Reply-To: Your message of "Mon, 11 Nov 2002 21:41:42 +0900." Date: Mon, 11 Nov 2002 13:55:17 +0100 Message-ID: <53679.1037019317@critter.freebsd.dk> From: Poul-Henning Kamp Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In message , TOMITA Yoshinori writes: >For the source code below, compiling gcc -O2/-O3 seem to produce >incorrect code. > >----------------------------------- >#include >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