:: commit b864506a0812c4ebbeef524d1a90e1d0e8210a06

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-05-06 15:05

parents: e713486b10

-Wall -Wextra, finish one of the TODOs

diff --git a/Makefile.in b/Makefile.in
index e68e5e0..56594e9 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -4,7 +4,7 @@ CFLAGS=-O2 -march=native -mtune=native -flto -Iinclude -g3
 
 .PHONY: all clean format install cloc
 
-LIBBZ3_OBJECTS=obj/libsais.o obj/crc32.o obj/mtf.o obj/srt.o obj/rle.o \
+LIBBZ3_OBJECTS=obj/libsais.o obj/crc32.o obj/srt.o obj/rle.o \
                obj/cm.o obj/libbz3.o obj/txt.o obj/lzp.o
 
 all: bzip3 bzip3.so
diff --git a/include/lzp.h b/include/lzp.h
index d07c956..5ea5e06 100644
--- a/include/lzp.h
+++ b/include/lzp.h
@@ -4,8 +4,8 @@
 
 #include "common.h"
 
-s32 lzp_compress(const u8 * input, u8 * output, s32 n, s32 hash, s32 min);
+s32 lzp_compress(const u8 * input, u8 * output, s32 n, s32 hash, s32 min, s32 * lut);
 
-s32 lzp_decompress(const u8 * input, u8 * output, s32 n, s32 hash, s32 min);
+s32 lzp_decompress(const u8 * input, u8 * output, s32 n, s32 hash, s32 min, s32 * lut);
 
 #endif
