Search code examples
assemblyx86bit-manipulationssebitwise-not

Bitwise negation of XMM register


How to get bitwise negation of values in XMM register? As far as I know there is no such instruction. The only instruction with negation is pandn, but to use it to simply negate values in one XMM register, I would have to have another XMM register padded filled with 1.

Is there another way to negate bits in XMM register? Or is there a clever way to fill XMM register with 1 without accessing memory?


Solution

  • To load a register with all 1s, use

    pcmpeqd xmm0, xmm0
    

    After that you can simply subtract xmmX from xmm0 to get ~xmmX, or use pandn

    You can also load other constants to xmm registers easily

    pcmpeqd xmm0, xmm0
    psrld   xmm0, 30   ; 3 (32-bit)
    
    pcmpeqd xmm0, xmm0 ; -1
    
    pcmpeqw xmm0, xmm0 ; 1.5f
    pslld   xmm0, 24
    psrld   xmm0, 2
    
    pcmpeqw xmm0, xmm0 ; -2.0f
    pslld   xmm0, 30
    

    Read Agner Fog's optimization guide, 13.4 Generating constants - Making constants for integer vectors in XMM registers