Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ASM_IA64_BITOPS_H |
| 2 | #define _ASM_IA64_BITOPS_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 1998-2003 Hewlett-Packard Co |
| 6 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 7 | * |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 8 | * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64 |
| 9 | * O(1) scheduler patch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/compiler.h> |
| 13 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/intrinsics.h> |
| 15 | |
| 16 | /** |
| 17 | * set_bit - Atomically set a bit in memory |
| 18 | * @nr: the bit to set |
| 19 | * @addr: the address to start counting from |
| 20 | * |
| 21 | * This function is atomic and may not be reordered. See __set_bit() |
| 22 | * if you do not require the atomic guarantees. |
| 23 | * Note that @nr may be almost arbitrarily large; this function is not |
| 24 | * restricted to acting on a single-word quantity. |
| 25 | * |
| 26 | * The address must be (at least) "long" aligned. |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 27 | * Note that there are driver (e.g., eepro100) which use these operations to |
| 28 | * operate on hw-defined data-structures, so we can't easily change these |
| 29 | * operations to force a bigger alignment. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). |
| 32 | */ |
| 33 | static __inline__ void |
| 34 | set_bit (int nr, volatile void *addr) |
| 35 | { |
| 36 | __u32 bit, old, new; |
| 37 | volatile __u32 *m; |
| 38 | CMPXCHG_BUGCHECK_DECL |
| 39 | |
| 40 | m = (volatile __u32 *) addr + (nr >> 5); |
| 41 | bit = 1 << (nr & 31); |
| 42 | do { |
| 43 | CMPXCHG_BUGCHECK(m); |
| 44 | old = *m; |
| 45 | new = old | bit; |
| 46 | } while (cmpxchg_acq(m, old, new) != old); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * __set_bit - Set a bit in memory |
| 51 | * @nr: the bit to set |
| 52 | * @addr: the address to start counting from |
| 53 | * |
| 54 | * Unlike set_bit(), this function is non-atomic and may be reordered. |
| 55 | * If it's called on the same region of memory simultaneously, the effect |
| 56 | * may be that only one operation succeeds. |
| 57 | */ |
| 58 | static __inline__ void |
| 59 | __set_bit (int nr, volatile void *addr) |
| 60 | { |
| 61 | *((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31)); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * clear_bit() has "acquire" semantics. |
| 66 | */ |
| 67 | #define smp_mb__before_clear_bit() smp_mb() |
| 68 | #define smp_mb__after_clear_bit() do { /* skip */; } while (0) |
| 69 | |
| 70 | /** |
| 71 | * clear_bit - Clears a bit in memory |
| 72 | * @nr: Bit to clear |
| 73 | * @addr: Address to start counting from |
| 74 | * |
| 75 | * clear_bit() is atomic and may not be reordered. However, it does |
| 76 | * not contain a memory barrier, so if it is used for locking purposes, |
| 77 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
| 78 | * in order to ensure changes are visible on other processors. |
| 79 | */ |
| 80 | static __inline__ void |
| 81 | clear_bit (int nr, volatile void *addr) |
| 82 | { |
| 83 | __u32 mask, old, new; |
| 84 | volatile __u32 *m; |
| 85 | CMPXCHG_BUGCHECK_DECL |
| 86 | |
| 87 | m = (volatile __u32 *) addr + (nr >> 5); |
| 88 | mask = ~(1 << (nr & 31)); |
| 89 | do { |
| 90 | CMPXCHG_BUGCHECK(m); |
| 91 | old = *m; |
| 92 | new = old & mask; |
| 93 | } while (cmpxchg_acq(m, old, new) != old); |
| 94 | } |
| 95 | |
| 96 | /** |
Nick Piggin | 87371e4 | 2007-10-18 03:06:52 -0700 | [diff] [blame^] | 97 | * clear_bit_unlock - Clears a bit in memory with release |
| 98 | * @nr: Bit to clear |
| 99 | * @addr: Address to start counting from |
| 100 | * |
| 101 | * clear_bit_unlock() is atomic and may not be reordered. It does |
| 102 | * contain a memory barrier suitable for unlock type operations. |
| 103 | */ |
| 104 | static __inline__ void |
| 105 | clear_bit_unlock (int nr, volatile void *addr) |
| 106 | { |
| 107 | __u32 mask, old, new; |
| 108 | volatile __u32 *m; |
| 109 | CMPXCHG_BUGCHECK_DECL |
| 110 | |
| 111 | m = (volatile __u32 *) addr + (nr >> 5); |
| 112 | mask = ~(1 << (nr & 31)); |
| 113 | do { |
| 114 | CMPXCHG_BUGCHECK(m); |
| 115 | old = *m; |
| 116 | new = old & mask; |
| 117 | } while (cmpxchg_rel(m, old, new) != old); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * __clear_bit_unlock - Non-atomically clear a bit with release |
| 122 | * |
| 123 | * This is like clear_bit_unlock, but the implementation may use a non-atomic |
| 124 | * store (this one uses an atomic, however). |
| 125 | */ |
| 126 | #define __clear_bit_unlock clear_bit_unlock |
| 127 | |
| 128 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * __clear_bit - Clears a bit in memory (non-atomic version) |
| 130 | */ |
| 131 | static __inline__ void |
| 132 | __clear_bit (int nr, volatile void *addr) |
| 133 | { |
| 134 | volatile __u32 *p = (__u32 *) addr + (nr >> 5); |
| 135 | __u32 m = 1 << (nr & 31); |
| 136 | *p &= ~m; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * change_bit - Toggle a bit in memory |
| 141 | * @nr: Bit to clear |
| 142 | * @addr: Address to start counting from |
| 143 | * |
| 144 | * change_bit() is atomic and may not be reordered. |
| 145 | * Note that @nr may be almost arbitrarily large; this function is not |
| 146 | * restricted to acting on a single-word quantity. |
| 147 | */ |
| 148 | static __inline__ void |
| 149 | change_bit (int nr, volatile void *addr) |
| 150 | { |
| 151 | __u32 bit, old, new; |
| 152 | volatile __u32 *m; |
| 153 | CMPXCHG_BUGCHECK_DECL |
| 154 | |
| 155 | m = (volatile __u32 *) addr + (nr >> 5); |
| 156 | bit = (1 << (nr & 31)); |
| 157 | do { |
| 158 | CMPXCHG_BUGCHECK(m); |
| 159 | old = *m; |
| 160 | new = old ^ bit; |
| 161 | } while (cmpxchg_acq(m, old, new) != old); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * __change_bit - Toggle a bit in memory |
| 166 | * @nr: the bit to set |
| 167 | * @addr: the address to start counting from |
| 168 | * |
| 169 | * Unlike change_bit(), this function is non-atomic and may be reordered. |
| 170 | * If it's called on the same region of memory simultaneously, the effect |
| 171 | * may be that only one operation succeeds. |
| 172 | */ |
| 173 | static __inline__ void |
| 174 | __change_bit (int nr, volatile void *addr) |
| 175 | { |
| 176 | *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31)); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * test_and_set_bit - Set a bit and return its old value |
| 181 | * @nr: Bit to set |
| 182 | * @addr: Address to count from |
| 183 | * |
| 184 | * This operation is atomic and cannot be reordered. |
| 185 | * It also implies a memory barrier. |
| 186 | */ |
| 187 | static __inline__ int |
| 188 | test_and_set_bit (int nr, volatile void *addr) |
| 189 | { |
| 190 | __u32 bit, old, new; |
| 191 | volatile __u32 *m; |
| 192 | CMPXCHG_BUGCHECK_DECL |
| 193 | |
| 194 | m = (volatile __u32 *) addr + (nr >> 5); |
| 195 | bit = 1 << (nr & 31); |
| 196 | do { |
| 197 | CMPXCHG_BUGCHECK(m); |
| 198 | old = *m; |
| 199 | new = old | bit; |
| 200 | } while (cmpxchg_acq(m, old, new) != old); |
| 201 | return (old & bit) != 0; |
| 202 | } |
| 203 | |
| 204 | /** |
Nick Piggin | 87371e4 | 2007-10-18 03:06:52 -0700 | [diff] [blame^] | 205 | * test_and_set_bit_lock - Set a bit and return its old value for lock |
| 206 | * @nr: Bit to set |
| 207 | * @addr: Address to count from |
| 208 | * |
| 209 | * This is the same as test_and_set_bit on ia64 |
| 210 | */ |
| 211 | #define test_and_set_bit_lock test_and_set_bit |
| 212 | |
| 213 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * __test_and_set_bit - Set a bit and return its old value |
| 215 | * @nr: Bit to set |
| 216 | * @addr: Address to count from |
| 217 | * |
| 218 | * This operation is non-atomic and can be reordered. |
| 219 | * If two examples of this operation race, one can appear to succeed |
| 220 | * but actually fail. You must protect multiple accesses with a lock. |
| 221 | */ |
| 222 | static __inline__ int |
| 223 | __test_and_set_bit (int nr, volatile void *addr) |
| 224 | { |
| 225 | __u32 *p = (__u32 *) addr + (nr >> 5); |
| 226 | __u32 m = 1 << (nr & 31); |
| 227 | int oldbitset = (*p & m) != 0; |
| 228 | |
| 229 | *p |= m; |
| 230 | return oldbitset; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * test_and_clear_bit - Clear a bit and return its old value |
| 235 | * @nr: Bit to set |
| 236 | * @addr: Address to count from |
| 237 | * |
| 238 | * This operation is atomic and cannot be reordered. |
| 239 | * It also implies a memory barrier. |
| 240 | */ |
| 241 | static __inline__ int |
| 242 | test_and_clear_bit (int nr, volatile void *addr) |
| 243 | { |
| 244 | __u32 mask, old, new; |
| 245 | volatile __u32 *m; |
| 246 | CMPXCHG_BUGCHECK_DECL |
| 247 | |
| 248 | m = (volatile __u32 *) addr + (nr >> 5); |
| 249 | mask = ~(1 << (nr & 31)); |
| 250 | do { |
| 251 | CMPXCHG_BUGCHECK(m); |
| 252 | old = *m; |
| 253 | new = old & mask; |
| 254 | } while (cmpxchg_acq(m, old, new) != old); |
| 255 | return (old & ~mask) != 0; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * __test_and_clear_bit - Clear a bit and return its old value |
| 260 | * @nr: Bit to set |
| 261 | * @addr: Address to count from |
| 262 | * |
| 263 | * This operation is non-atomic and can be reordered. |
| 264 | * If two examples of this operation race, one can appear to succeed |
| 265 | * but actually fail. You must protect multiple accesses with a lock. |
| 266 | */ |
| 267 | static __inline__ int |
| 268 | __test_and_clear_bit(int nr, volatile void * addr) |
| 269 | { |
| 270 | __u32 *p = (__u32 *) addr + (nr >> 5); |
| 271 | __u32 m = 1 << (nr & 31); |
| 272 | int oldbitset = *p & m; |
| 273 | |
| 274 | *p &= ~m; |
| 275 | return oldbitset; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * test_and_change_bit - Change a bit and return its old value |
| 280 | * @nr: Bit to set |
| 281 | * @addr: Address to count from |
| 282 | * |
| 283 | * This operation is atomic and cannot be reordered. |
| 284 | * It also implies a memory barrier. |
| 285 | */ |
| 286 | static __inline__ int |
| 287 | test_and_change_bit (int nr, volatile void *addr) |
| 288 | { |
| 289 | __u32 bit, old, new; |
| 290 | volatile __u32 *m; |
| 291 | CMPXCHG_BUGCHECK_DECL |
| 292 | |
| 293 | m = (volatile __u32 *) addr + (nr >> 5); |
| 294 | bit = (1 << (nr & 31)); |
| 295 | do { |
| 296 | CMPXCHG_BUGCHECK(m); |
| 297 | old = *m; |
| 298 | new = old ^ bit; |
| 299 | } while (cmpxchg_acq(m, old, new) != old); |
| 300 | return (old & bit) != 0; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * WARNING: non atomic version. |
| 305 | */ |
| 306 | static __inline__ int |
| 307 | __test_and_change_bit (int nr, void *addr) |
| 308 | { |
| 309 | __u32 old, bit = (1 << (nr & 31)); |
| 310 | __u32 *m = (__u32 *) addr + (nr >> 5); |
| 311 | |
| 312 | old = *m; |
| 313 | *m = old ^ bit; |
| 314 | return (old & bit) != 0; |
| 315 | } |
| 316 | |
| 317 | static __inline__ int |
| 318 | test_bit (int nr, const volatile void *addr) |
| 319 | { |
| 320 | return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31)); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * ffz - find the first zero bit in a long word |
| 325 | * @x: The long word to find the bit in |
| 326 | * |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 327 | * Returns the bit-number (0..63) of the first (least significant) zero bit. |
| 328 | * Undefined if no zero exists, so code should check against ~0UL first... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | static inline unsigned long |
| 331 | ffz (unsigned long x) |
| 332 | { |
| 333 | unsigned long result; |
| 334 | |
| 335 | result = ia64_popcnt(x & (~x - 1)); |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * __ffs - find first bit in word. |
| 341 | * @x: The word to search |
| 342 | * |
| 343 | * Undefined if no bit exists, so code should check against 0 first. |
| 344 | */ |
| 345 | static __inline__ unsigned long |
| 346 | __ffs (unsigned long x) |
| 347 | { |
| 348 | unsigned long result; |
| 349 | |
| 350 | result = ia64_popcnt((x-1) & ~x); |
| 351 | return result; |
| 352 | } |
| 353 | |
| 354 | #ifdef __KERNEL__ |
| 355 | |
| 356 | /* |
David Mosberger-Tang | 821376b | 2005-04-21 11:07:59 -0700 | [diff] [blame] | 357 | * Return bit number of last (most-significant) bit set. Undefined |
| 358 | * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | */ |
| 360 | static inline unsigned long |
| 361 | ia64_fls (unsigned long x) |
| 362 | { |
| 363 | long double d = x; |
| 364 | long exp; |
| 365 | |
| 366 | exp = ia64_getf_exp(d); |
| 367 | return exp - 0xffff; |
| 368 | } |
| 369 | |
David Mosberger-Tang | 821376b | 2005-04-21 11:07:59 -0700 | [diff] [blame] | 370 | /* |
| 371 | * Find the last (most significant) bit set. Returns 0 for x==0 and |
| 372 | * bits are numbered from 1..32 (e.g., fls(9) == 4). |
| 373 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | static inline int |
David Mosberger-Tang | 821376b | 2005-04-21 11:07:59 -0700 | [diff] [blame] | 375 | fls (int t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
David Mosberger-Tang | 821376b | 2005-04-21 11:07:59 -0700 | [diff] [blame] | 377 | unsigned long x = t & 0xffffffffu; |
| 378 | |
| 379 | if (!x) |
| 380 | return 0; |
| 381 | x |= x >> 1; |
| 382 | x |= x >> 2; |
| 383 | x |= x >> 4; |
| 384 | x |= x >> 8; |
| 385 | x |= x >> 16; |
| 386 | return ia64_popcnt(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 388 | |
| 389 | #include <asm-generic/bitops/fls64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 392 | * ffs: find first bit set. This is defined the same way as the libc and |
| 393 | * compiler builtin ffs routines, therefore differs in spirit from the above |
| 394 | * ffz (man ffs): it operates on "int" values only and the result value is the |
| 395 | * bit number + 1. ffs(0) is defined to return zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | */ |
| 397 | #define ffs(x) __builtin_ffs(x) |
| 398 | |
| 399 | /* |
| 400 | * hweightN: returns the hamming weight (i.e. the number |
| 401 | * of bits set) of a N-bit word |
| 402 | */ |
| 403 | static __inline__ unsigned long |
| 404 | hweight64 (unsigned long x) |
| 405 | { |
| 406 | unsigned long result; |
| 407 | result = ia64_popcnt(x); |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | #define hweight32(x) (unsigned int) hweight64((x) & 0xfffffffful) |
| 412 | #define hweight16(x) (unsigned int) hweight64((x) & 0xfffful) |
| 413 | #define hweight8(x) (unsigned int) hweight64((x) & 0xfful) |
| 414 | |
| 415 | #endif /* __KERNEL__ */ |
| 416 | |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 417 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | #ifdef __KERNEL__ |
| 420 | |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 421 | #include <asm-generic/bitops/ext2-non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Akinobu Mita | 2875aef | 2006-03-26 01:39:25 -0800 | [diff] [blame] | 426 | #include <asm-generic/bitops/minix.h> |
| 427 | #include <asm-generic/bitops/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | #endif /* __KERNEL__ */ |
| 430 | |
| 431 | #endif /* _ASM_IA64_BITOPS_H */ |