C ++:通过union绕过严格别名,然后使用__restrict扩展名

时间:2013-09-06 21:46:08

标签: c++ memory casting strict-aliasing restrict-qualifier

我想知道是否有可能针对特定设计的案例定制严格的别名要求,同时仍然保留一般的严格别名或-O2 / -O3优化。

更准确地说,在需要的情况下,可以使用匿名联合绕过严格的别名(如指出herehere):

#define PTR_CAST(type, x) &(((union {typeof(*x) src; type dst;}*) &(x))->dst)

现在我想知道在这样的转换获得的指针上使用__restrict是否会重新启用编译器中的无别名优化(或者如果这样的指针及其所有副本都被认为是所有的别名次)。像这样:

void bar(float* __restrict a, float* __restrict b) {
   // Do something with a and b assuming they don't overlap.
}

void baz(float* c) { /* Do something with c... */ }

void foo() {
   int32_t* buffer = new int32_t[1000];
   // Do something with buffer...

   float* bufCast1 = PTR_CAST(float, buffer);
   float* bufCast2 = PTR_CAST(float, buffer + 500);

   // Can the arguments be successfully __restrict-ed in this case?
   bar(bufCast1, bufCast2);

   // Also, would bufCast1 be treated as potentially aliasing inside of baz()?
   baz(bufCast1);
}

0 个答案:

没有答案