2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (c) 1994, 95, 96, 97, 98, 99, 2000 Ralf Baechle
7 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <asm/byteorder.h> /* sigh ... */
17 #error "Don't do this, sucker ..."
20 #include <asm/system.h>
21 #include <asm/sgidefs.h>
24 * set_bit - Atomically set a bit in memory
26 * @addr: the address to start counting from
28 * This function is atomic and may not be reordered. See __set_bit()
29 * if you do not require the atomic guarantees.
30 * Note that @nr may be almost arbitrarily large; this function is not
31 * restricted to acting on a single-word quantity.
33 static inline void set_bit(unsigned long nr, volatile void *addr)
35 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
39 "1:\tlld\t%0, %1\t\t# set_bit\n\t"
43 : "=&r" (temp), "=m" (*m)
44 : "ir" (1UL << (nr & 0x3f)), "m" (*m)
49 * __set_bit - Set a bit in memory
51 * @addr: the address to start counting from
53 * Unlike set_bit(), this function is non-atomic and may be reordered.
54 * If it's called on the same region of memory simultaneously, the effect
55 * may be that only one operation succeeds.
57 static inline void __set_bit(int nr, volatile void * addr)
59 unsigned long * m = ((unsigned long *) addr) + (nr >> 6);
61 *m |= 1UL << (nr & 0x3f);
65 * clear_bit - Clears a bit in memory
67 * @addr: Address to start counting from
69 * clear_bit() is atomic and may not be reordered. However, it does
70 * not contain a memory barrier, so if it is used for locking purposes,
71 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
72 * in order to ensure changes are visible on other processors.
74 static inline void clear_bit(unsigned long nr, volatile void *addr)
76 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
80 "1:\tlld\t%0, %1\t\t# clear_bit\n\t"
84 : "=&r" (temp), "=m" (*m)
85 : "ir" (~(1UL << (nr & 0x3f))), "m" (*m));
88 #define smp_mb__before_clear_bit() smp_mb()
89 #define smp_mb__after_clear_bit() smp_mb()
92 * change_bit - Toggle a bit in memory
94 * @addr: Address to start counting from
96 * change_bit() is atomic and may not be reordered.
97 * Note that @nr may be almost arbitrarily large; this function is not
98 * restricted to acting on a single-word quantity.
100 static inline void change_bit(unsigned long nr, volatile void *addr)
102 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
105 __asm__ __volatile__(
106 "1:\tlld\t%0, %1\t\t# change_bit\n\t"
110 :"=&r" (temp), "=m" (*m)
111 :"ir" (1UL << (nr & 0x3f)), "m" (*m));
115 * __change_bit - Toggle a bit in memory
116 * @nr: the bit to set
117 * @addr: the address to start counting from
119 * Unlike change_bit(), this function is non-atomic and may be reordered.
120 * If it's called on the same region of memory simultaneously, the effect
121 * may be that only one operation succeeds.
123 static inline void __change_bit(int nr, volatile void * addr)
125 unsigned long * m = ((unsigned long *) addr) + (nr >> 6);
127 *m ^= 1UL << (nr & 0x3f);
131 * test_and_set_bit - Set a bit and return its old value
133 * @addr: Address to count from
135 * This operation is atomic and cannot be reordered.
136 * It also implies a memory barrier.
138 static inline unsigned long test_and_set_bit(unsigned long nr,
141 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
142 unsigned long temp, res;
144 __asm__ __volatile__(
145 ".set\tnoreorder\t\t# test_and_set_bit\n"
146 "1:\tlld\t%0, %1\n\t"
150 " and\t%2, %0, %3\n\t"
155 : "=&r" (temp), "=m" (*m), "=&r" (res)
156 : "r" (1UL << (nr & 0x3f)), "m" (*m)
163 * __test_and_set_bit - Set a bit and return its old value
165 * @addr: Address to count from
167 * This operation is non-atomic and can be reordered.
168 * If two examples of this operation race, one can appear to succeed
169 * but actually fail. You must protect multiple accesses with a lock.
171 static inline int __test_and_set_bit(int nr, volatile void *addr)
173 unsigned long mask, retval;
174 long *a = (unsigned long *) addr;
177 mask = 1UL << (nr & 0x3f);
178 retval = ((mask & *a) != 0);
185 * test_and_clear_bit - Clear a bit and return its old value
187 * @addr: Address to count from
189 * This operation is atomic and cannot be reordered.
190 * It also implies a memory barrier.
192 static inline unsigned long test_and_clear_bit(unsigned long nr,
195 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
196 unsigned long temp, res;
198 __asm__ __volatile__(
199 ".set\tnoreorder\t\t# test_and_clear_bit\n"
200 "1:\tlld\t%0, %1\n\t"
205 " and\t%2, %0, %3\n\t"
210 : "=&r" (temp), "=m" (*m), "=&r" (res)
211 : "r" (1UL << (nr & 0x3f)), "m" (*m)
218 * __test_and_clear_bit - Clear a bit and return its old value
220 * @addr: Address to count from
222 * This operation is non-atomic and can be reordered.
223 * If two examples of this operation race, one can appear to succeed
224 * but actually fail. You must protect multiple accesses with a lock.
226 static inline int __test_and_clear_bit(int nr, volatile void * addr)
228 unsigned long mask, retval;
229 unsigned long *a = (unsigned long *) addr;
232 mask = 1UL << (nr & 0x3f);
233 retval = ((mask & *a) != 0);
240 * test_and_change_bit - Change a bit and return its new value
242 * @addr: Address to count from
244 * This operation is atomic and cannot be reordered.
245 * It also implies a memory barrier.
247 static inline unsigned long test_and_change_bit(unsigned long nr,
250 unsigned long *m = ((unsigned long *) addr) + (nr >> 6);
251 unsigned long temp, res;
253 __asm__ __volatile__(
254 ".set\tnoreorder\t\t# test_and_change_bit\n"
255 "1:\tlld\t%0, %1\n\t"
256 "xor\t%2, %0, %3\n\t"
259 " and\t%2, %0, %3\n\t"
264 : "=&r" (temp), "=m" (*m), "=&r" (res)
265 : "r" (1UL << (nr & 0x3f)), "m" (*m)
272 * __test_and_change_bit - Change a bit and return its old value
274 * @addr: Address to count from
276 * This operation is non-atomic and can be reordered.
277 * If two examples of this operation race, one can appear to succeed
278 * but actually fail. You must protect multiple accesses with a lock.
280 static inline int __test_and_change_bit(int nr, volatile void *addr)
282 unsigned long mask, retval;
283 unsigned long *a = (unsigned long *) addr;
286 mask = 1UL << (nr & 0x3f);
287 retval = ((mask & *a) != 0);
293 * test_bit - Determine whether a bit is set
294 * @nr: bit number to test
295 * @addr: Address to start counting from
297 static inline unsigned long test_bit(int nr, volatile void * addr)
299 return 1UL & (((volatile unsigned long *) addr)[nr >> 6] >> (nr & 0x3f));
304 /* Little endian versions. */
307 * find_first_zero_bit - find the first zero bit in a memory region
308 * @addr: The address to start the search at
309 * @size: The maximum size to search
311 * Returns the bit-number of the first zero bit, not the number of the byte
314 static inline int find_first_zero_bit (void *addr, unsigned size)
322 __asm__ (".set\tnoreorder\n\t"
324 "1:\tsubu\t$1,%6,%0\n\t"
328 #if (_MIPS_ISA == _MIPS_ISA_MIPS2 ) || (_MIPS_ISA == _MIPS_ISA_MIPS3 ) || \
329 (_MIPS_ISA == _MIPS_ISA_MIPS4 ) || (_MIPS_ISA == _MIPS_ISA_MIPS5 ) || \
330 (_MIPS_ISA == _MIPS_ISA_MIPS32) || (_MIPS_ISA == _MIPS_ISA_MIPS64)
340 "1:\tand\t%2,$1,%1\n\t"
348 : "=r" (res), "=r" (dummy), "=r" (addr)
349 : "0" ((signed int) 0), "1" ((unsigned int) 0xffffffff),
350 "2" (addr), "r" (size));
356 * find_next_zero_bit - find the first zero bit in a memory region
357 * @addr: The address to base the search on
358 * @offset: The bitnumber to start searching at
359 * @size: The maximum size to search
361 static inline int find_next_zero_bit (void * addr, int size, int offset)
363 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
364 int set = 0, bit = offset & 31, res;
369 * Look for zero in first byte
371 __asm__(".set\tnoreorder\n\t"
373 "1:\tand\t$1,%4,%1\n\t"
381 : "=r" (set), "=r" (dummy)
382 : "0" (0), "1" (1 << bit), "r" (*p));
383 if (set < (32 - bit))
389 * No zero yet, search remaining full bytes for a zero
391 res = find_first_zero_bit(p, size - 32 * (p - (unsigned int *) addr));
392 return offset + set + res;
395 #endif /* !(__MIPSEB__) */
398 * ffz - find first zero in word.
399 * @word: The word to search
401 * Undefined if no zero exists, so code should check against ~0UL first.
403 static __inline__ unsigned long ffz(unsigned long word)
408 s = 32; if (word << 32 != 0) s = 0; b += s; word >>= s;
409 s = 16; if (word << 48 != 0) s = 0; b += s; word >>= s;
410 s = 8; if (word << 56 != 0) s = 0; b += s; word >>= s;
411 s = 4; if (word << 60 != 0) s = 0; b += s; word >>= s;
412 s = 2; if (word << 62 != 0) s = 0; b += s; word >>= s;
413 s = 1; if (word << 63 != 0) s = 0; b += s;
422 * ffs - find first bit set
423 * @x: the word to search
425 * This is defined the same way as
426 * the libc and compiler builtin ffs routines, therefore
427 * differs in spirit from the above ffz (man ffs).
430 #define ffs(x) generic_ffs(x)
433 * hweightN - returns the hamming weight of a N-bit word
434 * @x: the word to weigh
436 * The Hamming Weight of a number is the total number of bits set in it.
439 #define hweight32(x) generic_hweight32(x)
440 #define hweight16(x) generic_hweight16(x)
441 #define hweight8(x) generic_hweight8(x)
443 #endif /* __KERNEL__ */
448 * find_next_zero_bit - find the first zero bit in a memory region
449 * @addr: The address to base the search on
450 * @offset: The bitnumber to start searching at
451 * @size: The maximum size to search
453 static inline unsigned long find_next_zero_bit(void *addr, unsigned long size,
454 unsigned long offset)
456 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
457 unsigned long result = offset & ~63UL;
466 tmp |= ~0UL >> (64-offset);
474 while (size & ~63UL) {
487 return result + ffz(tmp);
490 #define find_first_zero_bit(addr, size) \
491 find_next_zero_bit((addr), (size), 0)
493 #endif /* (__MIPSEB__) */
497 /* Now for the ext2 filesystem bit operations and helper routines. */
501 static inline int ext2_set_bit(int nr,void * addr)
503 int mask, retval, flags;
504 unsigned char *ADDR = (unsigned char *) addr;
507 mask = 1 << (nr & 0x07);
509 retval = (mask & *ADDR) != 0;
511 restore_flags(flags);
515 static inline int ext2_clear_bit(int nr, void * addr)
517 int mask, retval, flags;
518 unsigned char *ADDR = (unsigned char *) addr;
521 mask = 1 << (nr & 0x07);
523 retval = (mask & *ADDR) != 0;
525 restore_flags(flags);
529 static inline int ext2_test_bit(int nr, const void * addr)
532 const unsigned char *ADDR = (const unsigned char *) addr;
535 mask = 1 << (nr & 0x07);
536 return ((mask & *ADDR) != 0);
539 #define ext2_find_first_zero_bit(addr, size) \
540 ext2_find_next_zero_bit((addr), (size), 0)
542 static inline unsigned int ext2_find_next_zero_bit(void *addr,
544 unsigned long offset)
546 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
547 unsigned int result = offset & ~31UL;
555 /* We hold the little endian value in tmp, but then the
556 * shift is illegal. So we could keep a big endian value
559 * tmp = __swab32(*(p++));
560 * tmp |= ~0UL >> (32-offset);
562 * but this would decrease preformance, so we change the
566 tmp |= __swab32(~0UL >> (32-offset));
574 while(size & ~31UL) {
585 /* tmp is little endian, so we would have to swab the shift,
586 * see above. But then we have to swab tmp below for ffz, so
587 * we might as well do this here.
589 return result + ffz(__swab32(tmp) | (~0UL << size));
591 return result + ffz(__swab32(tmp));
593 #else /* !(__MIPSEB__) */
595 /* Native ext2 byte ordering, just collapse using defines. */
596 #define ext2_set_bit(nr, addr) test_and_set_bit((nr), (addr))
597 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr), (addr))
598 #define ext2_test_bit(nr, addr) test_bit((nr), (addr))
599 #define ext2_find_first_zero_bit(addr, size) find_first_zero_bit((addr), (size))
600 #define ext2_find_next_zero_bit(addr, size, offset) \
601 find_next_zero_bit((addr), (size), (offset))
603 #endif /* !(__MIPSEB__) */
606 * Bitmap functions for the minix filesystem.
607 * FIXME: These assume that Minix uses the native byte/bitorder.
608 * This limits the Minix filesystem's value for data exchange very much.
610 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
611 #define minix_set_bit(nr,addr) set_bit(nr,addr)
612 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
613 #define minix_test_bit(nr,addr) test_bit(nr,addr)
614 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
616 #endif /* __KERNEL__ */
618 #endif /* _ASM_BITOPS_H */