1 /* $FreeBSD: src/sys/gnu/ext2fs/alpha-bitops.h,v 1.1.2.1 2001/08/14 18:03:19 gallatin Exp $ */
2 /* $DragonFly: src/sys/vfs/gnu/ext2fs/Attic/alpha-bitops.h,v 1.2 2003/06/17 04:28:33 dillon Exp $ */
3 #ifndef _ALPHA_BITOPS_H
4 #define _ALPHA_BITOPS_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).
21 static __inline unsigned int set_bit(unsigned long, volatile void *);
22 static __inline unsigned int clear_bit(unsigned long, volatile void *);
23 static __inline unsigned int change_bit(unsigned long, volatile void *);
24 static __inline unsigned int test_bit(int, volatile void *);
25 static __inline unsigned long ffz_b(unsigned long x);
26 static __inline unsigned long ffz(unsigned long);
27 /* static __inline int ffs(int); */
28 static __inline void * memscan(void *, int, size_t);
30 static __inline unsigned long hweight64(unsigned long);
32 static __inline unsigned long
33 find_next_zero_bit(void *, unsigned long, unsigned long);
35 static __inline unsigned int set_bit(unsigned long nr, volatile void * addr)
39 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
49 ".section .text2,\"ax\"\n"
52 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
53 :"Ir" (1UL << (nr & 31)), "m" (*m));
57 static __inline unsigned int clear_bit(unsigned long nr, volatile void * addr)
61 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
71 ".section .text2,\"ax\"\n"
74 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
75 :"Ir" (1UL << (nr & 31)), "m" (*m));
79 static __inline unsigned int change_bit(unsigned long nr, volatile void * addr)
83 volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5);
90 ".section .text2,\"ax\"\n"
93 :"=&r" (temp), "=m" (*m), "=&r" (oldbit)
94 :"Ir" (1UL << (nr & 31)), "m" (*m));
98 static __inline unsigned int test_bit(int nr, volatile void * addr)
100 return 1UL & (((volatile int *) addr)[nr >> 5] >> (nr & 31));
104 * ffz = Find First Zero in word. Undefined if no zero exists,
105 * so code should check against ~0UL first..
107 * Do a binary search on the bits. Due to the nature of large
108 * constants on the alpha, it is worthwhile to split the search.
110 static __inline unsigned long ffz_b(unsigned long x)
112 unsigned long sum = 0;
114 x = ~x & -~x; /* set first 0 bit, clear others */
115 if (x & 0xF0) sum += 4;
116 if (x & 0xCC) sum += 2;
117 if (x & 0xAA) sum += 1;
122 static __inline unsigned long ffz(unsigned long word)
125 /* Whee. EV6 can calculate it directly. */
126 unsigned long result;
127 __asm__("ctlz %1,%0" : "=r"(result) : "r"(~word));
130 unsigned long bits, qofs, bofs;
132 __asm__("cmpbge %1,%2,%0" : "=r"(bits) : "r"(word), "r"(~0UL));
134 __asm__("extbl %1,%2,%0" : "=r"(bits) : "r"(word), "r"(qofs));
137 return qofs*8 + bofs;
144 * ffs: find first bit set. This is defined the same way as
145 * the libc and compiler builtin ffs routines, therefore
146 * differs in spirit from the above ffz (man ffs).
149 static __inline int ffs(int word)
151 int result = ffz(~word);
152 return word ? result+1 : 0;
157 * hweightN: returns the hamming weight (i.e. the number
158 * of bits set) of a N-bit word
162 /* Whee. EV6 can calculate it directly. */
163 static __inline unsigned long hweight64(unsigned long w)
165 unsigned long result;
166 __asm__("ctpop %1,%0" : "=r"(result) : "r"(w));
170 #define hweight32(x) hweight64((x) & 0xfffffffful)
171 #define hweight16(x) hweight64((x) & 0xfffful)
172 #define hweight8(x) hweight64((x) & 0xfful)
174 #define hweight32(x) generic_hweight32(x)
175 #define hweight16(x) generic_hweight16(x)
176 #define hweight8(x) generic_hweight8(x)
179 #endif /* __alpha_cix__ */
181 /* from lib/string.c */
182 static __inline void * memscan(void * addr, int c, size_t size)
184 unsigned char * p = (unsigned char *) addr;
197 * Find next zero bit in a bitmap reasonably efficiently..
199 static __inline unsigned long find_next_zero_bit(void * addr, unsigned long size, unsigned long offset)
201 unsigned long * p = ((unsigned long *) addr) + (offset >> 6);
202 unsigned long result = offset & ~63UL;
211 tmp |= ~0UL >> (64-offset);
219 while (size & ~63UL) {
231 return result + ffz(tmp);
235 * The optimizer actually does good code for this case..
237 #define find_first_zero_bit(addr, size) \
238 find_next_zero_bit((addr), (size), 0)
242 #define ext2_set_bit test_and_set_bit
243 #define ext2_clear_bit test_and_clear_bit
244 #define ext2_test_bit test_bit
245 #define ext2_find_first_zero_bit find_first_zero_bit
246 #define ext2_find_next_zero_bit find_next_zero_bit
248 /* Bitmap functions for the minix filesystem. */
249 #define minix_set_bit(nr,addr) test_and_set_bit(nr,addr)
250 #define minix_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
251 #define minix_test_bit(nr,addr) test_bit(nr,addr)
252 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
254 #endif /* __KERNEL__ */
256 #endif /* _ALPHA_BITOPS_H */