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
00036 #include "ctrump/common/bitmap.h"
00037 #include "ctrump/common/mempool.h"
00038 #include <string.h>
00039 #include <stdio.h>
00040
00044 static void
00045 clear_unused_bit(ctrump_bitmap_t bmp,
00046 unsigned int length)
00047 {
00048 unsigned int idx = length/BITMAP_NUM_BITS;
00049 unsigned int mod = length%BITMAP_NUM_BITS;
00050 ctrump_bitmap_bits_t mask = (((ctrump_bitmap_bits_t)1) << mod)-1;
00051 bmp[idx] = bmp[idx] & mask;
00052 }
00053
00054 #define GEN_OP2(name,OP) \
00055 void \
00056 name(ctrump_bitmap_t dest, \
00057 ctrump_bitmap_t src0, \
00058 ctrump_bitmap_t src1, \
00059 unsigned int length) \
00060 { \
00061 unsigned int n = WORDSIZE_OF_BITMAP(length); \
00062 unsigned int i; \
00063 for (i=0; i<n; i++) { \
00064 OP(dest[i],src0[i],src1[i]); \
00065 } \
00066 }
00067
00068
00069 void
00070 ctrump_bitmap_copy(ctrump_bitmap_t dest,
00071 ctrump_bitmap_t src0,
00072 unsigned int length)
00073 {
00074 unsigned int n = WORDSIZE_OF_BITMAP(length);
00075 unsigned int i;
00076 for (i=0; i<n; i++) {
00077 dest[i] = src0[i];
00078 }
00079 }
00080
00081
00082 #define OR(d,l,r) (d) = (l) | (r)
00083 #define NOR(d,l,r) (d) = (l) | ~(r)
00084 #define NAND(d,l,r) (d) = (l) & ~(r)
00085 #define AND(d,l,r) (d) = (l) & (r)
00086
00087 GEN_OP2(ctrump_bitmap_or, OR)
00088 GEN_OP2(ctrump_bitmap_nor, NOR)
00089 GEN_OP2(ctrump_bitmap_nand, NAND)
00090 GEN_OP2(ctrump_bitmap_and, AND)
00091
00092 #define GEN_OP3(name,OP) \
00093 void \
00094 name(ctrump_bitmap_t dest, \
00095 ctrump_bitmap_t src0, \
00096 ctrump_bitmap_t src1, \
00097 ctrump_bitmap_t src2, \
00098 unsigned int length) \
00099 { \
00100 unsigned int n = WORDSIZE_OF_BITMAP(length); \
00101 unsigned int i; \
00102 for (i=0; i<n; i++) { \
00103 OP(dest[i],src0[i],src1[i],src2[i]); \
00104 } \
00105 }
00106
00107 #define NAND_OR(d,s0,s1,s2) (d) = (((s0)&~(s1)) | (s2))
00108
00109 GEN_OP3(ctrump_bitmap_nand_or,NAND_OR)
00110
00111 int
00112 ctrump_bitmap_eq(ctrump_bitmap_t x,
00113 ctrump_bitmap_t y,
00114 unsigned int length)
00115 {
00116 unsigned int n = WORDSIZE_OF_BITMAP(length);
00117 ctrump_bitmap_bits_t mask = (1<<(length % sizeof(ctrump_bitmap_bits_t)))-1;
00118 unsigned int b;
00119 unsigned int ret = 1;
00120
00121 if (n) {
00122 while (n > 0) {
00123 n--;
00124
00125 ret = ret & (x[n] == y[n]);
00126 }
00127
00128 b = (x[n]&mask) == (y[n]&mask);
00129
00130 return ret & b;
00131 } else {
00132 return 1;
00133 }
00134 }
00135
00136 void
00137 ctrump_bitmap_tostr(char *buf,
00138 ctrump_bitmap_t x,
00139 unsigned int length)
00140 {
00141 int i;
00142 for (i=0; i<length; i++) {
00143 if (ctrump_bitmap_p(x,i)) {
00144 buf[i] = '1';
00145 } else {
00146 buf[i] = '0';
00147 }
00148 }
00149 }
00150
00151 int
00152 ctrump_print_bitmap_stderr(ctrump_bitmap_t x, unsigned int length)
00153 {
00154 char strbuf[length];
00155 int r;
00156 ctrump_bitmap_tostr(strbuf, x, length);
00157 r = fwrite(strbuf, 1, length, stderr);
00158 if (r < 0)
00159 return -1;
00160 fputc('\n', stderr);
00161 return 0;
00162 }
00163
00164 ctrump_bitmap_t
00165 ctrump_bmp_zero(unsigned int nbits, struct ctrump_mempool *pool)
00166 {
00167 ctrump_bitmap_t ret;
00168 int wsize = WORDSIZE_OF_BITMAP(nbits);
00169 int bsize = BYTESIZE_OF_BITMAP(nbits);
00170 int i;
00171 ret = ctrump_mempool_alloc(pool, bsize);
00172
00173 for (i=0; i<wsize; i++) {
00174 ret[i] = (ctrump_bitmap_bits_t)0;
00175 }
00176
00177 return ret;
00178 }
00179
00180 ctrump_bitmap_t
00181 ctrump_bmp_fill(unsigned int nbits, struct ctrump_mempool *pool)
00182 {
00183 ctrump_bitmap_t ret;
00184 int wsize = WORDSIZE_OF_BITMAP(nbits);
00185 int bsize = BYTESIZE_OF_BITMAP(nbits);
00186 int i;
00187 ret = ctrump_mempool_alloc(pool, bsize);
00188
00189 for (i=0; i<wsize; i++) {
00190 ret[i] = ~(ctrump_bitmap_bits_t)0;
00191 }
00192
00193 return ret;
00194 }
00195
00196 ctrump_bitmap_t
00197 ctrump_bmp_alloc(unsigned int nbits, struct ctrump_mempool *pool)
00198 {
00199 int size = BYTESIZE_OF_BITMAP(nbits);
00200 return ctrump_mempool_alloc(pool, size);
00201 }
00202
00203
00204 int
00205 ctrump_bitmap_popcnt(ctrump_bitmap_t bmp,
00206 unsigned int length)
00207 {
00208 unsigned int n = WORDSIZE_OF_BITMAP(length);
00209 int i;
00210 int cnt = 0;
00211
00212 clear_unused_bit(bmp, length);
00213
00214 for (i=0; i<n; i++) {
00215 #ifndef __GNUC__
00216 #error "require __builtin_popcount"
00217 #endif
00218 cnt += __builtin_popcount(bmp[i]);
00219 }
00220
00221 return cnt;
00222 }
00223