linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: A pettiness question.
@ 2005-09-21 19:46 Nick Warne
  2005-09-21 20:05 ` Vadim Lobanov
  2005-09-22  2:43 ` Fawad Lateef
  0 siblings, 2 replies; 15+ messages in thread
From: Nick Warne @ 2005-09-21 19:46 UTC (permalink / raw)
  To: linux-kernel

>> This give a enum of {0,1}. If test is not 0, !!test will give 1,
>> otherwise 0.
>>
>> Am I right?
> 
> Yes.  I think of it as a "truth value" predicate (or operator).

Interesting.  I thought maybe this way was trick, until later I experimented.

My post here (as Bill Stokes):

http://www.quakesrc.org/forums/viewtopic.php?t=5626

So what is the reason to doing !!num as opposed to num ? 1:0 (which is more 
readable I think, especially to a lesser experienced C coder).  Quicker to 
type?

My quick test shows compiler renders both the same?

Nick
-- 
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21 19:46 A pettiness question Nick Warne
@ 2005-09-21 20:05 ` Vadim Lobanov
  2005-09-22  7:10   ` Helge Hafting
  2005-09-22  2:43 ` Fawad Lateef
  1 sibling, 1 reply; 15+ messages in thread
From: Vadim Lobanov @ 2005-09-21 20:05 UTC (permalink / raw)
  To: Nick Warne; +Cc: linux-kernel

On Wed, 21 Sep 2005, Nick Warne wrote:

> >> This give a enum of {0,1}. If test is not 0, !!test will give 1,
> >> otherwise 0.
> >>
> >> Am I right?
> >
> > Yes.  I think of it as a "truth value" predicate (or operator).
>
> Interesting.  I thought maybe this way was trick, until later I experimented.
>
> My post here (as Bill Stokes):
>
> http://www.quakesrc.org/forums/viewtopic.php?t=5626
>
> So what is the reason to doing !!num as opposed to num ? 1:0 (which is more
> readable I think, especially to a lesser experienced C coder).  Quicker to
> type?

Some people also prefer the following form:
	num != 0

> My quick test shows compiler renders both the same?
>
> Nick
> --
> "When you're chewing on life's gristle,
> Don't grumble, Give a whistle..."
> -

-Vadim Lobanov

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21 19:46 A pettiness question Nick Warne
  2005-09-21 20:05 ` Vadim Lobanov
@ 2005-09-22  2:43 ` Fawad Lateef
  1 sibling, 0 replies; 15+ messages in thread
From: Fawad Lateef @ 2005-09-22  2:43 UTC (permalink / raw)
  To: Nick Warne; +Cc: linux-kernel

On 9/22/05, Nick Warne <nick@linicks.net> wrote:
>
> Interesting.  I thought maybe this way was trick, until later I experimented.
>
> My post here (as Bill Stokes):
>
> http://www.quakesrc.org/forums/viewtopic.php?t=5626
>
> So what is the reason to doing !!num as opposed to num ? 1:0 (which is more
> readable I think, especially to a lesser experienced C coder).  Quicker to
> type?
>

I think using !! is quick and the place where it is used, will look
little bit odd (like you say in #define or macros) if some one use num
? 1 : 0 ...... And I think lesser experienced C coder will learn other
ways of doing same things !!!!!

> My quick test shows compiler renders both the same?
>

Ya, I think both !! and num ? 1: 0 will result in same thing by compiler

--
Fawad Lateef

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21 20:05 ` Vadim Lobanov
@ 2005-09-22  7:10   ` Helge Hafting
  2005-09-22 12:21     ` Steven Rostedt
  0 siblings, 1 reply; 15+ messages in thread
From: Helge Hafting @ 2005-09-22  7:10 UTC (permalink / raw)
  To: Vadim Lobanov; +Cc: Nick Warne, linux-kernel

Vadim Lobanov wrote:

>On Wed, 21 Sep 2005, Nick Warne wrote:
>
>  
>
>>>>This give a enum of {0,1}. If test is not 0, !!test will give 1,
>>>>otherwise 0.
>>>>
>>>>Am I right?
>>>>        
>>>>
>>>Yes.  I think of it as a "truth value" predicate (or operator).
>>>      
>>>
>>Interesting.  I thought maybe this way was trick, until later I experimented.
>>
>>My post here (as Bill Stokes):
>>
>>http://www.quakesrc.org/forums/viewtopic.php?t=5626
>>
>>So what is the reason to doing !!num as opposed to num ? 1:0 (which is more
>>readable I think, especially to a lesser experienced C coder).  Quicker to
>>type?
>>    
>>
>
>Some people also prefer the following form:
>	num != 0
>  
>
That one looks good for if-tests and such. But if you need
a 0 or 1 for adding to a counter, then

