assembly - Efficient three valued compare -


for unsigned integers getting result of

if (a>b) => 1 if (a=b) => 0 if (a<b) => -1 

can optimized branchless version

return ((a > b) - (a < b)) 

this can written x86 assembly so:

4829d1           cmp rcx,rdx 0f94c1           setz cl     19c0             sbb eax,eax     83d8ff           sbb eax,-$01     d3e8             shr eax,cl  13 bytes in total 

is there way without branches in less 5 instructions or in fewer bytes?

a solution less bytes (11 bytes) , 1 less instruction (4 instructions) may faster:

483bca          cmp     rcx,rdx 1bc0            sbb     eax,eax 483bd1          cmp     rdx,rcx 83d000          adc     eax,0 

this can improved upon 10 bytes if have spare register known null.

... 11d8            adc     eax,ebx 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -