【问题标题】:Align a union to 16 byte boundary in x86.在 x86 中将联合与 16 字节边界对齐。
【发布时间】:2014-07-24 02:27:47
【问题描述】:

我得到了这个联合,我正在尝试使用 gcc 4.8 对齐到 16 字节边界。

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;

我试过了

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

但它不起作用。堆栈中变量 t 的地址未与 16 字节对齐。

我能够在 VS 10 中使用 __declspec align(16) 对其进行对齐。请让我知道在 gcc 中执行此操作的方法是什么。

【问题讨论】:

  • 你的意思是 16 位边界?我认为不可能对 unsigned long long 或任何其他大于 4 字节的数据类型进行半字对齐。不过,我可能错了。

标签: c gcc


【解决方案1】:

关键字__attribute__ 允许您在定义此类类型时指定structunion 类型的特殊属性。你需要做的

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;

【讨论】:

  • 没错,你需要定义的关键字:-)
  • 换句话说,属性是类型的一部分——而不是它的特定实例的一部分
猜你喜欢
  • 2020-03-26
  • 2012-04-30
  • 2017-12-21
  • 2016-12-19
  • 1970-01-01
  • 2023-03-25
  • 2019-04-10
  • 2013-03-05
  • 2017-07-28
相关资源
最近更新 更多