1 #ifndef _H8300_BITOPS_H
2 #define _H8300_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
6 * Copyright 2002, Yoshinori Sato
9 #include <linux/kernel.h>
10 #include <linux/config.h>
11 #include <linux/compiler.h>
12 #include <asm/byteorder.h> /* swab32 */
13 #include <asm/system.h>
17 * Function prototypes to keep gcc -Wall happy
21 * ffz = Find First Zero in word. Undefined if no zero exists,
22 * so code should check against ~0UL first..
24 static __inline__ unsigned long ffz(unsigned long word)
34 : "0" (result),"r" (word));
38 #define H8300_GEN_BITOP_CONST(OP,BIT) \
40 __asm__(OP " #" #BIT ",@%0"::"r"(b_addr):"memory"); \
43 #define H8300_GEN_BITOP(FNAME,OP) \
44 static __inline__ void FNAME(int nr, volatile unsigned long* addr) \
46 volatile unsigned char *b_addr; \
47 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
48 if (__builtin_constant_p(nr)) { \
50 H8300_GEN_BITOP_CONST(OP,0) \
51 H8300_GEN_BITOP_CONST(OP,1) \
52 H8300_GEN_BITOP_CONST(OP,2) \
53 H8300_GEN_BITOP_CONST(OP,3) \
54 H8300_GEN_BITOP_CONST(OP,4) \
55 H8300_GEN_BITOP_CONST(OP,5) \
56 H8300_GEN_BITOP_CONST(OP,6) \
57 H8300_GEN_BITOP_CONST(OP,7) \
60 __asm__(OP " %w0,@%1"::"r"(nr),"r"(b_addr):"memory"); \
65 * clear_bit() doesn't provide any barrier for the compiler.
67 #define smp_mb__before_clear_bit() barrier()
68 #define smp_mb__after_clear_bit() barrier()
70 H8300_GEN_BITOP(set_bit ,"bset")
71 H8300_GEN_BITOP(clear_bit ,"bclr")
72 H8300_GEN_BITOP(change_bit,"bnot")
73 #define __set_bit(nr,addr) set_bit((nr),(addr))
74 #define __clear_bit(nr,addr) clear_bit((nr),(addr))
75 #define __change_bit(nr,addr) change_bit((nr),(addr))
77 #undef H8300_GEN_BITOP
78 #undef H8300_GEN_BITOP_CONST
80 static __inline__ int test_bit(int nr, const unsigned long* addr)
82 return (*((volatile unsigned char *)addr +
83 ((nr >> 3) ^ 3)) & (1UL << (nr & 7))) != 0;
86 #define __test_bit(nr, addr) test_bit(nr, addr)
88 #define H8300_GEN_TEST_BITOP_CONST_INT(OP,BIT) \
90 __asm__("stc ccr,%w1\n\t" \
92 "bld #" #BIT ",@%4\n\t" \
93 OP " #" #BIT ",@%4\n\t" \
96 : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
97 : "0" (retval),"r" (b_addr) \
101 #define H8300_GEN_TEST_BITOP_CONST(OP,BIT) \
103 __asm__("bld #" #BIT ",@%3\n\t" \
104 OP " #" #BIT ",@%3\n\t" \
106 : "=r"(retval),"=m"(*b_addr) \
107 : "0" (retval),"r" (b_addr) \
111 #define H8300_GEN_TEST_BITOP(FNNAME,OP) \
112 static __inline__ int FNNAME(int nr, volatile void * addr) \
116 volatile unsigned char *b_addr; \
117 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
118 if (__builtin_constant_p(nr)) { \
120 H8300_GEN_TEST_BITOP_CONST_INT(OP,0) \
121 H8300_GEN_TEST_BITOP_CONST_INT(OP,1) \
122 H8300_GEN_TEST_BITOP_CONST_INT(OP,2) \
123 H8300_GEN_TEST_BITOP_CONST_INT(OP,3) \
124 H8300_GEN_TEST_BITOP_CONST_INT(OP,4) \
125 H8300_GEN_TEST_BITOP_CONST_INT(OP,5) \
126 H8300_GEN_TEST_BITOP_CONST_INT(OP,6) \
127 H8300_GEN_TEST_BITOP_CONST_INT(OP,7) \
130 __asm__("stc ccr,%w1\n\t" \
131 "orc #0x80,ccr\n\t" \
138 : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
139 : "0" (retval),"r" (b_addr),"r"(nr) \
145 static __inline__ int __ ## FNNAME(int nr, volatile void * addr) \
148 volatile unsigned char *b_addr; \
149 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
150 if (__builtin_constant_p(nr)) { \
152 H8300_GEN_TEST_BITOP_CONST(OP,0) \
153 H8300_GEN_TEST_BITOP_CONST(OP,1) \
154 H8300_GEN_TEST_BITOP_CONST(OP,2) \
155 H8300_GEN_TEST_BITOP_CONST(OP,3) \
156 H8300_GEN_TEST_BITOP_CONST(OP,4) \
157 H8300_GEN_TEST_BITOP_CONST(OP,5) \
158 H8300_GEN_TEST_BITOP_CONST(OP,6) \
159 H8300_GEN_TEST_BITOP_CONST(OP,7) \
162 __asm__("btst %w4,@%3\n\t" \
167 : "=r"(retval),"=m"(*b_addr) \
168 : "0" (retval),"r" (b_addr),"r"(nr) \
174 H8300_GEN_TEST_BITOP(test_and_set_bit, "bset")
175 H8300_GEN_TEST_BITOP(test_and_clear_bit, "bclr")
176 H8300_GEN_TEST_BITOP(test_and_change_bit,"bnot")
177 #undef H8300_GEN_TEST_BITOP_CONST
178 #undef H8300_GEN_TEST_BITOP_CONST_INT
179 #undef H8300_GEN_TEST_BITOP
181 #define find_first_zero_bit(addr, size) \
182 find_next_zero_bit((addr), (size), 0)
184 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
186 unsigned long *p = (unsigned long *)(((unsigned long)addr + (offset >> 3)) & ~3);
187 unsigned long result = offset & ~31UL;
196 tmp |= ~0UL >> (32-offset);
204 while (size & ~31UL) {
217 return result + ffz(tmp);
220 static __inline__ unsigned long __ffs(unsigned long word)
222 unsigned long result;
230 : "0"(result),"r"(word));
234 #define ffs(x) generic_ffs(x)
235 #define fls(x) generic_fls(x)
238 * Every architecture must define this function. It's the fastest
239 * way of searching a 140-bit bitmap where the first 100 bits are
240 * unlikely to be set. It's guaranteed that at least one of the 140
243 static inline int sched_find_first_bit(unsigned long *b)
248 return __ffs(b[1]) + 32;
250 return __ffs(b[2]) + 64;
252 return __ffs(b[3]) + 96;
253 return __ffs(b[4]) + 128;
257 * hweightN: returns the hamming weight (i.e. the number
258 * of bits set) of a N-bit word
261 #define hweight32(x) generic_hweight32(x)
262 #define hweight16(x) generic_hweight16(x)
263 #define hweight8(x) generic_hweight8(x)
265 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
269 volatile unsigned char *ADDR = (unsigned char *) addr;
272 mask = 1 << (nr & 0x07);
273 local_irq_save(flags);
274 retval = (mask & *ADDR) != 0;
276 local_irq_restore(flags);
279 #define ext2_set_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
281 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
285 volatile unsigned char *ADDR = (unsigned char *) addr;
288 mask = 1 << (nr & 0x07);
289 local_irq_save(flags);
290 retval = (mask & *ADDR) != 0;
292 local_irq_restore(flags);
295 #define ext2_clear_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
297 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
300 const volatile unsigned char *ADDR = (const unsigned char *) addr;
303 mask = 1 << (nr & 0x07);
304 return ((mask & *ADDR) != 0);
307 #define ext2_find_first_zero_bit(addr, size) \
308 ext2_find_next_zero_bit((addr), (size), 0)
310 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
312 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
313 unsigned long result = offset & ~31UL;
321 /* We hold the little endian value in tmp, but then the
322 * shift is illegal. So we could keep a big endian value
325 * tmp = __swab32(*(p++));
326 * tmp |= ~0UL >> (32-offset);
328 * but this would decrease performance, so we change the
332 tmp |= __swab32(~0UL >> (32-offset));
340 while(size & ~31UL) {
351 /* tmp is little endian, so we would have to swab the shift,
352 * see above. But then we have to swab tmp below for ffz, so
353 * we might as well do this here.
355 return result + ffz(__swab32(tmp) | (~0UL << size));
357 return result + ffz(__swab32(tmp));
360 /* Bitmap functions for the minix filesystem. */
361 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
362 #define minix_set_bit(nr,addr) set_bit(nr,addr)
363 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
364 #define minix_test_bit(nr,addr) test_bit(nr,addr)
365 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
367 #endif /* __KERNEL__ */
369 #endif /* _H8300_BITOPS_H */