]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/rangecoder/range_common.h
Sort of garbage collection commit. :-| Many things are still
[icculus/xz.git] / src / liblzma / rangecoder / range_common.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       range_common.h
4 /// \brief      Common things for range encoder and decoder
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2006 Lasse Collin
8 //
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.
13 //
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.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #ifndef LZMA_RANGE_COMMON_H
22 #define LZMA_RANGE_COMMON_H
23
24 #ifdef HAVE_CONFIG_H
25 #       include "common.h"
26 #endif
27
28
29 ///////////////
30 // Constants //
31 ///////////////
32
33 #define RC_SHIFT_BITS 8
34 #define RC_TOP_BITS 24
35 #define RC_TOP_VALUE (UINT32_C(1) << RC_TOP_BITS)
36 #define RC_BIT_MODEL_TOTAL_BITS 11
37 #define RC_BIT_MODEL_TOTAL (UINT32_C(1) << RC_BIT_MODEL_TOTAL_BITS)
38 #define RC_MOVE_BITS 5
39
40
41 ////////////
42 // Macros //
43 ////////////
44
45 // Resets the probability so that both 0 and 1 have probability of 50 %
46 #define bit_reset(prob) \
47         prob = RC_BIT_MODEL_TOTAL >> 1
48
49 // This does the same for a complete bit tree.
50 // (A tree represented as an array.)
51 #define bittree_reset(probs, bit_levels) \
52         for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
53                 bit_reset((probs)[bt_i])
54
55
56 //////////////////////
57 // Type definitions //
58 //////////////////////
59
60 /// \brief      Type of probabilities used with range coder
61 ///
62 /// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
63 /// However, on some architecture and compiler combinations, a bigger type
64 /// may give better speed, because the probability variables are accessed
65 /// a lot. On the other hand, bigger probability type increases cache
66 /// footprint, since there are 2 to 14 thousand probability variables in
67 /// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
68 /// would be about 1.5 million variables).
69 ///
70 /// With malicious files, the initialization speed of the LZMA decoder can
71 /// become important. In that case, smaller probability variables mean that
72 /// there is less bytes to write to RAM, which makes initialization faster.
73 /// With big probability type, the initialization can become so slow that it
74 /// can be a problem e.g. for email servers doing virus scanning.
75 ///
76 /// I will be sticking to uint16_t unless some specific architectures
77 /// are *much* faster (20-50 %) with uint32_t.
78 typedef uint16_t probability;
79
80 #endif