From owner-freebsd-current Mon Nov 11 10:40:26 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 5AA9B37B401 for ; Mon, 11 Nov 2002 10:40:24 -0800 (PST) Received: from mailhub.fokus.gmd.de (mailhub.fokus.gmd.de [193.174.154.14]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2F87043E3B for ; Mon, 11 Nov 2002 10:40:22 -0800 (PST) (envelope-from brandt@fokus.gmd.de) Received: from beagle (beagle [193.175.132.100]) by mailhub.fokus.gmd.de (8.11.6/8.11.6) with ESMTP id gABIeIl29115; Mon, 11 Nov 2002 19:40:18 +0100 (MET) Date: Mon, 11 Nov 2002 19:40:18 +0100 (CET) From: Harti Brandt To: Thomas David Rivers Cc: FreeBSD-current@FreeBSD.ORG, Subject: Re: gcc 3.2.1 optimization bug ? In-Reply-To: <200211111802.gABI2jX55939@lakes.dignus.com> Message-ID: <20021111193234.E32091-100000@beagle.fokus.gmd.de> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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 On Mon, 11 Nov 2002, Thomas David Rivers wrote: TDR>Harti Brandt wrote: TDR>> TDR>> TDR>> Hmm, I though the following would work: TDR>> TDR>> void TDR>> foo(unsigned short *s) TDR>> { TDR>> unsigned short temp; TDR>> TDR>> temp = s[0]; TDR>> s[0] = s[1]; TDR>> s[1] = temp; TDR>> } TDR>> TDR>> main() TDR>> { TDR>> int i = 0x12345678; TDR>> TDR>> foo(&i); TDR>> printf("%08x\n", i); TDR>> } TDR>> TDR>> because how would the compiler in main() know, that you do something wrong TDR>> in foo(). But... if you compile this with -O5, it does not work! This is TDR>> because the compiler inlines foo() into main and the program prints junk like TDR>> 0x12342804. TDR> TDR> TDR> Nope - that doesn't work either. The call to foo() is not compatible TDR> with the prototype (in fact, the Systems/C compiler issues a warning TDR> on this: TDR> TDR> Warning #2034: passing argument 1 from incompatible pointer type TDR> TDR> I believe gcc would as well. Yes, of course, but one would assume it to work (I suppose there is a large amount of code that assumes it will work). It's funny also to see, that the program changes behaviour if you swap main() and foo(). In this case the compiler cannot inline. I just tried to give a counter example to your hope, that every sane compiler would do the right thing with using a union for casting. I would also assume that every sane compiler would do the right thing for the above code. IMHO, the C-standard is broken with regard to the rules for type compatibility. As far as I remember these rules came into the standard from one or two well-known PC compiler vendors that wanted to allow very aggressive optimisation to show good benchmark results. The 'restrict' keyword seems to be also an outcome of this. harti -- harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private brandt@fokus.gmd.de, brandt@fokus.fhg.de To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message