是否有可能访问一个__m128变量的内部值作为一个C ++类的属性? [英] Is it possible to access internal values in a __m128 variable as attribute in a C++ class?

查看:184
本文介绍了是否有可能访问一个__m128变量的内部值作为一个C ++类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与上证所内部函数(所以我不会用__m128类型的第四元素)实现了一个Vector类(其中重presents的3彩车向量)。不过,我想能够轻松地访问他们喜欢的属性:所以与myVector.x将在VEC访问0-31位,myVector.y将访问VEC的32-63位,但无需调用一些的getX()方法。该'x'的属性将是一种别名为VEC的0-31位。
这可能吗?

 类Vector {
上市:
  浮X;
  浮ÿ;
  浮动ž;
私人的:
  __m128 VEC;
}


解决方案

没有,因为这违反了强大的别名规则

当然你可以使用强制类型转换或工会pretend的 __ M128 是float数组,而优化器将不会为你保持连贯性,因为你打破了语言的规则。

请参阅什么是严格别名规则?

(根据规则,利用共用的访问是安全的,但只有当你命名的结合。采用指针或引用工会成员,然后使用指针或引用以后直接是不是安全适用。)

I would like to have a Vector class (which represents vector of 3 floats) implemented with SSE intrinsics (so I will not use the 4th elements of the __m128 type). But I would like to be able to access them easily like attributes : so myVector.x will access the 0-31 bits in vec, myVector.y will access the 32-63 bits in vec, but without having to call some getX() method. The 'x' attribute would be a sort of alias for the 0-31 bits of 'vec'. Is it possible ?

class Vector {  
public:  
  float x;  
  float y;  
  float z;  
private:  
  __m128 vec;  
}

解决方案

No, because this violates the strong aliasing rule.

Sure you can use casts or unions to pretend the __m128 is an array of floats, but the optimizer will not maintain coherency for you, because you're breaking the language's rules.

See What is the strict aliasing rule?

(According to the rule, access using a union is safe, but that only applies when you are naming the union. Taking a pointer or reference to a union member and then using the pointer or reference directly later is not safe.)

这篇关于是否有可能访问一个__m128变量的内部值作为一个C ++类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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