[1] | 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 | |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <bits.h> |
---|
| 27 | |
---|
[23] | 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 | |
---|
[1] | 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 | |
---|
[351] | 67 | return (bitmap[word] & ( 1 << bit )) != 0; |
---|
[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 | { |
---|
[473] | 83 | if( bit == 0 ) val = 0xFFFFFFFF; |
---|
| 84 | else val = (uint32_t)((1 << (32 - bit)) - 1); |
---|
| 85 | |
---|
[1] | 86 | bitmap[word] |= (val << bit); |
---|
| 87 | word++; |
---|
| 88 | len -= (32 - bit); |
---|
| 89 | bit = 0; |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | bitmap[word] |= (((1 << len ) - 1) << bit); |
---|
| 94 | break; |
---|
| 95 | } |
---|
| 96 | } |
---|
[11] | 97 | } // bitmap_set_range() |
---|
[1] | 98 | |
---|
| 99 | /////////////////////////////////////////// |
---|
| 100 | void bitmap_clear_range( bitmap_t * bitmap, |
---|
| 101 | uint32_t index, |
---|
| 102 | uint32_t len ) |
---|
| 103 | { |
---|
| 104 | uint32_t val; |
---|
| 105 | uint32_t word = index / 32; |
---|
| 106 | uint32_t bit = index % 32; |
---|
| 107 | |
---|
| 108 | while((len > 0)) |
---|
| 109 | { |
---|
| 110 | if((len + bit) >= 32) |
---|
| 111 | { |
---|
[473] | 112 | if( bit == 0 ) val = 0xFFFFFFFF; |
---|
| 113 | else val = (uint32_t)((1 << (32 - bit)) - 1); |
---|
| 114 | |
---|
[1] | 115 | bitmap[word] &= ~(val << bit); |
---|
| 116 | word++; |
---|
| 117 | len -= (32 - bit); |
---|
| 118 | bit = 0; |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | bitmap[word] &= ~(((1 << len ) - 1) << bit); |
---|
| 123 | break; |
---|
| 124 | } |
---|
| 125 | } |
---|
[11] | 126 | } // bitmap_clear_range() |
---|
[1] | 127 | |
---|
| 128 | /////////////////////////////////////// |
---|
| 129 | uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
| 130 | uint32_t index, |
---|
| 131 | uint32_t size ) |
---|
| 132 | { |
---|
[11] | 133 | uint32_t max_word; |
---|
[1] | 134 | uint32_t word = index / 32; |
---|
| 135 | uint32_t bit = index % 32; |
---|
| 136 | |
---|
[11] | 137 | if( index < size ) |
---|
| 138 | { |
---|
| 139 | if( bit != 0 ) |
---|
| 140 | { |
---|
| 141 | for( ; bit < 32; bit++) |
---|
| 142 | { |
---|
| 143 | if((bitmap[word] & (1 << bit)) ) return (word*32 + bit); |
---|
| 144 | } |
---|
| 145 | word++; |
---|
| 146 | } |
---|
[1] | 147 | |
---|
[11] | 148 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 149 | |
---|
| 150 | for( ; word < max_word ; word++ ) |
---|
| 151 | { |
---|
| 152 | if(bitmap[word] != 0) |
---|
| 153 | { |
---|
| 154 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 155 | { |
---|
| 156 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
| 157 | } |
---|
| 158 | } |
---|
[1] | 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
[11] | 162 | return -1; |
---|
| 163 | |
---|
| 164 | } // bitmap_ffs2() |
---|
| 165 | |
---|
[1] | 166 | /////////////////////////////////////// |
---|
| 167 | uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
| 168 | uint32_t index, |
---|
| 169 | uint32_t size ) |
---|
| 170 | { |
---|
[11] | 171 | uint32_t max_word; |
---|
[1] | 172 | uint32_t word = index / 32; |
---|
| 173 | uint32_t bit = index % 32; |
---|
[11] | 174 | |
---|
| 175 | if( index < size ) |
---|
| 176 | { |
---|
| 177 | if( bit != 0 ) |
---|
| 178 | { |
---|
| 179 | for( ; bit < 32; bit++) |
---|
| 180 | { |
---|
| 181 | if( (bitmap[word] & (1 << bit)) == 0) return (word*32 + bit); |
---|
| 182 | } |
---|
| 183 | word++; |
---|
| 184 | } |
---|
[1] | 185 | |
---|
[11] | 186 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 187 | |
---|
| 188 | for( ; word < max_word ; word++ ) |
---|
| 189 | { |
---|
| 190 | if(bitmap[word] != 0xFFFFFFFF) |
---|
| 191 | { |
---|
| 192 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 193 | { |
---|
| 194 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
| 195 | } |
---|
| 196 | } |
---|
[1] | 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
[11] | 200 | return -1; |
---|
| 201 | |
---|
| 202 | } // bitmap_ffc2() |
---|
| 203 | |
---|
[1] | 204 | ////////////////////////////////////// |
---|
| 205 | uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
| 206 | uint32_t size ) |
---|
| 207 | { |
---|
[11] | 208 | uint32_t max_word; |
---|
[1] | 209 | uint32_t word; |
---|
| 210 | uint32_t bit; |
---|
[11] | 211 | |
---|
| 212 | if( size ) |
---|
| 213 | { |
---|
| 214 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 215 | |
---|
| 216 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 217 | { |
---|
| 218 | if(bitmap[word] != 0) |
---|
| 219 | { |
---|
| 220 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 221 | { |
---|
| 222 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
---|
| 223 | } |
---|
| 224 | } |
---|
[1] | 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
[11] | 228 | return -1; |
---|
| 229 | |
---|
| 230 | } // bitmap_ffs() |
---|
| 231 | |
---|
[1] | 232 | ////////////////////////////////////// |
---|
| 233 | uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
| 234 | uint32_t size ) |
---|
| 235 | { |
---|
[11] | 236 | uint32_t max_word; |
---|
[1] | 237 | uint32_t word; |
---|
| 238 | uint32_t bit; |
---|
| 239 | |
---|
[11] | 240 | if( size ) |
---|
| 241 | { |
---|
| 242 | max_word = ( (size-1) >>5 ) + 1; |
---|
| 243 | |
---|
| 244 | for( word = 0 ; word < max_word ; word++ ) |
---|
| 245 | { |
---|
| 246 | if(bitmap[word] != 0XFFFFFFFF) |
---|
| 247 | { |
---|
| 248 | for(bit = 0 ; bit < 32 ; bit++) |
---|
| 249 | { |
---|
| 250 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
---|
| 251 | } |
---|
| 252 | } |
---|
[1] | 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
[11] | 256 | return -1; |
---|
| 257 | |
---|
| 258 | } // bitmap_ffc() |
---|
| 259 | |
---|