Another "cc -O2" compiler bug?

From: Mattias Ohlsson <mattias_at_thep.lu.se>
Date: Thu, 5 Jan 1995 16:28:12 +0100

Consider this small piece of code:

------------------
#include <stdio.h>
#include <math.h>

void tmp( double );

void main()
{
  double x;

  x = 1.0;
  tmp(x);
}

void tmp( double xx )
{
  double fkn1, fkn2, arg, aux1, aux2;
  
  aux2 = xx;
  aux1 = 1.0;

  /** original definition - gives the inverse of the correct thing **/
  fkn1 = exp(-aux1 * aux2);

  /** alternative definition - gives the correct result **/
  arg = aux1 * aux2;
  fkn2 = exp(-arg);

  printf("%f %f %f %f\n", xx, fkn1, fkn2, fkn1 * fkn2);
}
------------------

If I compile with default optimization I get the following
correct result:

>> cc -o bug bug.c -lm
>> bug
1.000000 0.367879 0.367879 0.135335
>>

If I however compile with -O2 optimization, bad things happen.

>> cc -O2 -o bug bug.c -lm
>> bug
1.000000 2.718282 0.367879 1.000000
>>

For some strange reason the compiler forgets about the minus
sign in front of aux1.

fkn1 = exp(-aux1 * aux2);
-----------^

Can anyone verify this? If so, where is the patch?

My system:

>> uname -a
OSF1 selma.thep.lu.se V3.0 347 alpha


/Mattias

---------------------------------
Mattias Ohlsson
Department of Theoretical Physics
Lund University
Sweden
---------------------------------
Received on Thu Jan 05 1995 - 10:28:25 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:45 NZDT