From: "Robert P. J. Day" <rpjday@crashcourse.ca>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: David Daney <ddaney@caviumnetworks.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] Introduce a boolean "single_bit_set" function.
Date: Fri, 24 Apr 2009 09:51:21 -0400 (EDT) [thread overview]
Message-ID: <alpine.LFD.2.00.0904240948550.17112@localhost.localdomain> (raw)
In-Reply-To: <20090423165723.16c9f566.akpm@linux-foundation.org>
On Thu, 23 Apr 2009, Andrew Morton wrote:
> On Thu, 23 Apr 2009 12:57:11 -0700
> David Daney <ddaney@caviumnetworks.com> wrote:
>
> > > +static inline __attribute__((const))
> > > +bool single_bit_set(unsigned long n)
> > > +{
> > > + return (n != 0 && ((n & (n - 1)) == 0));
> > > +}
> > > +
> > > +
> >
> >
> > It would be nice to be able to override this per architecture.
> >
> > For example a more efficient implementation on CPUs that have a
> > population count instruction (__builtin_popcountl()) might be:
> >
> > static inline __attribute__((const))
> > bool singe_bit_set(unsigned long n)
> > {
> > return __builtin_popcountl(n) == 1;
> > }
>
> Already done, via hweight_long().
i'm guessing that taking advantage of helper functions like that
might simplify code like, say, this from fs/nfs/internal.h:
/*
* Determine the actual block size (and log2 thereof)
*/
static inline
unsigned long nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
{
/* make sure blocksize is a power of two */
if ((bsize & (bsize - 1)) || nrbitsp) {
unsigned char nrbits;
for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
;
bsize = 1 << nrbits;
if (nrbitsp)
*nrbitsp = nrbits;
}
return bsize;
}
surely, there's a simpler way to write that.
rday
--
========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA
Linux Consulting, Training and Annoying Kernel Pedantry.
Web page: http://crashcourse.ca
Linked In: http://www.linkedin.com/in/rpjday
Twitter: http://twitter.com/rpjday
========================================================================
next prev parent reply other threads:[~2009-04-24 13:52 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-23 17:43 [PATCH] Introduce a boolean "single_bit_set" function Robert P. J. Day
2009-04-23 19:57 ` David Daney
2009-04-23 20:11 ` Robert P. J. Day
2009-04-23 23:57 ` Andrew Morton
2009-04-24 10:40 ` Robert P. J. Day
2009-04-24 17:46 ` Andrew Morton
2009-04-25 22:09 ` Robert P. J. Day
2009-06-29 18:15 ` Petr Tesarik
2009-06-29 18:50 ` Robert P. J. Day
2009-06-30 6:12 ` Petr Tesarik
2009-06-30 10:18 ` Robert P. J. Day
2009-04-24 13:51 ` Robert P. J. Day [this message]
2009-05-28 12:21 ` Petr Tesarik
2009-05-28 12:27 ` Robert P. J. Day
2009-05-28 12:32 ` Robert P. J. Day
2009-05-28 13:12 ` Petr Tesarik
2009-06-29 18:50 ` H. Peter Anvin
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=alpine.LFD.2.00.0904240948550.17112@localhost.localdomain \
--to=rpjday@crashcourse.ca \
--cc=akpm@linux-foundation.org \
--cc=ddaney@caviumnetworks.com \
--cc=linux-kernel@vger.kernel.org \
/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).