Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 1 | #ifndef _PERF_LINUX_BITOPS_H_ |
| 2 | #define _PERF_LINUX_BITOPS_H_ |
| 3 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | a860a60 | 2011-01-22 19:07:36 -0200 | [diff] [blame] | 5 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 6 | #include <asm/hweight.h> |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 7 | |
Irina Tirdea | 3f34f6c | 2012-09-11 01:15:00 +0300 | [diff] [blame] | 8 | #ifndef __WORDSIZE |
| 9 | #define __WORDSIZE (__SIZEOF_LONG__ * 8) |
| 10 | #endif |
| 11 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 12 | #define BITS_PER_LONG __WORDSIZE |
| 13 | #define BITS_PER_BYTE 8 |
| 14 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 15 | #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64)) |
| 16 | #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32)) |
Sukadev Bhattiprolu | 1526813 | 2013-01-17 09:11:30 -0800 | [diff] [blame^] | 17 | #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE) |
Paul Mackerras | ee3d250 | 2009-11-24 15:19:43 +1100 | [diff] [blame] | 18 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 19 | #define for_each_set_bit(bit, addr, size) \ |
| 20 | for ((bit) = find_first_bit((addr), (size)); \ |
| 21 | (bit) < (size); \ |
| 22 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
| 23 | |
| 24 | /* same as for_each_set_bit() but use bit as value to start with */ |
Akinobu Mita | 307b1cd | 2012-03-23 15:02:03 -0700 | [diff] [blame] | 25 | #define for_each_set_bit_from(bit, addr, size) \ |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 26 | for ((bit) = find_next_bit((addr), (size), (bit)); \ |
| 27 | (bit) < (size); \ |
| 28 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
| 29 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 30 | static inline void set_bit(int nr, unsigned long *addr) |
| 31 | { |
| 32 | addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG); |
| 33 | } |
| 34 | |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 35 | static inline void clear_bit(int nr, unsigned long *addr) |
| 36 | { |
| 37 | addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG)); |
| 38 | } |
| 39 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 40 | static __always_inline int test_bit(unsigned int nr, const unsigned long *addr) |
| 41 | { |
| 42 | return ((1UL << (nr % BITS_PER_LONG)) & |
| 43 | (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; |
| 44 | } |
| 45 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 46 | static inline unsigned long hweight_long(unsigned long w) |
| 47 | { |
| 48 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
| 49 | } |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 50 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 51 | #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) |
| 52 | |
| 53 | /** |
| 54 | * __ffs - find first bit in word. |
| 55 | * @word: The word to search |
| 56 | * |
| 57 | * Undefined if no bit exists, so code should check against 0 first. |
| 58 | */ |
| 59 | static __always_inline unsigned long __ffs(unsigned long word) |
| 60 | { |
| 61 | int num = 0; |
| 62 | |
| 63 | #if BITS_PER_LONG == 64 |
| 64 | if ((word & 0xffffffff) == 0) { |
| 65 | num += 32; |
| 66 | word >>= 32; |
| 67 | } |
| 68 | #endif |
| 69 | if ((word & 0xffff) == 0) { |
| 70 | num += 16; |
| 71 | word >>= 16; |
| 72 | } |
| 73 | if ((word & 0xff) == 0) { |
| 74 | num += 8; |
| 75 | word >>= 8; |
| 76 | } |
| 77 | if ((word & 0xf) == 0) { |
| 78 | num += 4; |
| 79 | word >>= 4; |
| 80 | } |
| 81 | if ((word & 0x3) == 0) { |
| 82 | num += 2; |
| 83 | word >>= 2; |
| 84 | } |
| 85 | if ((word & 0x1) == 0) |
| 86 | num += 1; |
| 87 | return num; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Find the first set bit in a memory region. |
| 92 | */ |
| 93 | static inline unsigned long |
| 94 | find_first_bit(const unsigned long *addr, unsigned long size) |
| 95 | { |
| 96 | const unsigned long *p = addr; |
| 97 | unsigned long result = 0; |
| 98 | unsigned long tmp; |
| 99 | |
| 100 | while (size & ~(BITS_PER_LONG-1)) { |
| 101 | if ((tmp = *(p++))) |
| 102 | goto found; |
| 103 | result += BITS_PER_LONG; |
| 104 | size -= BITS_PER_LONG; |
| 105 | } |
| 106 | if (!size) |
| 107 | return result; |
| 108 | |
| 109 | tmp = (*p) & (~0UL >> (BITS_PER_LONG - size)); |
| 110 | if (tmp == 0UL) /* Are any bits set? */ |
| 111 | return result + size; /* Nope. */ |
| 112 | found: |
| 113 | return result + __ffs(tmp); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Find the next set bit in a memory region. |
| 118 | */ |
| 119 | static inline unsigned long |
| 120 | find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset) |
| 121 | { |
| 122 | const unsigned long *p = addr + BITOP_WORD(offset); |
| 123 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
| 124 | unsigned long tmp; |
| 125 | |
| 126 | if (offset >= size) |
| 127 | return size; |
| 128 | size -= result; |
| 129 | offset %= BITS_PER_LONG; |
| 130 | if (offset) { |
| 131 | tmp = *(p++); |
| 132 | tmp &= (~0UL << offset); |
| 133 | if (size < BITS_PER_LONG) |
| 134 | goto found_first; |
| 135 | if (tmp) |
| 136 | goto found_middle; |
| 137 | size -= BITS_PER_LONG; |
| 138 | result += BITS_PER_LONG; |
| 139 | } |
| 140 | while (size & ~(BITS_PER_LONG-1)) { |
| 141 | if ((tmp = *(p++))) |
| 142 | goto found_middle; |
| 143 | result += BITS_PER_LONG; |
| 144 | size -= BITS_PER_LONG; |
| 145 | } |
| 146 | if (!size) |
| 147 | return result; |
| 148 | tmp = *p; |
| 149 | |
| 150 | found_first: |
| 151 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
| 152 | if (tmp == 0UL) /* Are any bits set? */ |
| 153 | return result + size; /* Nope. */ |
| 154 | found_middle: |
| 155 | return result + __ffs(tmp); |
| 156 | } |
| 157 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 158 | #endif |