通过union legal访问__m128变量的字节? [英] Is accessing bytes of a __m128 variable via union legal?

查看:337
本文介绍了通过union legal访问__m128变量的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个变量声明:

union {
        struct {
            float x, y, z, padding;
        } components;
        __m128 sse;
    } _data;

我的想法是通过 x y z 字段,执行SSE2计算并通过 code>, y z 。我有点怀疑,是否是合法的,但。我的关注是对齐:MSDN说, __ m128 变量自动对齐到16字节边界,我想知道我的联合是否可以打破这种行为。
这里是否还有其他缺陷要考虑?

My idea is to assign the value through x, y, z fields, perform SSE2 computations and read the result through x, y, z. I have slight doubts as to whether it is legal, though. My concern is alignment: MSDN says that __m128 variables are automatically aligned to 16 byte boundary, and I wonder if my union can break this behavior. Are there any other pitfalls to consider here?

推荐答案

联合的对齐方式应该很好,但是在Windows您可以直接访问32位组件。从 xmmintrin.h DirectXMath ):

The union's alignment should be fine, but in the case of Windows you may be able to access the 32 bit components directly. From xmmintrin.h (DirectXMath):

typedef union __declspec(intrin_type) _CRT_ALIGN(16) __m128 {
     float               m128_f32[4];
     unsigned __int64    m128_u64[2];
     __int8              m128_i8[16];
     __int16             m128_i16[8];
     __int32             m128_i32[4];
     __int64             m128_i64[2];
     unsigned __int8     m128_u8[16];
     unsigned __int16    m128_u16[8];
     unsigned __int32    m128_u32[4];
 } __m128;

正如你所看到的,有4个浮点。如果你想成为偏执狂,你可以定义所有相同的对齐特性,这样确保没有什么会打破。然而,据我所知,考虑到你在回答中提到MSDN,你应该都很好去。如果你知道你有SSE兼容的东西,联合和访问它直接应该工作。你可以扯开DirectXMath头文件,以了解Windows如何定义和自己播放:它们定义了一些宏,这取决于编译时存在的内核和能力。

As you can see, there's 4 floats in there. If you want to be uber paranoid, you can probably define all the same alignment specialities and such to make sure nothing will break. As far as I can see, however, and given that you mentioned MSDN in your answer, you should be all good to go. Both the union and accessing it directly should work if you know you have SSE compatible stuff. You can poke around the DirectXMath headers as well to get a feel for how Windows does the definitions and wrangling itself: they define a few macros as well depending on which instrinsics and capabilities are present at compile-time.

编辑:正如R.MartinhoFernandes在评论中说的,直接访问它可能比在联合中重新定义它更容易头痛。

As the R.MartinhoFernandes says in the comments, accessing it directly is probably way less of a headache than redefining it in a union.

这篇关于通过union legal访问__m128变量的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