如何将__m128转换成整数

how invert __m128 into ints

本文关键字:整数 转换 m128      更新时间:2023-10-16
float a[4] = {1,2,3,4}, b[4] = {4,3,2,1};
uint32_t c[4];
int main() {
    __m128 pa = _mm_loadu_ps(a);
    __m128 pb = _mm_loadu_ps(b);
    __m128 pc = _mm_cmpgt_ps(pa, pb);
    _mm_storeu_ps((float*)c, pc);
    for (int i = 0;i  < 4; ++i) printf("%un", c[i]);
    return 0;
}

_mm_storeu_ps((float*)c, pc)的正确指令是什么?这里,c是一个整数数组…我认为这种方式不好,有更好的吗?

在SSE2中将__m128 (float向量)转换为__m128i (int32_t向量)有两个指令:_mm_cvtps_epi32(带舍入)和_mm_cvttps_epi32(带截断)。

__m128i vi = _mm_cvttps_epi32(pc);
_mm_storeu_si128((__m128i *)c, vi);

如果不能使用SSE2,将pc存储为float数组后,将float数组转换为int数组。

float d[4];
_mm_storeu_ps(d, pc);
c[0] = (int)d[0]; c[1] = (int)d[1]; c[2] = (int)d[2]; c[3] = (int)d[3];