source: trunk/kernel/libk/bits.h @ 16

Last change on this file since 16 was 14, checked in by alain, 7 years ago

Bugs fix.

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