Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 19:40:18 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
To:        Thomas David Rivers <rivers@dignus.com>
Cc:        FreeBSD-current@FreeBSD.ORG, <gallatin@cs.duke.edu>
Subject:   Re: gcc 3.2.1 optimization bug ?
Message-ID:  <20021111193234.E32091-100000@beagle.fokus.gmd.de>
In-Reply-To: <200211111802.gABI2jX55939@lakes.dignus.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 11 Nov 2002, Thomas David Rivers wrote:

TDR>Harti Brandt <brandt@fokus.gmd.de> 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




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