1 /*
2  * Copyright (c) 2008 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef __BITS_H
24 #define __BITS_H
25 
26 #include <compiler.h>
27 #include <arch/ops.h>
28 
29 __BEGIN_CDECLS;
30 
31 #define clz(x) __builtin_clz(x)
32 #define ctz(x) __builtin_ctz(x)
33 
34 #define BIT(x, bit) ((x) & (1UL << (bit)))
35 #define BIT_SHIFT(x, bit) (((x) >> (bit)) & 1)
36 #define BITS(x, high, low) ((x) & (((1UL<<((high)+1))-1) & ~((1UL<<(low))-1)))
37 #define BITS_SHIFT(x, high, low) (((x) >> (low)) & ((1UL<<((high)-(low)+1))-1))
38 #define BIT_SET(x, bit) (((x) & (1UL << (bit))) ? 1 : 0)
39 
40 #define BITMAP_BITS_PER_WORD (sizeof(unsigned long) * 8)
41 #define BITMAP_NUM_WORDS(x) (((x) + BITMAP_BITS_PER_WORD - 1) / BITMAP_BITS_PER_WORD)
42 #define BITMAP_WORD(x) ((x) / BITMAP_BITS_PER_WORD)
43 #define BITMAP_BIT_IN_WORD(x) ((x) & (BITMAP_BITS_PER_WORD - 1))
44 
45 #define BITMAP_BITS_PER_INT (sizeof(unsigned int) * 8)
46 #define BITMAP_BIT_IN_INT(x) ((x) & (BITMAP_BITS_PER_INT - 1))
47 #define BITMAP_INT(x) ((x) / BITMAP_BITS_PER_INT)
48 
49 #define BIT_MASK(x) (((x) >= sizeof(unsigned long) * 8) ? (0UL-1) : ((1UL << (x)) - 1))
50 
bitmap_set(unsigned long * bitmap,int bit)51 static inline int bitmap_set(unsigned long *bitmap, int bit)
52 {
53     unsigned long mask = 1UL << BITMAP_BIT_IN_INT(bit);
54     return atomic_or(&((int *)bitmap)[BITMAP_INT(bit)], (int)mask) & (int)mask ? 1 : 0;
55 }
56 
bitmap_clear(unsigned long * bitmap,int bit)57 static inline int bitmap_clear(unsigned long *bitmap, int bit)
58 {
59     unsigned long mask = 1UL << BITMAP_BIT_IN_INT(bit);
60 
61     return atomic_and(&((int *)bitmap)[BITMAP_INT(bit)], (int)~mask) & (int)mask ? 1:0;
62 }
63 
bitmap_test(unsigned long * bitmap,int bit)64 static inline int bitmap_test(unsigned long *bitmap, int bit)
65 {
66     return BIT_SET(bitmap[BITMAP_WORD(bit)], BITMAP_BIT_IN_WORD(bit));
67 }
68 
69 /* find first zero bit starting from LSB */
_ffz(unsigned long x)70 static inline unsigned long _ffz(unsigned long x)
71 {
72     return __builtin_ffsl((long)~x) - 1;
73 }
74 
bitmap_ffz(unsigned long * bitmap,int numbits)75 static inline int bitmap_ffz(unsigned long *bitmap, int numbits)
76 {
77     uint i;
78     int bit;
79 
80     for (i = 0; i < BITMAP_NUM_WORDS(numbits); i++) {
81         if (bitmap[i] == ~0UL)
82             continue;
83         bit = i * BITMAP_BITS_PER_WORD + _ffz(bitmap[i]);
84         if (bit < numbits)
85             return bit;
86         return -1;
87     }
88     return -1;
89 }
90 
91 __END_CDECLS;
92 
93 #endif
94