]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c/lowlevel/arch-x86/bitops.h
Initial checkin from Perforce.
[polintos/scott/priv.git] / include / c / lowlevel / arch-x86 / bitops.h
1 // Bit manipulation functions.  These functions are not privileged.
2
3 #ifndef _LL_ARCH_BITOPS_H
4 #define _LL_ARCH_BITOPS_H
5
6 // Find First Set, counting from 0, undefined if no bits set
7 static inline int ll_ffs(unsigned long val)
8 {
9         unsigned long ret;
10         asm("bsfl %1, %0" : "=r" (ret) : "r" (val));
11         return ret;
12 }
13
14 // Find Last Set, counting from 0, undefined if no bits set
15 static inline int ll_fls(unsigned long val)
16 {
17         unsigned long ret;
18         asm("bsrl %1, %0" : "=r" (ret) : "r" (val));
19         return ret;
20 }
21
22 // As above, but on 64-bit values, regardless of sizeof(long)
23 static inline int ll_ffs64(uint64_t val)
24 {
25         if ((uint32_t)val)
26                 return ll_ffs((uint32_t)val);
27         else
28                 return ll_ffs((uint32_t)(val >> 32));
29 }
30
31 static inline int ll_fls64(uint64_t val)
32 {
33         if ((uint32_t)(val >> 32))
34                 return ll_ffs((uint32_t)(val >> 32));
35         else
36                 return ll_ffs((uint32_t)val);
37 }
38
39 // Set/Clear the nth bit in a multiword bitmap.  These functions
40 // are endian and word-size dependent.
41
42 static inline void ll_multiword_set_bit(unsigned long *bitmap, int bit)
43 {
44         asm("bts %1, %0" : "=m" (bitmap[0]) : "r" (bit) : "memory");
45 }
46
47 static inline void ll_multiword_clear_bit(unsigned long *bitmap, int bit)
48 {
49         asm("btr %1, %0" : "=m" (bitmap[0]) : "r" (bit) : "memory");
50 }
51
52 #endif