This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimization o2 vs o3 issue
- From: Andrew Haley <aph at redhat dot com>
- To: manish_baphna at yahoo dot com
- Cc: gcc-help at gcc dot gnu dot org, John Fine <johnsfine at verizon dot net>
- Date: Mon, 22 Dec 2008 14:18:16 +0000
- Subject: Re: Optimization o2 vs o3 issue
- References: <196101.69698.qm@web94716.mail.in2.yahoo.com>
Manish Baphna wrote:
> Hi
> 1.Its NOT multi threaded application.
>
> 2. I created a small example where this issue is visible. Let me know what you think after it.
>
> ///////////// bug.cpp ///////////
> #include <iostream>
> #include <string>
> #include <stdio.h>
>
> using namespace std ;
>
> typedef unsigned char uint8_t ;
>
> class DBus {
> public:
> uint8_t data[16] ;
> };
>
> int main(){
> DBus dout ;
> long long unsigned upper_data = 0xFFFFFFFff ;
> long long unsigned int mask = 0xFF ;
>
> for(int count=0 ; count < 8 ; count++)
> {
> dout.data[15 - count] = (uint8_t)((upper_data & (mask << ((8+count)*8)))>>((8+count)*8));
> //cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl; // Line to be played with
> }
>
> for(int count=0 ; count < 8 ; count++)
> {
> cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl;
> }
>
>
>
>
> return 1 ;
> };
>
> ///////////// end of bug.cpp /////////
> Step1 : Compile using o2/o3
> bash>> g++-4.1.1 -g3 -O2 -fPIC -Wall -W -Wcast-qual -Wpointer-arith
> bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
> bash>> ./a.out
> Dout data 255
> Dout data 255
> Dout data 255
> Dout data 255
> Dout data 15
> Dout data 0
> Dout data 0
> Dout data 0
> bash>> g++-4.1.1 -g3 -O3 -fPIC -Wall -W -Wcast-qual -Wpointer-arith
> bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
> bash>> ./a.out
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
>
> Now if you uncomment the cout line , o3 would also work !!
>
>
> Let me know what you guys think of it.
It's undefined behaviour.
(mask << ((8+count)*8))
See C99, Section 6.5.7, Para 3.
Andrew.