1 | /* |
---|
2 | * bits.h - bits manipulation helper functions |
---|
3 | * |
---|
4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _BITS_H_ |
---|
26 | #define _BITS_H_ |
---|
27 | |
---|
28 | #include <kernel_config.h> |
---|
29 | #include <hal_types.h> |
---|
30 | |
---|
31 | /********************************************************************************************* |
---|
32 | * These macros are NOT used by the bitmat, but can be useful in other contexts... [AG] |
---|
33 | *********************************************************************************************/ |
---|
34 | |
---|
35 | #define ARROUND_UP(val, size) (((val) & ((size) -1)) ? ((val) & ~((size)-1)) + (size) : (val)) |
---|
36 | #define ARROUND_DOWN(val, size) ((val) & ~((size) - 1)) |
---|
37 | |
---|
38 | #define ABS(x) (((x) < 0) ? -(x) : (x)) |
---|
39 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
---|
40 | #define MAX(x,y) (((x) < (y)) ? (y) : (x)) |
---|
41 | |
---|
42 | /********************************************************************************************** |
---|
43 | * This macro returns the smallest power of 2 value, that is larger or equal to data value. |
---|
44 | * It returns 0xFFFFFFFF if data is larger than 0x80000000. |
---|
45 | *********************************************************************************************/ |
---|
46 | #define POW2_ROUNDUP(data) ( (data <= 0x00000001) ? 0x00000001 : \ |
---|
47 | (data <= 0x00000002) ? 0x00000002 : \ |
---|
48 | (data <= 0x00000004) ? 0x00000004 : \ |
---|
49 | (data <= 0x00000008) ? 0x00000008 : \ |
---|
50 | (data <= 0x00000010) ? 0x00000010 : \ |
---|
51 | (data <= 0x00000020) ? 0x00000020 : \ |
---|
52 | (data <= 0x00000040) ? 0x00000040 : \ |
---|
53 | (data <= 0x00000080) ? 0x00000080 : \ |
---|
54 | (data <= 0x00000100) ? 0x00000100 : \ |
---|
55 | (data <= 0x00000200) ? 0x00000200 : \ |
---|
56 | (data <= 0x00000400) ? 0x00000400 : \ |
---|
57 | (data <= 0x00000800) ? 0x00000800 : \ |
---|
58 | (data <= 0x00001000) ? 0x00001000 : \ |
---|
59 | (data <= 0x00002000) ? 0x00002000 : \ |
---|
60 | (data <= 0x00004000) ? 0x00004000 : \ |
---|
61 | (data <= 0x00008000) ? 0x00008000 : \ |
---|
62 | (data <= 0x00010000) ? 0x00010000 : \ |
---|
63 | (data <= 0x00020000) ? 0x00020000 : \ |
---|
64 | (data <= 0x00040000) ? 0x00040000 : \ |
---|
65 | (data <= 0x00080000) ? 0x00080000 : \ |
---|
66 | (data <= 0x00100000) ? 0x00100000 : \ |
---|
67 | (data <= 0x00200000) ? 0x00200000 : \ |
---|
68 | (data <= 0x00400000) ? 0x00400000 : \ |
---|
69 | (data <= 0x00800000) ? 0x00800000 : \ |
---|
70 | (data <= 0x01000000) ? 0x01000000 : \ |
---|
71 | (data <= 0x02000000) ? 0x02000000 : \ |
---|
72 | (data <= 0x04000000) ? 0x04000000 : \ |
---|
73 | (data <= 0x08000000) ? 0x08000000 : \ |
---|
74 | (data <= 0x10000000) ? 0x10000000 : \ |
---|
75 | (data <= 0x20000000) ? 0x20000000 : \ |
---|
76 | (data <= 0x40000000) ? 0x40000000 : \ |
---|
77 | (data <= 0x80000000) ? 0x80000000 : 0xFFFFFFFF ) |
---|
78 | |
---|
79 | /********************************************************************************************** |
---|
80 | * This macro returns the number of 32 bits words required to register <size> entries. |
---|
81 | *********************************************************************************************/ |
---|
82 | |
---|
83 | #define BITMAP_SIZE(size) ( ((size) & 31) ? (((size)>>5) + 1) : ((size)>>5) ) |
---|
84 | |
---|
85 | typedef uint32_t bitmap_t; |
---|
86 | |
---|
87 | /********************************************************************************************* |
---|
88 | * This function reset all bits in a bitmap. (array ot 32 bits words). |
---|
89 | ********************************************************************************************* |
---|
90 | * @ bitmap : pointer on first word in the bitmap. |
---|
91 | * @ len : number of bits to reset. |
---|
92 | ********************************************************************************************/ |
---|
93 | void bitmap_init( bitmap_t * bitmap, |
---|
94 | uint32_t len ); |
---|
95 | |
---|
96 | /********************************************************************************************* |
---|
97 | * This function set a specific bit in a bitmap. |
---|
98 | ********************************************************************************************* |
---|
99 | * @ bitmap : pointer on the bitmap |
---|
100 | * @ index : bit index in the bitmap |
---|
101 | ********************************************************************************************/ |
---|
102 | extern inline void bitmap_set( bitmap_t * bitmap, |
---|
103 | uint32_t index ); |
---|
104 | |
---|
105 | /********************************************************************************************* |
---|
106 | * This function clear a specific bit in a bitmap. |
---|
107 | ********************************************************************************************* |
---|
108 | * @ bitmap : pointer on the bitmap |
---|
109 | * @ index : bit index in the bitmap |
---|
110 | ********************************************************************************************/ |
---|
111 | extern inline void bitmap_clear( bitmap_t * bitmap, |
---|
112 | uint32_t index ); |
---|
113 | |
---|
114 | /********************************************************************************************* |
---|
115 | * This function returns a specific bit in a bitmap. |
---|
116 | ********************************************************************************************* |
---|
117 | * @ bitmap : pointer on the bitmap |
---|
118 | * @ index : bit index in the bitmap |
---|
119 | * @ returns true if bitmap[index] is set |
---|
120 | ********************************************************************************************/ |
---|
121 | extern inline bool_t bitmap_state( bitmap_t * bitmap, |
---|
122 | uint32_t index ); |
---|
123 | |
---|
124 | /********************************************************************************************* |
---|
125 | * This function set a range of bits in a bitmap : [index ... (index + len)[ |
---|
126 | ********************************************************************************************* |
---|
127 | * @ bitmap : pointer on the bitmap |
---|
128 | * @ index : first bit index in the bitmap |
---|
129 | * @ len : number of bits to set |
---|
130 | ********************************************************************************************/ |
---|
131 | extern void bitmap_set_range( bitmap_t * bitmap, |
---|
132 | uint32_t index, |
---|
133 | uint32_t len ); |
---|
134 | |
---|
135 | /********************************************************************************************* |
---|
136 | * This function reset a range of bits in a bitmap : [index ... (index + len)[ |
---|
137 | ********************************************************************************************* |
---|
138 | * @ bitmap : pointer on the bitmap |
---|
139 | * @ index : first bit index in the bitmap |
---|
140 | * @ len : number of bits to clear |
---|
141 | ********************************************************************************************/ |
---|
142 | extern void bitmap_clear_range( bitmap_t * bitmap, |
---|
143 | uint32_t index, |
---|
144 | uint32_t len ); |
---|
145 | |
---|
146 | /********************************************************************************************* |
---|
147 | * This function returns the index of first bit set in a bitmap, starting from index. |
---|
148 | ********************************************************************************************* |
---|
149 | * @ bitmap : pointer on the bitmap |
---|
150 | * @ index : first bit to analyse in the bitmap |
---|
151 | * @ size : number of bits to analyse in bitmap |
---|
152 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
153 | ********************************************************************************************/ |
---|
154 | extern uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
155 | uint32_t index, |
---|
156 | uint32_t size ); |
---|
157 | |
---|
158 | /********************************************************************************************* |
---|
159 | * This function returns the index of first bit cleared in a bitmap, starting from index. |
---|
160 | ********************************************************************************************* |
---|
161 | * @ bitmap : pointer on the bitmap |
---|
162 | * @ index : first bit to analyse in the bitmap |
---|
163 | * @ size : number of bits to analyse in bitmap |
---|
164 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
165 | ********************************************************************************************/ |
---|
166 | extern uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
167 | uint32_t index, |
---|
168 | uint32_t size ); |
---|
169 | |
---|
170 | /********************************************************************************************* |
---|
171 | * This function returns the index of first bit set in a bitmap, starting from bit 0. |
---|
172 | ********************************************************************************************* |
---|
173 | * @ bitmap : pointer on the bitmap |
---|
174 | * @ size : number of bits to analyse in bitmap |
---|
175 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
176 | ********************************************************************************************/ |
---|
177 | extern uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
178 | uint32_t size ); |
---|
179 | |
---|
180 | /********************************************************************************************* |
---|
181 | * This function returns the index of first bit cleared in a bitmap, starting from bit 0. |
---|
182 | ********************************************************************************************* |
---|
183 | * @ bitmap : pointer on the bitmap |
---|
184 | * @ size : number of bits to alalyse in bitmap |
---|
185 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
186 | ********************************************************************************************/ |
---|
187 | extern uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
188 | uint32_t size ); |
---|
189 | |
---|
190 | /********************************************************************************************* |
---|
191 | * This function returns the number of bits to code a non-zero unsigned integer value. |
---|
192 | ********************************************************************************************* |
---|
193 | * @ val : value to analyse |
---|
194 | * @ returns number of bits |
---|
195 | ********************************************************************************************/ |
---|
196 | static inline uint32_t bits_nr( uint32_t val ) |
---|
197 | { |
---|
198 | register uint32_t i; |
---|
199 | |
---|
200 | for( i=0 ; val > 0 ; i++ ) |
---|
201 | val = val >> 1; |
---|
202 | |
---|
203 | return i; |
---|
204 | } |
---|
205 | |
---|
206 | /********************************************************************************************* |
---|
207 | * This function takes an unsigned integer value as input argument, and returns another |
---|
208 | * unsigned integer, that is the (base 2) logarithm of the smallest power of 2 contained |
---|
209 | * in the input value. |
---|
210 | ********************************************************************************************* |
---|
211 | * @ val : value to analyse |
---|
212 | * @ returns logarithm value |
---|
213 | ********************************************************************************************/ |
---|
214 | static inline uint32_t bits_log2( uint32_t val ) |
---|
215 | { |
---|
216 | return (val == 0) ? 1 : bits_nr( val ) - 1; |
---|
217 | } |
---|
218 | |
---|
219 | #endif /* _BITS_H_ */ |
---|