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

Last change on this file since 640 was 635, checked in by alain, 5 years ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 11.1 KB
Line 
1/*
2 * bits.h - bits manipulation helper functions
3 *
4 * Author   Ghassan Almaless (2008,2009,2010,2011,2012)
5 *          Alain Greiner    (2016,2017,2018,2019)
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_kernel_types.h>
30
31/*********************************************************************************************
32 * These macros are NOT used by the bitmap, 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
85typedef uint32_t    bitmap_t;
86
87/*********************************************************************************************
88 * This function reset all bits in a bitmap. (array ot 32 bits words).
89 *********************************************************************************************
90 * @ bitmap  : pointer on first word in the bitmap.
91 * @ len     : number of bits to reset.
92 ********************************************************************************************/
93void bitmap_init( bitmap_t * bitmap,
94                  uint32_t   len );
95
96/*********************************************************************************************
97 * This function set a specific bit in a bitmap.
98 *********************************************************************************************
99 * @ bitmap  : pointer on the bitmap
100 * @ index   : bit index in the bitmap
101 ********************************************************************************************/
102extern inline void bitmap_set( bitmap_t * bitmap,
103                               uint32_t   index );
104
105/*********************************************************************************************
106 * This function clear a specific bit in a bitmap.
107 *********************************************************************************************
108 * @ bitmap  : pointer on the bitmap
109 * @ index   : bit index in the bitmap
110 ********************************************************************************************/
111extern inline void bitmap_clear( bitmap_t * bitmap, 
112                                 uint32_t   index );
113
114/*********************************************************************************************
115 * This function returns a specific bit in a bitmap.
116 *********************************************************************************************
117 * @ bitmap  : pointer on the bitmap
118 * @ index   : bit index in the bitmap
119 * @ returns true if bitmap[index] is set
120 ********************************************************************************************/
121extern inline bool_t bitmap_state( bitmap_t * bitmap, 
122                                   uint32_t   index );
123
124/*********************************************************************************************
125 * This function set a range of bits in a bitmap : [index ... (index + len)[
126 *********************************************************************************************
127 * @ bitmap  : pointer on the bitmap
128 * @ index   : first bit index in the bitmap
129 * @ len     : number of bits to set
130 ********************************************************************************************/
131extern void bitmap_set_range( bitmap_t * bitmap, 
132                              uint32_t   index, 
133                              uint32_t   len );
134
135/*********************************************************************************************
136 * This function reset a range of bits in a bitmap : [index ... (index + len)[
137 *********************************************************************************************
138 * @ bitmap  : pointer on the bitmap
139 * @ index   : first bit index in the bitmap
140 * @ len     : number of bits to clear
141 ********************************************************************************************/
142extern void bitmap_clear_range( bitmap_t * bitmap, 
143                                uint32_t   index, 
144                                uint32_t   len );
145
146/*********************************************************************************************
147 * This function returns the index of first bit set in a bitmap, starting from index.
148 *********************************************************************************************
149 * @ bitmap  : pointer on the bitmap
150 * @ index   : first bit to analyse in the bitmap
151 * @ size    : number of bits to analyse in bitmap
152 * @ returns index if found / returns 0xFFFFFFFF if bit not found
153 ********************************************************************************************/
154extern uint32_t bitmap_ffs2( bitmap_t * bitmap,
155                             uint32_t   index, 
156                             uint32_t   size );
157
158/*********************************************************************************************
159 * This function returns the index of first bit cleared in a bitmap, starting from index.
160 *********************************************************************************************
161 * @ bitmap  : pointer on the bitmap
162 * @ index   : first bit to analyse in the bitmap
163 * @ size    : number of bits to analyse in bitmap
164 * @ returns index if found / returns 0xFFFFFFFF if bit not found
165 ********************************************************************************************/
166extern uint32_t bitmap_ffc2( bitmap_t * bitmap,
167                             uint32_t   index, 
168                             uint32_t   size );
169
170/*********************************************************************************************
171 * This function returns the index of first bit set in a bitmap, starting from bit 0.
172 *********************************************************************************************
173 * @ bitmap  : pointer on the bitmap
174 * @ size    : number of bits to analyse in bitmap
175 * @ returns index if found / returns 0xFFFFFFFF if bit not found
176 ********************************************************************************************/
177extern uint32_t bitmap_ffs( bitmap_t * bitmap,
178                            uint32_t   size );
179
180/*********************************************************************************************
181 * This function returns the index of first bit cleared in a bitmap, starting from bit 0.
182 *********************************************************************************************
183 * @ bitmap  : pointer on the bitmap
184 * @ size    : number of bits to analyse in bitmap
185 * @ returns index if found / returns 0xFFFFFFFF if bit not found
186 ********************************************************************************************/
187extern uint32_t bitmap_ffc( bitmap_t * bitmap, 
188                            uint32_t   size );
189
190/*********************************************************************************************
191 * This function takes a positive integer <val> as input argument, and returns the smallest
192 * integer <order> such as : 1<<order >= val.
193 * In other words, <order> is the min number of bits to encode <val> values.
194 *********************************************************************************************
195 * @ val   : value to analyse
196 * @ returns logarithm value
197 ********************************************************************************************/
198static inline uint32_t bits_log2( uint32_t val )
199{
200    uint32_t i;
201
202    if( val > 0 )
203    {
204        val--;
205        for( i=0 ; val > 0 ; i++ ) val = val >> 1;
206        return i;
207    }
208    return 0;
209}
210
211#endif  /* _BITS_H_ */
Note: See TracBrowser for help on using the repository browser.