1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_common.h
4 /// \brief Common things for range encoder and decoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2006 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #ifndef LZMA_RANGE_COMMON_H
22 #define LZMA_RANGE_COMMON_H
35 #define TOP_VALUE (UINT32_C(1) << TOP_BITS)
36 #define BIT_MODEL_TOTAL_BITS 11
37 #define BIT_MODEL_TOTAL (UINT32_C(1) << BIT_MODEL_TOTAL_BITS)
40 #define MOVE_REDUCING_BITS 2
41 #define BIT_PRICE_SHIFT_BITS 6
48 // Resets the probability so that both 0 and 1 have probability of 50 %
49 #define bit_reset(prob) \
50 prob = BIT_MODEL_TOTAL >> 1
52 // This does the same for a complete bit tree.
53 // (A tree represented as an array.)
54 #define bittree_reset(probs, bit_levels) \
55 for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
56 bit_reset((probs)[bt_i])
59 //////////////////////
60 // Type definitions //
61 //////////////////////
63 /// \brief Type of probabilities used with range coder
65 /// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
66 /// However, on some architecture and compiler combinations, a bigger type
67 /// may give better speed, because the probability variables are accessed
68 /// a lot. On the other hand, bigger probability type increases cache
69 /// footprint, since there are 2 to 14 thousand probability variables in
70 /// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
71 /// would be about 1.5 million variables).
73 /// With malicious files, the initialization speed of the LZMA decoder can
74 /// become important. In that case, smaller probability variables mean that
75 /// there is less bytes to write to RAM, which makes initialization faster.
76 /// With big probability type, the initialization can become so slow that it
77 /// can be a problem e.g. for email servers doing virus scanning.
79 /// I will be sticking to uint16_t unless some specific architectures
80 /// are *much* faster (20-50 %) with uint32_t.
81 typedef uint16_t probability;