diff options
author | Rasmus Villemoes <linux@rasmusvillemoes.dk> | 2015-02-12 18:02:10 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-02-12 21:54:14 -0500 |
commit | f6a1f5db8d7a7a94ff07251996959d27daba4ee7 (patch) | |
tree | 60e8d219664966cab66732850ac0e40f9aa5dc5e /lib/bitmap.c | |
parent | df1d80a9eb16d98002673f68a7ebbe881f6e6946 (diff) |
lib/bitmap.c: simplify bitmap_ord_to_pos
Make the return value and the ord and nbits parameters of
bitmap_ord_to_pos unsigned.
Also, simplify the implementation and as a side effect make the result
fully defined, returning nbits for ord >= weight, in analogy with what
find_{first,next}_bit does. This is a better sentinel than the former
("unofficial") 0. No current users are affected by this change.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/bitmap.c')
-rw-r--r-- | lib/bitmap.c | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/lib/bitmap.c b/lib/bitmap.c index 84d20b5c6bf1..e8a38bde7af9 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c | |||
@@ -771,34 +771,28 @@ static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigne | |||
771 | * bitmap_ord_to_pos - find position of n-th set bit in bitmap | 771 | * bitmap_ord_to_pos - find position of n-th set bit in bitmap |
772 | * @buf: pointer to bitmap | 772 | * @buf: pointer to bitmap |
773 | * @ord: ordinal bit position (n-th set bit, n >= 0) | 773 | * @ord: ordinal bit position (n-th set bit, n >= 0) |
774 | * @bits: number of valid bit positions in @buf | 774 | * @nbits: number of valid bit positions in @buf |
775 | * | 775 | * |
776 | * Map the ordinal offset of bit @ord in @buf to its position in @buf. | 776 | * Map the ordinal offset of bit @ord in @buf to its position in @buf. |
777 | * Value of @ord should be in range 0 <= @ord < weight(buf), else | 777 | * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord |
778 | * results are undefined. | 778 | * >= weight(buf), returns @nbits. |
779 | * | 779 | * |
780 | * If for example, just bits 4 through 7 are set in @buf, then @ord | 780 | * If for example, just bits 4 through 7 are set in @buf, then @ord |
781 | * values 0 through 3 will get mapped to 4 through 7, respectively, | 781 | * values 0 through 3 will get mapped to 4 through 7, respectively, |
782 | * and all other @ord values return undefined values. When @ord value 3 | 782 | * and all other @ord values returns @nbits. When @ord value 3 |
783 | * gets mapped to (returns) @pos value 7 in this example, that means | 783 | * gets mapped to (returns) @pos value 7 in this example, that means |
784 | * that the 3rd set bit (starting with 0th) is at position 7 in @buf. | 784 | * that the 3rd set bit (starting with 0th) is at position 7 in @buf. |
785 | * | 785 | * |
786 | * The bit positions 0 through @bits are valid positions in @buf. | 786 | * The bit positions 0 through @nbits-1 are valid positions in @buf. |
787 | */ | 787 | */ |
788 | int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits) | 788 | unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits) |
789 | { | 789 | { |
790 | int pos = 0; | 790 | unsigned int pos; |
791 | 791 | ||
792 | if (ord >= 0 && ord < bits) { | 792 | for (pos = find_first_bit(buf, nbits); |
793 | int i; | 793 | pos < nbits && ord; |
794 | 794 | pos = find_next_bit(buf, nbits, pos + 1)) | |
795 | for (i = find_first_bit(buf, bits); | 795 | ord--; |
796 | i < bits && ord > 0; | ||
797 | i = find_next_bit(buf, bits, i + 1)) | ||
798 | ord--; | ||
799 | if (i < bits && ord == 0) | ||
800 | pos = i; | ||
801 | } | ||
802 | 796 | ||
803 | return pos; | 797 | return pos; |
804 | } | 798 | } |