#include "ldm.h"
+#define HASH_EVERY 7
+
#define LDM_MEMORY_USAGE 14
#define LDM_HASHLOG (LDM_MEMORY_USAGE-2)
#define LDM_HASHTABLESIZE (1 << (LDM_MEMORY_USAGE))
#define WINDOW_SIZE (1 << 20)
#define MAX_WINDOW_SIZE 31
-#define HASH_SIZE 4
-#define MINMATCH 4
+#define HASH_SIZE 8
+#define MINMATCH 8
#define ML_BITS 4
#define ML_MASK ((1U<<ML_BITS)-1)
} while (d < e);
}
+typedef struct compress_stats {
+ U32 num_matches;
+ U32 total_match_length;
+ U32 total_literal_length;
+ U64 total_offset;
+} compress_stats;
+
+static void print_compress_stats(const compress_stats *stats) {
+ printf("=====================\n");
+ printf("Compression statistics\n");
+ printf("Total number of matches: %u\n", stats->num_matches);
+ printf("Average match length: %.1f\n", ((double)stats->total_match_length) /
+ (double)stats->num_matches);
+ printf("Average literal length: %.1f\n",
+ ((double)stats->total_literal_length) / (double)stats->num_matches);
+ printf("Average offset length: %.1f\n",
+ ((double)stats->total_offset) / (double)stats->num_matches);
+ printf("=====================\n");
+}
+
struct hash_entry {
U64 offset;
tag t;
static void LDM_put_position_on_hash(const BYTE *p, U32 h, void *tableBase,
const BYTE *srcBase) {
// printf("Hashing: %zu\n", p - srcBase);
+ if (((p - srcBase) & HASH_EVERY) != HASH_EVERY) {
+ return;
+ }
+
U32 *hashTable = (U32 *) tableBase;
hashTable[h] = (U32)(p - srcBase);
}
static void LDM_put_position(const BYTE *p, void *tableBase,
const BYTE *srcBase) {
+ if (((p - srcBase) & HASH_EVERY) != HASH_EVERY) {
+ return;
+ }
U32 const h = LDM_hash_position(p);
LDM_put_position_on_hash(p, h, tableBase, srcBase);
}
const BYTE * const matchlimit = iend - HASH_SIZE;
const BYTE * const mflimit = iend - MINMATCH;
BYTE *op = (BYTE*) dest;
+
+ compress_stats compressStats = { 0 };
+
U32 hashTable[LDM_HASHTABLESIZE_U32];
memset(hashTable, 0, sizeof(hashTable));
} while (ip - match > WINDOW_SIZE ||
LDM_read64(match) != LDM_read64(ip));
}
+ compressStats.num_matches++;
- // TODO catchup
+ /* Catchup: look back to extend match from found match */
while (ip > anchor && match > istart && ip[-1] == match[-1]) {
ip--;
match--;
unsigned const litLength = (unsigned)(ip - anchor);
token = op++;
+ compressStats.total_literal_length += litLength;
+
#ifdef LDM_DEBUG
printf("Cur position: %zu\n", anchor - istart);
printf("LitLength %zu. (Match offset). %zu\n", litLength, ip - match);
_next_match:
/* Encode offset */
{
+ /*
+ LDM_writeLE16(op, ip-match);
+ op += 2;
+ */
LDM_write32(op, ip - match);
op += 4;
+ compressStats.total_offset += (ip - match);
}
/* Encode Match Length */
fwrite(ip, MINMATCH + matchCode, 1, stdout);
printf("\n");
#endif
-
+ compressStats.total_match_length += matchCode + MINMATCH;
unsigned ctr = 1;
ip++;
for (; ctr < MINMATCH + matchCode; ip++, ctr++) {
}
#ifdef LDM_DEBUG
printf("\n");
+
#endif
}
memcpy(op, anchor, lastRun);
op += lastRun;
}
+ print_compress_stats(&compressStats);
return (op - (BYTE *)dest);
}
op = cpy;
/* get offset */
+ /*
+ offset = LDM_readLE16(ip);
+ ip += 2;
+ */
offset = LDM_read32(ip);
-
+ ip += 4;
#ifdef LDM_DEBUG
printf("Offset: %zu\n", offset);
#endif
- ip += 4;
match = op - offset;
// LDM_write32(op, (U32)offset);