The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
bitmap.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <core/intrinsics.h>
17 
18 #include <limits.h> // for CHAR_BIT
19 #include <memory.h> // for memset()
20 
22 typedef unsigned char block_bitmap;
23 
24 /* macros for internal use */
25 #define B_BLOCK_TYPE uint_fast32_t
26 #define B_BLOCK_SIZE ((unsigned) sizeof(B_BLOCK_TYPE))
27 #define B_BITS_PER_BLOCK (B_BLOCK_SIZE * CHAR_BIT)
28 #define B_MASK ((B_BLOCK_TYPE)1U)
29 #define B_UNION_CAST(bitmap) ((B_BLOCK_TYPE*)(bitmap))
30 
31 // B_BITS_PER_BLOCK is a power of 2 in any real architecture
32 #define B_MOD_OF_BPB(n) (((unsigned)(n)) & ((unsigned)(B_BITS_PER_BLOCK - 1)))
33 
34 #define B_SET_BIT_AT(B,K) ( B |= (B_MASK << K) )
35 #define B_RESET_BIT_AT(B,K) ( B &= ~(B_MASK << K) )
36 #define B_CHECK_BIT_AT(B,K) ( B & (B_MASK << K) )
37 
38 #define B_SET_BIT(A, I) \
39  B_SET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
40 
41 #define B_RESET_BIT(A, I) \
42  B_RESET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
43 
44 #define B_CHECK_BIT(A, I) \
45  B_CHECK_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
46 
56 #define bitmap_required_size(requested_bits) \
57  (( \
58  ((requested_bits) / B_BITS_PER_BLOCK) + \
59  (B_MOD_OF_BPB(requested_bits) != 0) \
60  ) * B_BLOCK_SIZE)
61 
74 #define bitmap_initialize(memory_pointer, requested_bits) \
75  memset(memory_pointer, 0, bitmap_required_size(requested_bits))
76 
84 #define bitmap_set(bitmap, bit_index) \
85  (B_SET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
86 
94 #define bitmap_reset(bitmap, bit_index) \
95  (B_RESET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
96 
105 #define bitmap_check(bitmap, bit_index) \
106  (B_CHECK_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))) != 0)
107 
117 #define bitmap_count_set(bitmap, bitmap_size) \
118 __extension__({ \
119  unsigned __i = bitmap_size / B_BLOCK_SIZE; \
120  unsigned __ret = 0; \
121  B_BLOCK_TYPE *__block_b = B_UNION_CAST(bitmap); \
122  while (__i--){ \
123  __ret += SAFE_POPC(__block_b[__i]); \
124  } \
125  __ret; \
126 })
127 
137 #define bitmap_count_reset(bitmap, bitmap_size) \
138 __extension__({ \
139  bitmap_size * CHAR_BIT - bitmap_count_set(bitmap, bitmap_size); \
140 })
141 
151 #define bitmap_first_reset(bitmap, bitmap_size) \
152 __extension__({ \
153  unsigned __i, __blocks = bitmap_size / B_BLOCK_SIZE; \
154  unsigned __ret = UINT_MAX; \
155  B_BLOCK_TYPE __cur_block, \
156  *__block_b = B_UNION_CAST(bitmap); \
157  for(__i = 0; __i < __blocks; ++__i){ \
158  if((__cur_block = ~__block_b[__i])){ \
159  __ret = B_CTZ(__cur_block); \
160  break; \
161  } \
162  } \
163  __ret; \
164 })
165 
175 #define bitmap_foreach_set(bitmap, bitmap_size, func) \
176 __extension__({ \
177  unsigned __i, __fnd, __blocks = bitmap_size / B_BLOCK_SIZE; \
178  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
179  for(__i = 0; __i < __blocks; ++__i){ \
180  if((__cur_block = __block_b[__i])){ \
181  do { \
182  __fnd = SAFE_CTZ(__cur_block); \
183  B_RESET_BIT_AT(__cur_block, __fnd); \
184  func((__fnd + __i * B_BITS_PER_BLOCK)); \
185  } while(__cur_block); \
186  } \
187  } \
188 })
189 
200 #define bitmap_merge_or(dest, source, bitmap_size) \
201 __extension__({ \
202  unsigned __i = bitmap_size / B_BLOCK_SIZE; \
203  B_BLOCK_TYPE *__s_blocks = B_UNION_CAST(source); \
204  B_BLOCK_TYPE *__d_blocks = B_UNION_CAST(dest); \
205  while (__i--){ \
206  __d_blocks[__i] |= __s_blocks[__i]; \
207  } \
208  __d_blocks; \
209 })
block_bitmap
unsigned char block_bitmap
The type of a generic bitmap.
Definition: bitmap.h:22
intrinsics.h
Easier access to compiler extensions.