From: David Daney <ddaney@caviumnetworks.com>
To: "Robert P. J. Day" <rpjday@crashcourse.ca>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Andrew Morton <akpm@osdl.org>
Subject: Re: [PATCH] Introduce a boolean "single_bit_set" function.
Date: Thu, 23 Apr 2009 12:57:11 -0700 [thread overview]
Message-ID: <49F0C817.8050407@caviumnetworks.com> (raw)
In-Reply-To: <alpine.LFD.2.00.0904231339080.13089@localhost.localdomain>
Robert P. J. Day wrote:
> A boolean single_bit_set() routine would simplify the numerous
> constructs of the form (((n & (n - 1)) == 0)) when testing for
> single-bitness.
>
> Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
>
> ---
>
> This is similar to the current is_power_of_2() routine defined in
> include/linux/log2.h, which is mathematically identical but,
> semantically, should be defined independently just so the code is more
> readable.
>
> I'm open to an alternative function name.
>
> diff --git a/include/linux/bitops.h b/include/linux/bitops.h
> index 6182913..1c0c840 100644
> --- a/include/linux/bitops.h
> +++ b/include/linux/bitops.h
> @@ -45,6 +45,13 @@ static inline unsigned long hweight_long(unsigned long w)
> return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
> }
>
> +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;
}
Also, are we still putting 'inline' everywhere?
David Daney
next prev parent reply other threads:[~2009-04-23 19:57 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 [this message]
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
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=49F0C817.8050407@caviumnetworks.com \
--to=ddaney@caviumnetworks.com \
--cc=akpm@osdl.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rpjday@crashcourse.ca \
/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.