public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "john dot spelis at 3dlabs dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/38027]  New: bitfields and -O2 or greater
Date: Wed, 05 Nov 2008 19:22:00 -0000	[thread overview]
Message-ID: <bug-38027-8983@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2008-11-05 19:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-05 19:22 john dot spelis at 3dlabs dot com [this message]
2008-11-05 19:38 ` [Bug c++/38027] " rguenth at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-38027-8983@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).