How to reverse an __m128 type variable?

心已入冬 提交于 2021-02-04 05:37:04

问题


I know this should be a Googling question but I just cannot find the answer.

Say I have an __m128 variable a, whose content is a[0], a[1], a[2], a[3]. Is there a single function that can reverse it to be a[3], a[2], a[1], a[0]?


回答1:


Use _mm_shuffle_ps(). This instruction was already available in SSE and can gather 4 32-bit components in a single vector by combining two arbitrary 32-bit components from each of the two input vectors.

How to create the mask using the macro _MM_SHUFFLE()

The macro is defined as follows:

/* Create a selector for use with the SHUFPS instruction.  */
#define _MM_SHUFFLE(fp3,fp2,fp1,fp0) \
 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | (fp0))

Source and destination indices run from right to left in ascending order. The first two selector values (fp0 and fp1) designate source components in m1, the last two (fp2 and fp3) the ones in m2. Each selected source component is assigned to m3[index], where index corresponds to its selector parameter fp<index>.

Reversing 32-bit components in a vector

__m128 input = ...;
__m128 reversed = _mm_shuffle_ps(input,input,_MM_SHUFFLE(0, 1, 2, 3));

Note: The mask is an immediate value. It cannot be dynamic, as it is part of the resulting machine instruction.

Intel Intrinsics Guide: https://software.intel.com/sites/landingpage/IntrinsicsGuide/



来源:https://stackoverflow.com/questions/20051746/how-to-reverse-an-m128-type-variable

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