aboutsummaryrefslogtreecommitdiffstats
path: root/include/asm-sparc64/bitops.h
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /include/asm-sparc64/bitops.h
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'include/asm-sparc64/bitops.h')
-rw-r--r--include/asm-sparc64/bitops.h294
1 files changed, 294 insertions, 0 deletions
diff --git a/include/asm-sparc64/bitops.h b/include/asm-sparc64/bitops.h
new file mode 100644
index 000000000000..9d722dc8cca3
--- /dev/null
+++ b/include/asm-sparc64/bitops.h
@@ -0,0 +1,294 @@
1/* $Id: bitops.h,v 1.39 2002/01/30 01:40:00 davem Exp $
2 * bitops.h: Bit string operations on the V9.
3 *
4 * Copyright 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
5 */
6
7#ifndef _SPARC64_BITOPS_H
8#define _SPARC64_BITOPS_H
9
10#include <linux/config.h>
11#include <linux/compiler.h>
12#include <asm/byteorder.h>
13
14extern int test_and_set_bit(unsigned long nr, volatile unsigned long *addr);
15extern int test_and_clear_bit(unsigned long nr, volatile unsigned long *addr);
16extern int test_and_change_bit(unsigned long nr, volatile unsigned long *addr);
17extern void set_bit(unsigned long nr, volatile unsigned long *addr);
18extern void clear_bit(unsigned long nr, volatile unsigned long *addr);
19extern void change_bit(unsigned long nr, volatile unsigned long *addr);
20
21/* "non-atomic" versions... */
22
23static __inline__ void __set_bit(int nr, volatile unsigned long *addr)
24{
25 volatile unsigned long *m = addr + (nr >> 6);
26
27 *m |= (1UL << (nr & 63));
28}
29
30static __inline__ void __clear_bit(int nr, volatile unsigned long *addr)
31{
32 volatile unsigned long *m = addr + (nr >> 6);
33
34 *m &= ~(1UL << (nr & 63));
35}
36
37static __inline__ void __change_bit(int nr, volatile unsigned long *addr)
38{
39 volatile unsigned long *m = addr + (nr >> 6);
40
41 *m ^= (1UL << (nr & 63));
42}
43
44static __inline__ int __test_and_set_bit(int nr, volatile unsigned long *addr)
45{
46 volatile unsigned long *m = addr + (nr >> 6);
47 long old = *m;
48 long mask = (1UL << (nr & 63));
49
50 *m = (old | mask);
51 return ((old & mask) != 0);
52}
53
54static __inline__ int __test_and_clear_bit(int nr, volatile unsigned long *addr)
55{
56 volatile unsigned long *m = addr + (nr >> 6);
57 long old = *m;
58 long mask = (1UL << (nr & 63));
59
60 *m = (old & ~mask);
61 return ((old & mask) != 0);
62}
63
64static __inline__ int __test_and_change_bit(int nr, volatile unsigned long *addr)
65{
66 volatile unsigned long *m = addr + (nr >> 6);
67 long old = *m;
68 long mask = (1UL << (nr & 63));
69
70 *m = (old ^ mask);
71 return ((old & mask) != 0);
72}
73
74#ifdef CONFIG_SMP
75#define smp_mb__before_clear_bit() membar("#StoreLoad | #LoadLoad")
76#define smp_mb__after_clear_bit() membar("#StoreLoad | #StoreStore")
77#else
78#define smp_mb__before_clear_bit() barrier()
79#define smp_mb__after_clear_bit() barrier()
80#endif
81
82static __inline__ int test_bit(int nr, __const__ volatile unsigned long *addr)
83{
84 return (1UL & ((addr)[nr >> 6] >> (nr & 63))) != 0UL;
85}
86
87/* The easy/cheese version for now. */
88static __inline__ unsigned long ffz(unsigned long word)
89{
90 unsigned long result;
91
92 result = 0;
93 while(word & 1) {
94 result++;
95 word >>= 1;
96 }
97 return result;
98}
99
100/**
101 * __ffs - find first bit in word.
102 * @word: The word to search
103 *
104 * Undefined if no bit exists, so code should check against 0 first.
105 */
106static __inline__ unsigned long __ffs(unsigned long word)
107{
108 unsigned long result = 0;
109
110 while (!(word & 1UL)) {
111 result++;
112 word >>= 1;
113 }
114 return result;
115}
116
117/*
118 * fls: find last bit set.
119 */
120
121#define fls(x) generic_fls(x)
122
123#ifdef __KERNEL__
124
125/*
126 * Every architecture must define this function. It's the fastest
127 * way of searching a 140-bit bitmap where the first 100 bits are
128 * unlikely to be set. It's guaranteed that at least one of the 140
129 * bits is cleared.
130 */
131static inline int sched_find_first_bit(unsigned long *b)
132{
133 if (unlikely(b[0]))
134 return __ffs(b[0]);
135 if (unlikely(((unsigned int)b[1])))
136 return __ffs(b[1]) + 64;
137 if (b[1] >> 32)
138 return __ffs(b[1] >> 32) + 96;
139 return __ffs(b[2]) + 128;
140}
141
142/*
143 * ffs: find first bit set. This is defined the same way as
144 * the libc and compiler builtin ffs routines, therefore
145 * differs in spirit from the above ffz (man ffs).
146 */
147static __inline__ int ffs(int x)
148{
149 if (!x)
150 return 0;
151 return __ffs((unsigned long)x) + 1;
152}
153
154/*
155 * hweightN: returns the hamming weight (i.e. the number
156 * of bits set) of a N-bit word
157 */
158
159#ifdef ULTRA_HAS_POPULATION_COUNT
160
161static __inline__ unsigned int hweight64(unsigned long w)
162{
163 unsigned int res;
164
165 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w));
166 return res;
167}
168
169static __inline__ unsigned int hweight32(unsigned int w)
170{
171 unsigned int res;
172
173 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffffffff));
174 return res;
175}
176
177static __inline__ unsigned int hweight16(unsigned int w)
178{
179 unsigned int res;
180
181 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffff));
182 return res;
183}
184
185static __inline__ unsigned int hweight8(unsigned int w)
186{
187 unsigned int res;
188
189 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xff));
190 return res;
191}
192
193#else
194
195#define hweight64(x) generic_hweight64(x)
196#define hweight32(x) generic_hweight32(x)
197#define hweight16(x) generic_hweight16(x)
198#define hweight8(x) generic_hweight8(x)
199
200#endif
201#endif /* __KERNEL__ */
202
203/**
204 * find_next_bit - find the next set bit in a memory region
205 * @addr: The address to base the search on
206 * @offset: The bitnumber to start searching at
207 * @size: The maximum size to search
208 */
209extern unsigned long find_next_bit(const unsigned long *, unsigned long,
210 unsigned long);
211
212/**
213 * find_first_bit - find the first set bit in a memory region
214 * @addr: The address to start the search at
215 * @size: The maximum size to search
216 *
217 * Returns the bit-number of the first set bit, not the number of the byte
218 * containing a bit.
219 */
220#define find_first_bit(addr, size) \
221 find_next_bit((addr), (size), 0)
222
223/* find_next_zero_bit() finds the first zero bit in a bit string of length
224 * 'size' bits, starting the search at bit 'offset'. This is largely based
225 * on Linus's ALPHA routines, which are pretty portable BTW.
226 */
227
228extern unsigned long find_next_zero_bit(const unsigned long *,
229 unsigned long, unsigned long);
230
231#define find_first_zero_bit(addr, size) \
232 find_next_zero_bit((addr), (size), 0)
233
234#define test_and_set_le_bit(nr,addr) \
235 test_and_set_bit((nr) ^ 0x38, (addr))
236#define test_and_clear_le_bit(nr,addr) \
237 test_and_clear_bit((nr) ^ 0x38, (addr))
238
239static __inline__ int test_le_bit(int nr, __const__ unsigned long * addr)
240{
241 int mask;
242 __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
243
244 ADDR += nr >> 3;
245 mask = 1 << (nr & 0x07);
246 return ((mask & *ADDR) != 0);
247}
248
249#define find_first_zero_le_bit(addr, size) \
250 find_next_zero_le_bit((addr), (size), 0)
251
252extern unsigned long find_next_zero_le_bit(unsigned long *, unsigned long, unsigned long);
253
254#ifdef __KERNEL__
255
256#define __set_le_bit(nr, addr) \
257 __set_bit((nr) ^ 0x38, (addr))
258#define __clear_le_bit(nr, addr) \
259 __clear_bit((nr) ^ 0x38, (addr))
260#define __test_and_clear_le_bit(nr, addr) \
261 __test_and_clear_bit((nr) ^ 0x38, (addr))
262#define __test_and_set_le_bit(nr, addr) \
263 __test_and_set_bit((nr) ^ 0x38, (addr))
264
265#define ext2_set_bit(nr,addr) \
266 __test_and_set_le_bit((nr),(unsigned long *)(addr))
267#define ext2_set_bit_atomic(lock,nr,addr) \
268 test_and_set_le_bit((nr),(unsigned long *)(addr))
269#define ext2_clear_bit(nr,addr) \
270 __test_and_clear_le_bit((nr),(unsigned long *)(addr))
271#define ext2_clear_bit_atomic(lock,nr,addr) \
272 test_and_clear_le_bit((nr),(unsigned long *)(addr))
273#define ext2_test_bit(nr,addr) \
274 test_le_bit((nr),(unsigned long *)(addr))
275#define ext2_find_first_zero_bit(addr, size) \
276 find_first_zero_le_bit((unsigned long *)(addr), (size))
277#define ext2_find_next_zero_bit(addr, size, off) \
278 find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
279
280/* Bitmap functions for the minix filesystem. */
281#define minix_test_and_set_bit(nr,addr) \
282 test_and_set_bit((nr),(unsigned long *)(addr))
283#define minix_set_bit(nr,addr) \
284 set_bit((nr),(unsigned long *)(addr))
285#define minix_test_and_clear_bit(nr,addr) \
286 test_and_clear_bit((nr),(unsigned long *)(addr))
287#define minix_test_bit(nr,addr) \
288 test_bit((nr),(unsigned long *)(addr))
289#define minix_find_first_zero_bit(addr,size) \
290 find_first_zero_bit((unsigned long *)(addr),(size))
291
292#endif /* __KERNEL__ */
293
294#endif /* defined(_SPARC64_BITOPS_H) */