union alignment

Iain Buclaw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 21 01:38:27 PDT 2016


On Wednesday, 18 May 2016 at 01:46:37 UTC, tsbockman wrote:
> Shouldn't a union type always have an `alignof` at least as 
> great as the `alignof` for its largest member?
>

On x86, there's a difference between the type alignment and the 
field alignment.

The type align of ulong and double are 8 bytes on both x86 and 
x86_64.
The field align is worked out differently, something to the 
effect of:

uint fieldalignof(T)()
{
   version (X86_64)
     return T.alignof;
   else version (X86)
   {
     static if (isIntegral!T || is(T == double) || is(T == 
cdouble))
       return min(4, T.alignof);
     else
       return T.alignof;
   }
}

static assert(ulong.alignof == 8);
static assert(double.alignof == 8);
static assert(fieldalignof!ulong == 4);
static assert(fieldalignof!double == 4);


> DMD 32:
> sizeof:  (8 >= 8) == true
> alignof: (4 >= 4) == true
>
> GDC 32:
> sizeof:  (8 >= 8) == true
> alignof: (4 >= 8) == false  <<===== Bug?
>
> LDC 32:
> sizeof:  (8 >= 8) == true
> alignof: (4 >= 4) == true

GDC is in the right, it's all other compilers that are wrong.  :-)

Iain.


More information about the Digitalmars-d-learn mailing list