excray
excray

Reputation: 2858

Align a union to 16 byte boundary in x86.

I got this union, which I am trying to align to 16 byte boundary, using gcc 4.8.

typedef union {
 unsigned long long int ud[(1<<6)/8];
 long long int d[(1<<6)/8];
 unsigned int uw[(1<<6)/4];
 int w[(1<<6)/4];
 short uh[(1<<6)/2];
 unsigned short int h[(1<<6)/2];
 unsigned char ub[(1<<6)/1];
 char b[(1<<6)/1];
} vector_t;

I tried

vector_t __attribute__ ((aligned(16))) t; 

But it is not working. Address of variable t in stack is not aligned to 16 bytes.

I was able to align it using __declspec align(16) in VS 10. Please let me know what is the way to do this in gcc.

Upvotes: 1

Views: 639

Answers (1)

Jens Munk
Jens Munk

Reputation: 4725

The keyword __attribute__ allows you to specify special attributes of struct and union types when you define such types. You need to do

typedef union __attribute__ ((aligned(16))) {
  unsigned long long int ud[(1<<6)/8];
  long long int d[(1<<6)/8];
  unsigned int uw[(1<<6)/4];
  int w[(1<<6)/4];
  short uh[(1<<6)/2];
  unsigned short int h[(1<<6)/2];
  unsigned char ub[(1<<6)/1];
  char b[(1<<6)/1];
} vector_t;

Upvotes: 2

Related Questions