linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vadim Lobanov <vlobanov@speakeasy.net>
To: Andrea Arcangeli <andrea@suse.de>
Cc: Fawad Lateef <fawadlateef@gmail.com>,
	Ustyugov Roman <dr_unique@ymg.ru>, liyu <liyu@ccoss.com.cn>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: A pettiness question.
Date: Thu, 22 Sep 2005 19:36:11 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0509221931230.27735@shell2.speakeasy.net> (raw)
In-Reply-To: <20050921093007.GA11144@x30.random>

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

  reply	other threads:[~2005-09-23  2:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-21  7:49 A pettiness question 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 [this message]
2005-09-21 15:08   ` Randy.Dunlap
2005-09-21 19:11 ` Bill Davidsen
2005-09-21 19:46 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0509221931230.27735@shell2.speakeasy.net \
    --to=vlobanov@speakeasy.net \
    --cc=andrea@suse.de \
    --cc=dr_unique@ymg.ru \
    --cc=fawadlateef@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liyu@ccoss.com.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).