This is the mail archive of the gcc@gcc.gnu.org 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]

x86 FP optimizer behaviour


Hello,  

        I have spent today beating my head against a brick wall with
the following code (made somewhat more concise than what I started
with).  On the surface it looks like an optimizer bug: the value
printed out is different (and wrong) if you compile with -O than
without.  Notice in the second case however, that the problem goes
away if you move the printf statement to be before the comparison that
fails.  So I'm not sure what the actual problem is given the presence
of the printf shouldn't affect anything at all (surely the optimizer
can't do anything with it) ....



--
James Macnicol
j-macnicol@adfa.edu.au

-------------------------------------------------------------------------------
miranda(0):~% uname -a
Linux miranda 2.4.13 #7 Thu Nov 8 10:47:22 EST 2001 i686 unknown
miranda(0):~% gcc --version
2.95.4
miranda(0):~% cat dqp.c
#include <stdio.h>

int foo(const unsigned bar, const unsigned baz)
{ 
  double x;
  int rv = 0;

  x = (double)(bar - baz) / (double)(bar * bar);

  if (x < 0.008)
    rv = 1;
  else if (x < 0.012)
    rv = 2;

  printf("%.20g\n", x);

  return (rv);
}

int main(void)
{ 
  printf("%d\n", foo(25, 20));

  return (0);
}
miranda(0):~% gcc -Wall -o dqp dqp.c
miranda(0):~% ./dqp
0.0080000000000000001665
2
miranda(0):~% gcc -Wall -O -o dqp dqp.c
miranda(0):~% ./dqp
0.0080000000000000001665
1
miranda(0):~% cat dqp2.c
#include <stdio.h>

int foo(const unsigned bar, const unsigned baz)
{
  double x;
  int rv = 0;
  
  x = (double)(bar - baz) / (double)(bar * bar);

  printf("%.20g\n", x);

  if (x < 0.008)
    rv = 1;
  else if (x < 0.012)
    rv = 2;

  return (rv);
}

int main(void)
{
  printf("%d\n", foo(25, 20));

  return (0);
}
miranda(0):~% gcc -Wall -o dqp2 dqp2.c
miranda(0):~% ./dqp2
0.0080000000000000001665
2
miranda(0):~% gcc -Wall -O -o dqp2 dqp2.c
miranda(0):~% ./dqp2
0.0080000000000000001665
2
-------------------------------------------------------------------------------


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