From: Randy Dunlap <rdunlap@xenotime.net>
To: kernel-janitors@vger.kernel.org
Subject: Re: is there a "single_bit_set" macro somewhere?
Date: Thu, 23 Apr 2009 17:24:21 +0000 [thread overview]
Message-ID: <49F0A445.801@xenotime.net> (raw)
In-Reply-To: <alpine.LFD.2.00.0904231307160.12567@localhost.localdomain>
Robert P. J. Day wrote:
> once upon a time, the kernel source code was replete with
> conditional constructs of the form:
>
> if ((n & (n-1)) = 0)
>
> as a way of testing whether something was a power of two. mercifully,
> include/linux/log2.h was created which introduced, among other things:
>
> static inline __attribute__((const))
> bool is_power_of_2(unsigned long n)
> {
> return (n != 0 && ((n & (n - 1)) = 0));
> }
>
> so we could simply ask whether "is_power_of_2(n)", which is convenient
> when we're testing things like, oh, blocksize.
>
> similarly, there are bunches of places which need to test whether an
> integer value has only a single set bit (for instance, to make sure
> only one flag bit out of a number of mutually exclusive bits are set).
>
> mathematically, that would be the same test, of course, but
> semantically, it would be ugly and inappropriate. is there,
> somewhere, a corresponding macro/function that asks:
>
> single_bit_set(n)
>
> if not, that would be handy, could be plopped into
> include/linux/bitops.h and could be defined exactly the same way, and
> would allow piles of code to be simplified.
>
> thoughts?
single_bit_set(n) is equivalent to
hamming weight(n) = 1
lib/hweight.c implements hamming weights for 8/16/32/64-bit args.
next prev parent reply other threads:[~2009-04-23 17:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-23 17:14 is there a "single_bit_set" macro somewhere? Robert P. J. Day
2009-04-23 17:24 ` Randy Dunlap [this message]
2009-04-23 17:26 ` Julia Lawall
2009-04-23 17:34 ` Robert P. J. Day
2009-04-23 20:16 ` Julia Lawall
2009-04-23 20:24 ` Robert P. J. Day
2009-04-24 13:43 ` walter harms
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=49F0A445.801@xenotime.net \
--to=rdunlap@xenotime.net \
--cc=kernel-janitors@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.