aboutsummaryrefslogtreecommitdiffstats
path: root/lib/find_last_bit.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/find_last_bit.c')
-rw-r--r--lib/find_last_bit.c36
1 files changed, 14 insertions, 22 deletions
diff --git a/lib/find_last_bit.c b/lib/find_last_bit.c
index 91ca09fbf6f9..3e3be40c6a6e 100644
--- a/lib/find_last_bit.c
+++ b/lib/find_last_bit.c
@@ -4,6 +4,9 @@
4 * Written by Rusty Russell <rusty@rustcorp.com.au> 4 * Written by Rusty Russell <rusty@rustcorp.com.au>
5 * (Inspired by David Howell's find_next_bit implementation) 5 * (Inspired by David Howell's find_next_bit implementation)
6 * 6 *
7 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
8 * size and improve performance, 2015.
9 *
7 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 12 * as published by the Free Software Foundation; either version
@@ -11,37 +14,26 @@
11 */ 14 */
12 15
13#include <linux/bitops.h> 16#include <linux/bitops.h>
17#include <linux/bitmap.h>
14#include <linux/export.h> 18#include <linux/export.h>
15#include <asm/types.h> 19#include <linux/kernel.h>
16#include <asm/byteorder.h>
17 20
18#ifndef find_last_bit 21#ifndef find_last_bit
19 22
20unsigned long find_last_bit(const unsigned long *addr, unsigned long size) 23unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
21{ 24{
22 unsigned long words; 25 if (size) {
23 unsigned long tmp; 26 unsigned long val = BITMAP_LAST_WORD_MASK(size);
24 27 unsigned long idx = (size-1) / BITS_PER_LONG;
25 /* Start at final word. */
26 words = size / BITS_PER_LONG;
27 28
28 /* Partial final word? */ 29 do {
29 if (size & (BITS_PER_LONG-1)) { 30 val &= addr[idx];
30 tmp = (addr[words] & (~0UL >> (BITS_PER_LONG 31 if (val)
31 - (size & (BITS_PER_LONG-1))))); 32 return idx * BITS_PER_LONG + __fls(val);
32 if (tmp)
33 goto found;
34 }
35 33
36 while (words) { 34 val = ~0ul;
37 tmp = addr[--words]; 35 } while (idx--);
38 if (tmp) {
39found:
40 return words * BITS_PER_LONG + __fls(tmp);
41 }
42 } 36 }
43
44 /* Not found */
45 return size; 37 return size;
46} 38}
47EXPORT_SYMBOL(find_last_bit); 39EXPORT_SYMBOL(find_last_bit);