GCC 3.2.x - 3.3 Use of optimization -O2 results in an incorrect code.

Alexander Riedel riedel@complead.de
Tue May 20 14:37:00 GMT 2003


Hi !!!

Use of a mentioned below code with compilers gcc-3.2 gcc-3.2.2 gcc-3.3 
(optimization -O2 ) results in incorrect code. 

At use gcc-2.95.3 all OK!

Example: 
1)		gcc-2.95.3 -O2 test.cc -o test
		./test
		i=58		! All correct 

2)		gcc-3.3 -O0 test.cc -o test
		./test
		i=58 	! All correct 

3)		gcc-3.3 -O1 test.cc -o test
		./test
		i=58 	! All correct 

4)		gcc-3.3 -O2 test.cc -o test
		./test
		i=2 		!!! Error !!!

5)		gcc-3.3 -O3 test.cc -o test
		./test
		i=58 	!!!!!  All correct 


#include <stdio.h>
typedef unsigned long long RColor;
struct RColorStruct
{
        unsigned char flags;    //bits: 0 - BLINK, 1 - REVERSE
        unsigned char flags1;
        unsigned char b_blue;
        unsigned char b_green;
        unsigned char b_red;
        unsigned char f_blue;
        unsigned char f_green;
        unsigned char f_red;
};

int test(RColor color1);
#define R_BLINK    0x01LLU
#define R_REVERSE  0x02LLU
#define mycolor    0x18b218ffffff0000LLU

int main (void)
{
    int i = test( mycolor );
    printf("i=%d\n",i);
}

int test(RColor color1)
{
        RColorStruct *c = (RColorStruct *)&color1;
        int fg = 0;
        int bg = 0;
        int bold = 0;

        if(c->f_red == 0 && c->f_green == 0 && c->f_blue == 0) fg = 0;
        if(c->b_red == 0 && c->b_green == 0 && c->b_blue == 0) bg = 0;

        if(c->f_red == 178 && c->f_green == 24 && c->f_blue == 24) fg = 1;
        if(c->b_red == 178 && c->b_green == 24 && c->b_blue == 24) bg = 1;

        if(c->f_red == 24 && c->f_green == 178 && c->f_blue == 24) fg = 2;
        if(c->b_red == 24 && c->b_green == 178 && c->b_blue == 24) bg = 2;

        if(c->f_red == 178 && c->f_green == 104 && c->f_blue == 24) fg = 3;
        if(c->b_red == 178 && c->b_green == 104 && c->b_blue == 24) bg = 3;

        if(c->f_red == 24 && c->f_green == 24 && c->f_blue == 178) fg = 4;
        if(c->b_red == 24 && c->b_green == 24 && c->b_blue == 178) bg = 4;

        if(c->f_red == 178 && c->f_green == 24 && c->f_blue == 178) fg = 5;
        if(c->b_red == 178 && c->b_green == 24 && c->b_blue == 178) bg = 5;

        if(c->f_red == 24 && c->f_green == 178 && c->f_blue == 178) fg = 6;
        if(c->b_red == 24 && c->b_green == 178 && c->b_blue == 178) bg = 6;

        if(c->f_red == 178 && c->f_green == 178 && c->f_blue == 178) fg = 7;
        if(c->b_red == 178 && c->b_green == 178 && c->b_blue == 178) bg = 7;

//---------------------------------------------------------------------

        if(c->f_red == 104 && c->f_green == 104 && c->f_blue == 104) { fg = 0; 
bold = 1;}
        if(c->b_red == 104 && c->b_green == 104 && c->b_blue == 104) bg = 0;

        if(c->f_red == 255 && c->f_green == 84 && c->f_blue == 84) { fg = 1; 
bold = 1;}
        if(c->b_red == 255 && c->b_green == 84 && c->b_blue == 84) bg = 1;

        if(c->f_red == 84 && c->f_green == 255 && c->f_blue == 84) { fg = 2; 
bold = 1;}
        if(c->b_red == 84 && c->b_green == 255 && c->b_blue == 84) bg = 2;

        if(c->f_red == 255 && c->f_green == 255 && c->f_blue == 84) { fg = 3; 
bold = 1;}
        if(c->b_red == 255 && c->b_green == 255 && c->b_blue == 84) bg = 3;

        if(c->f_red == 84 && c->f_green == 84 && c->f_blue == 255) { fg = 4; 
bold = 1;}
        if(c->b_red == 84 && c->b_green == 84 && c->b_blue == 255) bg = 4;

        if(c->f_red == 255 && c->f_green == 84 && c->f_blue == 255) { fg = 5; 
bold = 1;}
        if(c->b_red == 255 && c->b_green == 84 && c->b_blue == 255) bg = 5;

        if(c->f_red == 84 && c->f_green == 255 && c->f_blue == 255) { fg = 6; 
bold = 1;}
        if(c->b_red == 84 && c->b_green == 255 && c->b_blue == 255) bg = 6;

        if(c->f_red == 255 && c->f_green == 255 && c->f_blue == 255) { fg = 7; 
bold = 1;}
        if(c->b_red == 255 && c->b_green == 255 && c->b_blue == 255) bg = 7;

        int tmpc = bg*8+fg;
        if( bold )      tmpc = tmpc | 1;
        if( c->flags & R_BLINK) tmpc = tmpc | 2;
        if( c->flags & R_REVERSE) tmpc = tmpc | 4;
        return tmpc;
}



More information about the Gcc-bugs mailing list