Alignment of struct containing SIMD field - GDC

Cecil Ward via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 1 17:25:25 PST 2017


On Wednesday, 1 March 2017 at 22:15:59 UTC, Iain Buclaw wrote:
> On Wednesday, 1 March 2017 at 19:09:24 UTC, Johan Engelen wrote:
>> On Wednesday, 1 March 2017 at 18:34:16 UTC, Iain Buclaw wrote:
>>>
>>> Simple test case would be:
>>>
>>> struct vec_struct {
>>>     bool b2;
>>>     struct {
>>>         bool b;
>>>         int8 field;
>>>     }
>>> }
>>>
>>> static assert(vec_struct.b.offsetof == 32);
>>> static assert(vec_struct.field.offsetof == 64);
>>
>> With explicit align(32), it works:
>> https://godbolt.org/g/3GjOHW
>>
>> - Johan
>
> Well obviously, because it adheres to explicit alignment.  The 
> compiler just has the wrong idea of how default alignment 
> should work.
>
> Raised bug here, and I'm raising a PR now also. 
> https://issues.dlang.org/show_bug.cgi?id=17237

In a dream world where engineers have nothing better to do, it 
would of course be better to effectively delete the inner 
anonymous struct so that the two bools could be packed together 
adjacently. I presume that that is theoretically ok? In contrast, 
if the sub-struct were named, you could presumably take the 
address of the entire sub-struct, so the space-inefficient offset 
to it is something you are just stuck with of course.

A noob question: is it illegal to reorder the fields in a struct? 
(e.g. so as to optimise packing as far as correct alignment 
allows) - (C compatibility? Would perhaps need to be an _option_?)


More information about the Digitalmars-d-learn mailing list