1 #ifndef _ALPHA_BITOPS_H
2 #define _ALPHA_BITOPS_H
4 #include <asm/compiler.h>
7 * Copyright 1994, Linus Torvalds.
11 * These have to be done with inline assembly: that way the bit-setting
12 * is guaranteed to be atomic. All bit operations return 0 if the bit
13 * was cleared before the operation and != 0 if it was not.
15 * To get proper branch prediction for the main line, we must branch
16 * forward to code at the end of this object's .text section, then
17 * branch back to restart the operation.
19 * bit 0 is the LSB of addr; bit 64 is the LSB of (addr+1).
23 set_bit(unsigned long nr, volatile void * addr)
26 int *m = ((int *) addr) + (nr >> 5);
36 :"=&r" (temp), "=m" (*m)
37 :"Ir" (1UL << (nr & 31)), "m" (*m));
41 * WARNING: non atomic version.
44 __set_bit(unsigned long nr, volatile void * addr)
46 int *m = ((int *) addr) + (nr >> 5);
51 #define smp_mb__before_clear_bit() smp_mb()
52 #define smp_mb__after_clear_bit() smp_mb()
55 clear_bit(unsigned long nr, volatile void * addr)
58 int *m = ((int *) addr) + (nr >> 5);
68 :"=&r" (temp), "=m" (*m)
69 :"Ir" (1UL << (nr & 31)), "m" (*m));
73 * WARNING: non atomic version.
75 static __inline__ void
76 __clear_bit(unsigned long nr, volatile void * addr)
78 int *m = ((int *) addr) + (nr >> 5);
80 *m &= ~(1 << (nr & 31));
84 change_bit(unsigned long nr, volatile void * addr)
87 int *m = ((int *) addr) + (nr >> 5);
97 :"=&r" (temp), "=m" (*m)
98 :"Ir" (1UL << (nr & 31)), "m" (*m));
102 * WARNING: non atomic version.
104 static __inline__ void
105 __change_bit(unsigned long nr, volatile void * addr)
107 int *m = ((int *) addr) + (nr >> 5);
109 *m ^= 1 << (nr & 31);
113 test_and_set_bit(unsigned long nr, volatile void *addr)
115 unsigned long oldbit;
117 int *m = ((int *) addr) + (nr >> 5);
119 __asm__ __volatile__(
136 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
137 :"Ir" (1UL << (nr & 31)), "m" (*m) : "memory");
143 * WARNING: non atomic version.
146 __test_and_set_bit(unsigned long nr, volatile void * addr)
148 unsigned long mask = 1 << (nr & 0x1f);
149 int *m = ((int *) addr) + (nr >> 5);
153 return (old & mask) != 0;
157 test_and_clear_bit(unsigned long nr, volatile void * addr)
159 unsigned long oldbit;
161 int *m = ((int *) addr) + (nr >> 5);
163 __asm__ __volatile__(
180 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
181 :"Ir" (1UL << (nr & 31)), "m" (*m) : "memory");
187 * WARNING: non atomic version.
190 __test_and_clear_bit(unsigned long nr, volatile void * addr)
192 unsigned long mask = 1 << (nr & 0x1f);
193 int *m = ((int *) addr) + (nr >> 5);
197 return (old & mask) != 0;
201 test_and_change_bit(unsigned long nr, volatile void * addr)
203 unsigned long oldbit;
205 int *m = ((int *) addr) + (nr >> 5);
207 __asm__ __volatile__(
222 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
223 :"Ir" (1UL << (nr & 31)), "m" (*m) : "memory");
229 * WARNING: non atomic version.
231 static __inline__ int
232 __test_and_change_bit(unsigned long nr, volatile void * addr)
234 unsigned long mask = 1 << (nr & 0x1f);
235 int *m = ((int *) addr) + (nr >> 5);
239 return (old & mask) != 0;
243 test_bit(int nr, const volatile void * addr)
245 return (1UL & (((const int *) addr)[nr >> 5] >> (nr & 31))) != 0UL;
249 * ffz = Find First Zero in word. Undefined if no zero exists,
250 * so code should check against ~0UL first..
252 * Do a binary search on the bits. Due to the nature of large
253 * constants on the alpha, it is worthwhile to split the search.
255 static inline unsigned long ffz_b(unsigned long x)
257 unsigned long sum, x1, x2, x4;
259 x = ~x & -~x; /* set first 0 bit, clear others */
264 sum += (x4 != 0) * 4;
270 static inline unsigned long ffz(unsigned long word)
272 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
273 /* Whee. EV67 can calculate it directly. */
274 return __kernel_cttz(~word);
276 unsigned long bits, qofs, bofs;
278 bits = __kernel_cmpbge(word, ~0UL);
280 bits = __kernel_extbl(word, qofs);
283 return qofs*8 + bofs;
288 * __ffs = Find First set bit in word. Undefined if no set bit exists.
290 static inline unsigned long __ffs(unsigned long word)
292 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
293 /* Whee. EV67 can calculate it directly. */
294 return __kernel_cttz(word);
296 unsigned long bits, qofs, bofs;
298 bits = __kernel_cmpbge(0, word);
300 bits = __kernel_extbl(word, qofs);
303 return qofs*8 + bofs;
310 * ffs: find first bit set. This is defined the same way as
311 * the libc and compiler builtin ffs routines, therefore
312 * differs in spirit from the above __ffs.
315 static inline int ffs(int word)
317 int result = __ffs(word) + 1;
318 return word ? result : 0;
322 * fls: find last bit set.
324 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
325 static inline int fls64(unsigned long word)
327 return 64 - __kernel_ctlz(word);
330 extern const unsigned char __flsm1_tab[256];
332 static inline int fls64(unsigned long x)
334 unsigned long t, a, r;
336 t = __kernel_cmpbge (x, 0x0101010101010101UL);
338 t = __kernel_extbl (x, a);
339 r = a*8 + __flsm1_tab[t] + (x != 0);
345 static inline int fls(int x)
347 return fls64((unsigned int) x);
351 * hweightN: returns the hamming weight (i.e. the number
352 * of bits set) of a N-bit word
355 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
356 /* Whee. EV67 can calculate it directly. */
357 static inline unsigned long hweight64(unsigned long w)
359 return __kernel_ctpop(w);
362 static inline unsigned int hweight32(unsigned int w)
367 static inline unsigned int hweight16(unsigned int w)
369 return hweight64(w & 0xffff);
372 static inline unsigned int hweight8(unsigned int w)
374 return hweight64(w & 0xff);
377 #include <asm-generic/bitops/hweight.h>
379 #include <asm-generic/bitops/lock.h>
381 #endif /* __KERNEL__ */
383 #include <asm-generic/bitops/find.h>
388 * Every architecture must define this function. It's the fastest
389 * way of searching a 140-bit bitmap where the first 100 bits are
390 * unlikely to be set. It's guaranteed that at least one of the 140
393 static inline unsigned long
394 sched_find_first_bit(unsigned long b[3])
396 unsigned long b0 = b[0], b1 = b[1], b2 = b[2];
399 ofs = (b1 ? 64 : 128);
401 ofs = (b0 ? 0 : ofs);
404 return __ffs(b0) + ofs;
407 #include <asm-generic/bitops/ext2-non-atomic.h>
409 #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
410 #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
412 #include <asm-generic/bitops/minix.h>
414 #endif /* __KERNEL__ */
416 #endif /* _ALPHA_BITOPS_H */