qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] linux-user: Use correct alignment for long long


From: Peter Maydell
Subject: Re: [Qemu-devel] [PATCH] linux-user: Use correct alignment for long long on i386 guests
Date: Thu, 28 Jul 2016 21:38:06 +0100

On 28 July 2016 at 20:19, Laurent Vivier <address@hidden> wrote:
> Why the following program from commit
>
>     c2e3dee linux-user: Define target alignment size
>
> int main(void)
> {
>     printf("alignof(short) %ld\n", __alignof__(short));
>     printf("alignof(int) %ld\n", __alignof__(int));
>     printf("alignof(long) %ld\n", __alignof__(long));
>     printf("alignof(long long) %ld\n", __alignof__(long long));
> }
>
>
> gives me:
>
> alignof(short) 2
> alignof(int) 4
> alignof(long) 4
> alignof(long long) 8
>
> ?

Because gcc __alignof__ gives you the maximum preferred
alignment, not the minimum alignment. If you want to know
what the alignment used for setting struct alignment is,
use C99 alignof from stdalign.h and/or cross-check by
using sizeof() on a struct with the type in it:

orth$ uname -a
Linux orth 3.16.0-4-686-pae #1 SMP Debian 3.16.7-ckt20-1+deb8u3
(2016-01-17) i686 GNU/Linux
orth$ cat /tmp/zz9.c
#include <stdio.h>
#include <stdalign.h>

struct epoll_event {
  int events;
  unsigned long long data;
};

#define PRINTALIGN(T) \
    printf(#T ": sizeof %zd __alignof__ %zd _Alignof %zd alignof %zd\n", \
    sizeof(T), __alignof__(T), _Alignof(T), alignof(T))

int main(void) {
    PRINTALIGN(unsigned long long);
    PRINTALIGN(struct epoll_event);
    return 0;
}
orth$ gcc -g -Wall -o /tmp/zz9 /tmp/zz9.c
orth$ /tmp/zz9
unsigned long long: sizeof 8 __alignof__ 8 _Alignof 4 alignof 4
struct epoll_event: sizeof 12 __alignof__ 4 _Alignof 4 alignof 4

Also watch out for broken clang versions:

orth$ clang -g -Wall -o /tmp/zz9clang /tmp/zz9.c
orth$ /tmp/zz9clang
unsigned long long: sizeof 8 __alignof__ 8 _Alignof 8 alignof 8
struct epoll_event: sizeof 12 __alignof__ 4 _Alignof 4 alignof 4
orth$ clang --version
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: i386-pc-linux-gnu
Thread model: posix

(that's fixed in later clang versions)

thanks
-- PMM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]