Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 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 | * |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 7 | * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease |
| 8 | * size and improve performance, 2015. |
| 9 | * |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 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> |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 17 | #include <linux/bitmap.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 20 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 21 | #ifndef find_last_bit |
| 22 | |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 23 | unsigned long find_last_bit(const unsigned long *addr, unsigned long size) |
| 24 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 25 | if (size) { |
| 26 | unsigned long val = BITMAP_LAST_WORD_MASK(size); |
| 27 | unsigned long idx = (size-1) / BITS_PER_LONG; |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 28 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 29 | do { |
| 30 | val &= addr[idx]; |
| 31 | if (val) |
| 32 | return idx * BITS_PER_LONG + __fls(val); |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 33 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 34 | val = ~0ul; |
| 35 | } while (idx--); |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 36 | } |
Rusty Russell | ab53d47 | 2009-01-01 10:12:19 +1030 | [diff] [blame] | 37 | return size; |
| 38 | } |
| 39 | EXPORT_SYMBOL(find_last_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 40 | |
| 41 | #endif |