如何使用SSE将_m128i转换为unsigned int?

bit*_*ise 4 c++ sse image-processing simd

我已经制作了一个用于分色图像的功能.

// =(
#define ARGB_COLOR(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))

inline UINT PosterizeColor(const UINT &color, const float &nColors)
{
    __m128 clr = _mm_cvtepi32_ps(  _mm_cvtepu8_epi32((__m128i&)color)  );

    clr = _mm_mul_ps(clr,  _mm_set_ps1(nColors / 255.0f)  );
    clr = _mm_round_ps(clr, _MM_FROUND_TO_NEAREST_INT);
    clr = _mm_mul_ps(clr, _mm_set_ps1(255.0f / nColors)  );

    __m128i iClr = _mm_cvttps_epi32(clr);

    return ARGB_COLOR(iClr.m128i_u8[12],
                      iClr.m128i_u8[8],
                      iClr.m128i_u8[4],
                      iClr.m128i_u8[0]);
}
Run Code Online (Sandbox Code Playgroud)

在第一行,我将颜色打包成4个浮点数,但我找不到正确的反向方法.

我搜索了SSE文档,找不到相反的结果 _mm_cvtepu8_epi32

一个存在吗?

Mar*_*han 8

您需要的组合_mm_shuffle_epi8_mm_cvtsi128_si32:

static const __m128i shuffleMask = _mm_setr_epi8(0,  4,  8, 12, -1, -1, -1, -1,
                                               -1, -1, -1, -1, -1, -1, -1, -1);
UINT color = _mm_cvtsi128_si32(_mm_shuffle_epi8(iClr, shuffleMask));
Run Code Online (Sandbox Code Playgroud)


Mys*_*ial 5

不幸的是,即使在AVX中也没有指令可以做到(没有我知道的).所以你必须像现在一样手动完成.

但是,您当前的方法非常不理想,并且您依赖于.m128i_u8哪个是MSVC扩展.根据我对MSVC的经验,它将使用对齐的缓冲区来访问各个元素.由于部分词语访问,这会受到非常严重的惩罚.

而不是.m128i_u8,使用_mm_extract_epi32().这是在SSE4.1中.但是你已经依赖SSE4.1了_mm_cvtepu8_epi32().

由于您使用的是1字节粒度,因此这种情况特别糟糕.如果您使用的是2字节(16位整数)粒度,则可以使用shuffle内在函数进行有效的解决方案.