Bug 38027 - bitfields and -O2 or greater
Summary: bitfields and -O2 or greater
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.2
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-05 19:21 UTC by john spelis
Modified: 2008-11-05 19:36 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux
Target: i686-pc-linux
Build: i686-pc-linux
Known to work: 4.2.4 4.3.2
Known to fail: 4.3.0 4.3.1
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description john spelis 2008-11-05 19:21:28 UTC
The following program when compiled with gcc 4.1.2 works but with 4.3.0 it 
produces incorrect output at optimisation level O2 or greater.

 / * ------ */

#include <iostream>
#include <stdio.h>
using namespace std;

typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

class CColourARGB4444;

class CColourARGB8888
{
public :
    union
    {
        struct
        {
            uint32_t mBlue      : 8;
            uint32_t mGreen     : 8;
            uint32_t mRed       : 8;
            uint32_t mAlpha     : 8;
        } mBits;
        uint32_t mWord;
    } mCol;
    CColourARGB8888(const CColourARGB4444 &rhs);
};

class CColourARGB4444
{
public :
    union
    {
        struct
        {
            uint16_t mBlue      : 4;
            uint16_t mGreen     : 4;
            uint16_t mRed       : 4;
            uint16_t mAlpha     : 4;
        } mBits;

        uint16_t mWord;
    } mCol;

public :
    /// CColourARGB4444 constructor
    CColourARGB4444(const uint8_t &alpha, const uint8_t &red, const uint8_t &green, const uint8_t &blue)
        { mCol.mBits.mAlpha = alpha; mCol.mBits.mRed = red; mCol.mBits.mGreen = green; mCol.mBits.mBlue = blue; }

};

__inline CColourARGB8888::CColourARGB8888(const CColourARGB4444 &rhs)
{
    mCol.mBits.mRed   = ((rhs.mCol.mBits.mRed << 4) & 0xF0) | rhs.mCol.mBits.mRed;
    mCol.mBits.mGreen = ((rhs.mCol.mBits.mGreen << 4) & 0xF0) | rhs.mCol.mBits.mGreen;
    mCol.mBits.mBlue  = ((rhs.mCol.mBits.mBlue << 4) & 0xF0) | rhs.mCol.mBits.mBlue;
    mCol.mBits.mAlpha = ((rhs.mCol.mBits.mAlpha << 4) & 0xF0) | rhs.mCol.mBits.mAlpha;
    printf("Constructing CColourARGB8888 %x from CColourARGB4444 %x\n", mCol.mWord, rhs.mCol.mWord);
}

void broken()
{
    for (uint8_t y = 0; y < 4; ++y) {
        for (uint8_t x = 0; x < 4; ++x) {
            CColourARGB4444 c4(0xF, x, y, 0);
            CColourARGB8888 c8(c4);
        }
    }
}

CColourARGB4444 myfoo(uint8_t XT, uint8_t YT) {
    return CColourARGB4444(0xF, XT, YT, 0);
}

void ok()
{
    for (uint8_t y = 0; y < 4; ++y) {
        for (uint8_t x = 0; x < 4; ++x) {
            CColourARGB4444 c4 = myfoo(x, y);
            CColourARGB8888 c8(c4);
        }
    }
}

int main()
{
        printf("this\n");
        ok();
        printf("that\n");
        broken();
}

    /* --- end --- */

When not working the bit fields have not been updated, e.g. a broken case
output is;

Constructing CColourARGB8888 ff000000 from CColourARGB4444 f330

When working the output has updated bitfields, e.g.

Constructing CColourARGB8888 ff333300 from CColourARGB4444 f330
Comment 1 Richard Biener 2008-11-05 19:36:39 UTC
Works with 4.3.2.