From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QcP55-0008SH-SF for garchives@archives.gentoo.org; Thu, 30 Jun 2011 21:47:28 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8CE7B1C02F; Thu, 30 Jun 2011 21:45:45 +0000 (UTC) Received: from QMTA11.westchester.pa.mail.comcast.net (qmta11.westchester.pa.mail.comcast.net [76.96.59.211]) by pigeon.gentoo.org (Postfix) with ESMTP id 42AF11C02F for ; Thu, 30 Jun 2011 21:45:45 +0000 (UTC) Received: from omta19.westchester.pa.mail.comcast.net ([76.96.62.98]) by QMTA11.westchester.pa.mail.comcast.net with comcast id 2Mei1h00527AodY5BMlli5; Thu, 30 Jun 2011 21:45:45 +0000 Received: from ajax ([24.11.47.14]) by omta19.westchester.pa.mail.comcast.net with comcast id 2MlY1h00r0JMh7c3fMlciE; Thu, 30 Jun 2011 21:45:40 +0000 Date: Thu, 30 Jun 2011 17:45:30 -0400 From: Frank Peters To: gentoo-amd64@lists.gentoo.org Subject: [gentoo-amd64] GCC-4.5.2 Has Serious Problems Message-Id: <20110630174530.9bcbcd47.frank.peters@comcast.net> X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; x86_64-unknown-linux-gnu) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-amd64@lists.gentoo.org Reply-to: gentoo-amd64@lists.gentoo.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Multipart=_Thu__30_Jun_2011_17_45_30_-0400_MdYZX4o+oZhXukI2" X-Archives-Salt: X-Archives-Hash: f7915760e23d981e858caa0a0d386004 This is a multi-part message in MIME format. --Multipart=_Thu__30_Jun_2011_17_45_30_-0400_MdYZX4o+oZhXukI2 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hello, After banging my head for a while over some strange results, I began to suspect GCC-4.5.2, the latest version in portage, was creating faulty code. It seems to a correct suspicion. What follows is a short C program that reproduces my particular problem, but there are likely many other situations where GCC could fail. The code may be a difficult for some people to follow but only the output is what actually matters. Simpler examples will certainly exist but I have not had the chance to develop them. The program is included here as both in-line text and as a file attachment (gcc_test.c). The problem is described below. ------------------------------------ #include #include int main(void) { int n; double x; unsigned long int arg; unsigned long int *px = (unsigned long int*)&x; x=0.0; n=0; arg=0x4010000000000000; while(n<5) { printf("%lx %g\n",arg, x); *px=arg; printf("%lx %g\n\n",arg, x); n++; arg=arg+0x0010000000000000; } exit(0); } ------------------------- What this code does is not very important (it uses pointers to enter hexadecimal values into a floating point variable within a short loop). The output is what matters. Compiling with "gcc -O2 -march=native -o gcc_test gcc_test.c" produces this output: Loop: 0 Val = 4010000000000000, X before = 0 Val = 4010000000000000, X after = 0 --> X should be 4!!! Loop: 1 Val = 4020000000000000, X before = 0 Val = 4020000000000000, X after = 4 Loop: 2 Val = 4030000000000000, X before = 4 Val = 4030000000000000, X after = 8 The printf statements are included before and after the variable, x, is assigned a value. Notice how in the first iteration of the loop the x variable does *not* get assigned a value. The value of x during the first iteration should be 4. The problem can be fixed by compiling the program with "O1" optimization: gcc -O1 -march=native -o gcc_test gcc_test.c Using "O1" the output now becomes: Loop: 0 Val = 4010000000000000, X before = 0 Val = 4010000000000000, X after = 4 --> Now it works!!! Loop: 1 Val = 4020000000000000, X before = 4 Val = 4020000000000000, X after = 8 Loop: 2 Val = 4030000000000000, X before = 8 Val = 4030000000000000, X after = 16 This is now the correct output. In the first iteration the X variable is assigned the proper value. For anyone who wants to try to duplicate these results, please feel free to do so. So I will have to conclude that GCC-4.5.2 has serious problems. This kind of erroneous behavior could appear anywhere. Frank Peters --Multipart=_Thu__30_Jun_2011_17_45_30_-0400_MdYZX4o+oZhXukI2 Content-Type: text/x-csrc; name="gcc_test.c" Content-Disposition: attachment; filename="gcc_test.c" Content-Transfer-Encoding: 7bit #include #include int main(void) { int n; double x; unsigned long int arg; unsigned long int *px = (unsigned long int*)&x; x=0.0; n=0; arg=0x4010000000000000; while(n<3) { printf("%s %d\n%s %lx, %s %g\n", "Loop:", n,"Val =", arg, "X before = ", x); *px=arg; printf("%s %lx, %s %g\n\n", "Val =", arg, "X after = ", x); n++; arg=arg+0x0010000000000000; } exit(0); } --Multipart=_Thu__30_Jun_2011_17_45_30_-0400_MdYZX4o+oZhXukI2--