ternary vs double exclamation

Vinícius Tinti viniciustinti at gmail.com
Mon Dec 29 19:40:41 EST 2014


On Mon, Dec 29, 2014 at 10:34 PM, Max Filippov <jcmvbkbc at gmail.com> wrote:
> 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.

I notice that too. If you use both functions in other code they both will have
the nopl. And "!expression" is as good as the "(expression) ? 0 : 1".

Thus no gain and worse readability I think.

>> 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



-- 
Simplicity is the ultimate sophistication



More information about the Kernelnewbies mailing list