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 <almos_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 | /********************************************************************************************** |
---|
86 | * This macro declare a bitmap data structure. It is actually an array of uint32_t. |
---|
87 | * size is the total number of entries in the bitmap. |
---|
88 | *********************************************************************************************/ |
---|
89 | |
---|
90 | #define BITMAP( name , size ) uint32_t name[BITMAP_SIZE(size)] |
---|
91 | |
---|
92 | typedef uint32_t bitmap_t; |
---|
93 | |
---|
94 | /********************************************************************************************** |
---|
95 | * This function set a specific bit in a bitmap. |
---|
96 | ********************************************************************************************** |
---|
97 | |
---|
98 | * @ bitmap : pointer on the bitmap |
---|
99 | * @ index : bit index in the bitmap |
---|
100 | *********************************************************************************************/ |
---|
101 | extern inline void bitmap_set( bitmap_t * bitmap, |
---|
102 | uint32_t index ); |
---|
103 | |
---|
104 | /********************************************************************************************** |
---|
105 | * This function clear a specific bit in a bitmap. |
---|
106 | ********************************************************************************************** |
---|
107 | * @ bitmap : pointer on the bitmap |
---|
108 | * @ index : bit index in the bitmap |
---|
109 | *********************************************************************************************/ |
---|
110 | extern inline void bitmap_clear( bitmap_t * bitmap, |
---|
111 | uint32_t index ); |
---|
112 | |
---|
113 | /********************************************************************************************** |
---|
114 | * This function returns a specific bit in a bitmap. |
---|
115 | ********************************************************************************************** |
---|
116 | * @ bitmap : pointer on the bitmap |
---|
117 | * @ index : bit index in the bitmap |
---|
118 | * @ returns tru if bitmap[index] is set |
---|
119 | *********************************************************************************************/ |
---|
120 | extern inline bool_t bitmap_state( bitmap_t * bitmap, |
---|
121 | uint32_t index ); |
---|
122 | |
---|
123 | /********************************************************************************************** |
---|
124 | * This function set a range of bits in a bitmap : [index ... (index + len)[ |
---|
125 | ********************************************************************************************** |
---|
126 | * @ bitmap : pointer on the bitmap |
---|
127 | * @ index : first bit index in the bitmap |
---|
128 | * @ len : number of bits to set |
---|
129 | *********************************************************************************************/ |
---|
130 | extern void bitmap_set_range( bitmap_t * bitmap, |
---|
131 | uint32_t index, |
---|
132 | uint32_t len ); |
---|
133 | |
---|
134 | /********************************************************************************************** |
---|
135 | * This function reset a range of bits in a bitmap : [index ... (index + len)[ |
---|
136 | ********************************************************************************************** |
---|
137 | * @ bitmap : pointer on the bitmap |
---|
138 | * @ index : first bit index in the bitmap |
---|
139 | * @ len : number of bits to clear |
---|
140 | *********************************************************************************************/ |
---|
141 | extern void bitmap_clear_range( bitmap_t * bitmap, |
---|
142 | uint32_t index, |
---|
143 | uint32_t len ); |
---|
144 | |
---|
145 | /********************************************************************************************** |
---|
146 | * This function returns the index of first bit set in a bitmap, starting from index. |
---|
147 | ********************************************************************************************** |
---|
148 | * @ bitmap : pointer on the bitmap |
---|
149 | * @ index : first bit to analyse in the bitmap |
---|
150 | * @ size : number of bits to analyse in bitmap |
---|
151 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
152 | *********************************************************************************************/ |
---|
153 | extern uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
154 | uint32_t index, |
---|
155 | uint32_t size ); |
---|
156 | |
---|
157 | /********************************************************************************************** |
---|
158 | * This function returns the index of first bit cleared in a bitmap, starting from index. |
---|
159 | ********************************************************************************************** |
---|
160 | * @ bitmap : pointer on the bitmap |
---|
161 | * @ index : first bit to analyse in the bitmap |
---|
162 | * @ size : number of bits to analyse in bitmap |
---|
163 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
164 | *********************************************************************************************/ |
---|
165 | extern uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
166 | uint32_t index, |
---|
167 | uint32_t size ); |
---|
168 | |
---|
169 | /********************************************************************************************** |
---|
170 | * This function returns the index of first bit set in a bitmap, starting from bit 0. |
---|
171 | ********************************************************************************************** |
---|
172 | * @ bitmap : pointer on the bitmap |
---|
173 | * @ size : number of bits to analyse in bitmap |
---|
174 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
175 | *********************************************************************************************/ |
---|
176 | extern uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
177 | uint32_t size ); |
---|
178 | |
---|
179 | /********************************************************************************************** |
---|
180 | * This function returns the index of first bit cleared in a bitmap, starting from bit 0. |
---|
181 | ********************************************************************************************** |
---|
182 | * @ bitmap : pointer on the bitmap |
---|
183 | * @ size : number of bits to alalyse in bitmap |
---|
184 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
185 | *********************************************************************************************/ |
---|
186 | extern uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
187 | uint32_t size ); |
---|
188 | |
---|
189 | /********************************************************************************************** |
---|
190 | * This function returns the number of bits to code a non-zero unsigned integer value. |
---|
191 | ********************************************************************************************** |
---|
192 | * @ val : value to analyse |
---|
193 | * @ returns number of bits |
---|
194 | *********************************************************************************************/ |
---|
195 | static inline uint32_t bits_nr( uint32_t val ) |
---|
196 | { |
---|
197 | register uint32_t i; |
---|
198 | |
---|
199 | for( i=0 ; val > 0 ; i++ ) |
---|
200 | val = val >> 1; |
---|
201 | |
---|
202 | return i; |
---|
203 | } |
---|
204 | |
---|
205 | /********************************************************************************************** |
---|
206 | * This function takes an unsigned integer value as input argument, and returns another |
---|
207 | * unsigned integer, that is the (base 2) logarithm of the smallest power of 2 contained |
---|
208 | * in the input value. |
---|
209 | ********************************************************************************************** |
---|
210 | * @ val : value to analyse |
---|
211 | * @ returns logarithm value |
---|
212 | *********************************************************************************************/ |
---|
213 | static inline uint32_t bits_log2( uint32_t val ) |
---|
214 | { |
---|
215 | return (val == 0) ? 1 : bits_nr( val ) - 1; |
---|
216 | } |
---|
217 | |
---|
218 | #endif /* _BITS_H_ */ |
---|