wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 1 | #ifndef _LINUX_BITOPS_H |
| 2 | #define _LINUX_BITOPS_H |
| 3 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 4 | #include <asm/types.h> |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 5 | |
| 6 | /* |
| 7 | * ffs: find first bit set. This is defined the same way as |
| 8 | * the libc and compiler builtin ffs routines, therefore |
| 9 | * differs in spirit from the above ffz (man ffs). |
| 10 | */ |
| 11 | |
| 12 | static inline int generic_ffs(int x) |
| 13 | { |
| 14 | int r = 1; |
| 15 | |
| 16 | if (!x) |
| 17 | return 0; |
| 18 | if (!(x & 0xffff)) { |
| 19 | x >>= 16; |
| 20 | r += 16; |
| 21 | } |
| 22 | if (!(x & 0xff)) { |
| 23 | x >>= 8; |
| 24 | r += 8; |
| 25 | } |
| 26 | if (!(x & 0xf)) { |
| 27 | x >>= 4; |
| 28 | r += 4; |
| 29 | } |
| 30 | if (!(x & 3)) { |
| 31 | x >>= 2; |
| 32 | r += 2; |
| 33 | } |
| 34 | if (!(x & 1)) { |
| 35 | x >>= 1; |
| 36 | r += 1; |
| 37 | } |
| 38 | return r; |
| 39 | } |
| 40 | |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 41 | /** |
| 42 | * fls - find last (most-significant) bit set |
| 43 | * @x: the word to search |
| 44 | * |
| 45 | * This is defined the same way as ffs. |
| 46 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 47 | */ |
| 48 | static inline int generic_fls(int x) |
| 49 | { |
| 50 | int r = 32; |
| 51 | |
| 52 | if (!x) |
| 53 | return 0; |
| 54 | if (!(x & 0xffff0000u)) { |
| 55 | x <<= 16; |
| 56 | r -= 16; |
| 57 | } |
| 58 | if (!(x & 0xff000000u)) { |
| 59 | x <<= 8; |
| 60 | r -= 8; |
| 61 | } |
| 62 | if (!(x & 0xf0000000u)) { |
| 63 | x <<= 4; |
| 64 | r -= 4; |
| 65 | } |
| 66 | if (!(x & 0xc0000000u)) { |
| 67 | x <<= 2; |
| 68 | r -= 2; |
| 69 | } |
| 70 | if (!(x & 0x80000000u)) { |
| 71 | x <<= 1; |
| 72 | r -= 1; |
| 73 | } |
| 74 | return r; |
| 75 | } |
| 76 | |
| 77 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 78 | /* |
| 79 | * hweightN: returns the hamming weight (i.e. the number |
| 80 | * of bits set) of a N-bit word |
| 81 | */ |
| 82 | |
| 83 | static inline unsigned int generic_hweight32(unsigned int w) |
| 84 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 85 | unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); |
| 86 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 87 | res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); |
| 88 | res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); |
| 89 | return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline unsigned int generic_hweight16(unsigned int w) |
| 93 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 94 | unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); |
| 95 | res = (res & 0x3333) + ((res >> 2) & 0x3333); |
| 96 | res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); |
| 97 | return (res & 0x00FF) + ((res >> 8) & 0x00FF); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static inline unsigned int generic_hweight8(unsigned int w) |
| 101 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 102 | unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); |
| 103 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 104 | return (res & 0x0F) + ((res >> 4) & 0x0F); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 107 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
| 108 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
| 109 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 110 | #include <asm/bitops.h> |
| 111 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 112 | /* linux/include/asm-generic/bitops/non-atomic.h */ |
| 113 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 114 | #ifndef PLATFORM__SET_BIT |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 115 | # define __set_bit generic_set_bit |
| 116 | #endif |
| 117 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 118 | #ifndef PLATFORM__CLEAR_BIT |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 119 | # define __clear_bit generic_clear_bit |
| 120 | #endif |
| 121 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 122 | #ifndef PLATFORM_FFS |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 123 | # define ffs generic_ffs |
| 124 | #endif |
| 125 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 126 | #ifndef PLATFORM_FLS |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 127 | # define fls generic_fls |
| 128 | #endif |
| 129 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 130 | /** |
| 131 | * __set_bit - Set a bit in memory |
| 132 | * @nr: the bit to set |
| 133 | * @addr: the address to start counting from |
| 134 | * |
| 135 | * Unlike set_bit(), this function is non-atomic and may be reordered. |
| 136 | * If it's called on the same region of memory simultaneously, the effect |
| 137 | * may be that only one operation succeeds. |
| 138 | */ |
| 139 | static inline void generic_set_bit(int nr, volatile unsigned long *addr) |
| 140 | { |
| 141 | unsigned long mask = BIT_MASK(nr); |
| 142 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 143 | |
| 144 | *p |= mask; |
| 145 | } |
| 146 | |
| 147 | static inline void generic_clear_bit(int nr, volatile unsigned long *addr) |
| 148 | { |
| 149 | unsigned long mask = BIT_MASK(nr); |
| 150 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 151 | |
| 152 | *p &= ~mask; |
| 153 | } |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 154 | |
| 155 | #endif |