通过索引获得__m128的成员? [英] Get member of __m128 by index?

查看:196
本文介绍了通过索引获得__m128的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,最初是由MSVC工作的人给我的,我想让它在Clang上工作。这是我遇到麻烦的函数:

I've got some code, originally given to me by someone working with MSVC, and I'm trying to get it to work on Clang. Here's the function that I'm having trouble with:

float vectorGetByIndex( __m128 V, unsigned int i )
{
    assert( i <= 3 );
    return V.m128_f32[i];
}

我得到的错误如下:

Member reference has base type '__m128' is not a structure or union.

我已经环顾四周,发现Clang(也许GCC)有一个问题,治疗__m128为结构或联合。然而,我没有找到一个直接的答案,如何我可以得到这些值。我试过使用下标运算符,不能这样做,我已经浏览了大量的SSE内在函数列表,但还没有找到一个合适的。

I've looked around and found that Clang (and maybe GCC) has a problem with treating __m128 as a struct or union. However I haven't managed to find a straight answer as to how I can get these values back. I've tried using the subscript operator and couldn't do that, and I've glanced around the huge list of SSE intrinsics functions and haven't yet found an appropriate one.

推荐答案

联合可能是最便携的方式:

A union is probably the most portable way to do this:

union {
    __m128 v;    // SSE 4 x float vector
    float a[4];  // scalar array of 4 floats
} U;

float vectorGetByIndex(__m128 V, unsigned int i)
{
    U u;

    assert(i <= 3);
    u.v = V;
    return u.a[i];
}

这篇关于通过索引获得__m128的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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