Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6
[sfrench/cifs-2.6.git] / lib / find_last_bit.c
1 /* find_last_bit.c: fallback find next bit implementation
2  *
3  * Copyright (C) 2008 IBM Corporation
4  * Written by Rusty Russell <rusty@rustcorp.com.au>
5  * (Inspired by David Howell's find_next_bit implementation)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/bitops.h>
14 #include <linux/module.h>
15 #include <asm/types.h>
16 #include <asm/byteorder.h>
17
18 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
19 {
20         unsigned long words;
21         unsigned long tmp;
22
23         /* Start at final word. */
24         words = size / BITS_PER_LONG;
25
26         /* Partial final word? */
27         if (size & (BITS_PER_LONG-1)) {
28                 tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
29                                          - (size & (BITS_PER_LONG-1)))));
30                 if (tmp)
31                         goto found;
32         }
33
34         while (words) {
35                 tmp = addr[--words];
36                 if (tmp) {
37 found:
38                         return words * BITS_PER_LONG + __fls(tmp);
39                 }
40         }
41
42         /* Not found */
43         return size;
44 }
45 EXPORT_SYMBOL(find_last_bit);