public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/22394] New: floating point comparison fails with -O2
@ 2005-07-10 15:15 glenn at aoi-industries dot com
  2005-07-10 15:37 ` [Bug c++/22394] " glenn at aoi-industries dot com
  2005-07-10 18:09 ` pinskia at gcc dot gnu dot org
  0 siblings, 2 replies; 3+ messages in thread
From: glenn at aoi-industries dot com @ 2005-07-10 15:15 UTC (permalink / raw)
  To: gcc-bugs

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.

[glenn@DepotSt 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:

[glenn@DepotSt 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:

[glenn@DepotSt 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/22394] floating point comparison fails with -O2
  2005-07-10 15:15 [Bug c++/22394] New: floating point comparison fails with -O2 glenn at aoi-industries dot com
@ 2005-07-10 15:37 ` glenn at aoi-industries dot com
  2005-07-10 18:09 ` pinskia at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: glenn at aoi-industries dot com @ 2005-07-10 15:37 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From glenn at aoi-industries dot com  2005-07-10 15:15 -------
Created an attachment (id=9239)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=9239&action=view)
save-temps file that demonstrates problem


-- 


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/22394] floating point comparison fails with -O2
  2005-07-10 15:15 [Bug c++/22394] New: floating point comparison fails with -O2 glenn at aoi-industries dot com
  2005-07-10 15:37 ` [Bug c++/22394] " glenn at aoi-industries dot com
@ 2005-07-10 18:09 ` pinskia at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-07-10 18:09 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-07-10 18:09 -------


*** This bug has been marked as a duplicate of 323 ***

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
 GCC target triplet|                            |i586-mandrake-linux-gnu
         Resolution|                            |DUPLICATE


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-07-10 18:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-10 15:15 [Bug c++/22394] New: floating point comparison fails with -O2 glenn at aoi-industries dot com
2005-07-10 15:37 ` [Bug c++/22394] " glenn at aoi-industries dot com
2005-07-10 18:09 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).