Browse Source

* add prefixes for functions & defines in bitmap.[ch]

master
Alex 'AdUser' Z 8 years ago
parent
commit
461aaeaaca
  1. 36
      src/bitmap.c
  2. 22
      src/bitmap.h
  3. 3
      src/database.c
  4. 4
      src/sample.c
  5. 20
      src/simdb-query.c
  6. 22
      tests/bitmap.c

36
src/bitmap.c

@ -39,14 +39,14 @@ unsigned char dict[256] = {
}; };
int int
bitmap_compare(const unsigned char *a, simdb_bitmap_compare(const unsigned char *a,
const unsigned char *b) const unsigned char *b)
{ {
unsigned char diff = 0; unsigned char diff = 0;
size_t i = 0; size_t i = 0;
size_t cnt = 0; size_t cnt = 0;
for (i = 0; i < BITMAP_SIZE; i++, a++, b++) { for (i = 0; i < SIMDB_BITMAP_SIZE; i++, a++, b++) {
diff = *a ^ *b; diff = *a ^ *b;
cnt += dict[diff]; cnt += dict[diff];
} }
@ -55,13 +55,13 @@ bitmap_compare(const unsigned char *a,
} }
size_t size_t
bitmap_diffmap(unsigned char *diff, simdb_bitmap_diffmap(unsigned char *diff,
const unsigned char *a, const unsigned char *a,
const unsigned char *b) const unsigned char *b)
{ {
size_t i = 0; size_t i = 0;
for (i = 0; i < BITMAP_SIZE; i++, a++, b++, diff++) { for (i = 0; i < SIMDB_BITMAP_SIZE; i++, a++, b++, diff++) {
*diff = *a ^ *b; *diff = *a ^ *b;
} }
@ -69,9 +69,9 @@ bitmap_diffmap(unsigned char *diff,
} }
size_t size_t
bitmap_unpack(const unsigned char *map, simdb_bitmap_unpack(const unsigned char *map,
unsigned char ** const buf) { unsigned char ** const buf) {
size_t buf_size = BITMAP_BITS; size_t buf_size = SIMDB_BITMAP_BITS;
uint16_t *p, row, mask; uint16_t *p, row, mask;
unsigned char *q = NULL; unsigned char *q = NULL;
@ -79,9 +79,9 @@ bitmap_unpack(const unsigned char *map,
p = (uint16_t *) map; p = (uint16_t *) map;
q = *buf; q = *buf;
for (size_t i = 0; i < BITMAP_SIDE; i++, p++) { for (size_t i = 0; i < SIMDB_BITMAP_SIDE; i++, p++) {
row = *p; mask = 0x1; row = *p; mask = 0x1;
for (size_t j = 0; j < BITMAP_SIDE; j++, q++) { for (size_t j = 0; j < SIMDB_BITMAP_SIDE; j++, q++) {
*q = (row & mask) ? 0xFF : 0x00; *q = (row & mask) ? 0xFF : 0x00;
mask <<= 1; mask <<= 1;
} }
@ -91,16 +91,16 @@ bitmap_unpack(const unsigned char *map,
} }
void void
bitmap_print(const unsigned char *map) { simdb_bitmap_print(const unsigned char *map) {
unsigned char *buf = NULL, *p = NULL; unsigned char *buf = NULL, *p = NULL;
char line[BITMAP_SIDE * 2 + 1]; char line[SIMDB_BITMAP_SIDE * 2 + 1];
bitmap_unpack(map, &buf); simdb_bitmap_unpack(map, &buf);
p = buf; p = buf;
line[BITMAP_SIDE * 2] = '\0'; line[SIMDB_BITMAP_SIDE * 2] = '\0';
for (size_t i = 0; i < BITMAP_SIDE; i++) { for (size_t i = 0; i < SIMDB_BITMAP_SIDE; i++) {
for (size_t j = 0; j < BITMAP_SIDE; j++, p++) { for (size_t j = 0; j < SIMDB_BITMAP_SIDE; j++, p++) {
line[(j * 2) + 0] = (*p == 0x00) ? CHAR_NONE : CHAR_USED; line[(j * 2) + 0] = (*p == 0x00) ? CHAR_NONE : CHAR_USED;
line[(j * 2) + 1] = (*p == 0x00) ? CHAR_NONE : CHAR_USED; line[(j * 2) + 1] = (*p == 0x00) ? CHAR_NONE : CHAR_USED;
} }

22
src/bitmap.h

@ -7,11 +7,11 @@
*/ */
/** Bits per bitmap side (currently - 16) */ /** Bits per bitmap side (currently - 16) */
#define BITMAP_SIDE 16 #define SIMDB_BITMAP_SIDE 16
/** Bitmap size in bytes (currently - 32) */ /** Bitmap size in bytes (currently - 32) */
#define BITMAP_SIZE (BITMAP_SIDE * (BITMAP_SIDE / 8)) #define SIMDB_BITMAP_SIZE (SIMDB_BITMAP_SIDE * (SIMDB_BITMAP_SIDE / 8))
/** Total bits in bitmap (currently - 256) */ /** Total bits in bitmap (currently - 256) */
#define BITMAP_BITS (BITMAP_SIZE * 8) #define SIMDB_BITMAP_BITS (SIMDB_BITMAP_SIZE * 8)
/** placeholders for various bit states */ /** placeholders for various bit states */
#define CHAR_USED '@' /**< bit is 1 */ #define CHAR_USED '@' /**< bit is 1 */
@ -24,8 +24,8 @@
* @returns Integer showing difference between bitmaps in bits (0-256) * @returns Integer showing difference between bitmaps in bits (0-256)
*/ */
int int
bitmap_compare(const unsigned char *a, simdb_bitmap_compare(const unsigned char *a,
const unsigned char *b); const unsigned char *b);
/** /**
* @brief Make difference map of to bitmaps * @brief Make difference map of to bitmaps
@ -35,9 +35,9 @@ bitmap_compare(const unsigned char *a,
* @returns Size of generated map (now is @a BITMAP_SIZE) * @returns Size of generated map (now is @a BITMAP_SIZE)
*/ */
size_t size_t
bitmap_diffmap(unsigned char *diff, simdb_bitmap_diffmap(unsigned char *diff,
const unsigned char *a, const unsigned char *a,
const unsigned char *b); const unsigned char *b);
/** /**
@ -47,8 +47,8 @@ bitmap_diffmap(unsigned char *diff,
* @returns Size of generated bytemap (now is @a BITMAP_BITS) * @returns Size of generated bytemap (now is @a BITMAP_BITS)
*/ */
size_t size_t
bitmap_unpack(const unsigned char *map, simdb_bitmap_unpack(const unsigned char *map,
unsigned char ** const buf); unsigned char ** const buf);
/** /**
* @brief Print bitmap to stdout as ascii-square * @brief Print bitmap to stdout as ascii-square
@ -57,5 +57,5 @@ bitmap_unpack(const unsigned char *map,
* but width is BITMAP_SIDE x 2, for ease of reading * but width is BITMAP_SIDE x 2, for ease of reading
*/ */
void void
bitmap_print(const unsigned char *map); simdb_bitmap_print(const unsigned char *map);
#endif #endif

3
src/database.c

@ -299,8 +299,7 @@ simdb_search(simdb_t * const db,
} }
/* - compare bitmap - more expensive */ /* - compare bitmap - more expensive */
match.diff_bitmap = (float) bitmap_compare(p + REC_OFF_BM, sample->data + REC_OFF_BM); match.diff_bitmap = simdb_bitmap_compare(p + REC_OFF_BM, sample->data + REC_OFF_BM) / SIMDB_BITMAP_BITS;
match.diff_bitmap /= BITMAP_BITS;
if (match.diff_bitmap > search->maxdiff_bitmap) if (match.diff_bitmap > search->maxdiff_bitmap)
continue; continue;

4
src/sample.c

@ -82,12 +82,12 @@ simdb_sample(simdb_rec_t * const rec,
#endif #endif
if (status == MagickPass) { if (status == MagickPass) {
assert(buf_size == BITMAP_SIZE); assert(buf_size == SIMDB_BITMAP_SIZE);
memset(rec->data, 0x0, SIMDB_REC_LEN); memset(rec->data, 0x0, SIMDB_REC_LEN);
rec->data[REC_OFF_RU] = 0xFF; rec->data[REC_OFF_RU] = 0xFF;
memcpy(&rec->data[REC_OFF_IW], &w, sizeof(uint16_t)); memcpy(&rec->data[REC_OFF_IW], &w, sizeof(uint16_t));
memcpy(&rec->data[REC_OFF_IH], &h, sizeof(uint16_t)); memcpy(&rec->data[REC_OFF_IH], &h, sizeof(uint16_t));
memcpy(&rec->data[REC_OFF_BM], buf, BITMAP_SIZE); memcpy(&rec->data[REC_OFF_BM], buf, SIMDB_BITMAP_SIZE);
} else { } else {
description = MagickGetException(wand, &severity); description = MagickGetException(wand, &severity);
fprintf(stderr, "%03d %.1024s\n", severity, description); fprintf(stderr, "%03d %.1024s\n", severity, description);

20
src/simdb-query.c

@ -131,7 +131,7 @@ int rec_bitmap(simdb_t *db, uint64_t number)
return 1; return 1;
} }
bitmap_print(&rec.data[REC_OFF_BM]); simdb_bitmap_print(&rec.data[REC_OFF_BM]);
return 0; return 0;
} }
@ -140,9 +140,9 @@ int rec_diff(simdb_t *db, uint64_t a, uint64_t b, unsigned short int showmap)
{ {
simdb_rec_t rec; simdb_rec_t rec;
float diff = 0.0; float diff = 0.0;
unsigned char one[BITMAP_SIZE]; unsigned char one[SIMDB_BITMAP_SIZE];
unsigned char two[BITMAP_SIZE]; unsigned char two[SIMDB_BITMAP_SIZE];
unsigned char map[BITMAP_SIZE]; unsigned char map[SIMDB_BITMAP_SIZE];
assert(db != NULL); assert(db != NULL);
memset(&rec, 0x0, sizeof(simdb_rec_t)); memset(&rec, 0x0, sizeof(simdb_rec_t));
@ -152,23 +152,23 @@ int rec_diff(simdb_t *db, uint64_t a, uint64_t b, unsigned short int showmap)
fprintf(stderr, "record diff: first sample not exists\n"); fprintf(stderr, "record diff: first sample not exists\n");
return 1; return 1;
} }
memcpy(one, &rec.data[REC_OFF_BM], BITMAP_SIZE); memcpy(one, &rec.data[REC_OFF_BM], SIMDB_BITMAP_SIZE);
rec.num = b; rec.num = b;
if (simdb_record_read(db, &rec) < 1) { if (simdb_record_read(db, &rec) < 1) {
fprintf(stderr, "record diff: second sample not exists\n"); fprintf(stderr, "record diff: second sample not exists\n");
return 1; return 1;
} }
memcpy(two, &rec.data[REC_OFF_BM], BITMAP_SIZE); memcpy(two, &rec.data[REC_OFF_BM], SIMDB_BITMAP_SIZE);
if (showmap) { if (showmap) {
bitmap_diffmap(map, one, two); simdb_bitmap_diffmap(map, one, two);
bitmap_print(map); simdb_bitmap_print(map);
return 0; return 0;
} }
diff = (float) bitmap_compare(one, two); diff = (float) simdb_bitmap_compare(one, two);
printf("%.2f%%\n", (diff / BITMAP_BITS) * 100); printf("%.2f%%\n", (diff / SIMDB_BITMAP_BITS) * 100);
return 0; return 0;
} }

22
tests/bitmap.c

@ -3,31 +3,39 @@
int int
main() { main() {
unsigned char a[BITMAP_SIZE], b[BITMAP_SIZE]; unsigned char a[SIMDB_BITMAP_SIZE];
unsigned char b[SIMDB_BITMAP_SIZE];
int ret;
memset (a, 0x00, sizeof(a)); memset (a, 0x00, sizeof(a));
memset (b, 0x00, sizeof(b)); memset (b, 0x00, sizeof(b));
assert(bitmap_compare(a, b) == 0); ret = simdb_bitmap_compare(a, b);
assert(ret == 0);
memset (a, 0xFE, sizeof(a)); memset (a, 0xFE, sizeof(a));
memset (b, 0xFF, sizeof(b)); memset (b, 0xFF, sizeof(b));
assert(bitmap_compare(a, b) == 32); ret = simdb_bitmap_compare(a, b);
assert(ret == 32);
memset (a, 0x00, sizeof(a)); memset (a, 0x00, sizeof(a));
memset (b, 0x55, sizeof(b)); memset (b, 0x55, sizeof(b));
assert(bitmap_compare(a, b) == 128); ret = simdb_bitmap_compare(a, b);
assert(ret == 128);
memset (a, 0x00, sizeof(a)); memset (a, 0x00, sizeof(a));
memset (b, 0xAA, sizeof(b)); memset (b, 0xAA, sizeof(b));
assert(bitmap_compare(a, b) == 128); ret = simdb_bitmap_compare(a, b);
assert(ret == 128);
memset (a, 0xAA, sizeof(a)); memset (a, 0xAA, sizeof(a));
memset (b, 0x55, sizeof(b)); memset (b, 0x55, sizeof(b));
assert(bitmap_compare(a, b) == 256); ret = simdb_bitmap_compare(a, b);
assert(ret == 256);
memset (a, 0x00, sizeof(a)); memset (a, 0x00, sizeof(a));
memset (b, 0xFF, sizeof(b)); memset (b, 0xFF, sizeof(b));
assert(bitmap_compare(a, b) == 256); ret = simdb_bitmap_compare(a, b);
assert(ret == 256);
return 0; return 0;
} }

Loading…
Cancel
Save