This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


No this is because you are abusing casts, aka the ISO C++ aliasing rules are being violated.
try this instead, it should work any on any gcc on a little endian (x86 included).


#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;
};

union colorUnion
{
        RColor color;
        RColorStruct colorS;
};

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)
{
        colorUnion c1;
        c1.color = color1;
        RColorStruct *c = &c1.colorS;
        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;
}


Thanks, Andrew Pinski


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]