Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _PARISC_BITOPS_H |
| 2 | #define _PARISC_BITOPS_H |
| 3 | |
| 4 | #include <linux/compiler.h> |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 5 | #include <asm/types.h> /* for BITS_PER_LONG/SHIFT_PER_LONG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <asm/byteorder.h> |
| 7 | #include <asm/atomic.h> |
| 8 | |
| 9 | /* |
| 10 | * HP-PARISC specific bit operations |
| 11 | * for a detailed description of the functions please refer |
| 12 | * to include/asm-i386/bitops.h or kerneldoc |
| 13 | */ |
| 14 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 15 | #define CHOP_SHIFTCOUNT(x) (((unsigned long) (x)) & (BITS_PER_LONG - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | |
| 18 | #define smp_mb__before_clear_bit() smp_mb() |
| 19 | #define smp_mb__after_clear_bit() smp_mb() |
| 20 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 21 | /* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion |
| 22 | * on use of volatile and __*_bit() (set/clear/change): |
| 23 | * *_bit() want use of volatile. |
| 24 | * __*_bit() are "relaxed" and don't use spinlock or volatile. |
| 25 | */ |
| 26 | |
| 27 | static __inline__ void set_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 29 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | unsigned long flags; |
| 31 | |
| 32 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | _atomic_spin_lock_irqsave(addr, flags); |
| 34 | *addr |= mask; |
| 35 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 36 | } |
| 37 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 38 | static __inline__ void __set_bit(unsigned long nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 40 | unsigned long *m = (unsigned long *) addr + (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 42 | *m |= 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 45 | static __inline__ void clear_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 47 | unsigned long mask = ~(1UL << CHOP_SHIFTCOUNT(nr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unsigned long flags; |
| 49 | |
| 50 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 52 | *addr &= mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 54 | } |
| 55 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 56 | static __inline__ void __clear_bit(unsigned long nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 58 | unsigned long *m = (unsigned long *) addr + (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 60 | *m &= ~(1UL << CHOP_SHIFTCOUNT(nr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 63 | static __inline__ void change_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 65 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | unsigned long flags; |
| 67 | |
| 68 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | _atomic_spin_lock_irqsave(addr, flags); |
| 70 | *addr ^= mask; |
| 71 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 72 | } |
| 73 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 74 | static __inline__ void __change_bit(unsigned long nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 76 | unsigned long *m = (unsigned long *) addr + (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 78 | *m ^= 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 81 | static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 83 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 84 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | unsigned long flags; |
| 86 | |
| 87 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 89 | oldbit = *addr; |
| 90 | *addr = oldbit | mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 92 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 93 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static __inline__ int __test_and_set_bit(int nr, volatile unsigned long * address) |
| 97 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 98 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 99 | unsigned long oldbit; |
| 100 | unsigned long *addr = (unsigned long *)address + (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 102 | oldbit = *addr; |
| 103 | *addr = oldbit | mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 105 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 108 | static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 110 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 111 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | unsigned long flags; |
| 113 | |
| 114 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 116 | oldbit = *addr; |
| 117 | *addr = oldbit & ~mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 119 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 120 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static __inline__ int __test_and_clear_bit(int nr, volatile unsigned long * address) |
| 124 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 125 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 126 | unsigned long *addr = (unsigned long *)address + (nr >> SHIFT_PER_LONG); |
| 127 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 129 | oldbit = *addr; |
| 130 | *addr = oldbit & ~mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 132 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 135 | static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 137 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 138 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | unsigned long flags; |
| 140 | |
| 141 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 143 | oldbit = *addr; |
| 144 | *addr = oldbit ^ mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 146 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 147 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static __inline__ int __test_and_change_bit(int nr, volatile unsigned long * address) |
| 151 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 152 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 153 | unsigned long *addr = (unsigned long *)address + (nr >> SHIFT_PER_LONG); |
| 154 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 156 | oldbit = *addr; |
| 157 | *addr = oldbit ^ mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 159 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static __inline__ int test_bit(int nr, const volatile unsigned long *address) |
| 163 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 164 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 165 | const unsigned long *addr = (const unsigned long *)address + (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | return !!(*addr & mask); |
| 168 | } |
| 169 | |
| 170 | #ifdef __KERNEL__ |
| 171 | |
| 172 | /** |
| 173 | * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1". |
| 174 | * @word: The word to search |
| 175 | * |
| 176 | * __ffs() return is undefined if no bit is set. |
| 177 | * |
| 178 | * 32-bit fast __ffs by LaMont Jones "lamont At hp com". |
| 179 | * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org". |
| 180 | * (with help from willy/jejb to get the semantics right) |
| 181 | * |
| 182 | * This algorithm avoids branches by making use of nullification. |
| 183 | * One side effect of "extr" instructions is it sets PSW[N] bit. |
| 184 | * How PSW[N] (nullify next insn) gets set is determined by the |
| 185 | * "condition" field (eg "<>" or "TR" below) in the extr* insn. |
| 186 | * Only the 1st and one of either the 2cd or 3rd insn will get executed. |
| 187 | * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so |
| 188 | * cycles for each mispredicted branch. |
| 189 | */ |
| 190 | |
| 191 | static __inline__ unsigned long __ffs(unsigned long x) |
| 192 | { |
| 193 | unsigned long ret; |
| 194 | |
| 195 | __asm__( |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 196 | #ifdef __LP64__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | " ldi 63,%1\n" |
| 198 | " extrd,u,*<> %0,63,32,%%r0\n" |
| 199 | " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */ |
| 200 | " addi -32,%1,%1\n" |
| 201 | #else |
| 202 | " ldi 31,%1\n" |
| 203 | #endif |
| 204 | " extru,<> %0,31,16,%%r0\n" |
| 205 | " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */ |
| 206 | " addi -16,%1,%1\n" |
| 207 | " extru,<> %0,31,8,%%r0\n" |
| 208 | " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */ |
| 209 | " addi -8,%1,%1\n" |
| 210 | " extru,<> %0,31,4,%%r0\n" |
| 211 | " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */ |
| 212 | " addi -4,%1,%1\n" |
| 213 | " extru,<> %0,31,2,%%r0\n" |
| 214 | " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */ |
| 215 | " addi -2,%1,%1\n" |
| 216 | " extru,= %0,31,1,%%r0\n" /* check last bit */ |
| 217 | " addi -1,%1,%1\n" |
| 218 | : "+r" (x), "=r" (ret) ); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | /* Undefined if no bit is zero. */ |
| 223 | #define ffz(x) __ffs(~x) |
| 224 | |
| 225 | /* |
| 226 | * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set) |
| 227 | * This is defined the same way as the libc and compiler builtin |
| 228 | * ffs routines, therefore differs in spirit from the above ffz (man ffs). |
| 229 | */ |
| 230 | static __inline__ int ffs(int x) |
| 231 | { |
| 232 | return x ? (__ffs((unsigned long)x) + 1) : 0; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * fls: find last (most significant) bit set. |
| 237 | * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 238 | */ |
| 239 | |
| 240 | static __inline__ int fls(int x) |
| 241 | { |
| 242 | int ret; |
| 243 | if (!x) |
| 244 | return 0; |
| 245 | |
| 246 | __asm__( |
| 247 | " ldi 1,%1\n" |
| 248 | " extru,<> %0,15,16,%%r0\n" |
| 249 | " zdep,TR %0,15,16,%0\n" /* xxxx0000 */ |
| 250 | " addi 16,%1,%1\n" |
| 251 | " extru,<> %0,7,8,%%r0\n" |
| 252 | " zdep,TR %0,23,24,%0\n" /* xx000000 */ |
| 253 | " addi 8,%1,%1\n" |
| 254 | " extru,<> %0,3,4,%%r0\n" |
| 255 | " zdep,TR %0,27,28,%0\n" /* x0000000 */ |
| 256 | " addi 4,%1,%1\n" |
| 257 | " extru,<> %0,1,2,%%r0\n" |
| 258 | " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */ |
| 259 | " addi 2,%1,%1\n" |
| 260 | " extru,= %0,0,1,%%r0\n" |
| 261 | " addi 1,%1,%1\n" /* if y & 8, add 1 */ |
| 262 | : "+r" (x), "=r" (ret) ); |
| 263 | |
| 264 | return ret; |
| 265 | } |
Stephen Hemminger | 3821af2 | 2005-12-21 19:30:53 -0800 | [diff] [blame] | 266 | #define fls64(x) generic_fls64(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * hweightN: returns the hamming weight (i.e. the number |
| 270 | * of bits set) of a N-bit word |
| 271 | */ |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 272 | #define hweight64(x) generic_hweight64(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | #define hweight32(x) generic_hweight32(x) |
| 274 | #define hweight16(x) generic_hweight16(x) |
| 275 | #define hweight8(x) generic_hweight8(x) |
| 276 | |
| 277 | /* |
| 278 | * Every architecture must define this function. It's the fastest |
| 279 | * way of searching a 140-bit bitmap where the first 100 bits are |
| 280 | * unlikely to be set. It's guaranteed that at least one of the 140 |
| 281 | * bits is cleared. |
| 282 | */ |
| 283 | static inline int sched_find_first_bit(const unsigned long *b) |
| 284 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 285 | #ifdef __LP64__ |
| 286 | if (unlikely(b[0])) |
| 287 | return __ffs(b[0]); |
| 288 | if (unlikely(b[1])) |
| 289 | return __ffs(b[1]) + 64; |
| 290 | return __ffs(b[2]) + 128; |
| 291 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (unlikely(b[0])) |
| 293 | return __ffs(b[0]); |
| 294 | if (unlikely(b[1])) |
| 295 | return __ffs(b[1]) + 32; |
| 296 | if (unlikely(b[2])) |
| 297 | return __ffs(b[2]) + 64; |
| 298 | if (b[3]) |
| 299 | return __ffs(b[3]) + 96; |
| 300 | return __ffs(b[4]) + 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | #endif |
| 302 | } |
| 303 | |
| 304 | #endif /* __KERNEL__ */ |
| 305 | |
| 306 | /* |
| 307 | * This implementation of find_{first,next}_zero_bit was stolen from |
| 308 | * Linus' asm-alpha/bitops.h. |
| 309 | */ |
| 310 | #define find_first_zero_bit(addr, size) \ |
| 311 | find_next_zero_bit((addr), (size), 0) |
| 312 | |
| 313 | static __inline__ unsigned long find_next_zero_bit(const void * addr, unsigned long size, unsigned long offset) |
| 314 | { |
| 315 | const unsigned long * p = ((unsigned long *) addr) + (offset >> SHIFT_PER_LONG); |
| 316 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
| 317 | unsigned long tmp; |
| 318 | |
| 319 | if (offset >= size) |
| 320 | return size; |
| 321 | size -= result; |
| 322 | offset &= (BITS_PER_LONG-1); |
| 323 | if (offset) { |
| 324 | tmp = *(p++); |
| 325 | tmp |= ~0UL >> (BITS_PER_LONG-offset); |
| 326 | if (size < BITS_PER_LONG) |
| 327 | goto found_first; |
| 328 | if (~tmp) |
| 329 | goto found_middle; |
| 330 | size -= BITS_PER_LONG; |
| 331 | result += BITS_PER_LONG; |
| 332 | } |
| 333 | while (size & ~(BITS_PER_LONG -1)) { |
| 334 | if (~(tmp = *(p++))) |
| 335 | goto found_middle; |
| 336 | result += BITS_PER_LONG; |
| 337 | size -= BITS_PER_LONG; |
| 338 | } |
| 339 | if (!size) |
| 340 | return result; |
| 341 | tmp = *p; |
| 342 | found_first: |
| 343 | tmp |= ~0UL << size; |
| 344 | found_middle: |
| 345 | return result + ffz(tmp); |
| 346 | } |
| 347 | |
| 348 | static __inline__ unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset) |
| 349 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 350 | const unsigned long *p = addr + (offset >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
| 352 | unsigned long tmp; |
| 353 | |
| 354 | if (offset >= size) |
| 355 | return size; |
| 356 | size -= result; |
| 357 | offset &= (BITS_PER_LONG-1); |
| 358 | if (offset) { |
| 359 | tmp = *(p++); |
| 360 | tmp &= (~0UL << offset); |
| 361 | if (size < BITS_PER_LONG) |
| 362 | goto found_first; |
| 363 | if (tmp) |
| 364 | goto found_middle; |
| 365 | size -= BITS_PER_LONG; |
| 366 | result += BITS_PER_LONG; |
| 367 | } |
| 368 | while (size & ~(BITS_PER_LONG-1)) { |
| 369 | if ((tmp = *(p++))) |
| 370 | goto found_middle; |
| 371 | result += BITS_PER_LONG; |
| 372 | size -= BITS_PER_LONG; |
| 373 | } |
| 374 | if (!size) |
| 375 | return result; |
| 376 | tmp = *p; |
| 377 | |
| 378 | found_first: |
| 379 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
| 380 | if (tmp == 0UL) /* Are any bits set? */ |
| 381 | return result + size; /* Nope. */ |
| 382 | found_middle: |
| 383 | return result + __ffs(tmp); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * find_first_bit - find the first set bit in a memory region |
| 388 | * @addr: The address to start the search at |
| 389 | * @size: The maximum size to search |
| 390 | * |
| 391 | * Returns the bit-number of the first set bit, not the number of the byte |
| 392 | * containing a bit. |
| 393 | */ |
| 394 | #define find_first_bit(addr, size) \ |
| 395 | find_next_bit((addr), (size), 0) |
| 396 | |
| 397 | #define _EXT2_HAVE_ASM_BITOPS_ |
| 398 | |
| 399 | #ifdef __KERNEL__ |
| 400 | /* |
| 401 | * test_and_{set,clear}_bit guarantee atomicity without |
| 402 | * disabling interrupts. |
| 403 | */ |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 404 | |
| 405 | /* '3' is bits per byte */ |
| 406 | #define LE_BYTE_ADDR ((sizeof(unsigned long) - 1) << 3) |
| 407 | |
| 408 | #define ext2_test_bit(nr, addr) \ |
| 409 | test_bit((nr) ^ LE_BYTE_ADDR, (unsigned long *)addr) |
| 410 | #define ext2_set_bit(nr, addr) \ |
| 411 | __test_and_set_bit((nr) ^ LE_BYTE_ADDR, (unsigned long *)addr) |
| 412 | #define ext2_clear_bit(nr, addr) \ |
| 413 | __test_and_clear_bit((nr) ^ LE_BYTE_ADDR, (unsigned long *)addr) |
| 414 | |
| 415 | #define ext2_set_bit_atomic(l,nr,addr) \ |
| 416 | test_and_set_bit((nr) ^ LE_BYTE_ADDR, (unsigned long *)addr) |
| 417 | #define ext2_clear_bit_atomic(l,nr,addr) \ |
| 418 | test_and_clear_bit( (nr) ^ LE_BYTE_ADDR, (unsigned long *)addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | #endif /* __KERNEL__ */ |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | #define ext2_find_first_zero_bit(addr, size) \ |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 424 | ext2_find_next_zero_bit((addr), (size), 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 426 | /* include/linux/byteorder does not support "unsigned long" type */ |
| 427 | static inline unsigned long ext2_swabp(unsigned long * x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 429 | #ifdef __LP64__ |
| 430 | return (unsigned long) __swab64p((u64 *) x); |
| 431 | #else |
| 432 | return (unsigned long) __swab32p((u32 *) x); |
| 433 | #endif |
| 434 | } |
| 435 | |
| 436 | /* include/linux/byteorder doesn't support "unsigned long" type */ |
| 437 | static inline unsigned long ext2_swab(unsigned long y) |
| 438 | { |
| 439 | #ifdef __LP64__ |
| 440 | return (unsigned long) __swab64((u64) y); |
| 441 | #else |
| 442 | return (unsigned long) __swab32((u32) y); |
| 443 | #endif |
| 444 | } |
| 445 | |
| 446 | static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset) |
| 447 | { |
| 448 | unsigned long *p = (unsigned long *) addr + (offset >> SHIFT_PER_LONG); |
| 449 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
| 450 | unsigned long tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | if (offset >= size) |
| 453 | return size; |
| 454 | size -= result; |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 455 | offset &= (BITS_PER_LONG - 1UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (offset) { |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 457 | tmp = ext2_swabp(p++); |
| 458 | tmp |= (~0UL >> (BITS_PER_LONG - offset)); |
| 459 | if (size < BITS_PER_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | goto found_first; |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 461 | if (~tmp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | goto found_middle; |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 463 | size -= BITS_PER_LONG; |
| 464 | result += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 466 | |
| 467 | while (size & ~(BITS_PER_LONG - 1)) { |
| 468 | if (~(tmp = *(p++))) |
| 469 | goto found_middle_swap; |
| 470 | result += BITS_PER_LONG; |
| 471 | size -= BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | if (!size) |
| 474 | return result; |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 475 | tmp = ext2_swabp(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | found_first: |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 477 | tmp |= ~0UL << size; |
| 478 | if (tmp == ~0UL) /* Are any bits zero? */ |
| 479 | return result + size; /* Nope. Skip ffz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | found_middle: |
| 481 | return result + ffz(tmp); |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 482 | |
| 483 | found_middle_swap: |
| 484 | return result + ffz(ext2_swab(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Grant Grundler | a366064 | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | /* Bitmap functions for the minix filesystem. */ |
| 489 | #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr) |
| 490 | #define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr)) |
| 491 | #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr) |
| 492 | #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr) |
| 493 | #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size) |
| 494 | |
| 495 | #endif /* _PARISC_BITOPS_H */ |