From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20279 invoked by alias); 1 Aug 2008 21:56:30 -0000 Received: (qmail 20262 invoked by uid 22791); 1 Aug 2008 21:56:29 -0000 X-Spam-Check-By: sourceware.org Received: from gamma.dmkhost.com (HELO gamma.dmkhost.com) (67.19.42.2) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 01 Aug 2008 21:56:04 +0000 Received: from [195.24.248.244] (port=50271 helo=[192.168.1.100]) by gamma.dmkhost.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1KP2bK-0003N8-EN for gcc-help@gcc.gnu.org; Fri, 01 Aug 2008 23:55:54 +0200 Message-ID: <4893866F.9050800@loskot.net> Date: Fri, 01 Aug 2008 21:56:00 -0000 From: Mateusz Loskot User-Agent: Thunderbird 2.0.0.16 (Macintosh/20080707) MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: [GCC 4.3] Strange -O2 optimization issue Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-08/txt/msg00004.txt.bz2 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 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