This problem exists in version 3.4.1; it doesn't in version 3.3.2 or 4.0.1, on
Intel 586 architecture (both AMD Athlon and P4 cpus).

I'm posting this note for reference, in case the change in the code generation
inadvertently (as opposed to intentionally) fixed the problem.

This program has different behavior when compiled with or without -O2.  The code
generated for the floating point comparison at line 32 is different.

[EMAIL PROTECTED] ras]$ g++ -v
Reading specs from /usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking --enable-long-long
--enable-__cxa_atexit --enable-clocale=gnu --disable-libunwind-exceptions
--enable-languages=c,c++,ada,f77,objc,java --host=i586-mandrake-linux-gnu
--with-system-zlib
Thread model: posix
gcc version 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)

Intel(R) Pentium(R) 4 CPU 2.80GHz
AMD Athlon(tm) Processor

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

#define irint(x)   ((int)((x) > 0 ? ((x) + .5):((x) - .5)))
#define sqr(x)      ((x)*(x))
#define NUM_ARC_PTS 9

main()
{
    struct {
        int x, y;
    } start, xy, ij, center;
    double startAng, endAng, radius, theta, dTheta;

    start.x = 7078039;
    start.y = 2942821;
    xy.x = 7078039;
    xy.y = 2942821;
    ij.x = -87702;
    ij.y = 702547;

    center.x = start.x + ij.x;
    center.y = start.y + ij.y;
        
    startAng = atan2(-ij.y, -ij.x);
    if (startAng < 0) startAng += 2*M_PI;

    endAng = atan2(xy.y - center.y, xy.x - center.x);
    if (endAng < 0) endAng += 2*M_PI;

    // clockwise traversal
    if (startAng > endAng) {
        printf("angles not equal\n");
        dTheta = (startAng - endAng) / (NUM_ARC_PTS + 1);
    } else
        dTheta = (2*M_PI - (endAng - startAng)) / (NUM_ARC_PTS + 1);
    
    printf("startAng %f, endAng %f, dTheta %f\n",
           startAng, endAng, dTheta);
    printf("startAng > endAng: %d\n", startAng > endAng);
    printf("startAng < endAng: %d\n", startAng < endAng);
    printf("startAng == endAng: %d\n", startAng == endAng);
    printf("startAng: %x %x\n", *((unsigned int*)&startAng),
           *((unsigned int*)&startAng+1));
    printf("endAng:   %x %x\n", *((unsigned int*)&endAng),
           *((unsigned int*)&endAng+1));
    printf("dTheta:   %x %x\n", *((unsigned int*)&dTheta),
           *((unsigned int*)&dTheta+1));
}

Correct output:

[EMAIL PROTECTED] ras]$ g++ ttt.cc -o ttt; ttt
startAng 4.836581, endAng 4.836581, dTheta 0.628319
startAng > endAng: 0
startAng < endAng: 0
startAng == endAng: 1
startAng: a803a092 401358a8
endAng:   a803a092 401358a8
dTheta:   769cf0e0 3fe41b2f

Incorrect output:

[EMAIL PROTECTED] ras]$ g++ -O2 ttt.cc -o ttt; ttt
angles not equal
startAng 4.836581, endAng 4.836581, dTheta 0.000000
startAng > endAng: 0
startAng < endAng: 0
startAng == endAng: 1
startAng: a803a092 401358a8
endAng:   a803a092 401358a8
dTheta:   0 0

-- 
           Summary: floating point comparison fails with -O2
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: glenn at aoi-industries dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22394

Reply via email to