assemblyx86-64masmssesse4

Is there a way to cast integers to bytes, knowing these ints are in range of bytes. Using SSE?


In an xmm register I have 3 integers with values less than 256. I want to cast these to bytes, and save them to memory. I don't know how to approach it.
I was thinking about getting those numbers from xmm1 and saving them to eax, then moving the lowest bytes to memory, but I am not sure how to get integers from an xmm register. I can get only element at 0th position, but how to move the rest?
There exists a perfect instruction that would work for me VPMOVDB, but I can't use it on my processor. Is there some alternative for it?


Solution

  • 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.