public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/38027]  New: bitfields and -O2 or greater
@ 2008-11-05 19:22 john dot spelis at 3dlabs dot com
  2008-11-05 19:38 ` [Bug c++/38027] " rguenth at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: john dot spelis at 3dlabs dot com @ 2008-11-05 19:22 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/38027] bitfields and -O2 or greater
  2008-11-05 19:22 [Bug c++/38027] New: bitfields and -O2 or greater john dot spelis at 3dlabs dot com
@ 2008-11-05 19:38 ` rguenth at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-11-05 19:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-11-05 19:36 -------
Works with 4.3.2.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
      Known to fail|                            |4.3.0 4.3.1
      Known to work|                            |4.2.4 4.3.2
         Resolution|                            |FIXED
   Target Milestone|---                         |4.3.2


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-11-05 19:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-05 19:22 [Bug c++/38027] New: bitfields and -O2 or greater john dot spelis at 3dlabs dot com
2008-11-05 19:38 ` [Bug c++/38027] " rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).