Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 1 | /* bit search implementation |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 2 | * |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 3 | * Copied from lib/find_bit.c to tools/lib/find_bit.c |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. |
| 6 | * Written by David Howells (dhowells@redhat.com) |
| 7 | * |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 8 | * Copyright (C) 2008 IBM Corporation |
| 9 | * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au> |
| 10 | * (Inspired by David Howell's find_next_bit implementation) |
| 11 | * |
| 12 | * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease |
| 13 | * size and improve performance, 2015. |
| 14 | * |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 22 | #include <linux/bitmap.h> |
| 23 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 24 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 25 | #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ |
| 26 | !defined(find_next_and_bit) |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 27 | |
| 28 | /* |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 29 | * This is a common helper function for find_next_bit, find_next_zero_bit, and |
| 30 | * find_next_and_bit. The differences are: |
| 31 | * - The "invert" argument, which is XORed with each fetched word before |
| 32 | * searching it for one bits. |
| 33 | * - The optional "addr2", which is anded with "addr1" if present. |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 34 | */ |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 35 | static inline unsigned long _find_next_bit(const unsigned long *addr1, |
| 36 | const unsigned long *addr2, unsigned long nbits, |
| 37 | unsigned long start, unsigned long invert) |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 38 | { |
| 39 | unsigned long tmp; |
| 40 | |
Matthew Wilcox | e4afd2e | 2017-02-24 15:00:58 -0800 | [diff] [blame] | 41 | if (unlikely(start >= nbits)) |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 42 | return nbits; |
| 43 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 44 | tmp = addr1[start / BITS_PER_LONG]; |
| 45 | if (addr2) |
| 46 | tmp &= addr2[start / BITS_PER_LONG]; |
| 47 | tmp ^= invert; |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 48 | |
| 49 | /* Handle 1st word. */ |
| 50 | tmp &= BITMAP_FIRST_WORD_MASK(start); |
| 51 | start = round_down(start, BITS_PER_LONG); |
| 52 | |
| 53 | while (!tmp) { |
| 54 | start += BITS_PER_LONG; |
| 55 | if (start >= nbits) |
| 56 | return nbits; |
| 57 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 58 | tmp = addr1[start / BITS_PER_LONG]; |
| 59 | if (addr2) |
| 60 | tmp &= addr2[start / BITS_PER_LONG]; |
| 61 | tmp ^= invert; |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return min(start + __ffs(tmp), nbits); |
| 65 | } |
| 66 | #endif |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 67 | |
| 68 | #ifndef find_next_bit |
| 69 | /* |
| 70 | * Find the next set bit in a memory region. |
| 71 | */ |
| 72 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
| 73 | unsigned long offset) |
| 74 | { |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 75 | return _find_next_bit(addr, NULL, size, offset, 0UL); |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 76 | } |
| 77 | #endif |
| 78 | |
| 79 | #ifndef find_first_bit |
| 80 | /* |
| 81 | * Find the first set bit in a memory region. |
| 82 | */ |
| 83 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
| 84 | { |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 85 | unsigned long idx; |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 86 | |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 87 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 88 | if (addr[idx]) |
| 89 | return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 90 | } |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 91 | |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 92 | return size; |
Arnaldo Carvalho de Melo | 23e1a35 | 2014-12-15 19:50:12 -0300 | [diff] [blame] | 93 | } |
| 94 | #endif |
Jiri Olsa | 02bc11d | 2016-10-10 09:26:33 +0200 | [diff] [blame] | 95 | |
| 96 | #ifndef find_first_zero_bit |
| 97 | /* |
| 98 | * Find the first cleared bit in a memory region. |
| 99 | */ |
| 100 | unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) |
| 101 | { |
| 102 | unsigned long idx; |
| 103 | |
| 104 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 105 | if (addr[idx] != ~0UL) |
| 106 | return min(idx * BITS_PER_LONG + ffz(addr[idx]), size); |
| 107 | } |
| 108 | |
| 109 | return size; |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | #ifndef find_next_zero_bit |
| 114 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
| 115 | unsigned long offset) |
| 116 | { |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 117 | return _find_next_bit(addr, NULL, size, offset, ~0UL); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | #ifndef find_next_and_bit |
| 122 | unsigned long find_next_and_bit(const unsigned long *addr1, |
| 123 | const unsigned long *addr2, unsigned long size, |
| 124 | unsigned long offset) |
| 125 | { |
| 126 | return _find_next_bit(addr1, addr2, size, offset, 0UL); |
Jiri Olsa | 02bc11d | 2016-10-10 09:26:33 +0200 | [diff] [blame] | 127 | } |
| 128 | #endif |