1 | /* |
---|
2 | * bits.c - bits manipulation functions implementation |
---|
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 | #include <hal_types.h> |
---|
26 | #include <bits.h> |
---|
27 | |
---|
28 | //////////////////////////////////// |
---|
29 | void bitmap_init( bitmap_t * bitmap, |
---|
30 | uint32_t len ) |
---|
31 | { |
---|
32 | uint32_t word; |
---|
33 | uint32_t nwords = BITMAP_SIZE( len ); |
---|
34 | for( word = 0 ; word < nwords ; word++ ) |
---|
35 | { |
---|
36 | bitmap[word] = 0; |
---|
37 | } |
---|
38 | } // end bitmap_init() |
---|
39 | |
---|
40 | ////////////////////////////////////////// |
---|
41 | inline void bitmap_set( bitmap_t * bitmap, |
---|
42 | uint32_t index ) |
---|
43 | { |
---|
44 | uint32_t word = index / 32; |
---|
45 | uint32_t bit = index % 32; |
---|
46 | |
---|
47 | bitmap[word] |= ( 1 << bit ); |
---|
48 | } |
---|
49 | |
---|
50 | //////////////////////////////////////////// |
---|
51 | inline void bitmap_clear( bitmap_t * bitmap, |
---|
52 | uint32_t index ) |
---|
53 | { |
---|
54 | uint32_t word = index / 32; |
---|
55 | uint32_t bit = index % 32; |
---|
56 | |
---|
57 | bitmap[word] &= ~( 1 << bit ); |
---|
58 | } |
---|
59 | |
---|
60 | ////////////////////////////////////////////// |
---|
61 | inline bool_t bitmap_state( bitmap_t * bitmap, |
---|
62 | uint32_t index ) |
---|
63 | { |
---|
64 | uint32_t word = index / 32; |
---|
65 | uint32_t bit = index % 32; |
---|
66 | |
---|
67 | return (bitmap[word] & ( 1 << bit )) && 1; |
---|
68 | } |
---|
69 | |
---|
70 | ////////////////////////////////////////// |
---|
71 | void bitmap_set_range( bitmap_t * bitmap, |
---|
72 | uint32_t index, |
---|
73 | uint32_t len ) |
---|
74 | { |
---|
75 | uint32_t val; |
---|
76 | uint32_t word = index / 32; |
---|
77 | uint32_t bit = index % 32; |
---|
78 | |
---|
79 | while((len > 0)) |
---|
80 | { |
---|
81 | if((len + bit) >= 32) |
---|
82 | { |
---|
83 | val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1; |
---|
84 | bitmap[word] |= (val << bit); |
---|
85 | word++; |
---|
86 | len -= (32 - bit); |
---|
87 | bit = 0; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | bitmap[word] |= (((1 << len ) - 1) << bit); |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | } // bitmap_set_range() |
---|
96 | |
---|
97 | /////////////////////////////////////////// |
---|
98 | void bitmap_clear_range( bitmap_t * bitmap, |
---|
99 | uint32_t index, |
---|
100 | uint32_t len ) |
---|
101 | { |
---|
102 | uint32_t val; |
---|
103 | uint32_t word = index / 32; |
---|
104 | uint32_t bit = index % 32; |
---|
105 | |
---|
106 | while((len > 0)) |
---|
107 | { |
---|
108 | if((len + bit) >= 32) |
---|
109 | { |
---|
110 | val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1; |
---|
111 | bitmap[word] &= ~(val << bit); |
---|
112 | word++; |
---|
113 | len -= (32 - bit); |
---|
114 | bit = 0; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | bitmap[word] &= ~(((1 << len ) - 1) << bit); |
---|
119 | break; |
---|
120 | } |
---|
121 | } |
---|
122 | } // bitmap_clear_range() |
---|
123 | |
---|
124 | /////////////////////////////////////// |
---|
125 | uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
126 | uint32_t index, |
---|
127 | uint32_t size ) |
---|
128 | { |
---|
129 | uint32_t max_word; |
---|
130 | uint32_t word = index / 32; |
---|
131 | uint32_t bit = index % 32; |
---|
132 | |
---|
133 | if( index < size ) |
---|
134 | { |
---|
135 | if( bit != 0 ) |
---|
136 | { |
---|
137 | for( ; bit < 32; bit++) |
---|
138 | { |
---|
139 | if((bitmap[word] & (1 << bit)) ) return (word*32 + bit); |
---|
140 | } |
---|
141 | word++; |
---|
142 | } |
---|
143 | |
---|
144 | max_word = ( (size-1) >>5 ) + 1; |
---|
145 | |
---|
146 | for( ; word < max_word ; word++ ) |
---|
147 | { |
---|
148 | if(bitmap[word] != 0) |
---|
149 | { |
---|
150 | for(bit = 0 ; bit < 32 ; bit++) |
---|
151 | { |
---|
152 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | return -1; |
---|
159 | |
---|
160 | } // bitmap_ffs2() |
---|
161 | |
---|
162 | /////////////////////////////////////// |
---|
163 | uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
164 | uint32_t index, |
---|
165 | uint32_t size ) |
---|
166 | { |
---|
167 | uint32_t max_word; |
---|
168 | uint32_t word = index / 32; |
---|
169 | uint32_t bit = index % 32; |
---|
170 | |
---|
171 | if( index < size ) |
---|
172 | { |
---|
173 | if( bit != 0 ) |
---|
174 | { |
---|
175 | for( ; bit < 32; bit++) |
---|
176 | { |
---|
177 | if( (bitmap[word] & (1 << bit)) == 0) return (word*32 + bit); |
---|
178 | } |
---|
179 | word++; |
---|
180 | } |
---|
181 | |
---|
182 | max_word = ( (size-1) >>5 ) + 1; |
---|
183 | |
---|
184 | for( ; word < max_word ; word++ ) |
---|
185 | { |
---|
186 | if(bitmap[word] != 0xFFFFFFFF) |
---|
187 | { |
---|
188 | for(bit = 0 ; bit < 32 ; bit++) |
---|
189 | { |
---|
190 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return -1; |
---|
197 | |
---|
198 | } // bitmap_ffc2() |
---|
199 | |
---|
200 | ////////////////////////////////////// |
---|
201 | uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
202 | uint32_t size ) |
---|
203 | { |
---|
204 | uint32_t max_word; |
---|
205 | uint32_t word; |
---|
206 | uint32_t bit; |
---|
207 | |
---|
208 | if( size ) |
---|
209 | { |
---|
210 | max_word = ( (size-1) >>5 ) + 1; |
---|
211 | |
---|
212 | for( word = 0 ; word < max_word ; word++ ) |
---|
213 | { |
---|
214 | if(bitmap[word] != 0) |
---|
215 | { |
---|
216 | for(bit = 0 ; bit < 32 ; bit++) |
---|
217 | { |
---|
218 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | return -1; |
---|
225 | |
---|
226 | } // bitmap_ffs() |
---|
227 | |
---|
228 | ////////////////////////////////////// |
---|
229 | uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
230 | uint32_t size ) |
---|
231 | { |
---|
232 | uint32_t max_word; |
---|
233 | uint32_t word; |
---|
234 | uint32_t bit; |
---|
235 | |
---|
236 | if( size ) |
---|
237 | { |
---|
238 | max_word = ( (size-1) >>5 ) + 1; |
---|
239 | |
---|
240 | for( word = 0 ; word < max_word ; word++ ) |
---|
241 | { |
---|
242 | if(bitmap[word] != 0XFFFFFFFF) |
---|
243 | { |
---|
244 | for(bit = 0 ; bit < 32 ; bit++) |
---|
245 | { |
---|
246 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | return -1; |
---|
253 | |
---|
254 | } // bitmap_ffc() |
---|
255 | |
---|