Extract 4 SSE integers to 4 chars

巧了我就是萌 提交于 2019-12-11 03:05:44

问题


Suppose I have a __m128i containing 4 32-bit integer values.

Is there some way I can store it inside a char[4], where the lower char from each int value is stored in a char value?

Desired result:

           r1          r2          r3          r4
__m128i    0x00000012  0x00000034  0x00000056  0x00000078

  |
  V

char[4]    0x12        0x34        0x56        0x78     

SSE2 and below is preferred. Compiling on MSVC++.


回答1:


With SSE2 you can use the following code:

char[4] array;
x = _mm_packs_epi32(x, x);
x = _mm_packus_epi16(x, x);
*((int*)array) = _mm_cvtsi128_si32(x);



回答2:


Just for completeness, with SSSE3 you can do it with only one shuffling operation using _mm_shuffle_epi8. See here. You do consume one register more though, it depends on what is more important for you.



来源:https://stackoverflow.com/questions/19213084/extract-4-sse-integers-to-4-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!