ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
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 
27 #define B_BLOCK_TYPE uint_fast32_t
29 #define B_BLOCK_SIZE ((unsigned)sizeof(B_BLOCK_TYPE))
31 #define B_BITS_PER_BLOCK (B_BLOCK_SIZE * CHAR_BIT)
33 #define B_MASK ((B_BLOCK_TYPE)1U)
35 #define B_UNION_CAST(bitmap) ((B_BLOCK_TYPE *)(bitmap))
36 
38 #define B_MOD_OF_BPB(n) (((unsigned)(n)) & ((unsigned)(B_BITS_PER_BLOCK - 1)))
39 
41 #define B_SET_BIT_AT(B, K) ((B) |= (B_MASK << (K)))
43 #define B_RESET_BIT_AT(B, K) ((B) &= ~(B_MASK << (K)))
45 #define B_CHECK_BIT_AT(B, K) ((B) & (B_MASK << (K)))
46 
48 #define B_SET_BIT(A, I) B_SET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
50 #define B_RESET_BIT(A, I) B_RESET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
52 #define B_CHECK_BIT(A, I) B_CHECK_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
53 
63 #define bitmap_required_size(requested_bits) \
64  ((((requested_bits) / B_BITS_PER_BLOCK) + (B_MOD_OF_BPB(requested_bits) != 0)) * B_BLOCK_SIZE)
65 
78 #define bitmap_initialize(memory_pointer, requested_bits) \
79  memset(memory_pointer, 0, bitmap_required_size(requested_bits))
80 
88 #define bitmap_set(bitmap, bit_index) (B_SET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
89 
97 #define bitmap_reset(bitmap, bit_index) (B_RESET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
98 
107 #define bitmap_check(bitmap, bit_index) (B_CHECK_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))) != 0)
108 
118 #define bitmap_count_set(bitmap, bitmap_size) \
119  __extension__({ \
120  unsigned __i = (bitmap_size) / B_BLOCK_SIZE; \
121  unsigned __ret = 0; \
122  B_BLOCK_TYPE *__block_b = B_UNION_CAST(bitmap); \
123  while(__i--) { \
124  __ret += intrinsics_popcount(__block_b[__i]); \
125  } \
126  __ret; \
127  })
128 
138 #define bitmap_count_reset(bitmap, bitmap_size) \
139  __extension__({ (bitmap_size) * CHAR_BIT - bitmap_count_set(bitmap, bitmap_size); })
140 
150 #define bitmap_first_reset(bitmap, bitmap_size) \
151  __extension__({ \
152  unsigned __i, __blocks = (bitmap_size) / B_BLOCK_SIZE; \
153  unsigned __ret = UINT_MAX; \
154  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
155  for(__i = 0; __i < __blocks; ++__i) { \
156  if((__cur_block = ~__block_b[__i])) { \
157  __ret = intrinsics_ctz(__cur_block); \
158  break; \
159  } \
160  } \
161  __ret; \
162  })
163 
173 #define bitmap_foreach_set(bitmap, bitmap_size, func) \
174  __extension__({ \
175  unsigned __i, __fnd, __blocks = (bitmap_size) / B_BLOCK_SIZE; \
176  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
177  for(__i = 0; __i < __blocks; ++__i) { \
178  if((__cur_block = __block_b[__i])) { \
179  do { \
180  __fnd = intrinsics_ctz(__cur_block); \
181  B_RESET_BIT_AT(__cur_block, __fnd); \
182  func((__fnd + __i * B_BITS_PER_BLOCK)); \
183  } while(__cur_block); \
184  } \
185  } \
186  })
187 
198 #define bitmap_merge_or(dest, source, bitmap_size) \
199  __extension__({ \
200  unsigned __i = (bitmap_size) / B_BLOCK_SIZE; \
201  B_BLOCK_TYPE *__s_blocks = B_UNION_CAST(source); \
202  B_BLOCK_TYPE *__d_blocks = B_UNION_CAST(dest); \
203  while(__i--) { \
204  __d_blocks[__i] |= __s_blocks[__i]; \
205  } \
206  __d_blocks; \
207  })
unsigned char block_bitmap
The type of a generic bitmap.
Definition: bitmap.h:22
Easier access to compiler extensions.