1 // Bit manipulation functions. These functions are not privileged.
6 #include <lowlevel/arch.h>
7 #include <lowlevel/types.h>
8 #include _LL_INC(bitops.h)
10 static inline int ll_get_order_round_up(unsigned long val)
12 return val != 1 ? ll_fls(val - 1) + 1 : 0;
15 static inline int ll_get_order_round_down(unsigned long val)
20 // Note that the multiword bit scans are endian and word size dependent.
21 // They return -1 if no suitable bit was found. Start and end are
24 static inline int ll_multiword_ffs(unsigned long *bitmap, int start, int len)
26 static const int bits_per_long = sizeof(unsigned long) * 8;
27 int off = start / bits_per_long;
28 int shift_first = start % bits_per_long;
31 unsigned long shifted = *bitmap >> shift_first;
34 return ll_ffs(shifted) + start;
37 start = off * bits_per_long;
40 while (off < len / bits_per_long) {
45 start += bits_per_long;
48 if (start < len && bitmap[off]) {
49 int ret = start + ll_ffs(bitmap[off]);
58 static inline int ll_multiword_ffc(unsigned long *bitmap, int start, int len)
60 static const int bits_per_long = sizeof(unsigned long) * 8;
61 int off = start / bits_per_long;
62 int shift_first = start % bits_per_long;
65 unsigned long shifted = *bitmap >> shift_first;
68 return ll_ffs(~shifted) + start;
71 start = off * bits_per_long;
74 while (off < len / bits_per_long) {
79 start += bits_per_long;
82 if (start < len && ~bitmap[off]) {
83 int ret = start + ll_ffs(~bitmap[off]);