ternary vs double exclamation
Vinícius Tinti
viniciustinti at gmail.com
Mon Dec 29 19:25:41 EST 2014
Hi,
I was looking the kernel source code and there are a lot of places in
which either "(expression) ? 1 : 0" or "(expression) ? 0 : 1" appear.
As fair as I can tell both can be replaced by "!!expression" and
"!expression".
Moreover there it seems that using "!!" does not add a "nopl"
instruction at the end of the call. Does anybody knows why?
Anyway. Wouldn't be nice if kernel provides something like
"boolean(x)" macro and "inv_boolean(x)" to do this operations?
#ifdef MOD_IF
int mod_if(int x) {
return (x == 0) ? 0 : 1;
}
#endif
#ifdef MOD_X
int mod_x(int x) {
return !!x;
}
#endif
0000000000000000 <mod_if>:
0: 31 c0 xor %eax,%eax
2: 85 ff test %edi,%edi
4: 0f 95 c0 setne %al
7: c3 retq
8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
f: 00
0000000000000010 <mod_x>:
10: 31 c0 xor %eax,%eax
12: 85 ff test %edi,%edi
14: 0f 95 c0 setne %al
17: c3 retq
Regards,
Vinícius
--
Simplicity is the ultimate sophistication
More information about the Kernelnewbies
mailing list