Home > Blockchain >  Is there way to cast integers to bytes, knowing ints are in range of bytes. Using SSE?
Is there way to cast integers to bytes, knowing ints are in range of bytes. Using SSE?

Time:01-16

Hey I have 3 integers with values max to 255 in xmm register. I want to cast them to bytes, and save them to memory. I dont know how to approach it. I was thinking about getting those numbers from xmm1 registers and saving them to simple eax registers then moving lowest bytes to memory, but I am not sure how to get integers from xmm register. I can get only element at 0th position, but how to move the rest? There is also perfect instruction that would work for me VPMOVDB But I cant use it on my processor is there some replacement for that?

CodePudding user response:

The easiest way is probably to use pshufb to permute the bytes, followed by movd to store the datum:

        ; convert dwords in xmm0 into bytes and store into dest
        pshufb  xmm0, xmmword ptr mask
        movd    dword ptr dest, xmm0

        ...
        align   16
mask    db      0, 4, 8, 12, 12 dup (-1)

This stores 4 bytes instead of 3, so make sure your code can handle that. Storing only 3 bytes is also possible, but requires more work:

        ; convert dwords in xmm0 into bytes and store into dest
        pshufb  xmm0, xmmword ptr mask
        movd    eax, xmm0
        mov     word ptr dest, ax
        bswap   eax
        mov     byte ptr dest 2, ah

        ...
        align   16
mask    db      0, 4, 8, 12, 12 dup (-1)

If this happens more than once, you can load the mask ahead of time to avoid the penalty of repeatedly loading it.

  •  Tags:  
  • Related