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]

[Bug c++/38027] New: bitfields and -O2 or greater


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


-- 
           Summary: bitfields and -O2 or greater
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: john dot spelis at 3dlabs dot com
 GCC build triplet: i686-pc-linux
  GCC host triplet: i686-pc-linux
GCC target triplet: i686-pc-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38027


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