UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

AR# 37753

EDK - How can I align a 64-bit struct member to have the smallest possible alignment?

Description


I have the following struct with a mixed of 32-bit word and 64-bit word:
struct my_Struct
{
int a;
long long b;
};
According to the PPC405 datasheet, the alignment might introduce padding of four bytes between "a" and "b".
How can I force "b" to be aligned to a 32-bit boundary and eliminate the padding?

Solution


The attribute "packed "can be used to specify that a variable or structure field should have the smallest possible alignment, unless you specify a larger value with the aligned attribute.
For an example of the above struct, you can use the "packed" attribute as follows:
struct my_Struct
{
int a;
long long b __attribute__ ((packed));
};
or
struct __attribute__ ((packed)) my_Struct
{
int a;
long long b ;
};
Alternatively, compiler option -fpack-struct can be used to pack all structure members together without holes.
Additional information:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html#PowerPC%20Type%20Attributes
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Code-Gen-Options.html#Code-Gen-Options
AR# 37753
Date Created 08/30/2010
Last Updated 05/19/2012
Status Archive
Type Design Advisory
Tools
  • EDK - 10.1 sp2
  • EDK - 10.1
  • EDK - 10.1 sp1
  • More
  • EDK - 10.1 sp3
  • EDK - 11.1
  • EDK - 11.2
  • EDK - 11.3
  • EDK - 11.4
  • EDK - 11.5
  • EDK - 12.1
  • EDK - 12.2
  • EDK - 12.3
  • Less