1 #ifndef _H8300_BITOPS_H
2 #define _H8300_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
6 * Copyright 2002, Yoshinori Sato
9 #include <linux/config.h>
10 #include <linux/compiler.h>
11 #include <asm/byteorder.h> /* swab32 */
12 #include <asm/system.h>
16 * Function prototypes to keep gcc -Wall happy
20 * ffz = Find First Zero in word. Undefined if no zero exists,
21 * so code should check against ~0UL first..
23 static __inline__ unsigned long ffz(unsigned long word)
33 : "0" (result),"r" (word));
37 #define H8300_GEN_BITOP_CONST(OP,BIT) \
39 __asm__(OP " #" #BIT ",@%0"::"r"(b_addr):"memory"); \
42 #define H8300_GEN_BITOP(FNAME,OP) \
43 static __inline__ void FNAME(int nr, volatile unsigned long* addr) \
45 volatile unsigned char *b_addr; \
46 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
47 if (__builtin_constant_p(nr)) { \
49 H8300_GEN_BITOP_CONST(OP,0) \
50 H8300_GEN_BITOP_CONST(OP,1) \
51 H8300_GEN_BITOP_CONST(OP,2) \
52 H8300_GEN_BITOP_CONST(OP,3) \
53 H8300_GEN_BITOP_CONST(OP,4) \
54 H8300_GEN_BITOP_CONST(OP,5) \
55 H8300_GEN_BITOP_CONST(OP,6) \
56 H8300_GEN_BITOP_CONST(OP,7) \
59 __asm__(OP " %w0,@%1"::"r"(nr),"r"(b_addr):"memory"); \
64 * clear_bit() doesn't provide any barrier for the compiler.
66 #define smp_mb__before_clear_bit() barrier()
67 #define smp_mb__after_clear_bit() barrier()
69 H8300_GEN_BITOP(set_bit ,"bset")
70 H8300_GEN_BITOP(clear_bit ,"bclr")
71 H8300_GEN_BITOP(change_bit,"bnot")
72 #define __set_bit(nr,addr) set_bit((nr),(addr))
73 #define __clear_bit(nr,addr) clear_bit((nr),(addr))
74 #define __change_bit(nr,addr) change_bit((nr),(addr))
76 #undef H8300_GEN_BITOP
77 #undef H8300_GEN_BITOP_CONST
79 static __inline__ int test_bit(int nr, const unsigned long* addr)
81 return (*((volatile unsigned char *)addr +
82 ((nr >> 3) ^ 3)) & (1UL << (nr & 7))) != 0;
85 #define __test_bit(nr, addr) test_bit(nr, addr)
87 #define H8300_GEN_TEST_BITOP_CONST_INT(OP,BIT) \
89 __asm__("stc ccr,%w1\n\t" \
91 "bld #" #BIT ",@%4\n\t" \
92 OP " #" #BIT ",@%4\n\t" \
95 : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
96 : "0" (retval),"r" (b_addr) \
100 #define H8300_GEN_TEST_BITOP_CONST(OP,BIT) \
102 __asm__("bld #" #BIT ",@%3\n\t" \
103 OP " #" #BIT ",@%3\n\t" \
105 : "=r"(retval),"=m"(*b_addr) \
106 : "0" (retval),"r" (b_addr) \
110 #define H8300_GEN_TEST_BITOP(FNNAME,OP) \
111 static __inline__ int FNNAME(int nr, volatile void * addr) \
115 volatile unsigned char *b_addr; \
116 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
117 if (__builtin_constant_p(nr)) { \
119 H8300_GEN_TEST_BITOP_CONST_INT(OP,0) \
120 H8300_GEN_TEST_BITOP_CONST_INT(OP,1) \
121 H8300_GEN_TEST_BITOP_CONST_INT(OP,2) \
122 H8300_GEN_TEST_BITOP_CONST_INT(OP,3) \
123 H8300_GEN_TEST_BITOP_CONST_INT(OP,4) \
124 H8300_GEN_TEST_BITOP_CONST_INT(OP,5) \
125 H8300_GEN_TEST_BITOP_CONST_INT(OP,6) \
126 H8300_GEN_TEST_BITOP_CONST_INT(OP,7) \
129 __asm__("stc ccr,%w1\n\t" \
130 "orc #0x80,ccr\n\t" \
137 : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
138 : "0" (retval),"r" (b_addr),"r"(nr) \
144 static __inline__ int __ ## FNNAME(int nr, volatile void * addr) \
147 volatile unsigned char *b_addr; \
148 b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
149 if (__builtin_constant_p(nr)) { \
151 H8300_GEN_TEST_BITOP_CONST(OP,0) \
152 H8300_GEN_TEST_BITOP_CONST(OP,1) \
153 H8300_GEN_TEST_BITOP_CONST(OP,2) \
154 H8300_GEN_TEST_BITOP_CONST(OP,3) \
155 H8300_GEN_TEST_BITOP_CONST(OP,4) \
156 H8300_GEN_TEST_BITOP_CONST(OP,5) \
157 H8300_GEN_TEST_BITOP_CONST(OP,6) \
158 H8300_GEN_TEST_BITOP_CONST(OP,7) \
161 __asm__("btst %w4,@%3\n\t" \
166 : "=r"(retval),"=m"(*b_addr) \
167 : "0" (retval),"r" (b_addr),"r"(nr) \
173 H8300_GEN_TEST_BITOP(test_and_set_bit, "bset")
174 H8300_GEN_TEST_BITOP(test_and_clear_bit, "bclr")
175 H8300_GEN_TEST_BITOP(test_and_change_bit,"bnot")
176 #undef H8300_GEN_TEST_BITOP_CONST
177 #undef H8300_GEN_TEST_BITOP_CONST_INT
178 #undef H8300_GEN_TEST_BITOP
180 #define find_first_zero_bit(addr, size) \
181 find_next_zero_bit((addr), (size), 0)
183 #define ffs(x) generic_ffs(x)
185 static __inline__ unsigned long __ffs(unsigned long word)
187 unsigned long result;
195 : "0"(result),"r"(word));
199 static __inline__ int find_next_zero_bit (const unsigned long * addr, int size, int offset)
201 unsigned long *p = (unsigned long *)(((unsigned long)addr + (offset >> 3)) & ~3);
202 unsigned long result = offset & ~31UL;
211 tmp |= ~0UL >> (32-offset);
219 while (size & ~31UL) {
232 return result + ffz(tmp);
235 static __inline__ unsigned long find_next_bit(const unsigned long *addr,
236 unsigned long size, unsigned long offset)
238 unsigned long *p = (unsigned long *)(((unsigned long)addr + (offset >> 3)) & ~3);
239 unsigned int result = offset & ~31UL;
248 tmp &= ~0UL << offset;
257 if ((tmp = *p++) != 0)
267 tmp &= ~0UL >> (32 - size);
269 return result + size;
271 return result + __ffs(tmp);
274 #define find_first_bit(addr, size) find_next_bit(addr, size, 0)
277 * Every architecture must define this function. It's the fastest
278 * way of searching a 140-bit bitmap where the first 100 bits are
279 * unlikely to be set. It's guaranteed that at least one of the 140
282 static inline int sched_find_first_bit(unsigned long *b)
287 return __ffs(b[1]) + 32;
289 return __ffs(b[2]) + 64;
291 return __ffs(b[3]) + 96;
292 return __ffs(b[4]) + 128;
296 * hweightN: returns the hamming weight (i.e. the number
297 * of bits set) of a N-bit word
300 #define hweight32(x) generic_hweight32(x)
301 #define hweight16(x) generic_hweight16(x)
302 #define hweight8(x) generic_hweight8(x)
304 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
308 volatile unsigned char *ADDR = (unsigned char *) addr;
311 mask = 1 << (nr & 0x07);
312 local_irq_save(flags);
313 retval = (mask & *ADDR) != 0;
315 local_irq_restore(flags);
318 #define ext2_set_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
320 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
324 volatile unsigned char *ADDR = (unsigned char *) addr;
327 mask = 1 << (nr & 0x07);
328 local_irq_save(flags);
329 retval = (mask & *ADDR) != 0;
331 local_irq_restore(flags);
334 #define ext2_clear_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
336 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
339 const volatile unsigned char *ADDR = (const unsigned char *) addr;
342 mask = 1 << (nr & 0x07);
343 return ((mask & *ADDR) != 0);
346 #define ext2_find_first_zero_bit(addr, size) \
347 ext2_find_next_zero_bit((addr), (size), 0)
349 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
351 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
352 unsigned long result = offset & ~31UL;
360 /* We hold the little endian value in tmp, but then the
361 * shift is illegal. So we could keep a big endian value
364 * tmp = __swab32(*(p++));
365 * tmp |= ~0UL >> (32-offset);
367 * but this would decrease performance, so we change the
371 tmp |= __swab32(~0UL >> (32-offset));
379 while(size & ~31UL) {
390 /* tmp is little endian, so we would have to swab the shift,
391 * see above. But then we have to swab tmp below for ffz, so
392 * we might as well do this here.
394 return result + ffz(__swab32(tmp) | (~0UL << size));
396 return result + ffz(__swab32(tmp));
399 /* Bitmap functions for the minix filesystem. */
400 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
401 #define minix_set_bit(nr,addr) set_bit(nr,addr)
402 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
403 #define minix_test_bit(nr,addr) test_bit(nr,addr)
404 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
406 #endif /* __KERNEL__ */
408 #define fls(x) generic_fls(x)
409 #define fls64(x) generic_fls64(x)
411 #endif /* _H8300_BITOPS_H */