简体   繁体   中英

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 immideate operand. Reading from memory is also bad variant.

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

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.

There is essentially one generic method in most ISAs providing branchess setting or clearing a register: generating an all zero or all ones mask from carry flag: sbb reg,reg clears a mask when carry is zero and sets the mask when carry is set. Followed by and dst, reg will either clear the destination register, or leave it unchanged.

One can invert the condition by toggling the mask, or inverting the carry flag. Test for zero can be achieved by either subtracting one from the register under test, or subtracting the register under test from zero. The first sets carry iff register was zero; the second form sets carry iff register was non-zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM