Here is what I'm trying to acheive. It's simple enough:
unsigned int foo1(bool cond, unsigned int num)
{
return cond ? num : 0;
}
Assmebly:
test dil, dil
mov eax, 0
cmovne eax, esi
ret
My question is, is there a faster way to do it? Here are some ways I thought of:
Using multiplication:
unsigned int foo2(bool cond, unsigned int num)
{
return cond * num;
}
Assmbly:
movzx eax, dil
imul eax, esi
ret
Using memory access:
unsigned int foo3(bool cond, unsigned int num)
{
static const unsigned int masks[2] = { 0x0, 0xFFFFFFFF };
return masks[cond] & num;
}
Assembly:
movzx edi, dil
mov eax, DWORD PTR foo3(bool, unsigned int)::masks[0+rdi*4]
and eax, esi
ret
Using some bit tricks:
unsigned int foo4(bool cond, unsigned int num)
{
return (0 - (unsigned)cond) & num;
}
Assembly:
movzx eax, dil
neg eax
and eax, esi
ret
Now, multiplication yields the least instructions, I think it's the best choice, but I'm not sure about the imul. Any suggestions?
Thanks in advance,