The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
bitmap.h
Go to the documentation of this file.
1 
36 #pragma once
37 
38 #include <limits.h> // for CHAR_BIT
39 #include <memory.h> // for memset()
40 #include <core/core.h> // for UNION_CAST()
41 
43 typedef unsigned char rootsim_bitmap;
44 
45 /* macros for internal use */
46 
47 #define B_BLOCK_TYPE unsigned
48 #define B_BLOCK_SIZE ((unsigned)sizeof(B_BLOCK_TYPE))
49 #define B_BITS_PER_BLOCK (B_BLOCK_SIZE*CHAR_BIT)
50 #define B_MASK ((B_BLOCK_TYPE)1U)
51 #define B_UNION_CAST(bitmap) UNION_CAST(((rootsim_bitmap*)(bitmap)), B_BLOCK_TYPE*)
52 
53 // we leverage the fact that B_BITS_PER_BLOCK is a power of 2 in any real architecture
54 #define B_MOD_OF_BPB(n) (((unsigned)(n)) & ((unsigned)(B_BITS_PER_BLOCK - 1)))
55 
56 #define B_SET_BIT_AT(B,K) ( B |= (B_MASK << K) )
57 #define B_RESET_BIT_AT(B,K) ( B &= ~(B_MASK << K) )
58 #define B_CHECK_BIT_AT(B,K) ( B & (B_MASK << K) )
59 
60 #define B_SET_BIT(A, I) B_SET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
61 #define B_RESET_BIT(A, I) B_RESET_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
62 #define B_CHECK_BIT(A, I) B_CHECK_BIT_AT((A)[((I) / B_BITS_PER_BLOCK)], (B_MOD_OF_BPB(I)))
63 
64 #define B_CTZ(x) (\
65  __builtin_choose_expr( \
66  __builtin_types_compatible_p (__typeof__ (x), unsigned), __builtin_ctz(x),\
67  __builtin_choose_expr(\
68  __builtin_types_compatible_p (__typeof__ (x), unsigned long), __builtin_ctzl(x),\
69  __builtin_choose_expr(\
70  __builtin_types_compatible_p (__typeof__ (x), unsigned long long), __builtin_ctzll(x),\
71  (void)0))))
72 
73 #define B_POPC(x) (\
74  __builtin_choose_expr( \
75  __builtin_types_compatible_p (__typeof__ (x), unsigned), __builtin_popcount(x),\
76  __builtin_choose_expr(\
77  __builtin_types_compatible_p (__typeof__ (x), unsigned long), __builtin_popcountl(x),\
78  __builtin_choose_expr(\
79  __builtin_types_compatible_p (__typeof__ (x), unsigned long long), __builtin_popcountll(x),\
80  (void)0))))
81 
92 #define bitmap_required_size(requested_bits) ((((requested_bits)/B_BITS_PER_BLOCK) + (B_MOD_OF_BPB(requested_bits) != 0))*B_BLOCK_SIZE)
93 
107 #define bitmap_initialize(memory_pointer, requested_bits) (memset(memory_pointer, 0, bitmap_required_size(requested_bits)))
108 
116 #define bitmap_set(bitmap, bit_index) (B_SET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
117 
125 #define bitmap_reset(bitmap, bit_index) (B_RESET_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))))
126 
135 #define bitmap_check(bitmap, bit_index) (B_CHECK_BIT(B_UNION_CAST(bitmap), ((unsigned)(bit_index))) != 0)
136 
147 #define bitmap_count_reset(bitmap, bitmap_size) ({ \
148  unsigned __i, __blocks = bitmap_size / B_BLOCK_SIZE; \
149  unsigned __ret = bitmap_size * CHAR_BIT; \
150  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
151  for(__i = 0; __i < __blocks; ++__i){ \
152  if((__cur_block = __block_b[__i])){ \
153  __ret -= B_POPC(__cur_block); \
154  } \
155  } \
156  __ret; \
157  })
158 
169 #define bitmap_first_reset(bitmap, bitmap_size) ({ \
170  unsigned __i, __blocks = bitmap_size / B_BLOCK_SIZE; \
171  unsigned __ret = UINT_MAX; \
172  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
173  for(__i = 0; __i < __blocks; ++__i){ \
174  if((__cur_block = ~__block_b[__i])){ \
175  __ret = B_CTZ(__cur_block); \
176  break; \
177  } \
178  } \
179  __ret; \
180  })
181 
192 #define bitmap_foreach_set(bitmap, bitmap_size, func) ({ \
193  unsigned __i, __fnd, __blocks = bitmap_size / B_BLOCK_SIZE; \
194  B_BLOCK_TYPE __cur_block, *__block_b = B_UNION_CAST(bitmap); \
195  for(__i = 0; __i < __blocks; ++__i){ \
196  if((__cur_block = __block_b[__i])){ \
197  do{ \
198  __fnd = B_CTZ(__cur_block); \
199  B_RESET_BIT_AT(__cur_block, __fnd); \
200  func((__fnd + __i * B_BITS_PER_BLOCK)); \
201  }while(__cur_block); \
202  } \
203  } \
204  })
Core ROOT-Sim functionalities.
unsigned char rootsim_bitmap
This defines a generic bitmap.
Definition: bitmap.h:43