Re: Need to change the default alignment of a data type

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

 



On Tue, Jan 13, 2009 at 11:07:45AM -0500, Rossi, Peter J wrote:
> Is there a way to globally change the default alignment for a data type, in
> particular type double? 
> 
> I am working on a project porting C code from AIX (xlC 7.0) to Linux (gcc
> 4.1.1) where the code will continue run on both platforms so there needs to
> be consistency with the size and alignment of various structs that get passed
> around.  For example, 
> 
>    typedef  struct_msg
>    {
>       int        header;
>       double     value;
>    } MY_MESSAGE;
> 
> On AIX, the xlC compiler aligns the double in this struct to a 4 byte
> boundary however, Linux gcc aligns it on an 8 byte boundary.  The result is
> on AIX the struct has size of 12 with the double at offset +4 while on Linux
> the struct has size of 16 with the double at offset +8.  When the struct is
> passed as part of a message between the two platforms, the double is not
> accessed properly on the other platform. 
> 
> Yes, I know I can use the   __attribute__ ((aligned(4)))   with the double
> but this would have to be done EVERYWHERE that a struct like this is declared
> which is not really practical for us to do.  
> 
> What we are looking for is a way for the compiler to globally know that all
> type double should be aligned on 4 byte boundary (the same as how AIX xlC is
> doing it).  The ideal solution is some option or other directive that I have
> not been able to locate yet, or worse case, could our 'gcc' be recompiled to
> use a different alignment for type double?
> 
> Any information would be appreciated.
> 
> --Pete Rossi
> Peter.J.rossi@xxxxxxxx
> 

As other people have said, a change to the alignment of fundamental types will
violate the ABI.  For example you would not be able to use any structures
defined by other parts of the system, such as the operating system or standard
libraries that contain double.

In general, you should never pass raw binary structures around between
different systems, because:
1) Int/long/float/double etc. might be different sizes;
2) Each system might have different alignment rules;
3) Not every system uses IEEE floating point for its floating point;
4) In the past there were systems that used different integer encodings;
5) One system might use little endian and the other big endian encoding;
6) There are systems that don't use ISO 646 as the base character set.

Note that there are various Linux systems out there, and in fact Linux on
PowerPC or x86_64 does have doubles aligned to to 64-bit.

In general you should define a blessed format for network transmission and use
encoders/decoders to convert between the local format and the network format.
If the encoding/decoding is not in the critical loop, consider using text
strings to go between the two.

However, for 32-bit linux on x86 boxes, there is the -malign-double option
which does make doubles double word aligned.  As I said, if there are any
structures used with the standard library that include doubles, you will not be
able to use those interfaces.  I would strongly recommend you not use this
option, but instead convert your code to be portable.

-- 
Michael Meissner, IBM
4 Technology Place Drive, MS 2203A, Westford, MA, 01886, USA
meissner@xxxxxxxxxxxxxxxxxx

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux