Reputation: 338
Extending this question, I was looking for a fast way to abs %ax.
I have two options, I will call them arithmetic and conditional, and avoid stack management for semplicity:
absax:
cwd
xor %dx, %ax
sub %dx, %ax
ret
The arithmetic version extends the sign of %ax into %dx, so the way it works is:
x = (~x) -(-1)
in case the sign bit is 1, essentially performing a neg
operation.
absax:
mov %ax, %dx
neg %ax
cmovs %dx, %ax
ret
This version is in my opinion simpler to understand and remember, and actually it is what gcc prefers. However I was not able to easily find human opinion about the conditional version. I was only able to find the arithmetic version, so I want to know once and for all what the best version is, and if my preferred one is inferior in any way.
NOTE: After searching for a bit more I found some discussion on the conditional version as well, actually it was present in the original answer, I am deeply sorry. However, I still feel that this question is an attempt to better address the differences between the two approaches.
Upvotes: 0
Views: 170