Skip to contentSkip to conversation

A pettiness question.

DISCUSSION liyu 8 participants 8 messages Sep 21, 2005 – Sep 23, 2005

lore
Share & tools
lore

liyu

author
#0 lore raw

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

#1 lore raw
Quoted 11 lines

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

Quoted 7 lines

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?

Signature · 1 line
RomanU
#2 lore raw
Quoted 20 lines · Ustyugov Roman
Quoted 11 lines

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;
   }
Quoted 7 lines

Who would like tell me why write like above?

Thanks in advanced.

Liyu

Quoted 8 lines

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

#3 lore raw
In reply to Ustyugov Roman
Quoted 13 lines · Ustyugov Roman
Quoted 6 lines

Hi, All.

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

Quoted 6 lines
    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?
Quoted 8 lines

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 ( !! ) ??

Signature · 1 line
Fawad Lateef
#4 lore raw
Quoted 19 lines · Fawad Lateef
Quoted 18 lines · Ustyugov Roman
Quoted 6 lines

Hi, All.

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

Quoted 6 lines
    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.

Quoted 7 lines
Quoted 3 lines

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!

#5 lore raw
Quoted 28 lines · Andrea Arcangeli
Quoted 19 lines · Fawad Lateef
Quoted 18 lines · Ustyugov Roman
Quoted 6 lines

Hi, All.

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

Quoted 6 lines
    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.

Quoted 7 lines
Quoted 3 lines

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 ( !! ) ??

Quoted 6 lines

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

Quoted 1 line

Thanks!

-Vadim Lobanov

#6 lore raw
In reply to Ustyugov Roman
Quoted 19 lines · Ustyugov Roman
Quoted 11 lines

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

Quoted 7 lines

Who would like tell me why write like above?

Thanks in advanced.

Liyu

Quoted 8 lines

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

Signature · 1 line
~Randy
#7 lore raw
In reply to liyu
Quoted 5 lines · liyu

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

Quoted 7 lines
   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)

Signature · 3 lines
   -bill davidsen ([email protected])
"The secret to procrastination is to put things off until the
 last possible moment - but no longer"  -me