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__(
133 :"=&r" (temp
), "=m" (*m
), "=&r" (oldbit
)
134 :"Ir" (1UL << (nr
& 31)), "m" (*m
) : "memory");
140 * WARNING: non atomic version.
143 __test_and_set_bit(unsigned long nr
, volatile void * addr
)
145 unsigned long mask
= 1 << (nr
& 0x1f);
146 int *m
= ((int *) addr
) + (nr
>> 5);
150 return (old
& mask
) != 0;
154 test_and_clear_bit(unsigned long nr
, volatile void * addr
)
156 unsigned long oldbit
;
158 int *m
= ((int *) addr
) + (nr
>> 5);
160 __asm__
__volatile__(
174 :"=&r" (temp
), "=m" (*m
), "=&r" (oldbit
)
175 :"Ir" (1UL << (nr
& 31)), "m" (*m
) : "memory");
181 * WARNING: non atomic version.
184 __test_and_clear_bit(unsigned long nr
, volatile void * addr
)
186 unsigned long mask
= 1 << (nr
& 0x1f);
187 int *m
= ((int *) addr
) + (nr
>> 5);
191 return (old
& mask
) != 0;
195 test_and_change_bit(unsigned long nr
, volatile void * addr
)
197 unsigned long oldbit
;
199 int *m
= ((int *) addr
) + (nr
>> 5);
201 __asm__
__volatile__(
213 :"=&r" (temp
), "=m" (*m
), "=&r" (oldbit
)
214 :"Ir" (1UL << (nr
& 31)), "m" (*m
) : "memory");
220 * WARNING: non atomic version.
222 static __inline__
int
223 __test_and_change_bit(unsigned long nr
, volatile void * addr
)
225 unsigned long mask
= 1 << (nr
& 0x1f);
226 int *m
= ((int *) addr
) + (nr
>> 5);
230 return (old
& mask
) != 0;
234 test_bit(int nr
, const volatile void * addr
)
236 return (1UL & (((const int *) addr
)[nr
>> 5] >> (nr
& 31))) != 0UL;
240 * ffz = Find First Zero in word. Undefined if no zero exists,
241 * so code should check against ~0UL first..
243 * Do a binary search on the bits. Due to the nature of large
244 * constants on the alpha, it is worthwhile to split the search.
246 static inline unsigned long ffz_b(unsigned long x
)
248 unsigned long sum
, x1
, x2
, x4
;
250 x
= ~x
& -~x
; /* set first 0 bit, clear others */
255 sum
+= (x4
!= 0) * 4;
261 static inline unsigned long ffz(unsigned long word
)
263 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
264 /* Whee. EV67 can calculate it directly. */
265 return __kernel_cttz(~word
);
267 unsigned long bits
, qofs
, bofs
;
269 bits
= __kernel_cmpbge(word
, ~0UL);
271 bits
= __kernel_extbl(word
, qofs
);
274 return qofs
*8 + bofs
;
279 * __ffs = Find First set bit in word. Undefined if no set bit exists.
281 static inline unsigned long __ffs(unsigned long word
)
283 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
284 /* Whee. EV67 can calculate it directly. */
285 return __kernel_cttz(word
);
287 unsigned long bits
, qofs
, bofs
;
289 bits
= __kernel_cmpbge(0, word
);
291 bits
= __kernel_extbl(word
, qofs
);
294 return qofs
*8 + bofs
;
301 * ffs: find first bit set. This is defined the same way as
302 * the libc and compiler builtin ffs routines, therefore
303 * differs in spirit from the above __ffs.
306 static inline int ffs(int word
)
308 int result
= __ffs(word
) + 1;
309 return word
? result
: 0;
313 * fls: find last bit set.
315 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
316 static inline int fls64(unsigned long word
)
318 return 64 - __kernel_ctlz(word
);
321 extern const unsigned char __flsm1_tab
[256];
323 static inline int fls64(unsigned long x
)
325 unsigned long t
, a
, r
;
327 t
= __kernel_cmpbge (x
, 0x0101010101010101UL
);
329 t
= __kernel_extbl (x
, a
);
330 r
= a
*8 + __flsm1_tab
[t
] + (x
!= 0);
336 static inline int fls(int x
)
338 return fls64((unsigned int) x
);
342 * hweightN: returns the hamming weight (i.e. the number
343 * of bits set) of a N-bit word
346 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
347 /* Whee. EV67 can calculate it directly. */
348 static inline unsigned long hweight64(unsigned long w
)
350 return __kernel_ctpop(w
);
353 static inline unsigned int hweight32(unsigned int w
)
358 static inline unsigned int hweight16(unsigned int w
)
360 return hweight64(w
& 0xffff);
363 static inline unsigned int hweight8(unsigned int w
)
365 return hweight64(w
& 0xff);
368 #include <asm-generic/bitops/hweight.h>
371 #endif /* __KERNEL__ */
373 #include <asm-generic/bitops/find.h>
378 * Every architecture must define this function. It's the fastest
379 * way of searching a 140-bit bitmap where the first 100 bits are
380 * unlikely to be set. It's guaranteed that at least one of the 140
383 static inline unsigned long
384 sched_find_first_bit(unsigned long b
[3])
386 unsigned long b0
= b
[0], b1
= b
[1], b2
= b
[2];
389 ofs
= (b1
? 64 : 128);
391 ofs
= (b0
? 0 : ofs
);
394 return __ffs(b0
) + ofs
;
397 #include <asm-generic/bitops/ext2-non-atomic.h>
399 #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
400 #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
402 #include <asm-generic/bitops/minix.h>
404 #endif /* __KERNEL__ */
406 #endif /* _ALPHA_BITOPS_H */