ben*_*wad 25 c++ sse simd clang intrinsics
我有一些代码,最初是由MSVC工作人员给我的,我正试图让它在Clang上工作.这是我遇到麻烦的功能:
float vectorGetByIndex( __m128 V, unsigned int i )
{
assert( i <= 3 );
return V.m128_f32[i];
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
Member reference has base type '__m128' is not a structure or union.
Run Code Online (Sandbox Code Playgroud)
我环顾四周,发现Clang(也许是GCC)在将__m128视为结构或联合时遇到了问题.但是我还没有找到一个直接的答案,我怎么能得到这些价值.我已经尝试过使用下标运算符而无法做到这一点,我已经浏览了大量的SSE内在函数列表并且尚未找到合适的函数.
Gun*_*iez 18
即使SSE4.1可用并且i是编译时常量,也不能以pextract这种方式使用等:
template<unsigned i>
float vectorGetByIndex( __m128 V) {
union {
__m128 v;
float a[4];
} converter;
converter.v = V;
return converter.a[i];
}
Run Code Online (Sandbox Code Playgroud)
我不删除它,因为它是一个有用的提醒,如何不做事情,让它作为公众羞辱.
更好用
// broken code starts here
template<unsigned i>
float vectorGetByIndex( __m128 V) {
return _mm_extract_epi32(V, i);
}
// broken code ends here
Run Code Online (Sandbox Code Playgroud)
无论可用的指令集如何,它都可以工作.
Pau*_*l R 17
联盟可能是最便携的方式:
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];
}
Run Code Online (Sandbox Code Playgroud)
Jas*_*n R 16
作为对hirschhornsalz解决方案的修改,如果i是编译时常量,则可以通过使用shuffle/store完全避免union路径:
template<unsigned i>
float vectorGetByIndex( __m128 V)
{
#ifdef __SSE4_1__
return _mm_extract_epi32(V, i);
#else
float ret;
// shuffle V so that the element that you want is moved to the least-
// significant element of the vector (V[0])
V = _mm_shuffle_ps(V, V, _MM_SHUFFLE(i, i, i, i));
// return the value in V[0]
return _mm_cvtss_f32(V);
#endif
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14035 次 |
| 最近记录: |