]> wimlib.net Git - wimlib/blob - include/wimlib/lzms_common.h
LZMS: decompression optimizations
[wimlib] / include / wimlib / lzms_common.h
1 /*
2  * lzms_common.h
3  *
4  * Declarations shared between LZMS compression and decompression.
5  */
6
7 #ifndef _LZMS_COMMON_H
8 #define _LZMS_COMMON_H
9
10 #include "wimlib/compiler.h"
11 #include "wimlib/lzms_constants.h"
12 #include "wimlib/types.h"
13
14 //#define ENABLE_LZMS_DEBUG
15 #ifdef ENABLE_LZMS_DEBUG
16 #       define LZMS_DEBUG DEBUG
17 #       define LZMS_ASSERT wimlib_assert
18 #       include "wimlib/assert.h"
19 #       include "wimlib/error.h"
20 #else
21 #       define LZMS_DEBUG(format, ...)
22 #       define LZMS_ASSERT(...)
23 #endif
24
25 extern void
26 lzms_x86_filter(u8 data[], s32 size, s32 last_target_usages[], bool undo);
27
28 /* Probability entry for use by the range coder when in a specific state.  */
29 struct lzms_probability_entry {
30
31         /* Number of zeroes in the most recent LZMS_PROBABILITY_MAX bits that
32          * have been coded using this probability entry.  This is a cached value
33          * because it can be computed as LZMS_PROBABILITY_MAX minus the number
34          * of bits set in the low-order LZMS_PROBABILITY_MAX bits of
35          * @recent_bits.  */
36         u32 num_recent_zero_bits;
37
38         /* The most recent LZMS_PROBABILITY_MAX bits that have been coded using
39          * this probability entry.  The size of this variable, in bits, must be
40          * at least LZMS_PROBABILITY_MAX.  */
41         u64 recent_bits;
42 };
43
44 /* Offset slot tables  */
45 extern const u32 lzms_offset_slot_base[LZMS_MAX_NUM_OFFSET_SYMS + 1];
46 extern const u8 lzms_extra_offset_bits[LZMS_MAX_NUM_OFFSET_SYMS];
47
48 /* Length slot tables  */
49 extern const u32 lzms_length_slot_base[LZMS_NUM_LENGTH_SYMS + 1];
50 extern const u8 lzms_extra_length_bits[LZMS_NUM_LENGTH_SYMS];
51
52 extern unsigned
53 lzms_get_slot(u32 value, const u32 slot_base_tab[], unsigned num_slots);
54
55 /* Return the offset slot for the specified offset  */
56 static inline unsigned
57 lzms_get_offset_slot(u32 offset)
58 {
59         return lzms_get_slot(offset, lzms_offset_slot_base, LZMS_MAX_NUM_OFFSET_SYMS);
60 }
61
62 /* Return the length slot for the specified length  */
63 static inline unsigned
64 lzms_get_length_slot(u32 length)
65 {
66         return lzms_get_slot(length, lzms_length_slot_base, LZMS_NUM_LENGTH_SYMS);
67 }
68
69 extern unsigned
70 lzms_get_num_offset_slots(size_t uncompressed_size);
71
72 extern void
73 lzms_init_probability_entries(struct lzms_probability_entry *entries, size_t count);
74
75 extern void
76 lzms_init_symbol_frequencies(u32 freqs[], size_t num_syms);
77
78 /* Given a decoded bit, update the probability entry.  */
79 static inline void
80 lzms_update_probability_entry(struct lzms_probability_entry *prob_entry, int bit)
81 {
82         s32 delta_zero_bits;
83
84         BUILD_BUG_ON(LZMS_PROBABILITY_MAX != sizeof(prob_entry->recent_bits) * 8);
85
86         delta_zero_bits = (s32)(prob_entry->recent_bits >> (LZMS_PROBABILITY_MAX - 1)) - bit;
87
88         prob_entry->num_recent_zero_bits += delta_zero_bits;
89         prob_entry->recent_bits <<= 1;
90         prob_entry->recent_bits |= bit;
91 }
92
93 /* Given a probability entry, return the chance out of LZMS_PROBABILITY_MAX that
94  * the next decoded bit will be a 0.  */
95 static inline u32
96 lzms_get_probability(const struct lzms_probability_entry *prob_entry)
97 {
98         u32 prob;
99
100         prob = prob_entry->num_recent_zero_bits;
101
102         /* 0% and 100% probabilities aren't allowed.  */
103         if (prob == 0)
104                 prob++;
105         if (prob == LZMS_PROBABILITY_MAX)
106                 prob--;
107         return prob;
108 }
109
110 #endif /* _LZMS_COMMON_H */