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
> 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
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 ([email protected]) <http://samba.org/eyal/>
On 9/21/05, Ustyugov Roman <[email protected]> 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
On Wed, Sep 21, 2005 at 02:01:11PM +0500, Fawad Lateef wrote:
> On 9/21/05, Ustyugov Roman <[email protected]> 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!
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
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 ([email protected])
"The secret to procrastination is to put things off until the
last possible moment - but no longer" -me
>> 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..."
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
On 9/22/05, Nick Warne <[email protected]> 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
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
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
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 [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
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 <[email protected]> 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
Steven Rostedt <[email protected]> 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.