Why must bitfields sum to a multiple of a byte?
monarch_dodra
monarchdodra at gmail.com
Thu Aug 2 02:26:04 PDT 2012
On Thursday, 2 August 2012 at 09:14:15 UTC, Era Scarecrow wrote:
> On Thursday, 2 August 2012 at 09:03:54 UTC, monarch_dodra wrote:
>> I had an (implementation) question for you: Does the
>> implementation actually require knowing what the size of the
>> padding is?
>>
>> eg:
>> struct A
>> {
>> int a;
>> mixin(bitfields!(
>> uint, "x", 2,
>> int, "y", 3,
>> ulong, "", 3 // <- This line right there
>> ));
>> }
>>
>> It that highlighted line really mandatory?
>> I'm fine with having it optional, in case I'd want to have,
>> say, a 59 bit padding, but can't the implementation figure it
>> out on it's own?
>
> The original code has it set that way, why? Perhaps so you are
> aware and actually have in place where all the bits are
> assigned (even if you aren't using them); Be horrible if you
> used accidently 33 bits and it extended to 64 without telling
> you (Wouldn't it?).
>
> However, having it fill the size in and ignore the last x bits
> wouldn't be too hard to do, I've been wondering if I should
> remove it.
Well, I was just trying to figure out the rationale: The most
obvious one for me being "it is much easier on the
implementation".
One of the *big* reasons I'm against having a hand chosen
padding, is that the implementation *should* be able to find out
what the most efficient padding is on the current machine (could
be 32 on some, could be 64 on some)
That said, something that could fix the above "problem" could be:
*Bitfields are automatically padded if the final field is not a
"padding field".
**Padding size is implementation chosen.
*If the final field is a "padding field", then the total size
must be 8/16/32/64.
EG:
//Case 1
bitfields!(
bool, "x", 1,
uint, "", 3, //Interfield padding
bool, "y", 1
)
//Fine, implementation chosen bitfield size
//Case 2
bitfields!(
bool, "x", 1,
uint, "", 3, //Interfield padding
bool, "y", 1
ulong, "", 59, //Pad to 64
)
//Fine, imposed 64 bit
//Case 3
bitfields!(
bool, "x", 1,
uint, "", 3, //Interfield padding
bool, "y", 1
ulong, "", 32, //Pad to 37
)
//ERROR: Padding requests the bitfield to be 37 bits longs
But I'd say that's another development anyways, if we ever decide
to go this way.
More information about the Digitalmars-d-learn
mailing list