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

Re: Long double alignment test



Neil Turton <neilt+rsp@chiark.greenend.org.uk> writes:

> Brett Cundal writes:
>  > The issue is with a test for long double alignment which reports an
>  > incorrect value on ia64 (and probably other archs).
>  > [...]
>  > Anyone know if this works?
>
> The code which was posted doesn't use the long double type, which
> makes the idea of it giving the alignment of that type somewhat
> doubtful.  A processor could have special alignment issues to do with
> long doubles which don't show up in long, double or void* (due to a
> weird floating point unit).
>
> I believe that the "if" condition is never false because "f" contains
> two unions and two chars, so its size is greater than that of two
> unions (by at least two chars) and therefore not equal.  As a result,
> it seems to give the alignment of the union, which is the maximum
> alignment of the three types inside.

If the alignment of the union is bigger than the union itself the char
can be merged into the alignment of the union and not take any space
at all. In that case the size of the union would be the alignment.

> I prefer the code below.  Enjoy!
>
> Cheers, Neil.
>
> #include <stdio.h>
>
> #define ALIGN_OF(t) \
>   ((int)(sizeof(  \
>      struct {  \
>        char c;  \
>        t x;  \
>      })  \
>    - sizeof(t) ))
>
> int main()
> {
>   printf("char: %d\n", ALIGN_OF(char));
>   printf("short: %d\n", ALIGN_OF(short));
>   printf("int: %d\n", ALIGN_OF(int));
>   printf("long: %d\n", ALIGN_OF(long));
>   printf("long long: %d\n", ALIGN_OF(long long));
>   printf("void*: %d\n", ALIGN_OF(void*));
>   printf("float: %d\n", ALIGN_OF(float));
>   printf("double: %d\n", ALIGN_OF(double));
>   printf("long double: %d\n", ALIGN_OF(long double));
>   return 0;
> }

Nice.

MfG
        Goswin



Reply to: