All of lore.kernel.org
 help / color / mirror / Atom feed
From: Benny Halevy <bhalevy@panasas.com>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] bitmap: Cleanup find_last_bit
Date: Tue, 17 Feb 2009 06:44:54 +0200	[thread overview]
Message-ID: <499A40C6.9070800@panasas.com> (raw)
In-Reply-To: <200902170940.59703.rusty@rustcorp.com.au>

On Feb. 17, 2009, 1:10 +0200, Rusty Russell <rusty@rustcorp.com.au> wrote:
> On Monday 16 February 2009 23:28:40 Benny Halevy wrote:
>> Fix cut & paste error in header comment.
>> Simplify implementation a bit.
> 
> Hi Benny,
> 
>   Nice catch!  But I disagree with one change:
> 
>>  	/* Partial final word? */
>> -	if (size & (BITS_PER_LONG-1)) {
>> -		tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
>> -					 - (size & (BITS_PER_LONG-1)))));
>> +	tmp = size & (BITS_PER_LONG-1);
>> +	if (tmp) {
>> +		tmp = addr[words] & (~0UL >> (BITS_PER_LONG - tmp));
>>  		if (tmp)
>>  			goto found;
>>  	}
> 
> The overloading of tmp to the remainder size here is not really a cleanup: it
> makes it into a dual-use var.  I know it's called tmp, but that's a sign of
> poor code too :)
> 
> I'd prefer a new final_bits var: gcc will almost certainly just slap it in
> a register anyway, but it's clearer.
> 
> tmp could then be called something like... word?

Yeah, I like that too.
I'm about to go on a flight
so the following was just tested to compile...

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 6182913..886ebf0 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -142,7 +142,7 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr,
  * @addr: The address to start the search at
  * @size: The maximum size to search
  *
- * Returns the bit number of the first set bit, or size.
+ * Returns the bit number of the last set bit, or size.
  */
 extern unsigned long find_last_bit(const unsigned long *addr,
 				   unsigned long size);
diff --git a/lib/find_last_bit.c b/lib/find_last_bit.c
index 5d202e3..6a35783 100644
--- a/lib/find_last_bit.c
+++ b/lib/find_last_bit.c
@@ -17,29 +17,31 @@
 
 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
 {
+	unsigned long final_bits = size;
 	unsigned long words;
-	unsigned long tmp;
+	unsigned long word;
 
 	/* Start at final word. */
-	words = size / BITS_PER_LONG;
+	words = final_bits / BITS_PER_LONG;
 
 	/* Partial final word? */
-	if (size & (BITS_PER_LONG-1)) {
-		tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
-					 - (size & (BITS_PER_LONG-1)))));
-		if (tmp)
+	size &= (BITS_PER_LONG-1);
+	if (size) {
+		word = addr[words] & (~0UL >> (BITS_PER_LONG - size));
+		if (word)
 			goto found;
 	}
 
 	while (words) {
-		tmp = addr[--words];
-		if (tmp) {
-found:
-			return words * BITS_PER_LONG + __fls(tmp);
-		}
+		word = addr[--words];
+		if (word)
+			goto found;
 	}
 
 	/* Not found */
-	return size;
+	return final_bits;
+
+found:
+	return words * BITS_PER_LONG + __fls(word);
 }
 EXPORT_SYMBOL(find_last_bit);
-- 
1.6.1.3


> 
> Thanks!
> Rusty.

      reply	other threads:[~2009-02-17  4:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-01  8:18 [PATCH 5/6] bitmap: find_last_bit() Rusty Russell
2009-02-16 12:58 ` [PATCH] bitmap: Cleanup find_last_bit Benny Halevy
2009-02-16 23:10   ` Rusty Russell
2009-02-17  4:44     ` Benny Halevy [this message]

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=499A40C6.9070800@panasas.com \
    --to=bhalevy@panasas.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    /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.