]> wimlib.net Git - wimlib/commitdiff
lzx_common: make lzx_offset_slot_base hold unadjusted offsets
authorEric Biggers <ebiggers3@gmail.com>
Sat, 9 Jul 2016 15:01:17 +0000 (10:01 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 9 Jul 2016 15:01:17 +0000 (10:01 -0500)
include/wimlib/lzx_common.h
src/lzx_common.c
src/lzx_compress.c
src/lzx_decompress.c

index 0b57530d7a49a2d21b24af4a1bafc6af0302d5fb..f93221d5b16c84b075b4e865bf3d7b1b63805274 100644 (file)
@@ -10,7 +10,7 @@
 #include "wimlib/lzx_constants.h"
 #include "wimlib/types.h"
 
-extern const u32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS + 1];
+extern const s32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS + 1];
 
 extern const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS];
 
index 7925f1f197b6803ce31bca7fed7564302f6220e1..194e8162da2e4bc67eb91fc213b8c4a8b78ab3e2 100644 (file)
 #include "wimlib/util.h"
 
 /* Mapping: offset slot => first match offset that uses that offset slot.
- */
-const u32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS + 1] = {
-       0      , 1      , 2      , 3      , 4      ,    /* 0  --- 4  */
-       6      , 8      , 12     , 16     , 24     ,    /* 5  --- 9  */
-       32     , 48     , 64     , 96     , 128    ,    /* 10 --- 14 */
-       192    , 256    , 384    , 512    , 768    ,    /* 15 --- 19 */
-       1024   , 1536   , 2048   , 3072   , 4096   ,    /* 20 --- 24 */
-       6144   , 8192   , 12288  , 16384  , 24576  ,    /* 25 --- 29 */
-       32768  , 49152  , 65536  , 98304  , 131072 ,    /* 30 --- 34 */
-       196608 , 262144 , 393216 , 524288 , 655360 ,    /* 35 --- 39 */
-       786432 , 917504 , 1048576, 1179648, 1310720,    /* 40 --- 44 */
-       1441792, 1572864, 1703936, 1835008, 1966080,    /* 45 --- 49 */
-       2097152                                         /* extra     */
+ * The offset slots for repeat offsets map to "fake" offsets < 1.  */
+const s32 lzx_offset_slot_base[LZX_MAX_OFFSET_SLOTS + 1] = {
+        -2     , -1     , 0      , 1      , 2      ,    /* 0  --- 4  */
+        4      , 6      , 10     , 14     , 22     ,    /* 5  --- 9  */
+        30     , 46     , 62     , 94     , 126    ,    /* 10 --- 14 */
+        190    , 254    , 382    , 510    , 766    ,    /* 15 --- 19 */
+        1022   , 1534   , 2046   , 3070   , 4094   ,    /* 20 --- 24 */
+        6142   , 8190   , 12286  , 16382  , 24574  ,    /* 25 --- 29 */
+        32766  , 49150  , 65534  , 98302  , 131070 ,    /* 30 --- 34 */
+        196606 , 262142 , 393214 , 524286 , 655358 ,    /* 35 --- 39 */
+        786430 , 917502 , 1048574, 1179646, 1310718,    /* 40 --- 44 */
+        1441790, 1572862, 1703934, 1835006, 1966078,    /* 45 --- 49 */
+        2097150                                         /* extra     */
 };
 
 /* Mapping: offset slot => how many extra bits must be read and added to the
@@ -93,10 +93,9 @@ lzx_get_num_main_syms(unsigned window_order)
         * disallows this case.  This reduces the number of needed offset slots
         * by 1.  */
        u32 window_size = (u32)1 << window_order;
-       u32 max_adjusted_offset = (window_size - LZX_MIN_MATCH_LEN - 1) +
-                                 LZX_OFFSET_ADJUSTMENT;
+       u32 max_offset = window_size - LZX_MIN_MATCH_LEN - 1;
        unsigned num_offset_slots = 30;
-       while (max_adjusted_offset >= lzx_offset_slot_base[num_offset_slots])
+       while (max_offset >= lzx_offset_slot_base[num_offset_slots])
                num_offset_slots++;
 
        return LZX_NUM_CHARS + (num_offset_slots * LZX_NUM_LEN_HEADERS);
index f0c3c5268c82c5e26347b9c42e5c008e811886c8..fdbce434cf794d7ccd3881b39c70bcb61c6d454b 100644 (file)
@@ -968,7 +968,8 @@ lzx_write_sequences(struct lzx_output_bitstream *os, int block_type,
                adjusted_offset = seq->adjusted_offset_and_match_hdr >> 9;
 
                num_extra_bits = lzx_extra_offset_bits[offset_slot];
-               extra_bits = adjusted_offset - lzx_offset_slot_base[offset_slot];
+               extra_bits = adjusted_offset - (lzx_offset_slot_base[offset_slot] +
+                                               LZX_OFFSET_ADJUSTMENT);
 
        #define MAX_MATCH_BITS  (MAIN_CODEWORD_LIMIT + LENGTH_CODEWORD_LIMIT + \
                                 14 + ALIGNED_CODEWORD_LIMIT)
@@ -2762,7 +2763,8 @@ lzx_init_offset_slot_tabs(struct lzx_compressor *c)
        for (; adjusted_offset < ARRAY_LEN(c->offset_slot_tab_1);
             adjusted_offset++)
        {
-               if (adjusted_offset >= lzx_offset_slot_base[slot + 1])
+               if (adjusted_offset >= lzx_offset_slot_base[slot + 1] +
+                                      LZX_OFFSET_ADJUSTMENT)
                        slot++;
                c->offset_slot_tab_1[adjusted_offset] = slot;
        }
@@ -2771,7 +2773,8 @@ lzx_init_offset_slot_tabs(struct lzx_compressor *c)
        for (; adjusted_offset < LZX_MAX_WINDOW_SIZE;
             adjusted_offset += (u32)1 << 14)
        {
-               if (adjusted_offset >= lzx_offset_slot_base[slot + 1])
+               if (adjusted_offset >= lzx_offset_slot_base[slot + 1] +
+                                      LZX_OFFSET_ADJUSTMENT)
                        slot++;
                c->offset_slot_tab_2[adjusted_offset >> 14] = slot;
        }
index 36d94ebf63d36d7b8d9d5e38c7f78c42ad054bb1..102c321d52ad138f611567e5e15eaf674dddcfc9 100644 (file)
@@ -414,9 +414,6 @@ lzx_decompress_block(struct lzx_decompressor *d, struct input_bitstream *is,
                                offset += bitstream_read_bits(is, num_extra_bits);
                        }
 
-                       /* Adjust the offset.  */
-                       offset -= LZX_OFFSET_ADJUSTMENT;
-
                        /* Update the match offset LRU queue.  */
                        STATIC_ASSERT(LZX_NUM_RECENT_OFFSETS == 3);
                        recent_offsets[2] = recent_offsets[1];