1 /* $FreeBSD: src/sys/gnu/ext2fs/alpha-bitops.h,v 1.1.2.1 2001/08/14 18:03:19 gallatin Exp $ */
2 #ifndef _ALPHA_BITOPS_H
3 #define _ALPHA_BITOPS_H
6 * Copyright 1994, Linus Torvalds.
10 * These have to be done with inline assembly: that way the bit-setting
11 * is guaranteed to be atomic. All bit operations return 0 if the bit
12 * was cleared before the operation and != 0 if it was not.
14 * To get proper branch prediction for the main line, we must branch
15 * forward to code at the end of this object's .text section, then
16 * branch back to restart the operation.
18 * bit 0 is the LSB of addr; bit 64 is the LSB of (addr+1).
20 static __inline unsigned int set_bit(unsigned long, volatile void *);
21 static __inline unsigned int clear_bit(unsigned long, volatile void *);
22 static __inline unsigned int change_bit(unsigned long, volatile void *);
23 static __inline unsigned int test_bit(int, volatile void *);
24 static __inline unsigned long ffz_b(unsigned long x);
25 static __inline unsigned long ffz(unsigned long);
26 /* static __inline int ffs(int); */
27 static __inline void * memscan(void *, int, size_t);
29 static __inline unsigned long hweight64(unsigned long);
31 static __inline unsigned long
32 find_next_zero_bit(void *, unsigned long, unsigned long);
34 static __inline unsigned int set_bit(unsigned long nr, volatile void * addr)
38 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
48 ".section .text2,\"ax\"\n"
51 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
52 :"Ir" (1UL << (nr & 31)), "m" (*m));
56 static __inline unsigned int clear_bit(unsigned long nr, volatile void * addr)
60 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
70 ".section .text2,\"ax\"\n"
73 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
74 :"Ir" (1UL << (nr & 31)), "m" (*m));
78 static __inline unsigned int change_bit(unsigned long nr, volatile void * addr)
82 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
89 ".section .text2,\"ax\"\n"
92 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
93 :"Ir" (1UL << (nr & 31)), "m" (*m));
97 static __inline unsigned int test_bit(int nr, volatile void * addr)
99 return 1UL & (((volatile int *) addr)[nr >> 5] >> (nr & 31));
103 * ffz = Find First Zero in word. Undefined if no zero exists,
104 * so code should check against ~0UL first..
106 * Do a binary search on the bits. Due to the nature of large
107 * constants on the alpha, it is worthwhile to split the search.
109 static __inline unsigned long ffz_b(unsigned long x)
111 unsigned long sum = 0;
113 x = ~x & -~x; /* set first 0 bit, clear others */
114 if (x & 0xF0) sum += 4;
115 if (x & 0xCC) sum += 2;
116 if (x & 0xAA) sum += 1;
121 static __inline unsigned long ffz(unsigned long word)
124 /* Whee. EV6 can calculate it directly. */
125 unsigned long result;
126 __asm__("ctlz %1,%0" : "=r"(result) : "r"(~word));
129 unsigned long bits, qofs, bofs;
131 __asm__("cmpbge %1,%2,%0" : "=r"(bits) : "r"(word), "r"(~0UL));
133 __asm__("extbl %1,%2,%0" : "=r"(bits) : "r"(word), "r"(qofs));
136 return qofs*8 + bofs;
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).
148 static __inline int ffs(int word)
150 int result = ffz(~word);
151 return word ? result+1 : 0;
156 * hweightN: returns the hamming weight (i.e. the number
157 * of bits set) of a N-bit word
161 /* Whee. EV6 can calculate it directly. */
162 static __inline unsigned long hweight64(unsigned long w)
164 unsigned long result;
165 __asm__("ctpop %1,%0" : "=r"(result) : "r"(w));
169 #define hweight32(x) hweight64((x) & 0xfffffffful)
170 #define hweight16(x) hweight64((x) & 0xfffful)
171 #define hweight8(x) hweight64((x) & 0xfful)
173 #define hweight32(x) generic_hweight32(x)
174 #define hweight16(x) generic_hweight16(x)
175 #define hweight8(x) generic_hweight8(x)
178 #endif /* __alpha_cix__ */
180 /* from lib/string.c */
181 static __inline void * memscan(void * addr, int c, size_t size)
183 unsigned char * p = (unsigned char *) addr;
196 * Find next zero bit in a bitmap reasonably efficiently..
198 static __inline unsigned long find_next_zero_bit(void * addr, unsigned long size, unsigned long offset)
200 unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
201 unsigned long result = offset & ~63UL;
210 tmp |= ~0UL >> (64-offset);
218 while (size & ~63UL) {
230 return result + ffz(tmp);
234 * The optimizer actually does good code for this case..
236 #define find_first_zero_bit(addr, size) \
237 find_next_zero_bit((addr), (size), 0)
241 #define ext2_set_bit test_and_set_bit
242 #define ext2_clear_bit test_and_clear_bit
243 #define ext2_test_bit test_bit
244 #define ext2_find_first_zero_bit find_first_zero_bit
245 #define ext2_find_next_zero_bit find_next_zero_bit
247 /* Bitmap functions for the minix filesystem. */
248 #define minix_set_bit(nr,addr) test_and_set_bit(nr,addr)
249 #define minix_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
250 #define minix_test_bit(nr,addr) test_bit(nr,addr)
251 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
253 #endif /* __KERNEL__ */
255 #endif /* _ALPHA_BITOPS_H */