Incompatibility between gcc and glibc

M.Schimschak masch@iff077.iff.kfa-juelich.de
Wed Mar 11 12:17:00 GMT 1998


I already submitted this report to bug-gcc@prep.ai.mit.edu
(gnu.gcc.bug), but didn't get any feedback yet. Since this bug also
applies to egcs-1.0.1 I send it to you as well. 
Here's the program which is compiled with errors by several gcc based
compilers when used with optimization, basically when inlining is
allowed (see below):

-------------------------------------------------------------------------------

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


int main(void)
{
  double     c=0;
  double     d=0;
  double     e[2];


  e[0] = 1.0;
  e[1] = 1.0;

  
  printf("%5g %5g   %10g %10g\n",e[0], e[1], c, d);

  c = atan2(e[0], e[1]);
  d = e[0] * e[0] + e[1] * e[1];

  printf("%5g %5g   %10g %10g\n",e[0], e[1], c, d);

  return 0;
}

-------------------------------------------------------------------------------
This program is supposed to produce the following output:

"    1     1                 0               0
     1     1      0.7853981634               2"

Instead it produces the following output with several compilers and
several optimization levels:

"    1     1                 0               0
     1     1      0.7853981634     1.785398163"
                                   ^^^^^^^^^^^

It seems that there is some kind of incompatibility between gcc and
glibc. I traced the bug down to the inline optimization. On a glibc
(2.0.6) based Red Hat Linux system, gcc-2.7.2.3 produces erroneous
code when used with -O2. On the same system  egcs-2.90.23 980102
(egcs-1.0.1 release) produces erroneous code when used with -O1.
In both cases the compilers produce correct code when used with
-fno-inline. In the case where the -fno-inline flag is used the
linking of libm is necessary, whereas in the case where inlining is
allowed the code for the atan2 function call is taken from
"/usr/include/__math.h" which is included by math.h. Here's the code
block from __math.h, maybe it will help (unfortunately I have no idea
what is going on there ;-)

-----------------------------------------------------------------------

__MATH_INLINE double atan2 (double __y, double __x);
__MATH_INLINE double
atan2 (double __y, double __x)
{
  register double __value;
  __asm __volatile__
    ("fpatan\n\t"
     "fldl %%st(0)"
     : "=t" (__value) : "0" (__x), "u" (__y));

  return __value;
}

----------------------------------------------------------------------

Systems that I checked so far involve 
   Red Hat 4.2 Linux system (libc 5.3.12, i486) -- no problems
       gcc version 2.7.2.1 -- no problems

  Red Hat 5.0 Linux system (glibc 2.0.6, i586) -- all with erroneous code
       gcc version 2.7.2.3
       gcc version 2.8.0
       gcc version 2.8.1
       egcs-2.90.23 980102 (egcs-1.0.1 release) (erroneous code for
                                                 -O1, save for -O0 or -O2)
       pgcc-2.91.04 980115 (gcc-2.8.0 release)

  Dec Alpha system  (Digital UNIX V4.0B, ?, alpha) -- no problems
       gcc-2.8.0

It would be interesting to know if the error also occurs on Dec Alphas 
when used with Red Hat 5.0 Linux.


Hope this helps for tracing the bug,

        Martin



More information about the Gcc-bugs mailing list