Re: [PATCH] Introduce a boolean "single_bit_set" function.
From: Robert P. J. Day
Date: Thu May 28 2009 - 08:29:32 EST
On Thu, 28 May 2009, Petr Tesarik wrote:
> Robert P. J. Day pÃÅe v Ät 23. 04. 2009 v 13:43 -0400:
> > 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@xxxxxxxxxxxxxx>
> > ---
> > 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.
> ispow2() ?
> Because what it really does is to check that a value is a power of
> two, doesn'it.
yes, mathematically it's identical, but *semantically*, i think it's
worth distinguishing between those two tests. there's still a number
of places in the code that can be rewritten with "is power of 2"
tests, but that rewriting makes sense only if that's really what
you're asking, as in checking an alleged block size value you've been
passed, or something similar.
OTOH, there are lots of places in the code that have to verify that
only a single (flag?) bit has been set, and that code would read a lot
better using a better worded test. someone already pointed out that
perhaps one of the hweight*() routines from include/linux/bitops.h
would be appropriate.
and, yes, i realize that there would be no functional change, but
rewriting tests like this not only makes the code more readable, it
allows for the removal of accompanying comments that have to explain
to the reader what's going on. but i'll leave the decision to those
higher up the food chain.
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