blob: 3e3be40c6a6e27fb702c244d21386edcc2ff3a66 [file] [log] [blame]
Rusty Russellab53d472009-01-01 10:12:19 +10301/* 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 *
Yury Norov2c57a0e2015-04-16 12:43:13 -07007 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
8 * size and improve performance, 2015.
9 *
Rusty Russellab53d472009-01-01 10:12:19 +103010 * 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>
Yury Norov2c57a0e2015-04-16 12:43:13 -070017#include <linux/bitmap.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050018#include <linux/export.h>
Yury Norov2c57a0e2015-04-16 12:43:13 -070019#include <linux/kernel.h>
Rusty Russellab53d472009-01-01 10:12:19 +103020
Akinobu Mita19de85e2011-05-26 16:26:09 -070021#ifndef find_last_bit
22
Rusty Russellab53d472009-01-01 10:12:19 +103023unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
24{
Yury Norov2c57a0e2015-04-16 12:43:13 -070025 if (size) {
26 unsigned long val = BITMAP_LAST_WORD_MASK(size);
27 unsigned long idx = (size-1) / BITS_PER_LONG;
Rusty Russellab53d472009-01-01 10:12:19 +103028
Yury Norov2c57a0e2015-04-16 12:43:13 -070029 do {
30 val &= addr[idx];
31 if (val)
32 return idx * BITS_PER_LONG + __fls(val);
Rusty Russellab53d472009-01-01 10:12:19 +103033
Yury Norov2c57a0e2015-04-16 12:43:13 -070034 val = ~0ul;
35 } while (idx--);
Rusty Russellab53d472009-01-01 10:12:19 +103036 }
Rusty Russellab53d472009-01-01 10:12:19 +103037 return size;
38}
39EXPORT_SYMBOL(find_last_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070040
41#endif