From e799bc169a644517fc2fe77783392259c555fac5 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 2 Apr 2014 16:47:49 +1100 Subject: [PATCH] * bitmap compare with dictionary --- src/bitmap.c | 24 ++++++++++++++++++++++++ src/bitmap.h | 1 + 2 files changed, 25 insertions(+) diff --git a/src/bitmap.c b/src/bitmap.c index f4797cf..7b80602 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -17,6 +17,27 @@ #include "main.h" #include "bitmap.h" +unsigned char dict[256] = { +/* 0x00 _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F */ +/* ---------------------------------------------------- */ +/* 0_ */ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, +/* 1_ */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, +/* 2_ */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, +/* 3_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* 4_ */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, +/* 5_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* 6_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* 7_ */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, +/* 8_ */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, +/* 9_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* A_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* B_ */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, +/* C_ */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, +/* D_ */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, +/* E_ */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, +/* F_ */ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, +}; + int bitmap_compare(const unsigned char *a, const unsigned char *b) { unsigned char diff = 0; @@ -25,10 +46,13 @@ int bitmap_compare(const unsigned char *a, const unsigned char *b) for (i = 0; i < BITMAP_SIZE; i++, a++, b++) { diff = *a ^ *b; +/* bruteforce: while (diff) { cnt += (diff & 1); diff >>= 1; } +*/ + cnt += dict[diff]; } return cnt; diff --git a/src/bitmap.h b/src/bitmap.h index 981b22b..f7989bf 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -5,5 +5,6 @@ typedef unsigned char bitmap_t[BITMAP_SIZE]; int bitmap_compare(const unsigned char *a, const unsigned char *b); + #endif #define HAS_BITMAP_H 1