Aymerik DIEBOLD
Aymerik DIEBOLD

Reputation: 1

How to duplicate last byte of each double-word?

I use SSE and I want to duplicate the last byte of each double word 4 times of XMM0 but I don't know how to do! (maybe with (un)packs?)

To illustrate, I'd like to do this.

Thanks for your help!

Upvotes: 0

Views: 190

Answers (1)

zx485
zx485

Reputation: 29042

You can do this with the SSSE3 command PSHUFB like this (MASM 32-bit assembly)

.data 
  align 16
  mask  db 0,0,0,0, 4,4,4,4, 8,8,8,8, 12,12,12,12
.code
  ; value in XMM0                  ; 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  pshufb xmm0, xmmword ptr [mask]  ; 12 12 12 12 08 08 08 08 04 04 04 04 00 00 00 00

That the output seems to match the mask is a coincident.
I couldn't test this at the moment, the order of the mask bytes may be reversed. But you should get the idea.

Anyways: take care of alignment, because

When the source operand is a 128-bit memory operand, the operand must be aligned on a 16-byte boundary or a general-protection exception (#GP) will be generated.

Upvotes: 2

Related Questions