public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mateusz Loskot <mateusz@loskot.net>
To: gcc-help@gcc.gnu.org
Subject: [GCC 4.3] Strange -O2 optimization issue
Date: Fri, 01 Aug 2008 21:56:00 -0000	[thread overview]
Message-ID: <4893866F.9050800@loskot.net> (raw)

Hello,

I'm experiencing a strange problem while building a small program
using GCC 4.3.1 (Debian, Lenny) with -O2 optimization.
The program is a simple hash generator from 2D point,
using high and low word of a coordinate (object of double type).
I include a small test program [1]

Here are 3 test cases that differ in optimization flag only

$ g++ -W -Wall -o bar bar.cpp
$ ./bar
1073741824
1073741824
1073741824
1073741824
1073741824

$ g++ -O1 -W -Wall -o bar bar.cpp
$ ./bar
1073741824
1073741824
1073741824
1073741824
1073741824

$ g++ -O2 -W -Wall -o bar bar.cpp
$ ./bar
136623933
1073741824
1073741824
1073741824
1073741824


Why the first value printed is different (136623933) in the 3rd
test case.

Also, could anyone enlighten me and explain what kind of optimization is 
applied when -O2 flag is used, so the first value printed is different?

I'd be also very thankful for references in C/C++ standards
explaining this behavior of GCC.

I've noticed, that if I uncomment the printf in HashDouble function,
third test case (-O2 optimization) returns all the same values.

Perhaps my test program is buggy, for example I suspect the cast in 
HashDouble function. Is it?

I run my tests cases using GCC 4.0.1 and the problem
does not leak there.

Thanks in advance for your help!



[1] Test program

// bar.cpp ////////////////////////////////////////////////
#include <cstdio>

struct Point
{
public:
     double x;
     double y;
     double getX() const { return x;}
     double getY() const { return y;}
};
static unsigned long HashDouble(double* pdfVal)
{
     unsigned int* pnValue = (unsigned int*)pdfVal;

     // printf("%p\n", pnValue); // Seems to fix the problem

     return pnValue[0] ^ pnValue[1];
}
static unsigned long HashPoint(const Point* point)
{
     double x = point->getX();
     double y = point->getY();
     return HashDouble(&x) ^ HashDouble(&y);
}
int main()
{
     Point* po = new Point();
     po->x = 1;
     po->y = 2;

     for (int i = 0; i < 5; ++i)
     {
         std::printf("%lu\n", HashPoint(po));
     }
     delete po;
     return 0;
}
///////////////////////////////////////////////////////////

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org

             reply	other threads:[~2008-08-01 21:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-01 21:56 Mateusz Loskot [this message]
2008-08-02  0:11 ` Brian Dessent
2008-08-02  2:15   ` Mateusz Loskot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4893866F.9050800@loskot.net \
    --to=mateusz@loskot.net \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).