Search code examples
assemblyx86flagsstatus-registerbranchless

Branchless way to conditionally clear register


Is there branchless way to clear 32-bit register depending on status register state? It can be achieved using additional clear register and CMOVcc, but it is too expensive on x86 in 32bit mode for me. Sadly CMOVcc have no version with immediate operand. Reading from memory is also bad variant.

There is SETcc (though, operand is 1 byte) but not "CLEARcc" instruction on x86.


Solution

  • This may disappoint you, but CMOVcc is very good in that regard. Using it with a variable ddZERO with the value 0 is not that bad, especially in a loop.

    CMOVcc rTarget, ddZERO
    

    resets the rTarget register to zero if the cc conditions are met.
    Otherwise (there is an otherwise) you can invert the scenario and CMOVcc on a NOT MATCHING condition. Which choice would be better depends on the frequency of the occurrence.

    If you have a register with the value 0 you should use that instead. But if you can't spare a register using a (cached) memory location is not that bad. This estimation is based on experience and IIRC using a constant in a L1 cached memory location has a practically negligible latency in a loop.