blob: 854e225c2f615a35f6503ac75c7739ae5d02aa99 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
5 *
6 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
7 *
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.
13 */
14
15#ifndef __ASM_ARM_BITOPS_H
16#define __ASM_ARM_BITOPS_H
17
18#ifdef __KERNEL__
19
20#define smp_mb__before_clear_bit() do { } while (0)
21#define smp_mb__after_clear_bit() do { } while (0)
22
23/*
24 * Function prototypes to keep gcc -Wall happy.
25 */
26extern void set_bit(int nr, volatile void * addr);
27
wdenkc6097192002-11-03 00:24:07 +000028extern void clear_bit(int nr, volatile void * addr);
29
wdenkc6097192002-11-03 00:24:07 +000030extern void change_bit(int nr, volatile void * addr);
31
32static inline void __change_bit(int nr, volatile void *addr)
33{
Simon Kagstrom4b15de02009-08-24 09:10:03 +020034 unsigned long mask = BIT_MASK(nr);
35 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
36
37 *p ^= mask;
wdenkc6097192002-11-03 00:24:07 +000038}
39
40extern int test_and_set_bit(int nr, volatile void * addr);
41
42static inline int __test_and_set_bit(int nr, volatile void *addr)
43{
Simon Kagstrom4b15de02009-08-24 09:10:03 +020044 unsigned long mask = BIT_MASK(nr);
45 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
46 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000047
Simon Kagstrom4b15de02009-08-24 09:10:03 +020048 *p = old | mask;
49 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000050}
51
52extern int test_and_clear_bit(int nr, volatile void * addr);
53
54static inline int __test_and_clear_bit(int nr, volatile void *addr)
55{
Simon Kagstrom4b15de02009-08-24 09:10:03 +020056 unsigned long mask = BIT_MASK(nr);
57 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
58 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000059
Simon Kagstrom4b15de02009-08-24 09:10:03 +020060 *p = old & ~mask;
61 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000062}
63
64extern int test_and_change_bit(int nr, volatile void * addr);
65
66static inline int __test_and_change_bit(int nr, volatile void *addr)
67{
Simon Kagstrom4b15de02009-08-24 09:10:03 +020068 unsigned long mask = BIT_MASK(nr);
69 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
70 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000071
Simon Kagstrom4b15de02009-08-24 09:10:03 +020072 *p = old ^ mask;
73 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000074}
75
76extern int find_first_zero_bit(void * addr, unsigned size);
77extern int find_next_zero_bit(void * addr, int size, int offset);
78
79/*
80 * This routine doesn't need to be atomic.
81 */
82static inline int test_bit(int nr, const void * addr)
83{
84 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
85}
86
87/*
88 * ffz = Find First Zero in word. Undefined if no zero exists,
89 * so code should check against ~0UL first..
90 */
91static inline unsigned long ffz(unsigned long word)
92{
93 int k;
94
95 word = ~word;
96 k = 31;
97 if (word & 0x0000ffff) { k -= 16; word <<= 16; }
98 if (word & 0x00ff0000) { k -= 8; word <<= 8; }
99 if (word & 0x0f000000) { k -= 4; word <<= 4; }
100 if (word & 0x30000000) { k -= 2; word <<= 2; }
101 if (word & 0x40000000) { k -= 1; }
wdenk8bde7f72003-06-27 21:31:46 +0000102 return k;
wdenkc6097192002-11-03 00:24:07 +0000103}
104
105/*
106 * ffs: find first bit set. This is defined the same way as
107 * the libc and compiler builtin ffs routines, therefore
108 * differs in spirit from the above ffz (man ffs).
109 */
110
111#define ffs(x) generic_ffs(x)
112
113/*
114 * hweightN: returns the hamming weight (i.e. the number
115 * of bits set) of a N-bit word
116 */
117
118#define hweight32(x) generic_hweight32(x)
119#define hweight16(x) generic_hweight16(x)
120#define hweight8(x) generic_hweight8(x)
121
122#define ext2_set_bit test_and_set_bit
123#define ext2_clear_bit test_and_clear_bit
124#define ext2_test_bit test_bit
125#define ext2_find_first_zero_bit find_first_zero_bit
126#define ext2_find_next_zero_bit find_next_zero_bit
127
128/* Bitmap functions for the minix filesystem. */
129#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
130#define minix_set_bit(nr,addr) set_bit(nr,addr)
131#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
132#define minix_test_bit(nr,addr) test_bit(nr,addr)
133#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
134
135#endif /* __KERNEL__ */
136
137#endif /* _ARM_BITOPS_H */