[1] | 1 | /* |
---|
[657] | 2 | * bits.c - bitmap API implementation |
---|
[1] | 3 | * |
---|
| 4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
[657] | 5 | * Alain Greiner (2016,2017,2018,2019,2020) |
---|
[1] | 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 | |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <bits.h> |
---|
| 27 | |
---|
[657] | 28 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 29 | ////////////// local access functions /////////////////////////////// |
---|
| 30 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 31 | |
---|
[23] | 32 | //////////////////////////////////// |
---|
| 33 | void bitmap_init( bitmap_t * bitmap, |
---|
| 34 | uint32_t len ) |
---|
| 35 | { |
---|
| 36 | uint32_t word; |
---|
| 37 | uint32_t nwords = BITMAP_SIZE( len ); |
---|
| 38 | for( word = 0 ; word < nwords ; word++ ) |
---|
| 39 | { |
---|
| 40 | bitmap[word] = 0; |
---|
| 41 | } |
---|
| 42 | } // end bitmap_init() |
---|
| 43 | |
---|
[1] | 44 | ////////////////////////////////////////// |
---|
| 45 | inline void bitmap_set( bitmap_t * bitmap, |
---|
| 46 | uint32_t index ) |
---|
| 47 | { |
---|
[657] | 48 | uint32_t word = index >> 5; |
---|
| 49 | uint32_t bit = index & 0x1F; |
---|
[1] | 50 | |
---|
| 51 | bitmap[word] |= ( 1 << bit ); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | //////////////////////////////////////////// |
---|
| 55 | inline void bitmap_clear( bitmap_t * bitmap, |
---|
| 56 | uint32_t index ) |
---|
| 57 | { |
---|
[657] | 58 | uint32_t word = index >> 5; |
---|
| 59 | uint32_t bit = index & 0x1F; |
---|
[1] | 60 | |
---|
| 61 | bitmap[word] &= ~( 1 << bit ); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | ////////////////////////////////////////////// |
---|
| 65 | inline bool_t bitmap_state( bitmap_t * bitmap, |
---|
| 66 | uint32_t index ) |
---|
| 67 | { |
---|
[657] | 68 | uint32_t word = index >> 5; |
---|
| 69 | uint32_t bit = index & 0x1F; |
---|
[1] | 70 | |
---|
[351] | 71 | return (bitmap[word] & ( 1 << bit )) != 0; |
---|
[1] | 72 | } |
---|
| 73 | |
---|
[657] | 74 | ///////////////////////////////////////// |
---|
| 75 | uint32_t bitmap_alloc( bitmap_t * bitmap, |
---|
| 76 | uint32_t size ) |
---|
| 77 | { |
---|
| 78 | uint32_t max_word; |
---|
| 79 | uint32_t max_bit; |
---|
| 80 | uint32_t word; |
---|
| 81 | uint32_t bit; |
---|
| 82 | |
---|
| 83 | if( size ) |
---|
| 84 | { |
---|
| 85 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 86 | |
---|
| 87 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 88 | { |
---|
| 89 | max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; |
---|
| 90 | |
---|
| 91 | if(bitmap[word] != 0XFFFFFFFF) |
---|
| 92 | { |
---|
| 93 | for(bit = 0 ; bit < max_bit ; bit++) |
---|
| 94 | { |
---|
| 95 | if( (bitmap[word] & (1 << bit)) == 0 ) |
---|
| 96 | { |
---|
| 97 | bitmap[word] |= (1 << bit); |
---|
| 98 | return (word*32 + bit); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return -1; |
---|
| 106 | |
---|
| 107 | } // end bitmap_alloc() |
---|
| 108 | |
---|
[1] | 109 | ////////////////////////////////////////// |
---|
| 110 | void bitmap_set_range( bitmap_t * bitmap, |
---|
| 111 | uint32_t index, |
---|
| 112 | uint32_t len ) |
---|
| 113 | { |
---|
| 114 | uint32_t val; |
---|
| 115 | uint32_t word = index / 32; |
---|
| 116 | uint32_t bit = index % 32; |
---|
| 117 | |
---|
| 118 | while((len > 0)) |
---|
| 119 | { |
---|
| 120 | if((len + bit) >= 32) |
---|
| 121 | { |
---|
[473] | 122 | if( bit == 0 ) val = 0xFFFFFFFF; |
---|
| 123 | else val = (uint32_t)((1 << (32 - bit)) - 1); |
---|
| 124 | |
---|
[1] | 125 | bitmap[word] |= (val << bit); |
---|
| 126 | word++; |
---|
| 127 | len -= (32 - bit); |
---|
| 128 | bit = 0; |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | bitmap[word] |= (((1 << len ) - 1) << bit); |
---|
| 133 | break; |
---|
| 134 | } |
---|
| 135 | } |
---|
[11] | 136 | } // bitmap_set_range() |
---|
[1] | 137 | |
---|
| 138 | /////////////////////////////////////////// |
---|
| 139 | void bitmap_clear_range( bitmap_t * bitmap, |
---|
| 140 | uint32_t index, |
---|
| 141 | uint32_t len ) |
---|
| 142 | { |
---|
| 143 | uint32_t val; |
---|
| 144 | uint32_t word = index / 32; |
---|
| 145 | uint32_t bit = index % 32; |
---|
| 146 | |
---|
| 147 | while((len > 0)) |
---|
| 148 | { |
---|
| 149 | if((len + bit) >= 32) |
---|
| 150 | { |
---|
[473] | 151 | if( bit == 0 ) val = 0xFFFFFFFF; |
---|
| 152 | else val = (uint32_t)((1 << (32 - bit)) - 1); |
---|
| 153 | |
---|
[1] | 154 | bitmap[word] &= ~(val << bit); |
---|
| 155 | word++; |
---|
| 156 | len -= (32 - bit); |
---|
| 157 | bit = 0; |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | bitmap[word] &= ~(((1 << len ) - 1) << bit); |
---|
| 162 | break; |
---|
| 163 | } |
---|
| 164 | } |
---|
[11] | 165 | } // bitmap_clear_range() |
---|
[1] | 166 | |
---|
| 167 | /////////////////////////////////////// |
---|
| 168 | uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
| 169 | uint32_t index, |
---|
| 170 | uint32_t size ) |
---|
| 171 | { |
---|
[11] | 172 | uint32_t max_word; |
---|
[1] | 173 | uint32_t word = index / 32; |
---|
| 174 | uint32_t bit = index % 32; |
---|
| 175 | |
---|
[11] | 176 | if( index < size ) |
---|
| 177 | { |
---|
| 178 | if( bit != 0 ) |
---|
| 179 | { |
---|
| 180 | for( ; bit < 32; bit++) |
---|
| 181 | { |
---|
| 182 | if((bitmap[word] & (1 << bit)) ) return (word*32 + bit); |
---|
| 183 | } |
---|
| 184 | word++; |
---|
| 185 | } |
---|
[1] | 186 | |
---|
[11] | 187 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 188 | |
---|
| 189 | for( ; word < max_word ; word++ ) |
---|
| 190 | { |
---|
| 191 | if(bitmap[word] != 0) |
---|
| 192 | { |
---|
| 193 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 194 | { |
---|
| 195 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
| 196 | } |
---|
| 197 | } |
---|
[1] | 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[11] | 201 | return -1; |
---|
| 202 | |
---|
| 203 | } // bitmap_ffs2() |
---|
| 204 | |
---|
[1] | 205 | /////////////////////////////////////// |
---|
| 206 | uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
| 207 | uint32_t index, |
---|
| 208 | uint32_t size ) |
---|
| 209 | { |
---|
[11] | 210 | uint32_t max_word; |
---|
[1] | 211 | uint32_t word = index / 32; |
---|
| 212 | uint32_t bit = index % 32; |
---|
[11] | 213 | |
---|
| 214 | if( index < size ) |
---|
| 215 | { |
---|
| 216 | if( bit != 0 ) |
---|
| 217 | { |
---|
| 218 | for( ; bit < 32; bit++) |
---|
| 219 | { |
---|
| 220 | if( (bitmap[word] & (1 << bit)) == 0) return (word*32 + bit); |
---|
| 221 | } |
---|
| 222 | word++; |
---|
| 223 | } |
---|
[1] | 224 | |
---|
[11] | 225 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 226 | |
---|
| 227 | for( ; word < max_word ; word++ ) |
---|
| 228 | { |
---|
| 229 | if(bitmap[word] != 0xFFFFFFFF) |
---|
| 230 | { |
---|
| 231 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 232 | { |
---|
| 233 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
| 234 | } |
---|
| 235 | } |
---|
[1] | 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[11] | 239 | return -1; |
---|
| 240 | |
---|
| 241 | } // bitmap_ffc2() |
---|
| 242 | |
---|
[1] | 243 | ////////////////////////////////////// |
---|
| 244 | uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
| 245 | uint32_t size ) |
---|
| 246 | { |
---|
[11] | 247 | uint32_t max_word; |
---|
[657] | 248 | uint32_t max_bit; |
---|
[1] | 249 | uint32_t word; |
---|
| 250 | uint32_t bit; |
---|
[11] | 251 | |
---|
| 252 | if( size ) |
---|
| 253 | { |
---|
| 254 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 255 | |
---|
| 256 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 257 | { |
---|
[657] | 258 | max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; |
---|
| 259 | |
---|
[11] | 260 | if(bitmap[word] != 0) |
---|
| 261 | { |
---|
[657] | 262 | for(bit = 0 ; bit < max_bit ; bit++) |
---|
[11] | 263 | { |
---|
| 264 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
| 265 | } |
---|
| 266 | } |
---|
[1] | 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[11] | 270 | return -1; |
---|
| 271 | |
---|
| 272 | } // bitmap_ffs() |
---|
| 273 | |
---|
[1] | 274 | ////////////////////////////////////// |
---|
| 275 | uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
| 276 | uint32_t size ) |
---|
| 277 | { |
---|
[11] | 278 | uint32_t max_word; |
---|
[657] | 279 | uint32_t max_bit; |
---|
[1] | 280 | uint32_t word; |
---|
| 281 | uint32_t bit; |
---|
| 282 | |
---|
[11] | 283 | if( size ) |
---|
| 284 | { |
---|
| 285 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 286 | |
---|
| 287 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 288 | { |
---|
[657] | 289 | max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; |
---|
| 290 | |
---|
[11] | 291 | if(bitmap[word] != 0XFFFFFFFF) |
---|
| 292 | { |
---|
[657] | 293 | for(bit = 0 ; bit < max_bit ; bit++) |
---|
[11] | 294 | { |
---|
| 295 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
| 296 | } |
---|
| 297 | } |
---|
[1] | 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
[11] | 301 | return -1; |
---|
| 302 | |
---|
| 303 | } // bitmap_ffc() |
---|
| 304 | |
---|
[657] | 305 | |
---|
| 306 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 307 | ////////////// remote access functions /////////////////////////////// |
---|
| 308 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 309 | |
---|
| 310 | //////////////////////////////////////////// |
---|
| 311 | void bitmap_remote_init( xptr_t bitmap_xp, |
---|
| 312 | uint32_t len ) |
---|
| 313 | { |
---|
| 314 | bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); |
---|
| 315 | cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); |
---|
| 316 | |
---|
| 317 | uint32_t word; |
---|
| 318 | uint32_t nwords = BITMAP_SIZE( len ); |
---|
| 319 | |
---|
| 320 | for( word = 0 ; word < nwords ; word++ ) |
---|
| 321 | { |
---|
| 322 | hal_remote_s32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , 0 ); |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | //////////////////////////////////////////////////// |
---|
| 327 | inline void bitmap_remote_set( xptr_t bitmap_xp, |
---|
| 328 | uint32_t index ) |
---|
| 329 | { |
---|
| 330 | bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); |
---|
| 331 | cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); |
---|
| 332 | |
---|
| 333 | uint32_t word = index / 32; |
---|
| 334 | uint32_t bit = index % 32; |
---|
| 335 | |
---|
| 336 | hal_remote_atomic_or( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , (1 <<bit) ); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | ////////////////////////////////////////////////////// |
---|
| 340 | inline void bitmap_remote_clear( xptr_t bitmap_xp, |
---|
| 341 | uint32_t index ) |
---|
| 342 | { |
---|
| 343 | bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); |
---|
| 344 | cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); |
---|
| 345 | |
---|
| 346 | uint32_t word = index / 32; |
---|
| 347 | uint32_t bit = index % 32; |
---|
| 348 | |
---|
| 349 | hal_remote_atomic_and( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , ~(1 <<bit) ); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /////////////////////////////////////////////////// |
---|
| 353 | uint32_t bitmap_remote_alloc( xptr_t bitmap_xp, |
---|
| 354 | uint32_t size ) |
---|
| 355 | { |
---|
| 356 | uint32_t max_word; |
---|
| 357 | uint32_t max_bit; |
---|
| 358 | uint32_t word; |
---|
| 359 | uint32_t bit; |
---|
| 360 | xptr_t word_xp; |
---|
| 361 | uint32_t value; |
---|
| 362 | |
---|
| 363 | bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); |
---|
| 364 | cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); |
---|
| 365 | |
---|
| 366 | if( size ) |
---|
| 367 | { |
---|
| 368 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 369 | |
---|
| 370 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 371 | { |
---|
| 372 | max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; |
---|
| 373 | |
---|
| 374 | word_xp = XPTR( bitmap_cxy , &bitmap_ptr[word] ); |
---|
| 375 | |
---|
| 376 | value = hal_remote_l32( word_xp ); |
---|
| 377 | |
---|
| 378 | if( value != 0XFFFFFFFF ) |
---|
| 379 | { |
---|
| 380 | for(bit = 0 ; bit < max_bit ; bit++) |
---|
| 381 | { |
---|
| 382 | if( (value & (1 << bit)) == 0 ) |
---|
| 383 | { |
---|
| 384 | hal_remote_s32( word_xp , value | (1 << bit) ); |
---|
| 385 | return (word*32 + bit); |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | return -1; |
---|
| 393 | |
---|
| 394 | } // end bitmap_alloc() |
---|
| 395 | |
---|
| 396 | /////////////////////////////////////////////// |
---|
| 397 | uint32_t bitmap_remote_ffc( xptr_t bitmap_xp, |
---|
| 398 | uint32_t size ) |
---|
| 399 | { |
---|
| 400 | uint32_t max_word; |
---|
| 401 | uint32_t max_bit; |
---|
| 402 | uint32_t word; |
---|
| 403 | uint32_t bit; |
---|
| 404 | uint32_t value; |
---|
| 405 | |
---|
| 406 | bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp ); |
---|
| 407 | cxy_t bitmap_cxy = GET_CXY( bitmap_xp ); |
---|
| 408 | |
---|
| 409 | if( size ) |
---|
| 410 | { |
---|
| 411 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 412 | |
---|
| 413 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 414 | { |
---|
| 415 | max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32; |
---|
| 416 | |
---|
| 417 | value = hal_remote_l32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) ); |
---|
| 418 | |
---|
| 419 | if( value != 0xFFFFFFFF ) |
---|
| 420 | { |
---|
| 421 | for(bit = 0 ; bit < max_bit ; bit++) |
---|
| 422 | { |
---|
| 423 | if( (value & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | return -1; |
---|
| 430 | |
---|
| 431 | } // bitmap_remote_ffc() |
---|
| 432 | |
---|
| 433 | |
---|