public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/9794: Problem with -O2 on solaris causing variables to be overwritten
@ 2003-02-21 21:36 wlp
  0 siblings, 0 replies; 2+ messages in thread
From: wlp @ 2003-02-21 21:36 UTC (permalink / raw)
  To: gcc-gnats


>Number:         9794
>Category:       optimization
>Synopsis:       Problem with -O2 on solaris causing variables to be overwritten
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Feb 21 21:36:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Will Pughe
>Release:        Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.2/specs
>Organization:
>Environment:
SunOS 5.8 Generic_108528-12 sun4u sparc SUNW,Sun-Fire-280R
>Description:
We have a library that packs a float into a character array,
then unpacks it later.  The values of the float were coming
out wrong when compiled with -O2.  If a value of 999.9 is
packed in, a value of 0 comes out.  This code worked on
all previous versions of g++, and works on g++3.2 for linux,
redhat 8.0.

The command line used is:
g++ -O2 prog.C -o prog -Wall
No warnings are reported.  The output is
fval 999.9
fval 0

The output should be
fval 999.9
fval 999.9
>How-To-Repeat:
#include <iostream>

using namespace std;

static char *buf;
static char *bufPtr;

void pack( float fval )
{
  // Following are volatile to prevent optimizers from reusing the 
  // allocated stack space for other purposes following the last 
  // direct reference to the address of the float value. (The g++ 3.2
  // optimizer apparently gets faked out by the cast of the float address
  // to an int address, and thinks the float address is free for reuse
  // prior to the underlying bits actually being packed into the buffer)
  //  volatile float fvalTmp = fval;
  
  //  volatile int *ival = (volatile int *)&fvalTmp;
  int *ival = (int *)&fval;

  *bufPtr++ = (*ival >> 24) & 0xff;
  *bufPtr++ = (*ival >> 16) & 0xff;
  *bufPtr++ = (*ival >> 8) & 0xff;
  *bufPtr++ = *ival & 0xff;
}

void unpack( float& val )
{
  int ival ;

  ival = *bufPtr++ << 24 ;
  ival |= (*bufPtr++ << 16) & 0x00ff0000;
  ival |= (*bufPtr++ << 8) & 0x0000ff00;
  ival |= (*bufPtr++ & 0xff);
  val = *((float *)&ival);
}

int main(int argc, char **argv )
{
  buf = new char[4];
  bufPtr = buf;

  float fval( 999.9 );
  cerr << "fval " << fval << endl;
  pack( fval );
  bufPtr = buf;
  unpack( fval );
  cerr << "fval " << fval << endl;
}
>Fix:
To fix the problem, uncomment the two volatile declarations
in the pack function, and comment out the next int
declaration.
>Release-Note:
>Audit-Trail:
>Unformatted:
 Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-nls
 Thread model: posix
 gcc version 3.2
 


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

* Re: optimization/9794: Problem with -O2 on solaris causing variables to be overwritten
@ 2003-02-22 10:24 ebotcazou
  0 siblings, 0 replies; 2+ messages in thread
From: ebotcazou @ 2003-02-22 10:24 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, wlp

Synopsis: Problem with -O2 on solaris causing variables to be overwritten

State-Changed-From-To: open->closed
State-Changed-By: ebotcazou
State-Changed-When: Sat Feb 22 10:24:47 2003
State-Changed-Why:
    Not a bug. Your code breaks the ISO C++ aliasing rules.
    GCC automatically turns on -fstrict-aliasing (see the docs,
    Optimize options section) at -O2, so you must either fix
    your code or compile it with -fno-strict-aliasing.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9794


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

end of thread, other threads:[~2003-02-22 10:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-21 21:36 optimization/9794: Problem with -O2 on solaris causing variables to be overwritten wlp
2003-02-22 10:24 ebotcazou

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).