4 ms·
I just had it pointed out to me that CMOV can be simulated with subtraction and bitwise operators. Specifically, subtraction, arithmetic shift right, and AND.
by Dwedit 4y ago
I just had it pointed out to me that CMOV can be simulated with subtraction and bitwise operators.
Specifically, subtraction, arithmetic shift right, and AND. `((A-B) ASR 31) AND C`. Your result is C if `A - B < 0`, and your result is 0 if `A - B >= 0`.
- spc476 4y agoIn C, shifting signed integers is undefined behavior.
- compiler-guy 4y agoAlthough it can bite you if you don’t know what you are doing, signed-integer shifting is not necessarily undefined behavior. Roughly, for positive signed integers, if the operation doesn’t overflow then the result is defined. For negative signed numbers, right shifts are implementation defined, but almost all modern systems define the result in the expected manner. https://en.cppreference.com/w/cpp/language/operator_arithmetic https://en.cppreference.com/w/cpp/language/operator_arithmet...
- kps 4y agoOh, it's too bad C2x doesn't seem to be following C++20 here, since they're apparently mandating two's complement.