严格别名和 __m128i 类型
当使用 SSE2 内部函数执行按位运算时,必须将指针从 int*
转换为 __m128i*
。这段代码是否违反了严格的别名规则?
void bit_twiddling_func(int size, int const* input, int* output) {
const __m128* x = (const __m128*)input;
const __m128* y = (const __m128*)output;
for (int i=0; i < size/4; ++i, ++x, ++y) {
__m128i x4 = _mm_load_si128(x); // load 4 integers
// do some bit twiddling
_mm_store_si128(y, x4); // store 4 integers
}
}
谢谢你!
When using SSE2 intrinsic functions to do bit-wise operations, one has to cast pointers from int*
to __m128i*
. Does this code break strict aliasing rule?
void bit_twiddling_func(int size, int const* input, int* output) {
const __m128* x = (const __m128*)input;
const __m128* y = (const __m128*)output;
for (int i=0; i < size/4; ++i, ++x, ++y) {
__m128i x4 = _mm_load_si128(x); // load 4 integers
// do some bit twiddling
_mm_store_si128(y, x4); // store 4 integers
}
}
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的;它违反了严格的别名规则。两种不同的类型不能指向内存中的同一位置。这里 x 指向输入,y 指向输出,但它们的类型不同。
您可以更改函数的签名以采用 __m128* 参数,但保留它可能是最简单的。如果您小心地将输入/输出参数指向具有正确对齐和大小限制的内存(即它们应该各自指向循环不会从末尾索引或加载未初始化数据的内容),它可能会工作得很好.)
Yes; it breaks strict aliasing rules. Two different types can't point to the same location in memory. Here you have x pointing to input and y pointing to output, but they're of differing types.
You could change the signature of your function to take __m128* parameters, but it's probably easiest to leave it be. It'll likely work just fine if you're careful that the input/output arguments point to memory with the proper alignment and size constraints (i.e. they should each point to something where your loop doesn't index off the end or load uninitialized data.)