public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/37067]  New: gcc creating wrong code with -O2
@ 2008-08-09 14:38 stefan dot bruens at rwth-aachen dot de
  2008-08-09 18:03 ` [Bug c/37067] " rguenth at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: stefan dot bruens at rwth-aachen dot de @ 2008-08-09 14:38 UTC (permalink / raw)
  To: gcc-bugs

The following code is producing false results with -O2 optimization (-O1 and O3
are correct):
-------
#include <stdio.h>
#define BLOCK_SIZE 100000

int main()
{
  int i;
  float block_f[BLOCK_SIZE];
  int d_phase = 0;
  int d_phase_inc = 0x100000;

  for (i = 0; i < BLOCK_SIZE; i++){
      block_f[i] = (float)(d_phase);
      d_phase += d_phase_inc;
    }

  printf ("%x %x\n", (int)block_f[1695], (int)block_f[1696]);
  return 0;
}
-----
With -O2, the loop is run only 0x6a0 times, instead of 0x186a0 times.

gcc is from openSUSE 11.0 (4.3.1 20080507 (prerelease) [gcc-4_3-branch revision
135036] (SUSE Linux)), but fc9 is most probably affected as well.

Failing code is a very simplified test case from gnuradio
(http://www.ruby-forum.com/topic/156807)


-- 
           Summary: gcc creating wrong code with -O2
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: stefan dot bruens at rwth-aachen dot de


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
@ 2008-08-09 18:03 ` rguenth at gcc dot gnu dot org
  2008-08-09 20:38 ` stefan dot bruens at rwth-aachen dot de
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-08-09 18:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-08-09 18:01 -------
This works for me as far as I can see (you didn't specify the expected
output, and certainly if the loop doesn't run as often as you want you
may print uninitialized memory).  Anyway, if it doesn't work then it is
because d_phase overflows and so invokes undefined behavior.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
  2008-08-09 18:03 ` [Bug c/37067] " rguenth at gcc dot gnu dot org
@ 2008-08-09 20:38 ` stefan dot bruens at rwth-aachen dot de
  2008-08-09 20:44 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: stefan dot bruens at rwth-aachen dot de @ 2008-08-09 20:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from stefan dot bruens at rwth-aachen dot de  2008-08-09 20:37 -------
The expected output is quite clear - 69f00000 6a000000 - but output is 69f00000
0.
The loop should run 100000 times, as i does not overflow.
d_phase does overflow after 2048 steps, but this should not influence the loop.

with -O2, gcc seems to do the following:
- d_phase is the same as i*0x100000
- the block should run 0x186a0 times
- (0x186a0 * 0x100000) & 0xffffffff = 0x6a000000
- break when d_phase reaches 0x6a000000 (1696 increment steps)

with -O3 and -O1, d_phase and i get their own variables
there should be at least a warning


-- 

stefan dot bruens at rwth-aachen dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
                   ` (2 preceding siblings ...)
  2008-08-09 20:44 ` rguenth at gcc dot gnu dot org
@ 2008-08-09 20:44 ` pinskia at gcc dot gnu dot org
  2008-08-09 20:47 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-08-09 20:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2008-08-09 20:42 -------
>The loop should run 100000 times, as i does not overflow.

i does not overflow but d_phase_inc does ...


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
  2008-08-09 18:03 ` [Bug c/37067] " rguenth at gcc dot gnu dot org
  2008-08-09 20:38 ` stefan dot bruens at rwth-aachen dot de
@ 2008-08-09 20:44 ` rguenth at gcc dot gnu dot org
  2008-08-09 20:44 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-08-09 20:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2008-08-09 20:43 -------
As I said - it works for me (i686, gcc (Debian 4.3.1-8) 4.3.1).  Still the
overflow invokes undefined behavior and thus _can_ affect the loop.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
                   ` (3 preceding siblings ...)
  2008-08-09 20:44 ` pinskia at gcc dot gnu dot org
@ 2008-08-09 20:47 ` pinskia at gcc dot gnu dot org
  2008-08-09 20:57 ` stefan dot bruens at rwth-aachen dot de
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-08-09 20:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2008-08-09 20:46 -------
>The loop should run 100000 times, as i does not overflow.

i does not overflow but d_phase does ...

2046 7ff00000
2047 80000000
That is an overflow.

So is:
4094 fff00000
4095 0


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
                   ` (4 preceding siblings ...)
  2008-08-09 20:47 ` pinskia at gcc dot gnu dot org
@ 2008-08-09 20:57 ` stefan dot bruens at rwth-aachen dot de
  2008-08-09 21:02 ` brian at dessent dot net
  2008-08-09 21:08 ` rguenth at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: stefan dot bruens at rwth-aachen dot de @ 2008-08-09 20:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from stefan dot bruens at rwth-aachen dot de  2008-08-09 20:56 -------
So is there any possibility to flag a variable to be expected to overflow and
"wrap around"? I think there is a lot of code in the wild assuming the loop to
run 100000 times and d_phase to overflow all the time, becoming negative.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
                   ` (5 preceding siblings ...)
  2008-08-09 20:57 ` stefan dot bruens at rwth-aachen dot de
@ 2008-08-09 21:02 ` brian at dessent dot net
  2008-08-09 21:08 ` rguenth at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: brian at dessent dot net @ 2008-08-09 21:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from brian at dessent dot net  2008-08-09 21:00 -------
Subject: Re:  gcc creating wrong code with -O2

Just make it unsigned since unsigned overflow is always defined.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/37067] gcc creating wrong code with -O2
  2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
                   ` (6 preceding siblings ...)
  2008-08-09 21:02 ` brian at dessent dot net
@ 2008-08-09 21:08 ` rguenth at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-08-09 21:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from rguenth at gcc dot gnu dot org  2008-08-09 21:07 -------
You can use -fwrapv.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37067


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2008-08-09 21:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-09 14:38 [Bug c/37067] New: gcc creating wrong code with -O2 stefan dot bruens at rwth-aachen dot de
2008-08-09 18:03 ` [Bug c/37067] " rguenth at gcc dot gnu dot org
2008-08-09 20:38 ` stefan dot bruens at rwth-aachen dot de
2008-08-09 20:44 ` rguenth at gcc dot gnu dot org
2008-08-09 20:44 ` pinskia at gcc dot gnu dot org
2008-08-09 20:47 ` pinskia at gcc dot gnu dot org
2008-08-09 20:57 ` stefan dot bruens at rwth-aachen dot de
2008-08-09 21:02 ` brian at dessent dot net
2008-08-09 21:08 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).