Continuing: "cc -O? compiler bug"

From: Joe Spanicek <joe_at_resptk.bhp.com.au>
Date: Wed, 11 Jan 95 10:52:34 EST

Hi osf-managers,

I been curious about the "cc -O? compiler bug" Mattias Ohlsson reported on
in several mailings. I have several bits of code that use the 'exp' function
and slightly worry calculations are going wrong...

With the following example again:
-------------------------------------------------------------------------------
#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, fkn3, 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);

  /** This syntax works **/
  fkn3 = exp(aux2 * -aux1);

  printf("xx=%f fkn1=%f fkn2=%f fkn3=%f fkn1*fkn2=%f\n", xx,
                                                                fkn1,
                                                                fkn2,
                                                                fkn3,
                                                                fkn1 * fkn2);

  /** This syntax within printf works as well **/
  printf("afkn1=%f\n", exp(-aux1 * aux2) );
}
-------------------------------------------------------------------------------

I obtained the following results:

cc -o test2 test2.c -lm
xx=1.000000 fkn1=0.367879 fkn2=0.367879 fkn3=0.367879 fkn1*fkn2=0.135335
afkn1=0.367879

cc -O -o test2 test2.c -lm
xx=1.000000 fkn1=2.718282 fkn2=0.367879 fkn3=0.367879 fkn1*fkn2=1.000000
afkn1=0.367879

cc -O2 -o test2 test2.c -lm
xx=1.000000 fkn1=2.718282 fkn2=0.367879 fkn3=0.367879 fkn1*fkn2=1.000000
afkn1=0.367879

cc -migrate -O2 -o test2 test2.c -lm
xx=1.000000 fkn1=0.367879 fkn2=0.367879 fkn3=0.367879 fkn1*fkn2=0.135335
afkn1=0.367879

The problem is evident on all optimization settings.

Using the -migrate command will remidy the problem, but I have problems
compiling using this option. It seems the easy way out or next best thing
is to change the code as for 'fkn3' shown above and the calculation will be
performed properly. With the afkn1 result I assume the optimization is not
performed with functions embedded within printf statements.

I believe this is a serious bug and should be immediately fixed by DEC, the
bug also lies with the 'expf' function and possibly other 'log' functions.

I am using a AXP 3000/300 running OSF/1 V3.0 and it may well be this version
is at fault, I will be checking with DEC on this. May be, later versions of
OSF/1 has the fix (hopefully).

I hope this note is of help for people who may be experiencing problems,
Just check if your code uses the 'exp' function and change it if possible.

Any other problems experienced by others will be a godsend to me as I will
be developeing code using extensive mathematical functions.

Cheers Joe.

+-----------------------------------------------------------------------------+
| |
| Joe Spanicek |
| /\/\ Research Officer |
| / / /\ Process Engineering R & D |
| / / / \ |
| / / / /\ \ BHP Research - Port Kembla Labs |
| \ \/ / / / P.O Box 77, Port Kembla, NSW 2505 |
| \ / / / Australia |
| \/\/\/ |
| Email: joe_at_resptk.bhp.com.au |
| |
+-----------------------------------------------------------------------------+
Received on Tue Jan 10 1995 - 18:47:39 NZDT

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