ternary vs double exclamation

Max Filippov jcmvbkbc at gmail.com
Mon Dec 29 19:34:36 EST 2014


On Tue, Dec 30, 2014 at 3:25 AM, Vinícius Tinti <viniciustinti at gmail.com> wrote:
> 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?

It seems that the nop instruction is inserted for alignment, and if you
reverse the order of functions in your c source, nop will still be
inserted between them.

> 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

-- 
Thanks.
-- Max



More information about the Kernelnewbies mailing list