a += !!x;

looks much better than

a += (x != 0);

There is nothing special about !!.  Just learn to use it, it
is easy to understand/read, and saves typing.

Perhaps a "C-contructs FAQ" might be useful, for the benefit
of newbies and beginners.  It could document things like
the use of !!, common kernel macro tricks, when not to
use zero initializers, and so on.

Helge Hafting

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-22  7:10   ` Helge Hafting
@ 2005-09-22 12:21     ` Steven Rostedt
  2005-09-22 14:40       ` linux-os (Dick Johnson)
  0 siblings, 1 reply; 15+ messages in thread
From: Steven Rostedt @ 2005-09-22 12:21 UTC (permalink / raw)
  To: Helge Hafting; +Cc: Vadim Lobanov, Nick Warne, linux-kernel


On Thu, 22 Sep 2005, Helge Hafting wrote:

> That one looks good for if-tests and such. But if you need
> a 0 or 1 for adding to a counter, then
>
> a += !!x;
>
> looks much better than
>
> a += (x != 0);
>

Actually I prefer:

a += (x == '-'-'-'?'-'-'-':'/'/'/');

  :)


-- Steve


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-22 12:21     ` Steven Rostedt
@ 2005-09-22 14:40       ` linux-os (Dick Johnson)
  0 siblings, 0 replies; 15+ messages in thread
From: linux-os (Dick Johnson) @ 2005-09-22 14:40 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Helge Hafting, Vadim Lobanov, Nick Warne, linux-kernel


On Thu, 22 Sep 2005, Steven Rostedt wrote:

>
> On Thu, 22 Sep 2005, Helge Hafting wrote:
>
>> That one looks good for if-tests and such. But if you need
>> a 0 or 1 for adding to a counter, then
>>
>> a += !!x;
>>
>> looks much better than
>>
>> a += (x != 0);
>>
>
> Actually I prefer:
>
> a += (x == '-'-'-'?'-'-'-':'/'/'/');
>
>  :)
>
> -- Steve

I like that! Nevertheless, for readability one should probably
standardize upon something returing 1 or 0 for a true/false
comparison. I see "(x)?1:0" a lot in the bit-banging code
and I'm pretty sure the compiler knows how to optimize it.

The bang/bang stuff was a way of hiding warnings back in the
days that `lint` was used as a code-checker. It's use should
be condemned to the fullest extent!

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
       [not found]     ` <4Pynh-fp-1@gated-at.bofh.it>
@ 2005-09-25 14:38       ` Bodo Eggert
  0 siblings, 0 replies; 15+ messages in thread
From: Bodo Eggert @ 2005-09-25 14:38 UTC (permalink / raw)
  To: Steven Rostedt, Steven Rostedt, Helge Hafting, Vadim Lobanov,
	Nick Warne, linux-kernel

Steven Rostedt <rostedt@goodmis.org> wrote:

> Actually I prefer:
> 
> a += (x == '-'-'-'?'-'-'-':'/'/'/');

a += (x^'^'^'^'?'/'/'/':'-'-'-');

CNR
-- 
Ich danke GMX dafür, die Verwendung meiner Adressen mittels per SPF
verbreiteten Lügen zu sabotieren.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  9:30     ` Andrea Arcangeli
@ 2005-09-23  2:36       ` Vadim Lobanov
  0 siblings, 0 replies; 15+ messages in thread
From: Vadim Lobanov @ 2005-09-23  2:36 UTC (permalink / raw)
  To: Andrea Arcangeli; +Cc: Fawad Lateef, Ustyugov Roman, liyu, lkml

On Wed, 21 Sep 2005, Andrea Arcangeli wrote:

> On Wed, Sep 21, 2005 at 02:01:11PM +0500, Fawad Lateef wrote:
> > On 9/21/05, Ustyugov Roman <dr_unique@ymg.ru> wrote:
> > > > Hi, All.
> > > >
> > > >     I found there are use double operator ! continuously sometimes in
> > > > kernel.
> > > > e.g:
> > > >
> > > >     static inline int is_page_cache_freeable(struct page *page)
> > > >     {
> > > >         return page_count(page) - !!PagePrivate(page) == 2;
> > > >     }
> > > >
> > > >     Who would like tell me why write like above?
> > >
> > > For example,
> > >
> > >         int test = 5;
> > >         !test will be  0,  !!test will be 1.
> > >
> > > This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.
> > >
> > > Am I right?
> >
> > Yes, but what abt the above case/example ??? PagePrivate is defined as
> > test_bit and test_bit will return 0 or 1 only ...... So y there is (
> > !! )  ??
>
> Note that gcc should optimize it away as long as the asm*/bitops is
> doing "return something != 0" like most archs do.
>
> Most of the time test_bit retval is checked against zero only, here it's
> one of the few cases where it's required to be 1 or 0. If you audit all
> archs then you can as well remove the !! from above.

After scanning through some of the archs, it seems that the !! is really
necessary in that case. Here's why:

PagePrivate() is just a macro wrapper around test_bit(). On i386, in
include/asm-i386/bitops.h, test_bit() may end up calling
variable_test_bit(). This inline function uses two assembly instructions
to compute the desired result, which end up returning '-1', not '1', in
the case that the bit is set.

(Hopefully this mail gets archived away and saves someone else the work
of digging through the archs.)

> Thanks!

-Vadim Lobanov

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  7:49 liyu
  2005-09-21  8:00 ` Ustyugov Roman
