2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
6 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
8 * Please note that the code in this file should never be included
9 * from user space. Many of these are not implemented in assembler
10 * since they would be too costly. Also, they require priviledged
11 * instructions (which are not available from user mode) to ensure
12 * that they are atomic.
15 #ifndef __ASM_ARM_BITOPS_H
16 #define __ASM_ARM_BITOPS_H
18 #include <asm-generic/bitops/__ffs.h>
19 #include <asm-generic/bitops/__fls.h>
20 #include <asm-generic/bitops/fls.h>
21 #include <asm-generic/bitops/fls64.h>
26 #include <linux/bitops.h>
28 #include <asm/proc-armv/system.h>
30 #define smp_mb__before_clear_bit() do { } while (0)
31 #define smp_mb__after_clear_bit() do { } while (0)
34 * Function prototypes to keep gcc -Wall happy.
36 extern void set_bit(int nr, volatile void * addr);
38 extern void clear_bit(int nr, volatile void * addr);
40 extern void change_bit(int nr, volatile void * addr);
42 static inline void __change_bit(int nr, volatile void *addr)
44 unsigned long mask = BIT_MASK(nr);
45 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
50 static inline int __test_and_set_bit(int nr, volatile void *addr)
52 unsigned long mask = BIT_MASK(nr);
53 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
54 unsigned long old = *p;
57 return (old & mask) != 0;
60 static inline int test_and_set_bit(int nr, volatile void * addr)
62 unsigned long flags = 0;
65 local_irq_save(flags);
66 out = __test_and_set_bit(nr, addr);
67 local_irq_restore(flags);
72 static inline int __test_and_clear_bit(int nr, volatile void *addr)
74 unsigned long mask = BIT_MASK(nr);
75 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
76 unsigned long old = *p;
79 return (old & mask) != 0;
82 static inline int test_and_clear_bit(int nr, volatile void * addr)
84 unsigned long flags = 0;
87 local_irq_save(flags);
88 out = __test_and_clear_bit(nr, addr);
89 local_irq_restore(flags);
94 extern int test_and_change_bit(int nr, volatile void * addr);
96 static inline int __test_and_change_bit(int nr, volatile void *addr)
98 unsigned long mask = BIT_MASK(nr);
99 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
100 unsigned long old = *p;
103 return (old & mask) != 0;
107 * This routine doesn't need to be atomic.
109 static inline int test_bit(int nr, const void * addr)
111 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
114 static inline int __ilog2(unsigned int x)
116 return generic_fls(x) - 1;
119 #define ffz(x) __ffs(~(x))
121 static inline int find_next_zero_bit(void *addr, int size, int offset)
123 unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
124 unsigned long result = offset & ~(BITS_PER_LONG - 1);
130 offset &= (BITS_PER_LONG - 1);
133 tmp |= ~0UL >> (BITS_PER_LONG - offset);
134 if (size < BITS_PER_LONG)
138 size -= BITS_PER_LONG;
139 result += BITS_PER_LONG;
141 while (size & ~(BITS_PER_LONG - 1)) {
145 result += BITS_PER_LONG;
146 size -= BITS_PER_LONG;
155 return result + ffz(tmp);
159 * hweightN: returns the hamming weight (i.e. the number
160 * of bits set) of a N-bit word
163 #define hweight32(x) generic_hweight32(x)
164 #define hweight16(x) generic_hweight16(x)
165 #define hweight8(x) generic_hweight8(x)
167 #define find_first_zero_bit(addr, size) \
168 find_next_zero_bit((addr), (size), 0)
170 #define ext2_set_bit test_and_set_bit
171 #define ext2_clear_bit test_and_clear_bit
172 #define ext2_test_bit test_bit
173 #define ext2_find_first_zero_bit find_first_zero_bit
174 #define ext2_find_next_zero_bit find_next_zero_bit
176 /* Bitmap functions for the minix filesystem. */
177 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
178 #define minix_set_bit(nr,addr) set_bit(nr,addr)
179 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
180 #define minix_test_bit(nr,addr) test_bit(nr,addr)
181 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
183 #endif /* __KERNEL__ */
185 #endif /* _ARM_BITOPS_H */