SSE 数据类型和原语
在网络上的大多数教程或代码片段中,人们会看到以下内容:
float *arr= (float*) _aligned_malloc(length * sizeof(float), 16);
__m128 *m1 = (__m128*)arr;
这是否违反了严格的别名规则?我认为确实如此,但肯定所有这些教程作者都不会为了方便而忽略它,因为 __m128 是一个包含 float[4] 的联合,也许我误解了它的一些复杂部分。
In most tutorials or code snippets on the net one sees the following:
float *arr= (float*) _aligned_malloc(length * sizeof(float), 16);
__m128 *m1 = (__m128*)arr;
Does this violate strict aliasing rules or not? I'd think it does, but then surely all those tutorial writers don't ignore it just for convenience and since __m128 is a union containing float[4] maybe I misunderstand some intricate parts about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但这还没有违反它。然而,通过一个指针写入并通过另一个指针读取将违反严格的别名。
相反,您应该使用如下函数:
_mm_load_ps
_mm_store_ps
That hasn't violated it -- yet. However, writing through one pointer and reading through the other would violate strict aliasing.
Instead, you should use functions like:
_mm_load_ps
_mm_store_ps
这是 GCC 的编译器特定答案
GCC 的 xmmintrin 标头4.4.3 定义了以下内容:
typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
所以,是的,您违反了严格别名,但您可以这样做那种类型。奇怪的是,
__v4sf
类型没有标记为__may_alias__
,因此不能以这种方式使用。This is a compiler specific answer for GCC
The xmmintrin header for GCC 4.4.3 defines the following:
typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
So, yes, you violate strict aliasing, but you are allowed to do so with that type. Oddly, the
__v4sf
type is not marked as__may_alias__
, so it cannot be used in this manner.