@ 2005-09-21 19:11 ` Bill Davidsen
  1 sibling, 0 replies; 15+ messages in thread
From: Bill Davidsen @ 2005-09-21 19:11 UTC (permalink / raw)
  To: liyu, Linux Kernel Mailing List

liyu wrote:
> Hi, All.
>      I found there are use double operator ! continuously sometimes in 
> kernel.
> e.g:
> 
>    static inline int is_page_cache_freeable(struct page *page)
>    {
>        return page_count(page) - !!PagePrivate(page) == 2;
>    }
> 
>    Who would like tell me why write like above?
>    
!!(N) is easier to write than ((N) ? 1 : 0)
-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  8:00 ` Ustyugov Roman
  2005-09-21  8:57   ` Eyal Lebedinsky
  2005-09-21  9:01   ` Fawad Lateef
@ 2005-09-21 15:08   ` Randy.Dunlap
  2 siblings, 0 replies; 15+ messages in thread
From: Randy.Dunlap @ 2005-09-21 15:08 UTC (permalink / raw)
  To: Ustyugov Roman; +Cc: liyu, lkml

On Wed, 21 Sep 2005, Ustyugov Roman wrote:

> > Hi, All.
> >
> >     I found there are use double operator ! continuously sometimes in
> > kernel.
> > e.g:
> >
> >     static inline int is_page_cache_freeable(struct page *page)
> >     {
> >         return page_count(page) - !!PagePrivate(page) == 2;
> >     }
> >
> >     Who would like tell me why write like above?
> >
> >
> >     Thanks in advanced.
> >
> >
> > Liyu
>
> For example,
>
> 	int test = 5;
> 	!test will be  0,  !!test will be 1.
>
> This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.
>
> Am I right?

Yes.  I think of it as a "truth value" predicate (or operator).

-- 
~Randy

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  9:01   ` Fawad Lateef
@ 2005-09-21  9:30     ` Andrea Arcangeli
  2005-09-23  2:36       ` Vadim Lobanov
  0 siblings, 1 reply; 15+ messages in thread
From: Andrea Arcangeli @ 2005-09-21  9:30 UTC (permalink / raw)
  To: Fawad Lateef; +Cc: Ustyugov Roman, liyu, lkml

On Wed, Sep 21, 2005 at 02:01:11PM +0500, Fawad Lateef wrote:
> On 9/21/05, Ustyugov Roman <dr_unique@ymg.ru> wrote:
> > > Hi, All.
> > >
> > >     I found there are use double operator ! continuously sometimes in
> > > kernel.
> > > e.g:
> > >
> > >     static inline int is_page_cache_freeable(struct page *page)
> > >     {
> > >         return page_count(page) - !!PagePrivate(page) == 2;
> > >     }
> > >
> > >     Who would like tell me why write like above?
> > 
> > For example,
> > 
> >         int test = 5;
> >         !test will be  0,  !!test will be 1.
> > 
> > This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.
> > 
> > Am I right?
> 
> Yes, but what abt the above case/example ??? PagePrivate is defined as
> test_bit and test_bit will return 0 or 1 only ...... So y there is (
> !! )  ??

Note that gcc should optimize it away as long as the asm*/bitops is
doing "return something != 0" like most archs do.

Most of the time test_bit retval is checked against zero only, here it's
one of the few cases where it's required to be 1 or 0. If you audit all
archs then you can as well remove the !! from above.

Thanks!

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  8:00 ` Ustyugov Roman
  2005-09-21  8:57   ` Eyal Lebedinsky