diff --git a/src/libbz3.c b/src/libbz3.c
index 857652e..7726065 100644
--- a/src/libbz3.c
+++ b/src/libbz3.c
@@ -37,7 +37,7 @@
 struct bz3_state {
     u8 *swap_buffer;
     s32 block_size;
-    s32 * sais_array;
+    s32 * sais_array, * lzp_lut;
     struct srt_state * srt_state;
     state * cm_state;
     s8 last_error;
@@ -77,7 +77,9 @@ struct bz3_state * bz3_new(s32 block_size) {
     bz3_state->srt_state = malloc(sizeof(struct srt_state));
 
     bz3_state->swap_buffer = malloc(block_size + block_size / 4);
-    bz3_state->sais_array = malloc(block_size * sizeof(s32) + 16);
+    bz3_state->sais_array = malloc(block_size * sizeof(s32));
+
+    bz3_state->lzp_lut = calloc(1 << LZP_DICTIONARY, sizeof(s32));
 
     bz3_state->block_size = block_size;
 
@@ -91,13 +93,14 @@ void bz3_free(struct bz3_state * state) {
     free(state->sais_array);
     free(state->srt_state);
     free(state->cm_state);
+    free(state->lzp_lut);
     free(state);
 }
 
 #define swap(x, y) { u8 * tmp = x; x = y; y = tmp; }
 
 s32 bz3_encode_block(struct bz3_state * state, u8 * buffer, s32 data_size) {
-    u8 * b1 = buffer, * b2 = state->swap_buffer; s32 initial_size = data_size;
+    u8 * b1 = buffer, * b2 = state->swap_buffer;
 
     if(data_size > state->block_size) {
         state->last_error = BZ3_ERR_DATA_TOO_BIG;
@@ -127,14 +130,14 @@ s32 bz3_encode_block(struct bz3_state * state, u8 * buffer, s32 data_size) {
         model |= 4;
     }
 
-    lzp_size = lzp_compress(b1, b2, data_size, LZP_DICTIONARY, LZP_MIN_MATCH);
+    lzp_size = lzp_compress(b1, b2, data_size, LZP_DICTIONARY, LZP_MIN_MATCH, state->lzp_lut);
     if(lzp_size > 0) {
         swap(b1, b2);
         data_size = lzp_size;
         model |= 2;
     }
 
-    s32 bwt_idx = libsais_bwt(b1, b2, state->sais_array, data_size, 16, NULL);
+    s32 bwt_idx = libsais_bwt(b1, b2, state->sais_array, data_size, 0, NULL);
     if(bwt_idx < 0) {
         state->last_error = BZ3_ERR_BWT;
         return -1;
@@ -226,7 +229,7 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
 
     // Undo LZP
     if(model & 2) {
-        size_src = lzp_decompress(b1, b2, lzp_size, LZP_DICTIONARY, LZP_MIN_MATCH);
+        size_src = lzp_decompress(b1, b2, lzp_size, LZP_DICTIONARY, LZP_MIN_MATCH, state->lzp_lut);
         swap(b1, b2);
     }
 
diff --git a/src/lzp.c b/src/lzp.c
index 0ba6dd5..e8935a7 100644
--- a/src/lzp.c
+++ b/src/lzp.c
@@ -1,6 +1,5 @@
 
 // Lempel Ziv Prediction code.
-// TODO: Move the LUT allocation out of block coding routine to save some clock cycles.
 
 #include <memory.h>
 #include <stdlib.h>
@@ -90,12 +89,9 @@ static s32 lzp_encode_block(const u8 * restrict in, const u8 * in_end, u8 * rest
     return out >= out_eob ? -1 : (s32)(out - outs);
 }
 
-static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, u8 * restrict out, s32 hash, s32 m_len) {
+static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, s32 * restrict lut, u8 * restrict out, s32 hash, s32 m_len) {
     if (in_end - in < 4) return -1;
 
-    s32 * restrict lut = calloc(1 << hash, sizeof(s32));
-    if (!lut) return -1;
-
     u32 mask = (s32)(1 << hash) - 1;
     const u8 * outs = out;
 
@@ -131,25 +127,19 @@ static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, u8 * rest
         }
     }
 
-    free(lut);
-
     return out - outs;
 }
 
-s32 lzp_compress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len) {
+s32 lzp_compress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len, s32 * lut) {
     s32 nblk = num_blocks(n);
 
     if (nblk == 1) {
         if (n - m_len < 32) return -1;
 
-        s32 * lut = calloc(1 << hash, sizeof(s32));
-
-        if (!lut) return -1;
+        memset(lut, 0, sizeof(s32) * (1 << hash));
 
         s32 r = lzp_encode_block(in, in + n, out + 1, out + n - 1, lut, (s32)(1 << hash) - 1, m_len);
 
-        free(lut);
-
         if (r >= 0) {
             out[0] = 1;
             r++;
@@ -172,15 +162,9 @@ s32 lzp_compress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len) {
         if (insz - m_len < 32)
             r = -1;
         else {
-            s32 * lut = calloc(1 << hash, sizeof(s32));
-
-            if (!lut)
-                r = -1;
-            else
-                r = lzp_encode_block(in + ins, in + ins + insz, out + out_ptr, out + out_ptr + outsz, lut,
-                                     (s32)(1 << hash) - 1, m_len);
+            memset(lut, 0, sizeof(s32) * (1 << hash));
 
-            free(lut);
+            r = lzp_encode_block(in + ins, in + ins + insz, out + out_ptr, out + out_ptr + outsz, lut, (s32)(1 << hash) - 1, m_len);
         }
 
         if (r < 0) {
@@ -197,10 +181,10 @@ s32 lzp_compress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len) {
     return out_ptr;
 }
 
-s32 lzp_decompress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len) {
+s32 lzp_decompress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len, s32 * lut) {
     s32 nblk = in[0];
 
-    if (nblk == 1) return lzp_decode_block(in + 1, in + n, out, hash, m_len);
+    if (nblk == 1) return lzp_decode_block(in + 1, in + n, lut, out, hash, m_len);
 
     s32 dec[256];
 
@@ -217,7 +201,7 @@ s32 lzp_decompress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len) {
         s32 outsz = *(s32 *)(in + 1 + 8 * b_id + 0);
 
         if (insz != outsz) {
-            dec[b_id] = lzp_decode_block(in + in_ptr, in + in_ptr + insz, out + out_ptr, hash, m_len);
+            dec[b_id] = lzp_decode_block(in + in_ptr, in + in_ptr + insz, lut, out + out_ptr, hash, m_len);
         } else {
             dec[b_id] = insz;
             memcpy(out + out_ptr, in + in_ptr, insz);
tab: 248 wrap: offon