Merge tag 'v4.1-rockchip-socfixes1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / lib / find_last_bit.c
CommitLineData
ab53d472
RR
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 *
2c57a0e2
YN
7 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
8 * size and improve performance, 2015.
9 *
ab53d472
RR
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#include <linux/bitops.h>
2c57a0e2 17#include <linux/bitmap.h>
8bc3bcc9 18#include <linux/export.h>
2c57a0e2 19#include <linux/kernel.h>
ab53d472 20
19de85ef
AM
21#ifndef find_last_bit
22
ab53d472
RR
23unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
24{
2c57a0e2
YN
25 if (size) {
26 unsigned long val = BITMAP_LAST_WORD_MASK(size);
27 unsigned long idx = (size-1) / BITS_PER_LONG;
ab53d472 28
2c57a0e2
YN
29 do {
30 val &= addr[idx];
31 if (val)
32 return idx * BITS_PER_LONG + __fls(val);
ab53d472 33
2c57a0e2
YN
34 val = ~0ul;
35 } while (idx--);
ab53d472 36 }
ab53d472
RR
37 return size;
38}
39EXPORT_SYMBOL(find_last_bit);
19de85ef
AM
40
41#endif