@ 2005-09-21  9:01   ` Fawad Lateef
  2005-09-21  9:30     ` Andrea Arcangeli
  2005-09-21 15:08   ` Randy.Dunlap
  2 siblings, 1 reply; 15+ messages in thread
From: Fawad Lateef @ 2005-09-21  9:01 UTC (permalink / raw)
  To: Ustyugov Roman; +Cc: liyu, lkml

On 9/21/05, Ustyugov Roman <dr_unique@ymg.ru> wrote:
> > Hi, All.
> >
> >     I found there are use double operator ! continuously sometimes in
> > kernel.
> > e.g:
> >
> >     static inline int is_page_cache_freeable(struct page *page)
> >     {
> >         return page_count(page) - !!PagePrivate(page) == 2;
> >     }
> >
> >     Who would like tell me why write like above?
> 
> For example,
> 
>         int test = 5;
>         !test will be  0,  !!test will be 1.
> 
> This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.
> 
> Am I right?

Yes, but what abt the above case/example ??? PagePrivate is defined as
test_bit and test_bit will return 0 or 1 only ...... So y there is (
!! )  ??

-- 
Fawad Lateef

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  8:00 ` Ustyugov Roman
@ 2005-09-21  8:57   ` Eyal Lebedinsky
  2005-09-21  9:01   ` Fawad Lateef
  2005-09-21 15:08   ` Randy.Dunlap
  2 siblings, 0 replies; 15+ messages in thread
From: Eyal Lebedinsky @ 2005-09-21  8:57 UTC (permalink / raw)
  To: lkml

Ustyugov Roman wrote:
>>Hi, All.
>>
>>    I found there are use double operator ! continuously sometimes in
>>kernel.
>>e.g:
>>
>>    static inline int is_page_cache_freeable(struct page *page)
>>    {
>>        return page_count(page) - !!PagePrivate(page) == 2;
>>    }
>>
>>    Who would like tell me why write like above?
>>
>>
>>    Thanks in advanced.
>>
>>
>>Liyu
> 
> 
> For example,
> 
> 	int test = 5;
> 	!test will be  0,  !!test will be 1.
> 
> This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.
> 
> Am I right?

Yes, !! converts {zero,not-zero} to {0,1} which is useable
in arithmetic.

--
Eyal Lebedinsky (eyal@eyal.emu.id.au) <http://samba.org/eyal/>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: A pettiness question.
  2005-09-21  7:49 liyu
@ 2005-09-21  8:00 ` Ustyugov Roman
  2005-09-21  8:57   ` Eyal Lebedinsky
                     ` (2 more replies)
  2005-09-21 19:11 ` Bill Davidsen
  1 sibling, 3 replies; 15+ messages in thread
From: Ustyugov Roman @ 2005-09-21  8:00 UTC (permalink / raw)
  To: liyu, lkml

> Hi, All.
>
>     I found there are use double operator ! continuously sometimes in
> kernel.
> e.g:
>
>     static inline int is_page_cache_freeable(struct page *page)
>     {
>         return page_count(page) - !!PagePrivate(page) == 2;
>     }
>
>     Who would like tell me why write like above?
>
>
>     Thanks in advanced.
>
>
> Liyu

For example,

	int test = 5;
	!test will be  0,  !!test will be 1.

This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.

Am I right?
-- 
RomanU

^ permalink raw reply	[flat|nested] 15+ messages in thread

* A pettiness question.
@ 2005-09-21  7:49 liyu
  2005-09-21  8:00 ` Ustyugov Roman
  2005-09-21 19:11 ` Bill Davidsen
  0 siblings, 2 replies; 15+ messages in thread
From: liyu @ 2005-09-21  7:49 UTC (permalink / raw)
  To: LKML

Hi, All.
   
    I found there are use double operator ! continuously sometimes in 
kernel.
e.g:

    static inline int is_page_cache_freeable(struct page *page)
    {
        return page_count(page) - !!PagePrivate(page) == 2;
    }

    Who would like tell me why write like above?
   
  
    Thanks in advanced.


Liyu

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2005-09-25 14:38 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-21 19:46 A pettiness question Nick Warne
2005-09-21 20:05 ` Vadim Lobanov
2005-09-22  7:10   ` Helge Hafting
2005-09-22 12:21     ` Steven Rostedt
2005-09-22 14:40       ` linux-os (Dick Johnson)
2005-09-22  2:43 ` Fawad Lateef
     [not found] <4PiLw-2yn-25@gated-at.bofh.it>
     [not found] ` <4Pj4M-3as-1@gated-at.bofh.it>
     [not found]   ` <4PtnM-1oW-55@gated-at.bofh.it>
     [not found]     ` <4Pynh-fp-1@gated-at.bofh.it>
2005-09-25 14:38       ` Bodo Eggert
  -- strict thread matches above, loose matches on Subject: below --
2005-09-21  7:49 liyu
2005-09-21  8:00 ` Ustyugov Roman
2005-09-21  8:57   ` Eyal Lebedinsky
2005-09-21  9:01   ` Fawad Lateef
2005-09-21  9:30     ` Andrea Arcangeli
2005-09-23  2:36       ` Vadim Lobanov
2005-09-21 15:08   ` Randy.Dunlap
2005-09-21 19:11 ` Bill Davidsen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).