This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

optimization/9840: Incorrect optimization for floating-point aliasing to int

>Number:         9840
>Category:       optimization
>Synopsis:       Incorrect optimization for floating-point aliasing to int
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Feb 24 21:26:00 UTC 2003
>Originator:     Alex Gontmakher
>Release:        gcc version 3.2.2 (Mandrake Linux 9.1 3.2.2-2mdk)
machine: Athlon MP 1800
The following code worked with previous versions of GCC, producing either 1 or 0. Now it produces 2.

A workaround is to uncomment the printf() line - obviously causes it to perform the initialization of the float prior to executing the ifs.

The problem happens only at optimization level O2 or higher.
#include <iostream>

int f()
  int *p;
  double x = 1.0;
  /* NOTE: this check assumes IEEE floating point format */
  p = (int *)&x;
  if (*p == 0)
    return 0;
  else if (*p == 0x3ff00000)
    return 1;
    return 2;

int main()
  std::cout << f() << std::endl;
I didn't read the optimization code for this, but it should probably notice that p aliases &x, and perform the initialization of x prior to reading *p.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]