public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Kaz Kylheku <kaz@kylheku.com>
To: gcc-help@gcc.gnu.org
Subject: Question about bitfields with aligned attribute.
Date: Tue, 24 May 2022 07:29:35 -0700	[thread overview]
Message-ID: <4d9dc2bd390f336e05b092316986b1e4@kylheku.com> (raw)

Hi all, 

I'm observing two contradictory effects in the treatment this structure: 

struct s16 {
  uint8_t x;
  __attribute__((aligned (2))) uint32_t a : 7;
  __attribute__((aligned (2))) uint32_t b : 7;
}; 

Firstly, the aligned attribute is documented as only being capable of increasing
alignment. Yet, what is observed is that it's being honored: bitfield a is placed
at offset 2, and b at offset 4. 

So there is an exception to the aligned attribute's "strengthen only" rule
for bitfields? Where is this documented? 

Secondly, in spite of the aligned attribute being honored, the structure is given
two bytes of padding, making it 8 bytes. Why is that? Nothing in the structure
has more than 2 byte alignment, which requires one byte to make a size of six.

What are the exact rules here? Does the bitfield member carry two alignment
properties: one coming from the attribute syntax, and one from the uint32_t
type? Such that the former is allowed to be smaller, even in the absence of
packed? And then the allocation of the bitfield takes into account the former,
but both are considered for the purposes of struct alignment?

Thanks.

                 reply	other threads:[~2022-05-24 14:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=4d9dc2bd390f336e05b092316986b1e4@kylheku.com \
    --to=kaz@kylheku.com \
    --cc=gcc-help@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).