]> wimlib.net Git - wimlib/blobdiff - src/lzms_decompress.c
Split prefetch() into prefetchr() and prefetchw()
[wimlib] / src / lzms_decompress.c
index 5dd500aa05345a5b8716fd60a03477fffd0c893e..345b51dbf38c6317b6d17b429e3325b36167a1ce 100644 (file)
@@ -314,17 +314,6 @@ struct lzms_huffman_rebuild_info {
        unsigned table_bits;
 };
 
-struct lzms_probabilites {
-       struct lzms_probability_entry main[LZMS_NUM_MAIN_PROBS];
-       struct lzms_probability_entry match[LZMS_NUM_MATCH_PROBS];
-       struct lzms_probability_entry lz[LZMS_NUM_LZ_PROBS];
-       struct lzms_probability_entry delta[LZMS_NUM_DELTA_PROBS];
-       struct lzms_probability_entry lz_rep[LZMS_NUM_LZ_REP_DECISIONS]
-                                           [LZMS_NUM_LZ_REP_PROBS];
-       struct lzms_probability_entry delta_rep[LZMS_NUM_DELTA_REP_DECISIONS]
-                                              [LZMS_NUM_DELTA_REP_PROBS];
-};
-
 struct lzms_decompressor {
 
        /* 'last_target_usages' is in union with everything else because it is
@@ -474,18 +463,22 @@ lzms_decode_bit(struct lzms_range_decoder *rd, u32 *state_p, u32 num_states,
        /* Load the probability entry corresponding to the current state.  */
        prob_entry = &probs[*state_p];
 
+       /* Update the state early.  We'll still need to OR the state with 1
+        * later if the decoded bit is a 1.  */
+       *state_p = (*state_p << 1) & (num_states - 1);
+
+       /* Get the probability (out of LZMS_PROBABILITY_DENOMINATOR) that the
+        * next bit is 0.  */
+       prob = lzms_get_probability(prob_entry);
+
        /* Normalize if needed.  */
-       if (rd->range <= 0xffff) {
+       if (!(rd->range & 0xFFFF0000)) {
                rd->range <<= 16;
                rd->code <<= 16;
                if (likely(rd->next != rd->end))
                        rd->code |= le16_to_cpu(*rd->next++);
        }
 
-       /* Get the probability (out of LZMS_PROBABILITY_DENOMINATOR) that the
-        * next bit is 0.  */
-       prob = lzms_get_probability(prob_entry);
-
        /* Based on the probability, calculate the bound between the 0-bit
         * region and the 1-bit region of the range.  */
        bound = (rd->range >> LZMS_PROBABILITY_BITS) * prob;
@@ -495,7 +488,6 @@ lzms_decode_bit(struct lzms_range_decoder *rd, u32 *state_p, u32 num_states,
                rd->range = bound;
 
                /* Update the state and probability entry based on the decoded bit.  */
-               *state_p = ((*state_p << 1) | 0) & (num_states - 1);
                lzms_update_probability_entry(prob_entry, 0);
                return 0;
        } else {
@@ -504,8 +496,8 @@ lzms_decode_bit(struct lzms_range_decoder *rd, u32 *state_p, u32 num_states,
                rd->code -= bound;
 
                /* Update the state and probability entry based on the decoded bit.  */
-               *state_p = ((*state_p << 1) | 1) & (num_states - 1);
                lzms_update_probability_entry(prob_entry, 1);
+               *state_p |= 1;
                return 1;
        }
 }
@@ -765,9 +757,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
 
        lzms_input_bitstream_init(&is, in, in_nbytes / sizeof(le16));
 
-       lzms_init_probability_entries((struct lzms_probability_entry *)&d->probs,
-                                     sizeof(d->probs) /
-                                       sizeof(struct lzms_probability_entry));
+       lzms_init_probabilities(&d->probs);
 
        lzms_init_huffman_codes(d, lzms_get_num_offset_slots(out_nbytes));
 
@@ -806,7 +796,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                                if (pending_lz_offset != 0 &&
                                    out_next != lz_offset_still_pending)
                                {
-                                       BUILD_BUG_ON(LZMS_NUM_LZ_REPS != 3);
+                                       STATIC_ASSERT(LZMS_NUM_LZ_REPS == 3);
                                        recent_lz_offsets[3] = recent_lz_offsets[2];
                                        recent_lz_offsets[2] = recent_lz_offsets[1];
                                        recent_lz_offsets[1] = recent_lz_offsets[0];
@@ -814,7 +804,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                                        pending_lz_offset = 0;
                                }
 
-                               BUILD_BUG_ON(LZMS_NUM_LZ_REPS != 3);
+                               STATIC_ASSERT(LZMS_NUM_LZ_REPS == 3);
                                if (!lzms_decode_bit(&rd, &lz_rep_states[0],
                                                     LZMS_NUM_LZ_REP_PROBS,
                                                     d->probs.lz_rep[0]))
@@ -837,7 +827,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                        }
 
                        if (pending_lz_offset != 0) {
-                               BUILD_BUG_ON(LZMS_NUM_LZ_REPS != 3);
+                               STATIC_ASSERT(LZMS_NUM_LZ_REPS == 3);
                                recent_lz_offsets[3] = recent_lz_offsets[2];
                                recent_lz_offsets[2] = recent_lz_offsets[1];
                                recent_lz_offsets[1] = recent_lz_offsets[0];
@@ -882,7 +872,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                                if (pending_delta_pair != 0 &&
                                    out_next != delta_pair_still_pending)
                                {
-                                       BUILD_BUG_ON(LZMS_NUM_DELTA_REPS != 3);
+                                       STATIC_ASSERT(LZMS_NUM_DELTA_REPS == 3);
                                        recent_delta_pairs[3] = recent_delta_pairs[2];
                                        recent_delta_pairs[2] = recent_delta_pairs[1];
                                        recent_delta_pairs[1] = recent_delta_pairs[0];
@@ -890,7 +880,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                                        pending_delta_pair = 0;
                                }
 
-                               BUILD_BUG_ON(LZMS_NUM_DELTA_REPS != 3);
+                               STATIC_ASSERT(LZMS_NUM_DELTA_REPS == 3);
                                if (!lzms_decode_bit(&rd, &delta_rep_states[0],
                                                     LZMS_NUM_DELTA_REP_PROBS,
                                                     d->probs.delta_rep[0]))
@@ -915,7 +905,7 @@ lzms_decompress(const void * const restrict in, const size_t in_nbytes,
                        }
 
                        if (pending_delta_pair != 0) {
-                               BUILD_BUG_ON(LZMS_NUM_DELTA_REPS != 3);
+                               STATIC_ASSERT(LZMS_NUM_DELTA_REPS == 3);
                                recent_delta_pairs[3] = recent_delta_pairs[2];
                                recent_delta_pairs[2] = recent_delta_pairs[1];
                                recent_delta_pairs[1] = recent_delta_pairs[0];