david_aiken

data alignment question

Discussion created by david_aiken on Oct 30, 2009
Latest reply on Oct 30, 2009 by MicahVillmow

suppose i have a struct on the host as follows:

 

typedef struct{

float x;

 

float y;

float z;

} FloatThree __attribute__ ((aligned (4)));

 

typedef struct 

{

uint u;

int i;

float f;

FloatThree ft;

char c;

}Params __attribute__ ((aligned (16)));

 

 

is it necessary to add padding/alignment? Section 5.1 in the AMD beta4 release notes says that structs must be packed and aligned. I see section 6.10.1 in the spec which states:

 

Note that the alignment of any given struct or union type is required by the ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct or union in question and must also be a power of two.

For FloatThree each member is 4 bytes, so the LCM is 4 bytes, which is a power of 2.. so an alignment of 4 seems correct.

For Params the first 3 members are 4 bytes each, the struct is 12 bytes (it is aligned to 4) and the char is 1 byte. According to 6.1.5 the char must be aligned to two bytes. So the LCM would seem to be 12 and the alignment is the next highest power of 2 which is 16. But it seems based on the 5.1 release notes example that i need to explicitly add padding:

 

typedef struct 

{

uint u;

int i;

float f;            // 12 bytes up to here

        char pad1[4]; // now 16

FloatThree ft; // another 12 bytes

char c;           // 1 byte

char pad[3];  // now 16

}Params __attribute__ ((aligned (16)));

Does this seem correct? I think if it's wrong it could conceivably cause at least some of issues i'm seeing.



 

Outcomes