![](/img/trans.png)
[英]Set an XMM register to a repeating byte pattern (broadcast a constant byte)
[英]Broadcast a word to an xmm register
我需要将16位字八次移入xmm寄存器以进行SSE操作
例如:我想在xmm0寄存器中使用16位字ABCD,以便最终结果看起来像
ABCD | ABCD | ABCD | ABCD | ABCD | ABCD | ABCD | ABCD
我想这样做,以便稍后使用paddw
操作。 到目前为止,我已经找到了可以执行我想做的pushfd
操作,但仅适用于双字(32位)。 pshufw
仅适用于-64位寄存器(如果我没有记错的话)。 我是否正在寻找所需的操作,还是必须使用多个pshufw
以某种方式模拟它?
您可以先随机播放然后再打开包装,以达到所需的目标。 使用NASM语法:
# load 16 bit from memory into all words of xmm0
# assuming 16-byte alignment
pshuflw xmm0, [mem], 0 # gives you [ M, M, M, M, ?, ?, ?, ? ]
punpcklwd xmm0, xmm0 # gives you [ M, M, M, M, M, M, M, M ]
请注意,这会从mem
读取16个字节,因此需要16个字节的对齐方式 。
实际上仅使用前2个字节。 如果该号码不在内存中,或者您不能保证可以读完末尾,请使用以下方法:
# load ax into all words of xmm0
movd xmm0, eax ; or movd xmm0, [mem] 4-byte load
pshuflw xmm0, xmm0, 0
punpcklwd xmm0, xmm0
使用AVX2,您可以使用vpbroadcast*
广播负载或来自注册源的广播。 如果愿意,目的地可以是YMM。
vpbroadcastw xmm0, [mem] ; 16-bit load + broadcast
要么
vmovd xmm0, eax
vpbroadcastw xmm0, xmm0
1或2字节元素的内存源广播仍会解码为Intel CPU上的load + shuffle uop,但4字节或8字节块的广播负载甚至更便宜:在加载端口中进行处理,无需shuffle uop 。
无论哪种方式,这仍然比不使用AVX2或SSSE3 pshufb
所需的2个单独的改组便宜。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.