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