| 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 | inline void bitmap_set( bitmap_t * bitmap, |
|---|
| 30 | uint32_t index ) |
|---|
| 31 | { |
|---|
| 32 | uint32_t word = index / 32; |
|---|
| 33 | uint32_t bit = index % 32; |
|---|
| 34 | |
|---|
| 35 | bitmap[word] |= ( 1 << bit ); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | //////////////////////////////////////////// |
|---|
| 39 | inline void bitmap_clear( bitmap_t * bitmap, |
|---|
| 40 | uint32_t index ) |
|---|
| 41 | { |
|---|
| 42 | uint32_t word = index / 32; |
|---|
| 43 | uint32_t bit = index % 32; |
|---|
| 44 | |
|---|
| 45 | bitmap[word] &= ~( 1 << bit ); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | ////////////////////////////////////////////// |
|---|
| 49 | inline bool_t bitmap_state( bitmap_t * bitmap, |
|---|
| 50 | uint32_t index ) |
|---|
| 51 | { |
|---|
| 52 | uint32_t word = index / 32; |
|---|
| 53 | uint32_t bit = index % 32; |
|---|
| 54 | |
|---|
| 55 | return (bitmap[word] & ( 1 << bit )) && 1; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | ////////////////////////////////////////// |
|---|
| 59 | void bitmap_set_range( bitmap_t * bitmap, |
|---|
| 60 | uint32_t index, |
|---|
| 61 | uint32_t len ) |
|---|
| 62 | { |
|---|
| 63 | uint32_t val; |
|---|
| 64 | uint32_t word = index / 32; |
|---|
| 65 | uint32_t bit = index % 32; |
|---|
| 66 | |
|---|
| 67 | while((len > 0)) |
|---|
| 68 | { |
|---|
| 69 | if((len + bit) >= 32) |
|---|
| 70 | { |
|---|
| 71 | val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1; |
|---|
| 72 | bitmap[word] |= (val << bit); |
|---|
| 73 | word++; |
|---|
| 74 | len -= (32 - bit); |
|---|
| 75 | bit = 0; |
|---|
| 76 | } |
|---|
| 77 | else |
|---|
| 78 | { |
|---|
| 79 | bitmap[word] |= (((1 << len ) - 1) << bit); |
|---|
| 80 | break; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /////////////////////////////////////////// |
|---|
| 86 | void bitmap_clear_range( bitmap_t * bitmap, |
|---|
| 87 | uint32_t index, |
|---|
| 88 | uint32_t len ) |
|---|
| 89 | { |
|---|
| 90 | uint32_t val; |
|---|
| 91 | uint32_t word = index / 32; |
|---|
| 92 | uint32_t bit = index % 32; |
|---|
| 93 | |
|---|
| 94 | while((len > 0)) |
|---|
| 95 | { |
|---|
| 96 | if((len + bit) >= 32) |
|---|
| 97 | { |
|---|
| 98 | val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1; |
|---|
| 99 | bitmap[word] &= ~(val << bit); |
|---|
| 100 | word++; |
|---|
| 101 | len -= (32 - bit); |
|---|
| 102 | bit = 0; |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | bitmap[word] &= ~(((1 << len ) - 1) << bit); |
|---|
| 107 | break; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /////////////////////////////////////// |
|---|
| 113 | uint32_t bitmap_ffs2( bitmap_t * bitmap, |
|---|
| 114 | uint32_t index, |
|---|
| 115 | uint32_t size ) |
|---|
| 116 | { |
|---|
| 117 | uint32_t word = index / 32; |
|---|
| 118 | uint32_t bit = index % 32; |
|---|
| 119 | |
|---|
| 120 | if(bit != 0) |
|---|
| 121 | { |
|---|
| 122 | for(; bit < 32; bit++) |
|---|
| 123 | { |
|---|
| 124 | if(bitmap[word] & (1 << bit)) return (word*32 + bit); |
|---|
| 125 | } |
|---|
| 126 | word++; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | for(; word < size/32; word++) |
|---|
| 130 | { |
|---|
| 131 | if(bitmap[word] != 0) |
|---|
| 132 | { |
|---|
| 133 | for(bit = 0; bit < 32; bit++) |
|---|
| 134 | { |
|---|
| 135 | if(bitmap[word] & (1 << bit)) return (word*32 + bit); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | return 0xFFFFFFFF; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /////////////////////////////////////// |
|---|
| 143 | uint32_t bitmap_ffc2( bitmap_t * bitmap, |
|---|
| 144 | uint32_t index, |
|---|
| 145 | uint32_t size ) |
|---|
| 146 | { |
|---|
| 147 | uint32_t word = index / 32; |
|---|
| 148 | uint32_t bit = index % 32; |
|---|
| 149 | |
|---|
| 150 | if(bit != 0) |
|---|
| 151 | { |
|---|
| 152 | for(; bit < 32; bit++) |
|---|
| 153 | { |
|---|
| 154 | if((bitmap[word] & (1 << bit)) == 0) return (word*32 + bit); |
|---|
| 155 | } |
|---|
| 156 | word++; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | for(; word < size/32; word++) |
|---|
| 160 | { |
|---|
| 161 | if(bitmap[word] != 0xFFFFFFFF) |
|---|
| 162 | { |
|---|
| 163 | for(bit = 0; bit < 32; bit++) |
|---|
| 164 | { |
|---|
| 165 | if((bitmap[word] & (1 << bit)) == 0) return (word*32 + bit); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | return 0xFFFFFFFF; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | ////////////////////////////////////// |
|---|
| 173 | uint32_t bitmap_ffs( bitmap_t * bitmap, |
|---|
| 174 | uint32_t size ) |
|---|
| 175 | { |
|---|
| 176 | uint32_t word; |
|---|
| 177 | uint32_t bit; |
|---|
| 178 | |
|---|
| 179 | for(word = 0 ; word < size/32 ; word++) |
|---|
| 180 | { |
|---|
| 181 | if(bitmap[word] != 0) |
|---|
| 182 | { |
|---|
| 183 | for(bit = 0 ; bit < 32 ; bit++) |
|---|
| 184 | { |
|---|
| 185 | if( bitmap[word] & (1 << bit) ) return (word*32 + bit); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | return 0xFFFFFFFF; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | ////////////////////////////////////// |
|---|
| 193 | uint32_t bitmap_ffc( bitmap_t * bitmap, |
|---|
| 194 | uint32_t size ) |
|---|
| 195 | { |
|---|
| 196 | uint32_t word; |
|---|
| 197 | uint32_t bit; |
|---|
| 198 | |
|---|
| 199 | for(word = 0 ; word < size/32 ; word++) |
|---|
| 200 | { |
|---|
| 201 | if(bitmap[word] != 0) |
|---|
| 202 | { |
|---|
| 203 | for(bit = 0 ; bit < 32 ; bit++) |
|---|
| 204 | { |
|---|
| 205 | if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | return 0xFFFFFFFF; |
|---|
| 210 | } |
|---|
| 211 | |
|---|