From: Linus Torvalds <torvalds@linux-foundation.org>
To: Ivo van Doorn <ivdoorn@gmail.com>
Cc: Johannes Berg <johannes@sipsolutions.net>,
Pavel Roskin <proski@gnu.org>,
linux-wireless@vger.kernel.org, linux-sparse@vger.kernel.org
Subject: Re: sparse using insane amounts of memory
Date: Thu, 8 Mar 2007 10:54:48 -0800 (PST) [thread overview]
Message-ID: <Pine.LNX.4.64.0703081023490.10832@woody.linux-foundation.org> (raw)
In-Reply-To: <200703081908.40997.IvDoorn@gmail.com>
On Thu, 8 Mar 2007, Ivo van Doorn wrote:
>
> Those checks are intended to doublecheck the register FIELD{16,32}
> defines. Since all register definitions were rewritten from the legacy driver,
> (legacy driver used unions and structs for all registers) some of those
> defines weren't done correctly (A bitmask could for example be in binary 000110111
> which is very wrong).
>
> To check those the register checks were added to ensure the register defines
> were at least correct. I am however open to suggestions on how this should be improved
> and cleaned up, since it is not my favorite piece of code. ;)
What is the check? Just checking that something is an exact power of two?
To check a value for being a nice range of consecutive bits, you can
simply do:
#define is_power_of_two(x) (!((x) & ((x)-1)))
#define low_bit_mask(x) (((x)-1) & ~(x))
#define is_contiguous_mask(x) is_power_of_two(1 + (x) + low_bit_mask(x))
and now you have a nice and simple (and efficient) expression for whether
something is a contiguous mask of bits.
You can then make it a compile-time failure with something like
extern unsigned int this_doesnt_exist_and_wont_link;
is_contiguous_mask(x) ? (x) : this_doesnt_exist_and_wont_link;
which returns "x" if it's ok, and an unlinkable expression if it isn't.
[ Explanation, if anybody cares:
- is_power_of_two(x) is hopefully obvious to all. But anyway: the "x-1"
means that the lowest bit set will be borrowed out of, turning all bits
*below* it to 1, and leaving all bits *above* it unchanged. So when you
do "x & (x-1)" that's zero *only* if "x" itself was zero, or it was a
power of two (ie there was just a single bit set - otherwise the bits
above that bit would survive the bitwise 'and' operation, and the end
result would be non-zero.
- low_bits_mask(x) takes "x", and turns the lowest zero bits on, and
clears all other bits. It does so by again subtracting 1 (same trick as
above: the bits below the first 1-bit will become 1 through the borrow,
and the lowest bit itself will be cleared.
Doing the "& ~x" will then mask off all the higher bits if there were
any (and obviouly the lowest bit too, since that was cleared by the
"-1" when we borrowed out of it).
- "is_contiguous_mask()" basically just says: if we take the low zero
bits, and turn them into ones, and add one, the end result should then
carry out to become a power-of-two.
Example: x = 0x01c ( 0000.0001.1100 )
x - 1 = 0x01b ( 0000.0001.1011 )
~x = 0xfe3 ( 1111.1110.0011 )
low = 0x003 ( 0000.0000.0011 ) (bitwise "and")
x + low = 0x01f ( 0000.0001.1111 ) (effectively just the bitwise "or")
1+x+low = 0x020 ( 0000.0010.0000 )
and that's obviously a power of two (test with the trivial thing). ]
Thus endeth Linus' "games with bits" lecture. It was probably more than
you really wanted to know. There's a ton of games you can play with simple
"x-1" and bitmasking ops like this).
Linus
next prev parent reply other threads:[~2007-03-08 18:55 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-08 2:02 sparse using insane amounts of memory Johannes Berg
[not found] ` <1173319356.3546.54.camel-YfaajirXv214zXjbi5bjpg@public.gmane.org>
2007-03-08 16:33 ` Pavel Roskin
2007-03-08 16:45 ` Johannes Berg
2007-03-08 17:13 ` Pavel Roskin
2007-03-08 17:31 ` Chris Wedgwood
2007-03-08 17:43 ` Linus Torvalds
[not found] ` <1173372315.3248.19.camel-YfaajirXv214zXjbi5bjpg@public.gmane.org>
2007-03-08 17:34 ` Pavel Roskin
2007-03-08 17:42 ` Johannes Berg
2007-03-08 17:43 ` Johannes Berg
2007-03-08 18:08 ` Ivo van Doorn
2007-03-08 18:54 ` Linus Torvalds [this message]
[not found] ` <Pine.LNX.4.64.0703081023490.10832-5CScLwifNT1QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>
2007-03-08 19:02 ` Sam Ravnborg
2007-03-08 19:08 ` Linus Torvalds
[not found] ` <Pine.LNX.4.64.0703081104010.10832-5CScLwifNT1QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>
2007-03-10 5:05 ` Darren Jenkins
2007-03-08 19:26 ` Ivo van Doorn
2007-03-09 1:12 ` OT [Re: sparse using insane amounts of memory] Tommy Thorn
2007-03-09 2:15 ` OT David Miller
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.64.0703081023490.10832@woody.linux-foundation.org \
--to=torvalds@linux-foundation.org \
--cc=ivdoorn@gmail.com \
--cc=johannes@sipsolutions.net \
--cc=linux-sparse@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=proski@gnu.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).