这就是我想要实现的目标.这很简单:
unsigned int foo1(bool cond,unsigned int num) { return cond ? num : 0; }
Assmebly:
test dil,dil mov eax,0 cmovne eax,esi ret
使用乘法:
unsigned int foo2(bool cond,unsigned int num) { return cond * num; }
ASSMBLY:
movzx eax,dil imul eax,esi ret
使用内存访问:
unsigned int foo3(bool cond,unsigned int num) { static const unsigned int masks[2] = { 0x0,0xFFFFFFFF }; return masks[cond] & num; }
部件:
movzx edi,DWORD PTR foo3(bool,unsigned int)::masks[0+rdi*4] and eax,esi ret
使用一些技巧:
unsigned int foo4(bool cond,unsigned int num) { return (0 - (unsigned)cond) & num; }
部件:
movzx eax,dil neg eax and eax,esi ret
现在,乘法产生最少的指令,我认为这是最好的选择,但我不确定imul.有什么建议?
提前致谢,