[Bug 226] Union alignof is less than the alignof for each of its fields

via D.gnu d.gnu at puremagic.com
Sat May 21 01:12:58 PDT 2016


http://bugzilla.gdcproject.org/show_bug.cgi?id=226

Iain Buclaw <ibuclaw at gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|NEW                         |RESOLVED

--- Comment #1 from Iain Buclaw <ibuclaw at gdcproject.org> ---
bug226.c
---
#include <stdio.h>
#define MAX(a,b) (((a)>(b))?(a):(b))

typedef unsigned long long ulong;
typedef union Foo
{
     double a;
     ulong b;
} Foo;

int main()
{
    printf("%d >= %d\n%d >= %d\n",
           sizeof(Foo), MAX(sizeof(double), sizeof(ulong)),
           __alignof__(Foo), MAX(__alignof__(double), __alignof__(ulong)));
}

---

bug226.cc
---
#include <iostream>
#include <algorithm>

union Foo
{
     double a;
     ulong b;
};

int main()
{
    std::cout << sizeof(Foo) << " >= "
        << std::max(sizeof(double), sizeof(ulong)) << "\n";
    std::cout << alignof(Foo) << " >= "
        << std::max(alignof(double), alignof(ulong)) << "\n";
}
---

bug226.d
---
import std.stdio;
import std.algorithm;

union Foo
{
     double a;
     ulong b;
}

void main()
{
    writeln(Foo.sizeof, " >= ", max(double.sizeof, ulong.sizeof));
    writeln(Foo.alignof, " >= ", max(double.alignof, ulong.alignof));
}
---

$ gcc -m32 bug226.c -o gccm32
$ g++ -m32 bug226.cc -o gxxm32
$ gdc -m32 bug226.d -o gdcm32
$ ./gccm32 
8 >= 8
4 >= 8
$ ./gxxm32 
8 >= 8
4 >= 8
$ ./gdcm32 
8 >= 8
4 >= 8
---

GDC may be different from other D compilers, but it stays true to ABI of C and
C++ here, so what do you expect me to do?  Everyone else is in the wrong here.

The alignment of double and ulong on x86 may be 8 bytes, but the field
alignment is worked out differently.

---
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;
}
---

The above is roughly what LDC and DMD should be doing.

-- 
You are receiving this mail because:
You are watching all bug changes.


More information about the D.gnu mailing list