[32] | 1 | #ifndef _I386_BITOPS_H
|
---|
| 2 | #define _I386_BITOPS_H
|
---|
| 3 |
|
---|
[679] | 4 | #include <linux/types.h>
|
---|
| 5 | #include <asm/bitops.h>
|
---|
| 6 |
|
---|
[32] | 7 | /*
|
---|
| 8 | * Copyright 1992, Linus Torvalds.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
[679] | 11 |
|
---|
[32] | 12 | /*
|
---|
| 13 | * These have to be done with inline assembly: that way the bit-setting
|
---|
| 14 | * is guaranteed to be atomic. All bit operations return 0 if the bit
|
---|
| 15 | * was cleared before the operation and != 0 if it was not.
|
---|
| 16 | *
|
---|
| 17 | * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /*
|
---|
| 21 | * Function prototypes to keep gcc -Wall happy
|
---|
| 22 | */
|
---|
| 23 | #include <asm\bitops.h>
|
---|
[441] | 24 | #define smp_mb__after_clear_bit()
|
---|
[679] | 25 | unsigned int hweight32(unsigned int w);
|
---|
| 26 | /**
|
---|
| 27 | * find_first_bit - find the first set bit in a memory region
|
---|
| 28 | * @addr: The address to start the search at
|
---|
| 29 | * @size: The maximum number of bits to search
|
---|
| 30 | *
|
---|
| 31 | * Returns the bit number of the first set bit.
|
---|
| 32 | * If no bits are set, returns @size.
|
---|
| 33 | */
|
---|
| 34 | extern unsigned long find_first_bit(const unsigned long *addr,
|
---|
| 35 | unsigned long size);
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * __ffs - find first bit in word.
|
---|
| 40 | * @word: The word to search
|
---|
| 41 | *
|
---|
| 42 | * Undefined if no bit exists, so code should check against 0 first.
|
---|
| 43 | */
|
---|
| 44 | static inline unsigned long __ffs(unsigned long word)
|
---|
| 45 | {
|
---|
| 46 | int num = 0;
|
---|
| 47 |
|
---|
| 48 | #if BITS_PER_LONG == 64
|
---|
| 49 | if ((word & 0xffffffff) == 0) {
|
---|
| 50 | num += 32;
|
---|
| 51 | word >>= 32;
|
---|
| 52 | }
|
---|
| 53 | #endif
|
---|
| 54 | if ((word & 0xffff) == 0) {
|
---|
| 55 | num += 16;
|
---|
| 56 | word >>= 16;
|
---|
| 57 | }
|
---|
| 58 | if ((word & 0xff) == 0) {
|
---|
| 59 | num += 8;
|
---|
| 60 | word >>= 8;
|
---|
| 61 | }
|
---|
| 62 | if ((word & 0xf) == 0) {
|
---|
| 63 | num += 4;
|
---|
| 64 | word >>= 4;
|
---|
| 65 | }
|
---|
| 66 | if ((word & 0x3) == 0) {
|
---|
| 67 | num += 2;
|
---|
| 68 | word >>= 2;
|
---|
| 69 | }
|
---|
| 70 | if ((word & 0x1) == 0)
|
---|
| 71 | num += 1;
|
---|
| 72 | return num;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * __fls - find last (most-significant) set bit in a long word
|
---|
| 77 | * @word: the word to search
|
---|
| 78 | *
|
---|
| 79 | * Undefined if no set bit exists, so code should check against 0 first.
|
---|
| 80 | */
|
---|
| 81 | static inline unsigned long __fls(unsigned long word)
|
---|
| 82 | {
|
---|
| 83 | int num = BITS_PER_LONG - 1;
|
---|
| 84 |
|
---|
| 85 | #if BITS_PER_LONG == 64
|
---|
| 86 | if (!(word & (~0ul << 32))) {
|
---|
| 87 | num -= 32;
|
---|
| 88 | word <<= 32;
|
---|
| 89 | }
|
---|
| 90 | #endif
|
---|
| 91 | if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
|
---|
| 92 | num -= 16;
|
---|
| 93 | word <<= 16;
|
---|
| 94 | }
|
---|
| 95 | if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
|
---|
| 96 | num -= 8;
|
---|
| 97 | word <<= 8;
|
---|
| 98 | }
|
---|
| 99 | if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
|
---|
| 100 | num -= 4;
|
---|
| 101 | word <<= 4;
|
---|
| 102 | }
|
---|
| 103 | if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
|
---|
| 104 | num -= 2;
|
---|
| 105 | word <<= 2;
|
---|
| 106 | }
|
---|
| 107 | if (!(word & (~0ul << (BITS_PER_LONG-1))))
|
---|
| 108 | num -= 1;
|
---|
| 109 | return num;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /* same as for_each_set_bit() but use bit as value to start with */
|
---|
| 113 | #define for_each_set_bit_from(bit, addr, size) \
|
---|
| 114 | for ((bit) = find_next_bit((addr), (size), (bit)); \
|
---|
| 115 | (bit) < (size); \
|
---|
| 116 | (bit) = find_next_bit((addr), (size), (bit) + 1))
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * fls - find last (most-significant) bit set
|
---|
| 120 | * @x: the word to search
|
---|
| 121 | *
|
---|
| 122 | * This is defined the same way as ffs.
|
---|
| 123 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
|
---|
| 124 | */
|
---|
| 125 |
|
---|
| 126 | static inline int fls(int x)
|
---|
| 127 | {
|
---|
| 128 | int r = 32;
|
---|
| 129 |
|
---|
| 130 | if (!x)
|
---|
| 131 | return 0;
|
---|
| 132 | if (!(x & 0xffff0000u)) {
|
---|
| 133 | x <<= 16;
|
---|
| 134 | r -= 16;
|
---|
| 135 | }
|
---|
| 136 | if (!(x & 0xff000000u)) {
|
---|
| 137 | x <<= 8;
|
---|
| 138 | r -= 8;
|
---|
| 139 | }
|
---|
| 140 | if (!(x & 0xf0000000u)) {
|
---|
| 141 | x <<= 4;
|
---|
| 142 | r -= 4;
|
---|
| 143 | }
|
---|
| 144 | if (!(x & 0xc0000000u)) {
|
---|
| 145 | x <<= 2;
|
---|
| 146 | r -= 2;
|
---|
| 147 | }
|
---|
| 148 | if (!(x & 0x80000000u)) {
|
---|
| 149 | x <<= 1;
|
---|
| 150 | r -= 1;
|
---|
| 151 | }
|
---|
| 152 | return r;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | #define BIT(nr) (1UL << (nr))
|
---|
| 156 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
|
---|
| 157 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
---|
| 158 | #define BITS_PER_BYTE 8
|
---|
| 159 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * Create a contiguous bitmask starting at bit position @l and ending at
|
---|
| 163 | * position @h. For example
|
---|
| 164 | * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
|
---|
| 165 | */
|
---|
| 166 | #define GENMASK(h, l) \
|
---|
| 167 | (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
|
---|
| 168 |
|
---|
| 169 | #define GENMASK_ULL(h, l) \
|
---|
| 170 | (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
|
---|
| 171 |
|
---|
| 172 | #define for_each_set_bit(bit, addr, size) \
|
---|
| 173 | for ((bit) = find_first_bit((addr), (size)); \
|
---|
| 174 | (bit) < (size); \
|
---|
| 175 | (bit) = find_next_bit((addr), (size), (bit) + 1))
|
---|
| 176 |
|
---|
| 177 | static inline int get_bitmask_order(unsigned int count)
|
---|
| 178 | {
|
---|
| 179 | int order;
|
---|
| 180 |
|
---|
| 181 | order = fls(count);
|
---|
| 182 | return order; /* We could be slightly more clever with -1 here... */
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static inline int get_count_order(unsigned int count)
|
---|
| 186 | {
|
---|
| 187 | int order;
|
---|
| 188 |
|
---|
| 189 | order = fls(count) - 1;
|
---|
| 190 | if (count & (count - 1))
|
---|
| 191 | order++;
|
---|
| 192 | return order;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | static inline unsigned fls_long(unsigned long l)
|
---|
| 196 | {
|
---|
| 197 | return fls(l);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[709] | 200 | static inline unsigned long hweight_long(unsigned long w)
|
---|
| 201 | {
|
---|
| 202 | return hweight32(w);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[32] | 205 | #endif /* _I386_BITOPS_H */
|
---|