我想知道是否可以为专门设计的情况定制严格的别名要求,同时仍然保留一般的严格别名或分别进行 -O2/-O3 优化。
更准确地说,在需要的情况下,可以使用匿名联合绕过严格的别名(如此处和此处所指出的):
#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);
}