C - 如何使用GCC SSE向量扩展访问向量的元素 [英] C - How to access elements of vector using GCC SSE vector extension

查看:198
本文介绍了C - 如何使用GCC SSE向量扩展访问向量的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我使用以下类型的3D矢量:

  typedef vec3_t float [3]; 

使用smth初始化向量。例如:

  vec3_t x_basis = {1.0,0.0,0.0}; 
vec3_t y_basis = {0.0,1.0,0.0};
vec3_t z_basis = {0.0,0.0,1.0};

并使用smth访问它们。如:

  x_basis [X] * y_basis [X] + ... 

现在我需要使用SSE指令的矢量运算。我有以下代码:

  typedef float v4sf __attribute__((mode(V4SF)))
int main(void)
{
v4sf a,b,c;
a =(v4sf){0.1f,0.2f,0.3f,0.4f};
b =(v4sf){0.1f,0.2f,0.3f,0.4f};
c =(v4sf){0.1f,0.2f,0.3f,0.4f};
a = b + c;
printf(a =%f \\\
,a);
返回0;
}

GCC支持这种方式。但是...
首先,它给了我0.00000的结果。其次,我无法访问这些向量的元素。
我的问题是:如何访问这些向量的元素?我需要帮助。就像[0]访问X元素,[1]访问Y元素等。



PS:我使用以下代码编译此代码:

  gcc -msse testgcc.c -o testgcc 


<解决方案访问元素的安全和推荐方法是使用联合而不是指针类型双击,这会愚弄编译器的别名检测机制,并可能导致不稳定代码。


  union Vec4 {
v4sf v;
float e [4];
};

Vec4 vec;
vec.v =(v4sf){0.1f,0.2f,0.3f,0.4f};
printf(%f%f%f%f\\\
,vec.e [0],vec.e [1],vec.e [2],vec.e [3]);


Usually I work with 3D vectors using following types:

typedef vec3_t float[3];

initializing vectors using smth. like:

vec3_t x_basis = {1.0, 0.0, 0.0};
vec3_t y_basis = {0.0, 1.0, 0.0};
vec3_t z_basis = {0.0, 0.0, 1.0};

and accessing them using smth. like:

x_basis[X] * y_basis[X] + ...

Now I need a vector arithmetics using SSE instructions. I have following code:

typedef float v4sf __attribute__ ((mode(V4SF)))
int main(void)
{
    v4sf   a,b,c;
    a = (v4sf){0.1f,0.2f,0.3f,0.4f};
    b = (v4sf){0.1f,0.2f,0.3f,0.4f};
    c = (v4sf){0.1f,0.2f,0.3f,0.4f};
    a = b + c;
    printf("a=%f \n", a);
    return 0;
}

GCC supports such way. But... First, it gives me 0.00000 as result. Second, I cannot access the elements of such vectors. My question is: how can I access elements of such vectors? I need smth. like a[0] to access X element, a[1] to access Y element, etc.

PS: I compile this code using:

gcc -msse testgcc.c -o testgcc

解决方案

The safe and recommended way to access the elements is with a union, instead of pointer type punning, which fools the aliasing detection mechanisms of the compiler and may lead to unstable code.

union Vec4 {
    v4sf v;
    float e[4];
};

Vec4 vec;
vec.v = (v4sf){0.1f,0.2f,0.3f,0.4f};
printf("%f %f %f %f\n", vec.e[0], vec.e[1], vec.e[2], vec.e[3]);

这篇关于C - 如何使用GCC SSE向量扩展访问向量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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