![](/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.