00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00041 #ifndef CTRUMP_COMMON_BITMAP_H
00042 #define CTRUMP_COMMON_BITMAP_H
00043
00044 #include <strings.h>
00045 #include <string.h>
00046 #include "ctrump/common/dll.h"
00047
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051
00052 struct ctrump_mempool;
00053 typedef unsigned int ctrump_bitmap_bits_t;
00054 typedef ctrump_bitmap_bits_t *ctrump_bitmap_t;
00055
00057 #define BITMAP_NUM_BITS (sizeof(ctrump_bitmap_t)*8U)
00058
00060 #define BYTESIZE_OF_BITMAP(n) ((((n)+7U)/8U+sizeof(ctrump_bitmap_bits_t)-1U)&~(sizeof(ctrump_bitmap_bits_t)-1U))
00061
00063 #define WORDSIZE_OF_BITMAP(n) ((((n)+(BITMAP_NUM_BITS-1U))/BITMAP_NUM_BITS))
00064
00066 #define ctrump_bitmap_all_zero(bmp,nelem) memset(bmp, 0, BYTESIZE_OF_BITMAP(nelem))
00067
00068 CTRUMP_EXTDEF void ctrump_bitmap_or(ctrump_bitmap_t dest,
00069 ctrump_bitmap_t src0,
00070 ctrump_bitmap_t src1,
00071 unsigned int length);
00072
00073 CTRUMP_EXTDEF void ctrump_bitmap_nor(ctrump_bitmap_t dest,
00074 ctrump_bitmap_t src0,
00075 ctrump_bitmap_t src1,
00076 unsigned int length);
00077
00078 CTRUMP_EXTDEF void ctrump_bitmap_nand(ctrump_bitmap_t dest,
00079 ctrump_bitmap_t src0,
00080 ctrump_bitmap_t src1,
00081 unsigned int length);
00082
00083 CTRUMP_EXTDEF void ctrump_bitmap_and(ctrump_bitmap_t dest,
00084 ctrump_bitmap_t src0,
00085 ctrump_bitmap_t src1,
00086 unsigned int length);
00087
00088
00089 CTRUMP_EXTDEF void ctrump_bitmap_nand_or(ctrump_bitmap_t dest,
00090 ctrump_bitmap_t src0,
00091 ctrump_bitmap_t src1,
00092 ctrump_bitmap_t src2,
00093 unsigned int length);
00094
00095 CTRUMP_EXTDEF void ctrump_bitmap_copy(ctrump_bitmap_t dest,
00096 ctrump_bitmap_t src,
00097 unsigned int length);
00098
00099 CTRUMP_EXTDEF int ctrump_bitmap_eq(ctrump_bitmap_t x,
00100 ctrump_bitmap_t y,
00101 unsigned int length);
00102
00107 CTRUMP_EXTDEF void ctrump_bitmap_tostr(char *buf,
00108 ctrump_bitmap_t x,
00109 unsigned int length);
00110
00114 CTRUMP_EXTDEF int ctrump_print_bitmap_stderr(ctrump_bitmap_t x, unsigned int length);
00115
00119 CTRUMP_EXTDEF int ctrump_bitmap_popcnt(ctrump_bitmap_t bmp, unsigned int length);
00120
00126 static inline int
00127 ctrump_bitmap_ffs(ctrump_bitmap_t bmp, unsigned int length)
00128 {
00129 unsigned int n = WORDSIZE_OF_BITMAP(length);
00130 unsigned int i;
00131 for (i=0; i<n; i++) {
00132 if (bmp[i]) {
00133 unsigned int ret = (__builtin_ffs(bmp[i])-1) + i*BITMAP_NUM_BITS;
00134 if (ret > length)
00135 return -1;
00136 return ret;
00137 }
00138 }
00139 return -1;
00140 }
00141
00145 static inline void
00146 ctrump_bitmap_set(ctrump_bitmap_t bmp, unsigned int idx)
00147 {
00148 unsigned int wordpos = idx/(sizeof(ctrump_bitmap_bits_t)*8U);
00149 unsigned int bitpos = idx%(sizeof(ctrump_bitmap_bits_t)*8U);
00150
00151 bmp[wordpos] |= (1<<bitpos);
00152 }
00153
00157 static inline void
00158 ctrump_bitmap_clear(ctrump_bitmap_t bmp, unsigned int idx)
00159 {
00160 unsigned int wordpos = idx/(sizeof(ctrump_bitmap_bits_t)*8U);
00161 unsigned int bitpos = idx%(sizeof(ctrump_bitmap_bits_t)*8U);
00162
00163 bmp[wordpos] &= ~(1<<bitpos);
00164 }
00165
00169 static inline int
00170 ctrump_bitmap_p(ctrump_bitmap_t bmp, unsigned int idx)
00171 {
00172 unsigned int wordpos = idx/(sizeof(ctrump_bitmap_bits_t)*8U);
00173 unsigned int bitpos = idx%(sizeof(ctrump_bitmap_bits_t)*8U);
00174
00175 return bmp[wordpos]&(1<<bitpos);
00176 }
00177
00181 static inline void
00182 ctrump_bitmap_clear_all(ctrump_bitmap_t bmp, unsigned int length)
00183 {
00184 unsigned int n = WORDSIZE_OF_BITMAP(length);
00185 unsigned int i;
00186 for (i=0; i<n; i++) {
00187 bmp[i] = (ctrump_bitmap_bits_t)0;
00188 }
00189 }
00190
00194 static inline void
00195 ctrump_bitmap_set_all(ctrump_bitmap_t bmp, unsigned int length)
00196 {
00197 unsigned int n = WORDSIZE_OF_BITMAP(length);
00198 unsigned int i;
00199 for (i=0; i<n; i++) {
00200 bmp[i] = ~(ctrump_bitmap_bits_t)0;
00201 }
00202 }
00203
00204
00205 #define BMP_P ctrump_bitmap_p
00206 #define BMP_SET ctrump_bitmap_set
00207
00211 CTRUMP_EXTDEF ctrump_bitmap_t ctrump_bmp_alloc(unsigned int nbits, struct ctrump_mempool *pool);
00215 CTRUMP_EXTDEF ctrump_bitmap_t ctrump_bmp_zero(unsigned int nbits, struct ctrump_mempool *pool);
00219 CTRUMP_EXTDEF ctrump_bitmap_t ctrump_bmp_fill(unsigned int nbits, struct ctrump_mempool *pool);
00220
00221 #ifdef __cplusplus
00222 }
00223 #endif
00224
00225 #endif