A few minor compressor cleanups
authorEric Biggers <ebiggers3@gmail.com>
Sun, 21 Sep 2014 20:33:05 +0000 (15:33 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 21 Sep 2014 20:33:05 +0000 (15:33 -0500)
src/lzms-compress.c
src/lzx-common.c
src/lzx-compress.c
src/xpress-compress.c

index fb1f777..00c8f5d 100644 (file)
@@ -716,7 +716,7 @@ lzms_do_init_rc_costs(void)
                for (u32 j = 0; j < LZMS_COST_SHIFT; j++) {
                        w *= w;
                        bit_count <<= 1;
-                       while (w >= (1U << 16)) {
+                       while (w >= ((u32)1 << 16)) {
                                w >>= 1;
                                ++bit_count;
                        }
@@ -888,8 +888,7 @@ lzms_consider_lz_explicit_offset_matches(const struct lzms_compressor *c,
        len = 2;
        i = 0;
        do {
-               position_cost = base_cost + lzms_lz_offset_cost(c,
-                                                               matches[i].offset);
+               position_cost = base_cost + lzms_lz_offset_cost(c, matches[i].offset);
                do {
                        cost = position_cost + lzms_fast_length_cost(c, len);
                        if (cost < (cur_optimum_ptr + len)->cost) {
@@ -1218,8 +1217,8 @@ begin:
                 *      the parser will not go too long without updating the
                 *      probability tables.
                 *
-                * Note: no check for end-of-block is needed because
-                * end-of-block will trigger condition (1).
+                * Note: no check for end-of-window is needed because
+                * end-of-window will trigger condition (1).
                 */
                if (cur_optimum_ptr == end_optimum_ptr ||
                    cur_optimum_ptr == c->optimum_end)
@@ -1376,26 +1375,26 @@ lzms_finalize(struct lzms_compressor *c, u8 *cdata, size_t csize_avail)
  * maximum window size.  */
 static void
 lzms_build_params(unsigned int compression_level,
-                 struct lzms_compressor_params *lzms_params)
+                 struct lzms_compressor_params *params)
 {
        /* Allow length 2 matches if the compression level is sufficiently high.
         */
        if (compression_level >= 45)
-               lzms_params->min_match_length = 2;
+               params->min_match_length = 2;
        else
-               lzms_params->min_match_length = 3;
+               params->min_match_length = 3;
 
        /* Scale nice_match_length and max_search_depth with the compression
         * level.  But to allow an optimization on length cost calculations,
         * don't allow nice_match_length to exceed LZMS_NUM_FAST_LENGTH.  */
-       lzms_params->nice_match_length = ((u64)compression_level * 32) / 50;
-       if (lzms_params->nice_match_length < lzms_params->min_match_length)
-               lzms_params->nice_match_length = lzms_params->min_match_length;
-       if (lzms_params->nice_match_length > LZMS_NUM_FAST_LENGTHS)
-               lzms_params->nice_match_length = LZMS_NUM_FAST_LENGTHS;
-       lzms_params->max_search_depth = compression_level;
-
-       lzms_params->optim_array_length = 1024;
+       params->nice_match_length = ((u64)compression_level * 32) / 50;
+       if (params->nice_match_length < params->min_match_length)
+               params->nice_match_length = params->min_match_length;
+       if (params->nice_match_length > LZMS_NUM_FAST_LENGTHS)
+               params->nice_match_length = LZMS_NUM_FAST_LENGTHS;
+       params->max_search_depth = compression_level;
+
+       params->optim_array_length = 1024;
 }
 
 /* Given the internal compression parameters and maximum window size, build the
index 827b1b2..d733ada 100644 (file)
@@ -73,12 +73,12 @@ lzx_get_window_order(size_t max_block_size)
 {
        unsigned order;
 
-       if (max_block_size == 0 || max_block_size > (1 << LZX_MAX_WINDOW_ORDER))
+       if (max_block_size == 0 || max_block_size > LZX_MAX_WINDOW_SIZE)
                return 0;
 
        order = bsr32(max_block_size);
 
-       if ((1 << order) != max_block_size)
+       if (((u32)1 << order) != max_block_size)
                order++;
 
        return max(order, LZX_MIN_WINDOW_ORDER);
@@ -89,7 +89,7 @@ lzx_get_window_order(size_t max_block_size)
 unsigned
 lzx_get_num_main_syms(unsigned window_order)
 {
-       u32 window_size = 1 << window_order;
+       u32 window_size = (u32)1 << window_order;
 
        /* NOTE: the calculation *should* be as follows:
         *
index c7ff96f..a972cc3 100644 (file)
@@ -904,8 +904,7 @@ lzx_get_matches_nocache_multiblock(struct lzx_compressor *c,
  * offset.
  */
 static inline unsigned
-lzx_get_matches(struct lzx_compressor *c,
-               const struct lz_match **matches_ret)
+lzx_get_matches(struct lzx_compressor *c, const struct lz_match **matches_ret)
 {
        return (*c->get_matches_func)(c, matches_ret);
 }
@@ -1207,7 +1206,7 @@ lzx_match_cost_raw(unsigned len, unsigned offset_slot,
        unsigned main_symbol;
 
        if (len - LZX_MIN_MATCH_LEN < LZX_NUM_PRIMARY_LENS) {
-               len_header = len - LZX_MIN_MATCH_LEN ;
+               len_header = len - LZX_MIN_MATCH_LEN;
                cost = 0;
        } else {
                len_header = LZX_NUM_PRIMARY_LENS;
@@ -1892,11 +1891,11 @@ static int
 lzx_choose_verbatim_or_aligned(const struct lzx_freqs * freqs,
                               const struct lzx_codes * codes)
 {
-       unsigned aligned_cost = 0;
-       unsigned verbatim_cost = 0;
+       u32 aligned_cost = 0;
+       u32 verbatim_cost = 0;
 
-       /* A verbatim block require 3 bits in each place that an aligned symbol
-        * was used.  */
+       /* A verbatim block requires 3 bits in each place that an aligned symbol
+        * would be used in an aligned offset block.  */
        for (unsigned i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
                verbatim_cost += 3 * freqs->aligned[i];
                aligned_cost += codes->lens.aligned[i] * freqs->aligned[i];
index f39d547..66f1746 100644 (file)
@@ -136,7 +136,7 @@ struct xpress_mc_pos_data {
         */
        u32 mc_item_data;
 #define MC_OFFSET_SHIFT 16
-#define MC_LEN_MASK ((1 << MC_OFFSET_SHIFT) - 1)
+#define MC_LEN_MASK (((u32)1 << MC_OFFSET_SHIFT) - 1)
 };
 
 
@@ -589,7 +589,7 @@ xpress_set_costs(u8 costs[], const u8 lens[])
  * @matches must be sorted by strictly increasing length and strictly
  * increasing offset.  This is guaranteed by the match-finder.
  *
- * We consider each length from the minimum (2) to the longest
+ * We consider each length from the minimum (3) to the longest
  * (matches[num_matches - 1].len).  For each length, we consider only
  * the smallest offset for which that length is available.  Although
  * this is not guaranteed to be optimal due to the possibility of a
@@ -793,8 +793,8 @@ begin:
                 *      XPRESS_OPTIM_ARRAY_LENGTH is high enough that on most
                 *      inputs this limit is never reached.
                 *
-                * Note: no check for end-of-block is needed because
-                * end-of-block will trigger condition (1).
+                * Note: no check for end-of-window is needed because
+                * end-of-window will trigger condition (1).
                 */
                if (cur_optimum_ptr == end_optimum_ptr ||
                    cur_optimum_ptr == &c->optimum[XPRESS_OPTIM_ARRAY_LENGTH])
@@ -1208,14 +1208,16 @@ xpress_compress(const void *uncompressed_data, size_t uncompressed_size,
        c->cur_window_size = uncompressed_size;
        lz_mf_load_window(c->mf, c->cur_window, c->cur_window_size);
        memset(c->freqs, 0, sizeof(c->freqs));
+
        num_chosen_items = xpress_choose_items(c);
+
        c->freqs[XPRESS_END_OF_DATA]++;
        xpress_make_huffman_code(c);
 
        /* Output the Huffman code as a series of 512 4-bit lengths.  */
        cptr = compressed_data;
        for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS; i += 2)
-               *cptr++ = (c->lens[i] & 0xf) | (c->lens[i + 1] << 4);
+               *cptr++ = (c->lens[i + 1] << 4) | c->lens[i];
 
        /* Output the encoded matches/literals.  */
        xpress_init_output(&os, cptr,
@@ -1239,8 +1241,8 @@ xpress_free_compressor(void *_c)
 
        if (c) {
                lz_mf_free(c->mf);
-               FREE(c->cached_matches);
                FREE(c->optimum);
+               FREE(c->cached_matches);
                FREE(c->chosen_items);
                FREE(c);
        }